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