-
Notifications
You must be signed in to change notification settings - Fork 23
/
migrations.go
66 lines (58 loc) · 1.22 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
package main
import "database/sql"
func applyMigrations(db *sql.DB) error {
// create new table on database
var err error
_, err = db.Exec("CREATE TABLE IF NOT EXISTS seen (url TEXT, date TEXT, summary TEXT)")
if err != nil {
return err
}
err = addSummaryColumnIfNotExists(db)
if err != nil {
return err
}
return nil
}
func addSummaryColumnIfNotExists(db *sql.DB) error {
tx, err := db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
// Check if the 'summary' column already exists in the 'seen' table
rows, err := tx.Query("PRAGMA table_info(seen)")
if err != nil {
return err
}
defer rows.Close()
var columnExists bool
for rows.Next() {
var cid int
var name string
var dataType string
var notNull int
var dfltValue interface{}
var pk int
err := rows.Scan(&cid, &name, &dataType, ¬Null, &dfltValue, &pk)
if err != nil {
return err
}
if name == "summary" {
columnExists = true
break
}
}
// If the 'summary' column doesn't exist, add it to the table
if !columnExists {
_, err = tx.Exec("ALTER TABLE seen ADD COLUMN summary TEXT")
if err != nil {
return err
}
}
// Commit the transaction
err = tx.Commit()
if err != nil {
return err
}
return nil
}