1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
<?php
namespace Fisharebest\Webtrees\Module\FamilyTreeFavorites\Schema;
/**
* webtrees: online genealogy
* Copyright (C) 2015 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use Fisharebest\Webtrees\Database;
use Fisharebest\Webtrees\Schema\MigrationInterface;
use PDOException;
/**
* Class Migration1 - upgrade the database schema from version 1 to version 2.
*/
class Migration1 implements MigrationInterface {
/** {@inheritDoc} */
public function upgrade() {
// Add the new columns
try {
Database::exec(
"ALTER TABLE `##favorites`" .
" CHANGE fv_id favorite_id INTEGER AUTO_INCREMENT NOT NULL," .
" CHANGE fv_gid xref VARCHAR(20) NULL," .
" CHANGE fv_type favorite_type ENUM('INDI', 'FAM', 'SOUR', 'REPO', 'OBJE', 'NOTE', 'URL') NOT NULL," .
" CHANGE fv_url url VARCHAR(255) NULL," .
" CHANGE fv_title title VARCHAR(255) NULL," .
" CHANGE fv_note note VARCHAR(1000) NULL," .
" ADD user_id INTEGER NULL AFTER favorite_id," .
" ADD gedcom_id INTEGER NOT NULL AFTER user_id," .
" DROP KEY ix1," .
" ADD KEY news_ix1 (gedcom_id, user_id)"
);
} catch (PDOException $ex) {
// Already updated?
}
// Migrate data from the old columns to the new ones
try {
Database::exec(
"UPDATE `##favorites` f" .
" LEFT JOIN `##gedcom` g ON (f.fv_file =g.gedcom_name)" .
" LEFT JOIN `##user` u ON (f.fv_username=u.user_name)" .
" SET f.gedcom_id=g.gedcom_id, f.user_id=u.user_id"
);
} catch (PDOException $ex) {
// Already updated?
}
// Delete orphaned rows
try {
Database::exec(
"DELETE FROM `##favorites` WHERE user_id IS NULL AND gedcom_id IS NULL"
);
} catch (PDOException $ex) {
// Already updated?
}
// Delete the old column
try {
Database::exec(
"ALTER TABLE `##favorites` DROP fv_username, DROP fv_file"
);
} catch (PDOException $ex) {
// Already updated?
}
// Rename the table
try {
Database::exec(
"RENAME TABLE `##favorites` TO `##favorite`"
);
} catch (PDOException $ex) {
// Already updated?
}
}
}
|