-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrations.go
136 lines (108 loc) · 2.61 KB
/
migrations.go
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
package migrations
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
_ "github.com/mattn/go-sqlite3"
)
func RunMigrations(db *sql.DB) error {
tx, err := db.BeginTx(context.Background(), nil)
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()
err = migration1(tx)
if err != nil {
return err
}
err = migration2(tx)
if err != nil {
return err
}
latestMigration := getLatestMigration(tx)
if latestMigration < 3 {
err = migration3(tx)
if err != nil {
return err
}
}
return tx.Commit()
}
func migration1(tx *sql.Tx) error {
statements := make([]string, 0)
statements = append(statements, `
CREATE TABLE IF NOT EXISTS bookmarks (
id INTEGER PRIMARY KEY,
url TEXT NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
is_private BOOL NOT NULL,
created TEXT NOT NULL)`)
statements = append(statements, `
CREATE TABLE IF NOT EXISTS bookmark_tags (
bookmark_id INTEGER,
tag VARCHAR(30))`)
for _, statement := range statements {
_, err := tx.Exec(statement)
if err != nil {
return err
}
}
return nil
}
func migration2(tx *sql.Tx) error {
// Hack to check if this version is already applied
var name string
row := tx.QueryRow("SELECT name FROM sqlite_master WHERE type='table' AND name='migrations'")
err := row.Scan(&name)
if err == nil {
return nil
} else if !errors.Is(err, sql.ErrNoRows) {
return err
}
statements := make([]string, 0)
statements = append(statements, `
CREATE TABLE IF NOT EXISTS migrations (
version INTEGER PRIMARY KEY,
created TEXT NOT NULL)`)
statements = append(statements, `
ALTER TABLE bookmarks
ADD COLUMN is_working BOOLEAN NOT NULL DEFAULT true`)
statements = append(statements, fmt.Sprintf(`
INSERT INTO migrations (version, created) VALUES (%d, "%s")`, 2, time.Now().UTC().Format(time.RFC3339)))
for _, statement := range statements {
_, err := tx.Exec(statement)
if err != nil {
return err
}
}
return nil
}
func migration3(tx *sql.Tx) error {
statements := make([]string, 0)
statements = append(statements, `
ALTER TABLE bookmarks
ADD COLUMN ignore_check BOOLEAN NOT NULL DEFAULT false`)
statements = append(statements, fmt.Sprintf(`
INSERT INTO migrations (version, created) VALUES (%d, "%s")`, 3, time.Now().UTC().Format(time.RFC3339)))
for _, statement := range statements {
_, err := tx.Exec(statement)
if err != nil {
return err
}
}
return nil
}
func getLatestMigration(tx *sql.Tx) int64 {
var version int64
row := tx.QueryRow("SELECT version FROM migrations ORDER BY version DESC LIMIT 1")
err := row.Scan(&version)
if err != nil {
return 0
}
return version
}