xref: /webtrees/app/Schema/Migration39.php (revision ddf438a50ef4ca889f688fe1c566ddf89d256ed1)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2018 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Schema;
19
20use Fisharebest\Webtrees\Database;
21use Fisharebest\Webtrees\DebugBar;
22use PDOException;
23
24/**
25 * Upgrade the database schema from version 39 to version 40.
26 */
27class Migration39 implements MigrationInterface
28{
29    /**
30     * Upgrade to to the next version
31     *
32     * @return void
33     */
34    public function upgrade()
35    {
36        // The following migrations were once part of the favorites module.
37
38        // Create the tables, as per PhpGedView 4.2.1
39        Database::exec(
40            "CREATE TABLE IF NOT EXISTS `##favorites` (" .
41            " fv_id       INTEGER AUTO_INCREMENT NOT NULL," .
42            " fv_username VARCHAR(32)            NOT NULL," .
43            " fv_gid      VARCHAR(20)                NULL," .
44            " fv_type     VARCHAR(15)                NULL," .
45            " fv_file     VARCHAR(100)               NULL," .
46            " fv_url      VARCHAR(255)               NULL," .
47            " fv_title    VARCHAR(255)               NULL," .
48            " fv_note     TEXT                       NULL," .
49            " PRIMARY KEY (fv_id)," .
50            "         KEY ix1 (fv_username)" .
51            ") COLLATE utf8_unicode_ci ENGINE=InnoDB"
52        );
53
54        // Add the new columns
55        try {
56            Database::exec(
57                "ALTER TABLE `##favorites`" .
58                " CHANGE fv_id    favorite_id   INTEGER AUTO_INCREMENT NOT NULL," .
59                " CHANGE fv_gid   xref          VARCHAR(20) NULL," .
60                " CHANGE fv_type  favorite_type ENUM('INDI', 'FAM', 'SOUR', 'REPO', 'OBJE', 'NOTE', 'URL') NOT NULL," .
61                " CHANGE fv_url   url           VARCHAR(255) NULL," .
62                " CHANGE fv_title title         VARCHAR(255) NULL," .
63                " CHANGE fv_note  note          VARCHAR(1000) NULL," .
64                " ADD user_id   INTEGER     NULL AFTER favorite_id," .
65                " ADD gedcom_id INTEGER NOT NULL AFTER user_id," .
66                " DROP KEY ix1," .
67                " ADD KEY news_ix1 (gedcom_id, user_id)"
68            );
69        } catch (PDOException $ex) {
70            // Already updated?
71        }
72
73        // Migrate data from the old columns to the new ones
74        try {
75            Database::exec(
76                "UPDATE `##favorites` f" .
77                " LEFT JOIN `##gedcom` g ON (f.fv_file    =g.gedcom_name)" .
78                " LEFT JOIN `##user`   u ON (f.fv_username=u.user_name)" .
79                " SET f.gedcom_id=g.gedcom_id, f.user_id=u.user_id"
80            );
81        } catch (PDOException $ex) {
82            // Already updated?
83        }
84
85        // Delete orphaned rows
86        Database::exec(
87            "DELETE FROM `##favorites` WHERE user_id IS NULL AND gedcom_id IS NULL"
88        );
89
90        // Delete the old column
91        try {
92            Database::exec(
93                "ALTER TABLE `##favorites` DROP fv_username, DROP fv_file"
94            );
95        } catch (PDOException $ex) {
96            // Already updated?
97        }
98
99        // Rename the table
100        try {
101            Database::exec(
102                "RENAME TABLE `##favorites` TO `##favorite`"
103            );
104        } catch (PDOException $ex) {
105            // Already updated?
106        }
107
108        // Add foreign key constraints
109        // Delete any data that might violate the new constraints
110        Database::exec(
111            "DELETE FROM `##favorite`" .
112            " WHERE user_id   NOT IN (SELECT user_id   FROM `##user`  )" .
113            " OR    gedcom_id NOT IN (SELECT gedcom_id FROM `##gedcom`)"
114        );
115
116        // Add the new constraints
117        try {
118            Database::exec(
119                "ALTER TABLE `##favorite`" .
120                " ADD FOREIGN KEY `##favorite_fk1` (user_id  ) REFERENCES `##user`   (user_id) ON DELETE CASCADE," .
121                " ADD FOREIGN KEY `##favorite_fk2` (gedcom_id) REFERENCES `##gedcom` (gedcom_id) ON DELETE CASCADE"
122            );
123        } catch (PDOException $ex) {
124            // Already updated?
125        }
126    }
127}
128