xref: /webtrees/app/Schema/Migration39.php (revision 43e2aec9e526d1a242c3e80ff940736aec157724)
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 */
16namespace Fisharebest\Webtrees\Schema;
17
18use Fisharebest\Webtrees\Database;
19use Fisharebest\Webtrees\DebugBar;
20use PDOException;
21
22/**
23 * Upgrade the database schema from version 39 to version 40.
24 */
25class Migration39 implements MigrationInterface
26{
27    /**
28     * Upgrade to to the next version
29     */
30    public function upgrade()
31    {
32        // The following migrations were once part of the favorites module.
33
34        // Create the tables, as per PhpGedView 4.2.1
35        Database::exec(
36            "CREATE TABLE IF NOT EXISTS `##favorites` (" .
37            " fv_id       INTEGER AUTO_INCREMENT NOT NULL," .
38            " fv_username VARCHAR(32)            NOT NULL," .
39            " fv_gid      VARCHAR(20)                NULL," .
40            " fv_type     VARCHAR(15)                NULL," .
41            " fv_file     VARCHAR(100)               NULL," .
42            " fv_url      VARCHAR(255)               NULL," .
43            " fv_title    VARCHAR(255)               NULL," .
44            " fv_note     TEXT                       NULL," .
45            " PRIMARY KEY (fv_id)," .
46            "         KEY ix1 (fv_username)" .
47            ") COLLATE utf8_unicode_ci ENGINE=InnoDB"
48        );
49
50        // Add the new columns
51        try {
52            Database::exec(
53                "ALTER TABLE `##favorites`" .
54                " CHANGE fv_id    favorite_id   INTEGER AUTO_INCREMENT NOT NULL," .
55                " CHANGE fv_gid   xref          VARCHAR(20) NULL," .
56                " CHANGE fv_type  favorite_type ENUM('INDI', 'FAM', 'SOUR', 'REPO', 'OBJE', 'NOTE', 'URL') NOT NULL," .
57                " CHANGE fv_url   url           VARCHAR(255) NULL," .
58                " CHANGE fv_title title         VARCHAR(255) NULL," .
59                " CHANGE fv_note  note          VARCHAR(1000) NULL," .
60                " ADD user_id   INTEGER     NULL AFTER favorite_id," .
61                " ADD gedcom_id INTEGER NOT NULL AFTER user_id," .
62                " DROP KEY ix1," .
63                " ADD KEY news_ix1 (gedcom_id, user_id)"
64            );
65        } catch (PDOException $ex) {
66            DebugBar::addThrowable($ex);
67
68            // Already updated?
69        }
70
71        // Migrate data from the old columns to the new ones
72        try {
73            Database::exec(
74                "UPDATE `##favorites` f" .
75                " LEFT JOIN `##gedcom` g ON (f.fv_file    =g.gedcom_name)" .
76                " LEFT JOIN `##user`   u ON (f.fv_username=u.user_name)" .
77                " SET f.gedcom_id=g.gedcom_id, f.user_id=u.user_id"
78            );
79        } catch (PDOException $ex) {
80            DebugBar::addThrowable($ex);
81
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            DebugBar::addThrowable($ex);
97
98            // Already updated?
99        }
100
101        // Rename the table
102        try {
103            Database::exec(
104                "RENAME TABLE `##favorites` TO `##favorite`"
105            );
106        } catch (PDOException $ex) {
107            DebugBar::addThrowable($ex);
108
109            // Already updated?
110        }
111
112        // Add foreign key constraints
113        // Delete any data that might violate the new constraints
114        Database::exec(
115            "DELETE FROM `##favorite`" .
116            " WHERE user_id   NOT IN (SELECT user_id   FROM `##user`  )" .
117            " OR    gedcom_id NOT IN (SELECT gedcom_id FROM `##gedcom`)"
118        );
119
120        // Add the new constraints
121        try {
122            Database::exec(
123                "ALTER TABLE `##favorite`" .
124                " ADD FOREIGN KEY `##favorite_fk1` (user_id  ) REFERENCES `##user`   (user_id) ON DELETE CASCADE," .
125                " ADD FOREIGN KEY `##favorite_fk2` (gedcom_id) REFERENCES `##gedcom` (gedcom_id) ON DELETE CASCADE"
126            );
127        } catch (PDOException $ex) {
128            DebugBar::addThrowable($ex);
129
130            // Already updated?
131        }
132    }
133}
134