xref: /webtrees/app/Schema/Migration37.php (revision 530749aa3936273ddfb12c66ee2eafdac35cde61)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 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;
19
20/**
21 * Upgrade the database schema from version 37 to version 38.
22 */
23class Migration37 implements MigrationInterface {
24	/**
25	 * Upgrade to to the next version
26	 */
27	public function upgrade() {
28		// Move repositories to their own table
29		Database::exec(
30			"CREATE TABLE IF NOT EXISTS `##repository` (" .
31			" repository_id INTEGER AUTO_INCREMENT                      NOT NULL," .
32			" gedcom_id     INTEGER                                     NOT NULL," .
33			" xref          VARCHAR(20)                                 NOT NULL," .
34			" gedcom        LONGTEXT                                    NOT NULL," .
35			" name          VARCHAR(90)                                 NOT NULL," .
36			" address       VARCHAR(255)                                NOT NULL," .
37			" restriction   ENUM('', 'confidential', 'privacy', 'none') NOT NULL," .
38			" uid           VARCHAR(34)                                 NOT NULL," .
39			" changed_at    DATETIME                                    NOT NULL," .
40			" PRIMARY KEY (repository_id)," .
41			" UNIQUE  KEY `##repository_ix1` (gedcom_id, xref)," .
42			" UNIQUE  KEY `##repository_ix2` (xref, gedcom_id)," .
43			"         KEY `##repository_ix3` (name)," .
44			"         KEY `##repository_ix4` (address)," .
45			"         KEY `##repository_ix5` (restriction)," .
46			"         KEY `##repository_ix6` (uid)," .
47			"         KEY `##repository_ix7` (changed_at)," .
48			" CONSTRAINT `##repository_fk1` FOREIGN KEY (gedcom_id) REFERENCES `##gedcom` (gedcom_id)" .
49			") COLLATE utf8_unicode_ci ENGINE=InnoDB"
50		);
51
52		Database::exec("START TRANSACTION");
53
54		$repositories = Database::prepare("SELECT * FROM `##other` WHERE o_type = 'REPO'")->fetchAll();
55
56		foreach ($repositories as $n => $repository) {
57			Database::prepare(
58				"INSERT INTO `##repository` (" .
59				" gedcom_id, xref, gedcom, name, address, restriction, uid, changed_at" .
60				") VALUES (" .
61				" :gedcom_id, :xref, :gedcom, :name, :address, :restriction, :uid, :changed_at" .
62				")"
63			)->execute([
64				'gedcom_id'   => $repository->o_file,
65				'xref'        => $repository->o_id,
66				'gedcom'      => $repository->o_gedcom,
67				'name'        => '',
68				'address'     => '',
69				'restriction' => '',
70				'uid'         => '',
71				'changed_at'  => '',
72			]);
73
74			Database::prepare(
75				"DELETE FROM `##other` WHERE o_file = :gedcom_id AND o_id = :xref"
76			)->execute([
77				'gedcom_id' => $repository->o_file,
78				'xref'      => $repository->o_id,
79			]);
80
81			if ($n % 500 === 499) {
82				Database::exec("COMMIT");
83				Database::exec("START TRANSACTION");
84
85			}
86		}
87
88		Database::exec("COMMIT");
89	}
90}
91