File size: 28,364 Bytes
e4f4821
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
<?php

namespace Schema;

require_once __DIR__.'/Migration.php';

use PDO;
use Kanboard\Core\Security\Token;
use Kanboard\Core\Security\Role;

const VERSION = 2;

function version_2(PDO $pdo)
{
  $pdo->exec("ALTER TABLE dbo.users ADD theme nvarchar(50) DEFAULT N'light' NOT NULL");
}

function version_1(PDO $pdo)
{
    // create tables
    $pdo->exec("
        CREATE TABLE dbo.users (
          id int identity PRIMARY KEY
          , username nvarchar(255) NOT NULL
          , password nvarchar(255)
          , is_ldap_user bit DEFAULT 0
          , name nvarchar(255)
          , email nvarchar(255)
          , google_id nvarchar(255)
          , github_id nvarchar(30)
          , notifications_enabled bit DEFAULT 0
          , timezone nvarchar(50) DEFAULT N''
          , language nvarchar(11) DEFAULT N''
          , disable_login_form bit DEFAULT 0
          , twofactor_activated bit DEFAULT 0
          , twofactor_secret char(16)
          , token nvarchar(255) DEFAULT N''
          , notifications_filter int DEFAULT 4
          , nb_failed_login int DEFAULT 0
          , lock_expiration_date bigint DEFAULT 0
          , gitlab_id int
          , role nvarchar(25) NOT NULL
          , is_active bit DEFAULT 1
          , avatar_path nvarchar(255)
          , api_access_token nvarchar(255)
          , filter nvarchar(max) DEFAULT N''
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.projects (
          id int identity PRIMARY KEY
          , name nvarchar(max) NOT NULL
          , is_active bit DEFAULT 1
          , token nvarchar(255)
          , last_modified bigint DEFAULT 0
          , is_public bit DEFAULT 0
          , is_private bit DEFAULT 0
          , description nvarchar(max)
          , identifier nvarchar(50) DEFAULT N''
          , start_date nvarchar(10) DEFAULT ''
          , end_date nvarchar(10) DEFAULT ''
          , owner_id int DEFAULT 0
          , priority_default int DEFAULT 0
          , priority_start int DEFAULT 0
          , priority_end int DEFAULT 3
          , email nvarchar(max)
          , predefined_email_subjects nvarchar(max)
          , per_swimlane_task_limits bit DEFAULT 0 NOT NULL
          , task_limit int DEFAULT 0
          , enable_global_tags bit DEFAULT 1 NOT NULL
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.columns (
          id int identity PRIMARY KEY
          , title nvarchar(255) NOT NULL
          , position int
          , project_id int NOT NULL
          , task_limit int DEFAULT 0
          , description nvarchar(max)
          , hide_in_dashboard bit DEFAULT 0 NOT NULL
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , UNIQUE (title, project_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_users (
          project_id int NOT NULL
          , user_id int NOT NULL
          , role nvarchar(255) NOT NULL
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , UNIQUE(project_id, user_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.actions (
            id int identity PRIMARY KEY
          , project_id int NOT NULL
          , event_name nvarchar(max) NOT NULL
          , action_name nvarchar(max) NOT NULL
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.action_has_params (
            id int identity PRIMARY KEY
          , action_id int NOT NULL
          , name nvarchar(max) NOT NULL
          , value nvarchar(max) NOT NULL
          , FOREIGN KEY(action_id) REFERENCES dbo.actions(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.remember_me (
            id int identity PRIMARY KEY
          , user_id int NOT NULL
          , ip nvarchar(45)
          , user_agent nvarchar(255)
          , token nvarchar(255)
          , sequence nvarchar(255)
          , expiration int
          , date_creation bigint
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.last_logins (
            id int identity PRIMARY KEY
          , auth_type nvarchar(25)
          , user_id int NOT NULL
          , ip nvarchar(45)
          , user_agent nvarchar(255)
          , date_creation bigint
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_categories (
            id int identity PRIMARY KEY
          , name nvarchar(255) NOT NULL
          , project_id int NOT NULL
          , description nvarchar(max)
          , color_id nvarchar(50)
          , UNIQUE (project_id, name)
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.swimlanes (
            id int identity PRIMARY KEY
          , name nvarchar(848) NOT NULL  /* max size for unique index */
          , position int DEFAULT 1
          , is_active bit DEFAULT 1
          , project_id int NOT NULL
          , description nvarchar(max)
          , task_limit int DEFAULT 0
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , UNIQUE (name, project_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.tasks
        (
            id                   int identity PRIMARY KEY
          , title                nvarchar(max) NOT NULL
          , description          nvarchar(max)
          , date_creation        bigint
          , color_id             nvarchar(255)
          , project_id           int NOT NULL
          , column_id            int NOT NULL
          , owner_id             int DEFAULT 0
          , position             int
          , is_active            bit DEFAULT 1
          , date_completed       bigint
          , score                int
          , date_due             bigint
          , category_id          int DEFAULT 0
          , creator_id           int DEFAULT 0
          , date_modification    int DEFAULT 0
          , reference            nvarchar(max) DEFAULT ''
          , date_started         bigint
          , time_spent           float DEFAULT 0
          , time_estimated       float DEFAULT 0
          , swimlane_id          int NOT NULL
          , date_moved           bigint DEFAULT 0
          , recurrence_status    int DEFAULT 0 NOT NULL
          , recurrence_trigger   int DEFAULT 0 NOT NULL
          , recurrence_factor    int DEFAULT 0 NOT NULL
          , recurrence_timeframe int DEFAULT 0 NOT NULL
          , recurrence_basedate  int DEFAULT 0 NOT NULL
          , recurrence_parent    int
          , recurrence_child     int
          , priority             int DEFAULT 0
          , external_provider    nvarchar(255)
          , external_uri         nvarchar(255)
          , FOREIGN KEY (project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , FOREIGN KEY (column_id) REFERENCES dbo.columns(id) ON DELETE NO ACTION /* columns_cascade_delete_trigger */
          , FOREIGN KEY (swimlane_id) REFERENCES dbo.swimlanes(id) ON DELETE NO ACTION /* swimlanes_cascade_delete_trigger */
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.task_has_files (
            id int identity PRIMARY KEY
          , name nvarchar(max) NOT NULL
          , path nvarchar(max)
          , is_image bit DEFAULT 0
          , task_id int NOT NULL
          , date bigint NOT NULL DEFAULT 0
          , user_id int NOT NULL DEFAULT 0
          , size int NOT NULL DEFAULT 0
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.subtasks (
            id int identity PRIMARY KEY
          , title nvarchar(max) NOT NULL
          , status smallint DEFAULT 0
          , time_estimated float DEFAULT 0
          , time_spent float DEFAULT 0
          , task_id int NOT NULL
          , user_id int
          , position int DEFAULT 1
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.user_has_notifications (
            user_id int NOT NULL
          , project_id int NOT NULL
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
          , UNIQUE(project_id, user_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.settings (
            [option] nvarchar(100) PRIMARY KEY
          , value nvarchar(max) DEFAULT ''
          , changed_by int DEFAULT 0 NOT NULL
          , changed_on int DEFAULT 0 NOT NULL
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_daily_column_stats (
            id int identity PRIMARY KEY
          , day nchar(10) NOT NULL
          , project_id int NOT NULL
          , column_id int NOT NULL
          , total int NOT NULL DEFAULT 0
          , score int NOT NULL DEFAULT 0
          , FOREIGN KEY(column_id) REFERENCES dbo.columns(id) ON DELETE CASCADE
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.subtask_time_tracking (
            id int identity PRIMARY KEY
          , user_id int NOT NULL
          , subtask_id int NOT NULL
          , [start] bigint DEFAULT 0
          , [end] bigint DEFAULT 0
          , time_spent real DEFAULT 0
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , FOREIGN KEY(subtask_id) REFERENCES dbo.subtasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.links (
            id int identity PRIMARY KEY
          , label nvarchar(255) NOT NULL
          , opposite_id int DEFAULT 0
          , UNIQUE(label)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.task_has_links (
            id int identity PRIMARY KEY
          , link_id int NOT NULL
          , task_id int NOT NULL
          , opposite_task_id int NOT NULL
          , FOREIGN KEY(link_id) REFERENCES dbo.links(id) ON DELETE CASCADE
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
          , FOREIGN KEY(opposite_task_id) REFERENCES dbo.tasks(id) ON DELETE NO ACTION /* Handled in tasks_cascade_delete_trigger */
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.transitions (
            id int identity PRIMARY KEY
          , user_id int NOT NULL
          , project_id int NOT NULL
          , task_id int NOT NULL
          , src_column_id int NOT NULL
          , dst_column_id int NOT NULL
          , date bigint NOT NULL
          , time_spent int DEFAULT 0
          , FOREIGN KEY(src_column_id) REFERENCES dbo.columns(id) ON DELETE NO ACTION  /* columns_cascade_delete_trigger */
          , FOREIGN KEY(dst_column_id) REFERENCES dbo.columns(id) ON DELETE NO ACTION  /* columns_cascade_delete_trigger */
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.currencies (
            currency nvarchar(3) NOT NULL UNIQUE
          , rate REAL DEFAULT 0
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.comments (
            id int identity PRIMARY KEY
          , task_id int NOT NULL
          , user_id int DEFAULT 0
          , date_creation bigint NOT NULL
          , comment nvarchar(max) NOT NULL
          , reference nvarchar(max) DEFAULT N''
          , date_modification bigint
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_daily_stats (
            id int identity PRIMARY KEY
          , day nchar(10) NOT NULL
          , project_id int NOT NULL
          , avg_lead_time int NOT NULL DEFAULT 0
          , avg_cycle_time int NOT NULL DEFAULT 0
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.plugin_schema_versions (
            plugin nvarchar(80) NOT NULL PRIMARY KEY
          , version int NOT NULL DEFAULT 0
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.custom_filters (
            id int identity PRIMARY KEY
          , filter nvarchar(max) NOT NULL
          , project_id int NOT NULL
          , user_id int NOT NULL
          , name nvarchar(max) NOT NULL
          , is_shared bit DEFAULT 0
          , append bit DEFAULT 0
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.user_has_unread_notifications (
            id int identity PRIMARY KEY
          , user_id int NOT NULL
          , date_creation bigint NOT NULL
          , event_name nvarchar(max) NOT NULL
          , event_data nvarchar(max) NOT NULL
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.user_has_notification_types (
            id int identity PRIMARY KEY
          , user_id int NOT NULL
          , notification_type nvarchar(50)
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_notification_types (
            id int identity PRIMARY KEY
          , project_id int NOT NULL
          , notification_type nvarchar(50) NOT NULL
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
          , UNIQUE(project_id, notification_type)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.user_has_metadata (
            user_id int NOT NULL
          , name nvarchar(50) NOT NULL
          , value nvarchar(255) DEFAULT ''
          , changed_by int DEFAULT 0 NOT NULL
          , changed_on int DEFAULT 0 NOT NULL /* TODO: should be bigint?? */
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , UNIQUE(user_id, name)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_metadata (
            project_id int NOT NULL
          , name nvarchar(50) NOT NULL
          , value nvarchar(255) DEFAULT ''
          , changed_by int DEFAULT 0 NOT NULL
          , changed_on int DEFAULT 0 NOT NULL /* TODO: should be bigint?? */
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
          , UNIQUE(project_id, name)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.task_has_metadata (
            task_id int NOT NULL
          , name nvarchar(50) NOT NULL
          , value nvarchar(255) DEFAULT ''
          , changed_by int DEFAULT 0 NOT NULL
          , changed_on int DEFAULT 0 NOT NULL /* TODO: should be bigint?? */
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
          , UNIQUE(task_id, name)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.groups (
            id int identity PRIMARY KEY
          , external_id nvarchar(255) DEFAULT ''
          , name nvarchar(850) NOT NULL UNIQUE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.group_has_users (
            group_id int NOT NULL
          , user_id int NOT NULL
          , FOREIGN KEY(group_id) REFERENCES dbo.groups(id) ON DELETE CASCADE
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , UNIQUE(group_id, user_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_groups (
            group_id int NOT NULL
          , project_id int NOT NULL
          , role nvarchar(255) NOT NULL
          , FOREIGN KEY(group_id) REFERENCES dbo.groups(id) ON DELETE CASCADE
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
          , UNIQUE(group_id, project_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.password_reset (
            token nvarchar(80) PRIMARY KEY
          , user_id int NOT NULL
          , date_expiration int NOT NULL /* TODO: bigint?? */
          , date_creation int NOT NULL /* TODO: bigint?? */
          , ip nvarchar(45) NOT NULL
          , user_agent nvarchar(255) NOT NULL
          , is_active bit NOT NULL
          , FOREIGN KEY(user_id) REFERENCES dbo.users(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.task_has_external_links (
            id int identity PRIMARY KEY
          , link_type nvarchar(100) NOT NULL
          , dependency nvarchar(100) NOT NULL
          , title nvarchar(max) NOT NULL
          , url nvarchar(max) NOT NULL
          , date_creation int NOT NULL /* TODO: bigint?? */
          , date_modification int NOT NULL /* TODO: bigint?? */
          , task_id int NOT NULL
          , creator_id int DEFAULT 0
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_files (
            id int identity PRIMARY KEY
          , project_id int NOT NULL
          , name nvarchar(max) NOT NULL
          , path nvarchar(max) NOT NULL
          , is_image bit DEFAULT 0
          , size int DEFAULT 0 NOT NULL
          , user_id int DEFAULT 0 NOT NULL
          , date int DEFAULT 0 NOT NULL /* TODO: bigint?? */
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.tags (
            id int identity PRIMARY KEY
          , name nvarchar(255) NOT NULL
          , project_id int NOT NULL
          , color_id nvarchar(50) DEFAULT NULL
          , UNIQUE(project_id, name)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.task_has_tags (
            task_id int NOT NULL
          , tag_id int NOT NULL
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
          , FOREIGN KEY(tag_id) REFERENCES dbo.tags(id) ON DELETE CASCADE
          , UNIQUE(tag_id, task_id)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_has_roles (
            role_id int identity PRIMARY KEY
          , role nvarchar(255) NOT NULL
          , project_id int NOT NULL
          , UNIQUE(project_id, role)
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.column_has_move_restrictions (
            restriction_id int identity PRIMARY KEY
          , project_id int NOT NULL
          , role_id int NOT NULL
          , src_column_id int NOT NULL
          , dst_column_id int NOT NULL
          , only_assigned bit DEFAULT 0
          , UNIQUE(role_id, src_column_id, dst_column_id)
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , FOREIGN KEY(role_id) REFERENCES dbo.project_has_roles(role_id) ON DELETE CASCADE
          , FOREIGN KEY(src_column_id) REFERENCES dbo.columns(id) ON DELETE NO ACTION /* columns_cascade_delete_trigger */
          , FOREIGN KEY(dst_column_id) REFERENCES dbo.columns(id) ON DELETE NO ACTION /* columns_cascade_delete_trigger */
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_role_has_restrictions (
            restriction_id int identity PRIMARY KEY
          , project_id int NOT NULL
          , role_id int NOT NULL
          , [rule] nvarchar(255) NOT NULL
          , UNIQUE(role_id, [rule])
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , FOREIGN KEY(role_id) REFERENCES dbo.project_has_roles(role_id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.column_has_restrictions (
            restriction_id int identity PRIMARY KEY
          , project_id int NOT NULL
          , role_id int NOT NULL
          , column_id int NOT NULL
          , [rule] nvarchar(255) NOT NULL
          , UNIQUE(role_id, column_id, [rule])
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , FOREIGN KEY(role_id) REFERENCES dbo.project_has_roles(role_id) ON DELETE CASCADE
          , FOREIGN KEY(column_id) REFERENCES dbo.columns(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.invites (
            email nvarchar(255) NOT NULL
          , project_id int NOT NULL
          , token nvarchar(255) NOT NULL
          , PRIMARY KEY(email, token)
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.project_activities (
            id int identity PRIMARY KEY
          , date_creation bigint NOT NULL
          , event_name nvarchar(max) NOT NULL
          , creator_id int NOT NULL
          , project_id int NOT NULL
          , task_id int NOT NULL
          , data nvarchar(max)
          , FOREIGN KEY(creator_id) REFERENCES dbo.users(id) ON DELETE CASCADE
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE NO ACTION /* projects_cascade_delete_trigger */
          , FOREIGN KEY(task_id) REFERENCES dbo.tasks(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.predefined_task_descriptions (
            id int identity PRIMARY KEY
          , project_id int NOT NULL
          , title nvarchar(max) NOT NULL
          , description nvarchar(max) NOT NULL
          , FOREIGN KEY(project_id) REFERENCES dbo.projects(id) ON DELETE CASCADE
        );
    ");
    $pdo->exec("
        CREATE TABLE dbo.sessions (
            id nvarchar(450) PRIMARY KEY /* max length for primary key */
          , expire_at int NOT NULL
          , data nvarchar(max) DEFAULT ''
        );
    ");

    // create triggers -- each of which must be in its own batch
    $pdo->exec("
        CREATE TRIGGER dbo.columns_cascade_delete_trigger
        ON dbo.columns INSTEAD OF DELETE
        AS
          SET NOCOUNT ON;
          DELETE dbo.column_has_move_restrictions
          WHERE src_column_id IN (SELECT id FROM deleted)
            OR  dst_column_id IN (SELECT id FROM deleted);
          DELETE dbo.transitions
          WHERE src_column_id IN (SELECT id FROM deleted)
            OR  dst_column_id IN (SELECT id FROM deleted);
          DELETE dbo.tasks
          WHERE column_id IN (SELECT id FROM deleted);
          DELETE dbo.columns
          WHERE id IN (SELECT id FROM deleted);
    ");

    $pdo->exec("
        CREATE TRIGGER projects_cascade_delete_trigger
        ON dbo.projects INSTEAD OF DELETE
        AS
          SET NOCOUNT ON;
          DELETE dbo.column_has_move_restrictions
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.column_has_restrictions
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.columns
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.project_activities
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.project_daily_column_stats
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.project_role_has_restrictions
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.swimlanes
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.tasks
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.transitions
          WHERE project_id IN (SELECT id FROM deleted);
          DELETE dbo.projects
          WHERE id IN (SELECT id FROM deleted);
    ");

    $pdo->exec("
        CREATE TRIGGER dbo.swimlanes_cascade_delete_trigger
        ON dbo.swimlanes INSTEAD OF DELETE
        AS
          SET NOCOUNT ON;
          DELETE dbo.tasks
          WHERE swimlane_id IN (SELECT id FROM deleted);
          DELETE dbo.swimlanes
          WHERE id IN (SELECT id FROM deleted);
    ");

    $pdo->exec("
        CREATE TRIGGER dbo.tasks_cascade_delete_trigger
        ON dbo.tasks INSTEAD OF DELETE
        AS
          SET NOCOUNT ON;
          DELETE dbo.task_has_links
          WHERE opposite_task_id IN (SELECT id FROM deleted);
          DELETE dbo.tasks
          WHERE id IN (SELECT id FROM deleted);
    ");

    // set defaults
    $pdo->exec("
        ALTER TABLE dbo.project_has_users
        ADD DEFAULT N'" .Role::PROJECT_VIEWER. "' FOR role;
    ");
    $pdo->exec("
        ALTER TABLE dbo.users
        ADD DEFAULT N'" .Role::APP_USER. "' FOR role;
    ");

    // insert starting data
    $aui = $pdo->prepare("INSERT INTO dbo.users (username, password, role) VALUES (?, ?, ?);");
    $aui->execute(array('admin', \password_hash('admin', PASSWORD_BCRYPT), Role::APP_ADMIN));

    $rq = $pdo->prepare('INSERT INTO dbo.settings ([option],value) VALUES (?, ?);');
    $rq->execute(array('api_token', Token::getToken()));
    $rq->execute(array('application_url', defined('KANBOARD_URL') ? KANBOARD_URL : ''));
    $rq->execute(array('board_highlight_period', defined('RECENT_TASK_PERIOD') ? RECENT_TASK_PERIOD : 48*60*60));
    $rq->execute(array('board_private_refresh_interval', defined('BOARD_CHECK_INTERVAL') ? BOARD_CHECK_INTERVAL : 10));
    $rq->execute(array('board_public_refresh_interval', defined('BOARD_PUBLIC_CHECK_INTERVAL') ? BOARD_PUBLIC_CHECK_INTERVAL : 60));
    $rq->execute(array('webhook_token', Token::getToken()));

    $pdo->exec("
        INSERT INTO dbo.settings ([option], value) VALUES
          ('application_currency','USD'),
          ('application_date_format','m/d/Y'),
          ('application_language','en_US'),
          ('application_stylesheet',''),
          ('application_time_format','H:i'),
          ('application_timezone','UTC'),
          ('board_columns',''),
          ('calendar_project_tasks','date_started'),
          ('calendar_user_subtasks_time_tracking','0'),
          ('calendar_user_tasks','date_started'),
          ('cfd_include_closed_tasks','1'),
          ('default_color','yellow'),
          ('integration_gravatar','0'),
          ('password_reset','1'),
          ('project_categories',''),
          ('subtask_restriction','0'),
          ('subtask_time_tracking','1'),
          ('webhook_url','')
        ;
    ");

    $pdo->exec("
        SET IDENTITY_INSERT dbo.links ON;
        INSERT INTO dbo.links (id, label, opposite_id) VALUES
          (1,'relates to',0),
          (2,'blocks',3),
          (3,'is blocked by',2),
          (4,'duplicates',5),
          (5,'is duplicated by',4),
          (6,'is a child of',7),
          (7,'is a parent of',6),
          (8,'targets milestone',9),
          (9,'is a milestone of',8),
          (10,'fixes',11),
          (11,'is fixed by',10)
        ;
        SET IDENTITY_INSERT dbo.links OFF;
    ");

    // create indexes
    $pdo->exec("
        CREATE UNIQUE INDEX users_username_idx ON dbo.users(username);
        CREATE UNIQUE INDEX project_daily_column_stats_idx ON dbo.project_daily_column_stats(day, project_id, column_id);
        CREATE UNIQUE INDEX task_has_links_unique ON dbo.task_has_links(link_id, task_id, opposite_task_id);
        CREATE UNIQUE INDEX project_daily_stats_idx ON dbo.project_daily_stats(day, project_id);
        CREATE UNIQUE INDEX user_has_notification_types_user_idx ON dbo.user_has_notification_types(user_id, notification_type);

        CREATE INDEX columns_project_idx ON dbo.columns(project_id);
        CREATE INDEX swimlanes_project_idx ON dbo.swimlanes(project_id);
        CREATE INDEX categories_project_idx ON dbo.project_has_categories(project_id);
        CREATE INDEX subtasks_task_idx ON dbo.subtasks(task_id);
        CREATE INDEX files_task_idx ON dbo.task_has_files(task_id);
        CREATE INDEX task_has_links_task_index ON dbo.task_has_links(task_id);
        CREATE INDEX transitions_task_index ON dbo.transitions(task_id);
        CREATE INDEX transitions_project_index ON dbo.transitions(project_id);
        CREATE INDEX transitions_user_index ON dbo.transitions(user_id);
    ");
}