xref: /webtrees/app/Schema/Migration39.php (revision 19d913785a45657df3e7bc9cd4411f501dd5071b)
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     * @return void
31     */
32    public function upgrade()
33    {
34        // The following migrations were once part of the favorites module.
35
36        // Create the tables, as per PhpGedView 4.2.1
37        Database::exec(
38            "CREATE TABLE IF NOT EXISTS `##favorites` (" .
39            " fv_id       INTEGER AUTO_INCREMENT NOT NULL," .
40            " fv_username VARCHAR(32)            NOT NULL," .
41            " fv_gid      VARCHAR(20)                NULL," .
42            " fv_type     VARCHAR(15)                NULL," .
43            " fv_file     VARCHAR(100)               NULL," .
44            " fv_url      VARCHAR(255)               NULL," .
45            " fv_title    VARCHAR(255)               NULL," .
46            " fv_note     TEXT                       NULL," .
47            " PRIMARY KEY (fv_id)," .
48            "         KEY ix1 (fv_username)" .
49            ") COLLATE utf8_unicode_ci ENGINE=InnoDB"
50        );
51
52        // Add the new columns
53        try {
54            Database::exec(
55                "ALTER TABLE `##favorites`" .
56                " CHANGE fv_id    favorite_id   INTEGER AUTO_INCREMENT NOT NULL," .
57                " CHANGE fv_gid   xref          VARCHAR(20) NULL," .
58                " CHANGE fv_type  favorite_type ENUM('INDI', 'FAM', 'SOUR', 'REPO', 'OBJE', 'NOTE', 'URL') NOT NULL," .
59                " CHANGE fv_url   url           VARCHAR(255) NULL," .
60                " CHANGE fv_title title         VARCHAR(255) NULL," .
61                " CHANGE fv_note  note          VARCHAR(1000) NULL," .
62                " ADD user_id   INTEGER     NULL AFTER favorite_id," .
63                " ADD gedcom_id INTEGER NOT NULL AFTER user_id," .
64                " DROP KEY ix1," .
65                " ADD KEY news_ix1 (gedcom_id, user_id)"
66            );
67        } catch (PDOException $ex) {
68            DebugBar::addThrowable($ex);
69
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            DebugBar::addThrowable($ex);
83
84            // Already updated?
85        }
86
87        // Delete orphaned rows
88        Database::exec(
89            "DELETE FROM `##favorites` WHERE user_id IS NULL AND gedcom_id IS NULL"
90        );
91
92        // Delete the old column
93        try {
94            Database::exec(
95                "ALTER TABLE `##favorites` DROP fv_username, DROP fv_file"
96            );
97        } catch (PDOException $ex) {
98            DebugBar::addThrowable($ex);
99
100            // Already updated?
101        }
102
103        // Rename the table
104        try {
105            Database::exec(
106                "RENAME TABLE `##favorites` TO `##favorite`"
107            );
108        } catch (PDOException $ex) {
109            DebugBar::addThrowable($ex);
110
111            // Already updated?
112        }
113
114        // Add foreign key constraints
115        // Delete any data that might violate the new constraints
116        Database::exec(
117            "DELETE FROM `##favorite`" .
118            " WHERE user_id   NOT IN (SELECT user_id   FROM `##user`  )" .
119            " OR    gedcom_id NOT IN (SELECT gedcom_id FROM `##gedcom`)"
120        );
121
122        // Add the new constraints
123        try {
124            Database::exec(
125                "ALTER TABLE `##favorite`" .
126                " ADD FOREIGN KEY `##favorite_fk1` (user_id  ) REFERENCES `##user`   (user_id) ON DELETE CASCADE," .
127                " ADD FOREIGN KEY `##favorite_fk2` (gedcom_id) REFERENCES `##gedcom` (gedcom_id) ON DELETE CASCADE"
128            );
129        } catch (PDOException $ex) {
130            DebugBar::addThrowable($ex);
131
132            // Already updated?
133        }
134    }
135}
136