-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_upgrade.inc.php
98 lines (92 loc) · 3.38 KB
/
db_upgrade.inc.php
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
87
88
89
90
91
92
93
94
95
96
97
98
<?php
if ($version < 1) {
$upgrade_ok = $upgrade_ok && $db->exec(
'CREATE TABLE "options" (
"name" VARCHAR(16) PRIMARY KEY NOT NULL,
"value" TEXT NOT NULL
);');
$upgrade_ok = $upgrade_ok && $db->exec(
'INSERT INTO options("name", "value")
VALUES("version", "'.strval(DB_VER).'");');
}
if ($version < 2) {
// Cleanup botched upgrades
@$db->exec('DROP TABLE "new_invites";');
@$db->exec('DROP TABLE "new_users";');
@$db->exec('DROP TABLE "email_spool";');
// Upgrade users table
$upgrade_ok = $upgrade_ok && $db->exec(
'CREATE TABLE "new_users" (
"name" VARCHAR(32) PRIMARY KEY NOT NULL,
"email" VARCHAR(320) DEFAULT NULL,
"hash" TEXT NOT NULL,
"comment" TEXT NOT NULL DEFAULT ""
);');
$res = $db->query('SELECT * FROM "users";');
if ($res === false) {
$upgrade_ok = false;
} else {
while ($row = $res->fetchArray(SQLITE3_ASSOC)) {
$stmt = $db->prepare(
'INSERT INTO "new_users"
("name", "hash", "comment")
VALUES (:name, :hash, :comment);');
if ($stmt == false) {
$upgrade_ok = false;
break;
}
foreach ($row as $k=>$v) {
// Some older versions had NULL values
if (is_null($v)) {
$row[$k] = '';
}
}
$upgrade_ok = $upgrade_ok &&
$stmt->bindValue(":name", $row['name']);
$upgrade_ok = $upgrade_ok &&
$stmt->bindValue(":hash", $row['hash']);
$upgrade_ok = $upgrade_ok &&
$stmt->bindValue(":comment", $row['comment']);
if ($stmt->execute() === false) {
$upgrade_ok = false;
}
if (!$upgrade_ok) break;
}
}
$upgrade_ok = $upgrade_ok &&
$db->exec('DROP TABLE "users";');
$upgrade_ok = $upgrade_ok &&
$db->exec('ALTER TABLE "new_users" RENAME TO "users";');
// Add email_spool
$upgrade_ok = $upgrade_ok && $db->exec(
'CREATE TABLE "email_spool" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
"ts" DATETIME NOT NULL,
"email" VARCHAR(340) NOT NULL REFERENCES users("email")
ON DELETE CASCADE
ON UPDATE CASCADE,
"msg" TEXT NOT NULL
);');
// Upgrade invites table
$upgrade_ok = $upgrade_ok && $db->exec(
'CREATE TABLE "new_invites" (
"token" VARCHAR(22) NOT NULL PRIMARY KEY,
"type" INTEGER NOT NULL DEFAULT 0,
"expiry" DATETIME NOT NULL,
"name" VARCHAR(32) DEFAULT NULL,
"email" VARCHAR(320) DEFAULT NULL,
"groups" TEXT NOT NULL,
"comment" TEXT NOT NULL
);');
$upgrade_ok = $upgrade_ok && $db->exec(
'INSERT INTO "new_invites" ("token", "expiry", "groups", "comment")
SELECT "token", "expiry", "groups", "comment" FROM "invites";'
);
$upgrade_ok = $upgrade_ok && $db->exec('DROP TABLE "invites";');
$upgrade_ok = $upgrade_ok &&
$db->exec('ALTER TABLE "new_invites" RENAME TO "invites";');
// Done, now update database version
$upgrade_ok = $upgrade_ok &&
$db->exec('UPDATE options SET value="'.strval(DB_VER).'"
WHERE name="version";');
}