xref: /webtrees/app/Schema/Migration20.php (revision 362be83a34a1d0ba0292c4387f55a3b0ea10cb2f)
13bfb94b0SGreg Roach<?php
23bfb94b0SGreg Roach/**
33bfb94b0SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
53bfb94b0SGreg Roach * This program is free software: you can redistribute it and/or modify
63bfb94b0SGreg Roach * it under the terms of the GNU General Public License as published by
73bfb94b0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
83bfb94b0SGreg Roach * (at your option) any later version.
93bfb94b0SGreg Roach * This program is distributed in the hope that it will be useful,
103bfb94b0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
113bfb94b0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
123bfb94b0SGreg Roach * GNU General Public License for more details.
133bfb94b0SGreg Roach * You should have received a copy of the GNU General Public License
143bfb94b0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
153bfb94b0SGreg Roach */
16e7f56f2aSGreg Roachdeclare(strict_types=1);
17e7f56f2aSGreg Roach
1876692c8bSGreg Roachnamespace Fisharebest\Webtrees\Schema;
1976692c8bSGreg Roach
203bfb94b0SGreg Roachuse Fisharebest\Webtrees\Database;
21bd52fa32SGreg Roachuse Fisharebest\Webtrees\DebugBar;
223bfb94b0SGreg Roachuse PDOException;
233bfb94b0SGreg Roach
243bfb94b0SGreg Roach/**
2576692c8bSGreg Roach * Upgrade the database schema from version 20 to version 21.
263bfb94b0SGreg Roach */
27c1010edaSGreg Roachclass Migration20 implements MigrationInterface
28c1010edaSGreg Roach{
2976692c8bSGreg Roach    /**
30*362be83aSGreg Roach     * Upgrade to to the next version.
3119d91378SGreg Roach     *
3219d91378SGreg Roach     * @return void
3376692c8bSGreg Roach     */
34*362be83aSGreg Roach    public function upgrade(): void
35c1010edaSGreg Roach    {
363bfb94b0SGreg Roach        // Delete some old/unused configuration settings
373bfb94b0SGreg Roach        Database::exec("DELETE FROM `##gedcom_setting` WHERE setting_name IN ('MEDIA_EXTERNAL')");
383bfb94b0SGreg Roach
393bfb94b0SGreg Roach        // Delete old table
403bfb94b0SGreg Roach        Database::exec("DROP TABLE IF EXISTS `##media_mapping`");
413bfb94b0SGreg Roach
423bfb94b0SGreg Roach        // Make this table look like all the others
433bfb94b0SGreg Roach        try {
443bfb94b0SGreg Roach            Database::exec(
453bfb94b0SGreg Roach                "ALTER TABLE `##media`" .
463bfb94b0SGreg Roach                " DROP   m_id," .
473bfb94b0SGreg Roach                " CHANGE m_media   m_id       VARCHAR(20)  COLLATE utf8_unicode_ci NOT NULL," .
483bfb94b0SGreg Roach                " CHANGE m_file    m_filename VARCHAR(512) COLLATE utf8_unicode_ci DEFAULT NULL," .
493bfb94b0SGreg Roach                " CHANGE m_gedfile m_file     INTEGER                              NOT NULL," .
503bfb94b0SGreg Roach                " CHANGE m_gedrec  m_gedcom   MEDIUMTEXT   COLLATE utf8_unicode_ci DEFAULT NULL," .
513bfb94b0SGreg Roach                " ADD    m_type               VARCHAR(20)  COLLATE utf8_unicode_ci NULL AFTER m_ext," .
523bfb94b0SGreg Roach                " ADD    PRIMARY KEY     (m_file, m_id)," .
533bfb94b0SGreg Roach                " ADD            KEY ix2 (m_ext, m_type)," .
543bfb94b0SGreg Roach                " ADD            KEY ix3 (m_titl)"
553bfb94b0SGreg Roach            );
563bfb94b0SGreg Roach        } catch (PDOException $ex) {
573bfb94b0SGreg Roach            // Assume we've already done this
583bfb94b0SGreg Roach        }
593bfb94b0SGreg Roach
603bfb94b0SGreg Roach        // Populate the new column
613bfb94b0SGreg Roach        Database::exec("UPDATE `##media` SET m_type = SUBSTRING_INDEX(SUBSTRING_INDEX(m_gedcom, '\n3 TYPE ', -1), '\n', 1) WHERE m_gedcom LIKE '%\n3 TYPE %'");
623bfb94b0SGreg Roach    }
633bfb94b0SGreg Roach}
64