@@ -2,6 +2,7 @@ package migrations
22
33import (
44 "context"
5+ "fmt"
56
67 "gorm.io/gorm"
78)
@@ -21,45 +22,69 @@ func (m *InitialSchemaMigration) Name() string {
2122}
2223
2324func (m * InitialSchemaMigration ) Up (ctx context.Context , db * gorm.DB ) error {
25+ dialect := db .Dialector .Name ()
26+
27+ var (
28+ autoInc string
29+ strCol string
30+ idxIne string
31+ tokenIdxCol string
32+ )
33+
34+ switch dialect {
35+ case "sqlite" :
36+ autoInc = "AUTOINCREMENT"
37+ strCol = "TEXT"
38+ idxIne = "IF NOT EXISTS "
39+ tokenIdxCol = "token"
40+ case "mysql" :
41+ autoInc = "AUTO_INCREMENT"
42+ strCol = "VARCHAR(255)"
43+ idxIne = ""
44+ tokenIdxCol = "token(255)"
45+ default :
46+ return fmt .Errorf ("unsupported dialect: %s" , dialect )
47+ }
48+
2449 statements := []string {
25- `CREATE TABLE IF NOT EXISTS users (
26- id INTEGER PRIMARY KEY AUTOINCREMENT ,
27- display_name TEXT NOT NULL DEFAULT '',
28- email TEXT NOT NULL DEFAULT '' UNIQUE,
29- password TEXT NOT NULL DEFAULT '',
50+ fmt . Sprintf ( `CREATE TABLE IF NOT EXISTS users (
51+ id INTEGER PRIMARY KEY %s ,
52+ display_name %s NOT NULL DEFAULT '',
53+ email %s NOT NULL DEFAULT '' UNIQUE,
54+ password %s NOT NULL DEFAULT '',
3055 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
3156 updated_at DATETIME DEFAULT NULL,
3257 disabled BOOLEAN DEFAULT false
33- )` ,
58+ )` , autoInc , strCol , strCol , strCol ),
3459 `CREATE TABLE IF NOT EXISTS user_password_resets (
3560 user_id INTEGER NOT NULL PRIMARY KEY,
3661 email TEXT NOT NULL,
3762 token TEXT NOT NULL,
3863 expiration_date DATETIME NOT NULL
3964 )` ,
40- `CREATE TABLE IF NOT EXISTS app_tokens (
41- id INTEGER PRIMARY KEY AUTOINCREMENT ,
65+ fmt . Sprintf ( `CREATE TABLE IF NOT EXISTS app_tokens (
66+ id INTEGER PRIMARY KEY %s ,
4267 user_id INTEGER NOT NULL,
43- name TEXT NOT NULL,
44- token TEXT NOT NULL,
68+ name %s NOT NULL,
69+ token %s NOT NULL,
4570 scopes TEXT,
4671 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
4772 expires_at DATETIME DEFAULT CURRENT_TIMESTAMP,
4873 CONSTRAINT fk_users_app_tokens FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
49- )` ,
50- `CREATE INDEX IF NOT EXISTS idx_app_tokens_token ON app_tokens(token)` ,
51- `CREATE TABLE IF NOT EXISTS labels (
52- id INTEGER PRIMARY KEY AUTOINCREMENT ,
53- name TEXT NOT NULL,
74+ )` , autoInc , strCol , strCol ),
75+ fmt . Sprintf ( `CREATE INDEX %sidx_app_tokens_token ON app_tokens(%s)` , idxIne , tokenIdxCol ) ,
76+ fmt . Sprintf ( `CREATE TABLE IF NOT EXISTS labels (
77+ id INTEGER PRIMARY KEY %s ,
78+ name %s NOT NULL,
5479 color VARCHAR(7) NOT NULL,
5580 created_by INTEGER NOT NULL,
5681 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
5782 updated_at DATETIME DEFAULT NULL,
5883 CONSTRAINT fk_users_labels FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
59- )` ,
60- `CREATE INDEX IF NOT EXISTS idx_labels_created_by ON labels(created_by)` ,
61- `CREATE TABLE IF NOT EXISTS tasks (
62- id INTEGER PRIMARY KEY AUTOINCREMENT ,
84+ )` , autoInc , strCol ),
85+ fmt . Sprintf ( `CREATE INDEX %sidx_labels_created_by ON labels(created_by)` , idxIne ) ,
86+ fmt . Sprintf ( `CREATE TABLE IF NOT EXISTS tasks (
87+ id INTEGER PRIMARY KEY %s ,
6388 title TEXT NOT NULL,
6489 frequency_type VARCHAR(9),
6590 frequency_on VARCHAR(18) DEFAULT NULL,
@@ -79,25 +104,25 @@ func (m *InitialSchemaMigration) Up(ctx context.Context, db *gorm.DB) error {
79104 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
80105 updated_at DATETIME DEFAULT NULL,
81106 CONSTRAINT fk_users_tasks FOREIGN KEY (created_by) REFERENCES users(id) ON DELETE CASCADE
82- )` ,
83- `CREATE INDEX IF NOT EXISTS idx_tasks_next_due_date ON tasks(next_due_date)` ,
84- `CREATE INDEX IF NOT EXISTS idx_tasks_created_by ON tasks(created_by)` ,
85- `CREATE INDEX IF NOT EXISTS idx_tasks_is_active ON tasks(is_active)` ,
107+ )` , autoInc ),
108+ fmt . Sprintf ( `CREATE INDEX %sidx_tasks_next_due_date ON tasks(next_due_date)` , idxIne ) ,
109+ fmt . Sprintf ( `CREATE INDEX %sidx_tasks_created_by ON tasks(created_by)` , idxIne ) ,
110+ fmt . Sprintf ( `CREATE INDEX %sidx_tasks_is_active ON tasks(is_active)` , idxIne ) ,
86111 `CREATE TABLE IF NOT EXISTS task_labels (
87112 task_id INTEGER NOT NULL,
88113 label_id INTEGER NOT NULL,
89114 PRIMARY KEY (task_id, label_id),
90115 CONSTRAINT fk_task_labels_task FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE,
91116 CONSTRAINT fk_task_labels_label FOREIGN KEY (label_id) REFERENCES labels(id) ON DELETE CASCADE
92117 )` ,
93- `CREATE TABLE IF NOT EXISTS task_histories (
94- id INTEGER PRIMARY KEY AUTOINCREMENT ,
118+ fmt . Sprintf ( `CREATE TABLE IF NOT EXISTS task_histories (
119+ id INTEGER PRIMARY KEY %s ,
95120 task_id INTEGER NOT NULL,
96121 completed_date DATETIME,
97122 due_date DATETIME,
98123 CONSTRAINT fk_tasks_history FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
99- )` ,
100- `CREATE INDEX IF NOT EXISTS idx_task_histories_task_id ON task_histories(task_id)` ,
124+ )` , autoInc ),
125+ fmt . Sprintf ( `CREATE INDEX %sidx_task_histories_task_id ON task_histories(task_id)` , idxIne ) ,
101126 `CREATE TABLE IF NOT EXISTS notification_settings (
102127 user_id INTEGER NOT NULL,
103128 notifications_provider_type VARCHAR(7),
@@ -110,8 +135,8 @@ func (m *InitialSchemaMigration) Up(ctx context.Context, db *gorm.DB) error {
110135 notifications_triggers_overdue BOOLEAN DEFAULT false,
111136 CONSTRAINT fk_users_notification_settings FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
112137 )` ,
113- `CREATE TABLE IF NOT EXISTS notifications (
114- id INTEGER PRIMARY KEY AUTOINCREMENT ,
138+ fmt . Sprintf ( `CREATE TABLE IF NOT EXISTS notifications (
139+ id INTEGER PRIMARY KEY %s ,
115140 task_id INTEGER NOT NULL,
116141 user_id INTEGER NOT NULL,
117142 text TEXT NOT NULL,
@@ -121,11 +146,11 @@ func (m *InitialSchemaMigration) Up(ctx context.Context, db *gorm.DB) error {
121146 created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
122147 CONSTRAINT fk_tasks_notifications FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE,
123148 CONSTRAINT fk_users_notifications FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
124- )` ,
125- `CREATE INDEX IF NOT EXISTS idx_notifications_task_id ON notifications(task_id)` ,
126- `CREATE INDEX IF NOT EXISTS idx_notifications_user_id ON notifications(user_id)` ,
127- `CREATE INDEX IF NOT EXISTS idx_notifications_is_sent ON notifications(is_sent)` ,
128- `CREATE INDEX IF NOT EXISTS idx_notifications_scheduled_for ON notifications(scheduled_for)` ,
149+ )` , autoInc ),
150+ fmt . Sprintf ( `CREATE INDEX %sidx_notifications_task_id ON notifications(task_id)` , idxIne ) ,
151+ fmt . Sprintf ( `CREATE INDEX %sidx_notifications_user_id ON notifications(user_id)` , idxIne ) ,
152+ fmt . Sprintf ( `CREATE INDEX %sidx_notifications_is_sent ON notifications(is_sent)` , idxIne ) ,
153+ fmt . Sprintf ( `CREATE INDEX %sidx_notifications_scheduled_for ON notifications(scheduled_for)` , idxIne ) ,
129154 }
130155
131156 for _ , stmt := range statements {
0 commit comments