-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSqlite.php
320 lines (291 loc) · 16.4 KB
/
Sqlite.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<?php
/**
* Sqlite schema
* @package Kanboard\Plugin\TodoNotes\Schema
* @author Im[F(x)]
*/
namespace Kanboard\Plugin\TodoNotes\Schema;
use PDO;
const VERSION = 1;
//////////////////////////////////////////////////
// VERSION = 1
//////////////////////////////////////////////////
//------------------------------------------------
// v.1 schema routines
//------------------------------------------------
function version_1(PDO $pdo)
{
// create+insert+index custom projects
$pdo->exec('CREATE TABLE IF NOT EXISTS todonotes_custom_projects (
id INTEGER PRIMARY KEY,
owner_id INTEGER NOT NULL DEFAULT 0,
position INTEGER,
project_name TEXT
)');
$pdo->exec('INSERT INTO todonotes_custom_projects
(owner_id, position, project_name)
VALUES (0, 1, "Global Notes")
');
$pdo->exec('INSERT INTO todonotes_custom_projects
(owner_id, position, project_name)
VALUES (0, 2, "Global TODO")
');
$pdo->exec('CREATE INDEX todonotes_custom_projects_owner_ix ON todonotes_custom_projects(owner_id)');
$pdo->exec('CREATE INDEX todonotes_custom_projects_position_ix ON todonotes_custom_projects(position)');
// create+index sharing permissions
$pdo->exec('CREATE TABLE IF NOT EXISTS todonotes_sharing_permissions (
project_id INTEGER NOT NULL,
shared_from_user_id INTEGER NOT NULL,
shared_to_user_id INTEGER NOT NULL,
permissions INTEGER NOT NULL
)');
$pdo->exec('CREATE INDEX todonotes_sharing_permissions_project_ix ON todonotes_sharing_permissions(project_id)');
$pdo->exec('CREATE INDEX todonotes_sharing_permissions_shared_from_user_ix ON todonotes_sharing_permissions(shared_from_user_id)');
$pdo->exec('CREATE INDEX todonotes_sharing_permissions_shared_to_user_ix ON todonotes_sharing_permissions(shared_to_user_id)');
// create+insert+index entries
$pdo->exec('CREATE TABLE IF NOT EXISTS todonotes_entries (
id INTEGER PRIMARY KEY,
project_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
position INTEGER,
is_active INTEGER,
title TEXT,
category TEXT,
description TEXT,
date_created INTEGER,
date_modified INTEGER,
date_notified INTEGER,
last_notified INTEGER,
flags_notified INTEGER,
date_restored INTEGER,
last_change_user_id INTEGER
)');
$pdo->exec('INSERT INTO todonotes_entries
(project_id, user_id, position, is_active, date_created, date_modified, date_notified, last_notified, flags_notified, date_restored, last_change_user_id)
VALUES (0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0)
');
$pdo->exec('CREATE INDEX todonotes_entries_project_ix ON todonotes_entries(project_id)');
$pdo->exec('CREATE INDEX todonotes_entries_user_ix ON todonotes_entries(user_id)');
$pdo->exec('CREATE INDEX todonotes_entries_position_ix ON todonotes_entries(position)');
$pdo->exec('CREATE INDEX todonotes_entries_active_ix ON todonotes_entries(is_active)');
$pdo->exec('CREATE INDEX todonotes_entries_created_ix ON todonotes_entries(date_created)');
$pdo->exec('CREATE INDEX todonotes_entries_modified_ix ON todonotes_entries(date_modified)');
$pdo->exec('CREATE INDEX todonotes_entries_notified_ix ON todonotes_entries(date_notified)');
$pdo->exec('CREATE INDEX todonotes_entries_last_notified_ix ON todonotes_entries(last_notified)');
$pdo->exec('CREATE INDEX todonotes_entries_restored_ix ON todonotes_entries(date_restored)');
// create+insert+index archive entries
$pdo->exec('CREATE TABLE IF NOT EXISTS todonotes_archive_entries (
id INTEGER PRIMARY KEY,
project_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
title TEXT,
category TEXT,
description TEXT,
date_created INTEGER,
date_modified INTEGER,
date_notified INTEGER,
last_notified INTEGER,
date_archived INTEGER,
last_change_user_id INTEGER
)');
$pdo->exec('INSERT INTO todonotes_archive_entries
(project_id, user_id, date_created, date_modified, date_notified, last_notified, date_archived, last_change_user_id)
VALUES (0, 0, 0, -1, 0, 0, 0, 0)
');
$pdo->exec('CREATE INDEX todonotes_archive_entries_project_ix ON todonotes_archive_entries(project_id)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_user_ix ON todonotes_archive_entries(user_id)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_created_ix ON todonotes_archive_entries(date_created)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_modified_ix ON todonotes_archive_entries(date_modified)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_notified_ix ON todonotes_archive_entries(date_notified)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_last_notified_ix ON todonotes_archive_entries(last_notified)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_archived_ix ON todonotes_archive_entries(date_archived)');
// create+index webpn subscriptions
$pdo->exec('CREATE TABLE IF NOT EXISTS todonotes_webpn_subscriptions (
endpoint TEXT NOT NULL PRIMARY KEY,
user_id INTEGER NOT NULL,
subscription TEXT NOT NULL
)');
$pdo->exec('CREATE INDEX todonotes_webpn_subscriptions_user_ix ON todonotes_webpn_subscriptions(user_id)');
}
//------------------------------------------------
// v.1 reindex routines
//------------------------------------------------
function Reindex_Rename_OldTables_1(PDO $pdo)
{
$pdo->exec('ALTER TABLE todonotes_custom_projects RENAME TO todonotes_custom_projects_OLD');
$pdo->exec('ALTER TABLE todonotes_sharing_permissions RENAME TO todonotes_sharing_permissions_OLD');
$pdo->exec('ALTER TABLE todonotes_entries RENAME TO todonotes_entries_OLD');
$pdo->exec('ALTER TABLE todonotes_archive_entries RENAME TO todonotes_archive_entries_OLD');
}
function Reindex_AddAndUpdate_OldProjectIds_1(PDO $pdo)
{
$pdo->exec('ALTER TABLE todonotes_custom_projects_OLD ADD old_project_id INTEGER');
$pdo->exec('UPDATE todonotes_custom_projects_OLD SET old_project_id = id');
$pdo->exec('ALTER TABLE todonotes_sharing_permissions_OLD ADD old_project_id INTEGER');
$pdo->exec('UPDATE todonotes_sharing_permissions_OLD SET old_project_id = project_id');
$pdo->exec('ALTER TABLE todonotes_entries_OLD ADD old_project_id INTEGER');
$pdo->exec('UPDATE todonotes_entries_OLD SET old_project_id = project_id');
$pdo->exec('ALTER TABLE todonotes_archive_entries_OLD ADD old_project_id INTEGER');
$pdo->exec('UPDATE todonotes_archive_entries_OLD SET old_project_id = project_id');
}
function Reindex_CreateAndInsert_NewShrunkCustomProjects_1(PDO $pdo)
{
$pdo->exec('CREATE TABLE todonotes_custom_projects (
id INTEGER PRIMARY KEY,
owner_id INTEGER NOT NULL DEFAULT 0,
position INTEGER,
project_name TEXT
)');
$pdo->exec('INSERT INTO todonotes_custom_projects
(owner_id, position, project_name)
SELECT owner_id, position, project_name
FROM todonotes_custom_projects_OLD
');
$pdo->exec('CREATE TABLE todonotes_custom_projects_REINDEX (
id INTEGER PRIMARY KEY,
old_project_id INTEGER
)');
$pdo->exec('INSERT INTO todonotes_custom_projects_REINDEX
(old_project_id)
SELECT old_project_id
FROM todonotes_custom_projects_OLD
');
}
function Reindex_CrossUpdate_ReindexedProjectIds_1(PDO $pdo)
{
$pdo->exec('UPDATE todonotes_sharing_permissions_OLD
SET project_id = -
(SELECT tProjects.id FROM todonotes_custom_projects_REINDEX tProjects
WHERE todonotes_sharing_permissions_OLD.old_project_id = -tProjects.old_project_id)
WHERE EXISTS (SELECT tProjects.id FROM todonotes_custom_projects_REINDEX tProjects
WHERE todonotes_sharing_permissions_OLD.old_project_id = -tProjects.old_project_id)
');
$pdo->exec('UPDATE todonotes_entries_OLD
SET project_id = -
(SELECT tProjects.id FROM todonotes_custom_projects_REINDEX tProjects
WHERE todonotes_entries_OLD.old_project_id = -tProjects.old_project_id)
WHERE EXISTS (SELECT tProjects.id FROM todonotes_custom_projects_REINDEX tProjects
WHERE todonotes_entries_OLD.old_project_id = -tProjects.old_project_id)
');
$pdo->exec('UPDATE todonotes_archive_entries_OLD
SET project_id = -
(SELECT tProjects.id FROM todonotes_custom_projects_REINDEX tProjects
WHERE todonotes_archive_entries_OLD.old_project_id = -tProjects.old_project_id)
WHERE EXISTS (SELECT tProjects.id FROM todonotes_custom_projects_REINDEX tProjects
WHERE todonotes_archive_entries_OLD.old_project_id = -tProjects.old_project_id)
');
}
function Reindex_CreateAndInsert_NewShrunkSharingPermissions_1(PDO $pdo)
{
$pdo->exec('CREATE TABLE todonotes_sharing_permissions (
project_id INTEGER NOT NULL,
shared_from_user_id INTEGER NOT NULL,
shared_to_user_id INTEGER NOT NULL,
permissions INTEGER NOT NULL
)');
$pdo->exec('INSERT INTO todonotes_sharing_permissions
(project_id, shared_from_user_id, shared_to_user_id, permissions)
SELECT project_id, shared_from_user_id, shared_to_user_id, permissions
FROM todonotes_sharing_permissions_OLD
WHERE project_id <> 0 AND shared_from_user_id > 0 AND shared_to_user_id > 0 AND permissions > 0
');
}
function Reindex_CreateAndInsert_NewShrunkEntries_1(PDO $pdo)
{
$pdo->exec('CREATE TABLE todonotes_entries (
id INTEGER PRIMARY KEY,
project_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
position INTEGER,
is_active INTEGER,
title TEXT,
category TEXT,
description TEXT,
date_created INTEGER,
date_modified INTEGER,
date_notified INTEGER,
last_notified INTEGER,
flags_notified INTEGER,
date_restored INTEGER,
last_change_user_id INTEGER
)');
$pdo->exec('INSERT INTO todonotes_entries
(project_id, user_id, position, is_active, date_created, date_modified, date_notified, last_notified, flags_notified, date_restored, last_change_user_id)
VALUES (0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0)
');
$pdo->exec('INSERT INTO todonotes_entries
(project_id, user_id, position, is_active, title, category, description, date_created, date_modified, date_notified, last_notified, flags_notified, date_restored, last_change_user_id)
SELECT project_id, user_id, position, is_active, title, category, description, date_created, date_modified, date_notified, last_notified, flags_notified, date_restored, last_change_user_id
FROM todonotes_entries_OLD
WHERE project_id <> 0 AND user_id > 0 AND position > 0 AND is_active >= 0
');
}
function Reindex_CreateAndInsert_NewShrunkArchiveEntries_1(PDO $pdo)
{
$pdo->exec('CREATE TABLE IF NOT EXISTS todonotes_archive_entries (
id INTEGER PRIMARY KEY,
project_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
title TEXT,
category TEXT,
description TEXT,
date_created INTEGER,
date_modified INTEGER,
date_notified INTEGER,
last_notified INTEGER,
date_archived INTEGER,
last_change_user_id INTEGER
)');
$pdo->exec('INSERT INTO todonotes_archive_entries
(project_id, user_id, date_created, date_modified, date_notified, last_notified, date_archived, last_change_user_id)
VALUES (0, 0, 0, -1, 0, 0, 0, 0)
');
$pdo->exec('INSERT INTO todonotes_archive_entries
(project_id, user_id, title, category, description, date_created, date_modified, date_notified, last_notified, date_archived, last_change_user_id)
SELECT project_id, user_id, title, category, description, date_created, date_modified, date_notified, last_notified, date_archived, last_change_user_id
FROM todonotes_archive_entries_OLD
WHERE project_id <> 0 AND user_id > 0 AND date_modified > 0 AND date_archived > 0
');
}
function Reindex_Drop_OldTables_1(PDO $pdo)
{
$pdo->exec('DROP TABLE todonotes_custom_projects_REINDEX');
$pdo->exec('DROP TABLE todonotes_custom_projects_OLD');
$pdo->exec('DROP TABLE todonotes_sharing_permissions_OLD');
$pdo->exec('DROP TABLE todonotes_entries_OLD');
$pdo->exec('DROP TABLE todonotes_archive_entries_OLD');
}
function Reindex_RecreateIndices_CustomProjects_1(PDO $pdo)
{
$pdo->exec('CREATE INDEX todonotes_custom_projects_owner_ix ON todonotes_custom_projects(owner_id)');
$pdo->exec('CREATE INDEX todonotes_custom_projects_position_ix ON todonotes_custom_projects(position)');
}
function Reindex_RecreateIndices_SharingPermissions_1(PDO $pdo)
{
$pdo->exec('CREATE INDEX todonotes_sharing_permissions_project_ix ON todonotes_sharing_permissions(project_id)');
$pdo->exec('CREATE INDEX todonotes_sharing_permissions_shared_from_user_ix ON todonotes_sharing_permissions(shared_from_user_id)');
$pdo->exec('CREATE INDEX todonotes_sharing_permissions_shared_to_user_ix ON todonotes_sharing_permissions(shared_to_user_id)');
}
function Reindex_RecreateIndices_Entries_1(PDO $pdo)
{
$pdo->exec('CREATE INDEX todonotes_entries_project_ix ON todonotes_entries(project_id)');
$pdo->exec('CREATE INDEX todonotes_entries_user_ix ON todonotes_entries(user_id)');
$pdo->exec('CREATE INDEX todonotes_entries_position_ix ON todonotes_entries(position)');
$pdo->exec('CREATE INDEX todonotes_entries_active_ix ON todonotes_entries(is_active)');
$pdo->exec('CREATE INDEX todonotes_entries_created_ix ON todonotes_entries(date_created)');
$pdo->exec('CREATE INDEX todonotes_entries_modified_ix ON todonotes_entries(date_modified)');
$pdo->exec('CREATE INDEX todonotes_entries_notified_ix ON todonotes_entries(date_notified)');
$pdo->exec('CREATE INDEX todonotes_entries_last_notified_ix ON todonotes_entries(last_notified)');
$pdo->exec('CREATE INDEX todonotes_entries_restored_ix ON todonotes_entries(date_restored)');
}
function Reindex_RecreateIndices_ArchiveEntries_1(PDO $pdo)
{
$pdo->exec('CREATE INDEX todonotes_archive_entries_project_ix ON todonotes_archive_entries(project_id)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_user_ix ON todonotes_archive_entries(user_id)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_created_ix ON todonotes_archive_entries(date_created)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_modified_ix ON todonotes_archive_entries(date_modified)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_notified_ix ON todonotes_archive_entries(date_notified)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_last_notified_ix ON todonotes_archive_entries(last_notified)');
$pdo->exec('CREATE INDEX todonotes_archive_entries_archived_ix ON todonotes_archive_entries(date_archived)');
}
//////////////////////////////////////////////////