-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate.go
89 lines (78 loc) · 1.77 KB
/
migrate.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
package schema
import (
"github.com/GuiaBolso/darwin"
"github.com/jmoiron/sqlx"
)
// migrations contains the queries needed to construct the database schema.
// Entries should never be removed from this slice once they have been ran in
// production.
//
// Including the queries directly in this file has the same pros/cons mentioned
// in seeds.go
var migrations = []darwin.Migration{
{
Version: 1,
Description: "Add cards",
Script: `
CREATE TABLE cards (
card_id UUID,
name TEXT,
content TEXT,
date_created TIMESTAMP,
date_updated TIMESTAMP,
PRIMARY KEY (card_id)
);`,
},
{
Version: 2,
Description: "Add menus",
Script: `
CREATE TABLE menus (
menu_id UUID,
name TEXT,
card_id UUID,
date_created TIMESTAMP,
date_updated TIMESTAMP,
PRIMARY KEY (menu_id),
FOREIGN KEY (card_id) REFERENCES cards(card_id) ON DELETE CASCADE
);`,
},
{
Version: 3,
Description: "Add users",
Script: `
CREATE TABLE users (
user_id UUID,
name TEXT,
email TEXT UNIQUE,
roles TEXT[],
password_hash TEXT,
date_created TIMESTAMP,
date_updated TIMESTAMP,
PRIMARY KEY (user_id)
);`,
},
{
Version: 4,
Description: "Add user column to cards",
Script: `
ALTER TABLE cards
ADD COLUMN user_id UUID DEFAULT '00000000-0000-0000-0000-000000000000'
`,
},
{
Version: 5,
Description: "Add user column to menus",
Script: `
ALTER TABLE menus
ADD COLUMN user_id UUID DEFAULT '00000000-0000-0000-0000-000000000000'
`,
},
}
// Migrate attempts to bring the schema for db up to date with the migrations
// defined in this package.
func Migrate(db *sqlx.DB) error {
driver := darwin.NewGenericDriver(db.DB, darwin.PostgresDialect{})
d := darwin.New(driver, migrations, nil)
return d.Migrate()
}