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