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