aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjloup <jeanloup.jamet@gmail.com>2018-04-30 21:12:37 +0200
committerjloup <jeanloup.jamet@gmail.com>2018-04-30 21:12:37 +0200
commit4215df47c93c6ea26f21c74a9d52f78d1d103dde (patch)
treec8892d241c0c1f7663a09b4f3b8e1c7d2dc7abc5
parent1fc43bfa508b3579ca5a8944f8fc3e84cacee500 (diff)
downloadFront-4215df47c93c6ea26f21c74a9d52f78d1d103dde.tar.gz
Front-4215df47c93c6ea26f21c74a9d52f78d1d103dde.tar.zst
Front-4215df47c93c6ea26f21c74a9d52f78d1d103dde.zip
Databse migrations.
-rw-r--r--Gopkg.lock8
-rw-r--r--db/db.go86
-rw-r--r--db/migrations.go41
3 files changed, 98 insertions, 37 deletions
diff --git a/Gopkg.lock b/Gopkg.lock
index 1e1aad8..8d11158 100644
--- a/Gopkg.lock
+++ b/Gopkg.lock
@@ -52,6 +52,12 @@
52 version = "v1.2" 52 version = "v1.2"
53 53
54[[projects]] 54[[projects]]
55 name = "github.com/go-pg/migrations"
56 packages = ["."]
57 revision = "91a824bb86a910befc819700ec6e21858c41b022"
58 version = "v6.2.0"
59
60[[projects]]
55 name = "github.com/go-pg/pg" 61 name = "github.com/go-pg/pg"
56 packages = [ 62 packages = [
57 ".", 63 ".",
@@ -156,6 +162,6 @@
156[solve-meta] 162[solve-meta]
157 analyzer-name = "dep" 163 analyzer-name = "dep"
158 analyzer-version = 1 164 analyzer-version = 1
159 inputs-digest = "d3c9b3094ed174bcf1631e3a998a75d557c65c195d7a8fd5ca9912f71f334ce1" 165 inputs-digest = "e3fa83f0fc133d3c408abdc47e5c2ca08062fec7c34ea04c21ea122492ab863d"
160 solver-name = "gps-cdcl" 166 solver-name = "gps-cdcl"
161 solver-version = 1 167 solver-version = 1
diff --git a/db/db.go b/db/db.go
index bc4b8b3..7f87201 100644
--- a/db/db.go
+++ b/db/db.go
@@ -1,9 +1,7 @@
1package db 1package db
2 2
3import ( 3import (
4 "fmt" 4 migrate "github.com/go-pg/migrations"
5 "strings"
6
7 "github.com/go-pg/pg" 5 "github.com/go-pg/pg"
8 "github.com/go-pg/pg/orm" 6 "github.com/go-pg/pg/orm"
9 "github.com/jloup/utils" 7 "github.com/jloup/utils"
@@ -25,15 +23,60 @@ func Init(config DBConfig) {
25 23
26 DB = connect(config) 24 DB = connect(config)
27 25
28 err = createSchema(DB) 26 err = migratedb()
29 if err != nil { 27 if err != nil {
30 log.Errorf("cannot create schemas %v\n", err) 28 log.Fatalf("cannot migratedb '%v'\n", err)
31 } 29 }
30}
32 31
33 err = createIndexes(DB) 32func migratedb() error {
33 /* Remove after first MEP */
34 version, err := migrate.Version(DB)
34 if err != nil { 35 if err != nil {
35 log.Errorf("cannot create indexes %v\n", err) 36 return err
37 }
38
39 if version == 0 {
40 return migrate.SetVersion(DB, 1)
36 } 41 }
42 /***/
43
44 mig := make([]migrate.Migration, 0)
45
46 for _, migration := range migrations {
47 mig = append(mig, migrate.Migration{
48 Version: migration.Version,
49 Up: func(db orm.DB) error {
50 for _, query := range migration.Up {
51 _, err := db.Exec(query)
52 if err != nil {
53 return err
54 }
55 }
56
57 return nil
58 },
59 Down: func(db orm.DB) error {
60 for _, query := range migration.Down {
61 _, err := db.Exec(query)
62 if err != nil {
63 return err
64 }
65 }
66
67 return nil
68 },
69 })
70 }
71
72 oldVersion, newVersion, err := migrate.RunMigrations(DB, mig, "up")
73
74 if oldVersion != newVersion {
75 log.Infof("Migrate DB: %v -> %v", oldVersion, newVersion)
76 } else {
77 log.Infof("DB up-to-date: version '%v'", newVersion)
78 }
79 return err
37} 80}
38 81
39func connect(config DBConfig) *pg.DB { 82func connect(config DBConfig) *pg.DB {
@@ -44,32 +87,3 @@ func connect(config DBConfig) *pg.DB {
44 Addr: config.Address, 87 Addr: config.Address,
45 }) 88 })
46} 89}
47
48func createSchema(db *pg.DB) error {
49 for _, model := range []interface{}{&User{}, &MarketConfig{}} {
50 err := db.CreateTable(model, &orm.CreateTableOptions{IfNotExists: true})
51 if err != nil {
52 return err
53 }
54 }
55 return nil
56}
57
58func createIndexes(db *pg.DB) error {
59 indexes := []struct {
60 TableName string
61 Name string
62 Columns []string
63 }{
64 {"market_configs", "market_name_user_id_idx", []string{"user_id", "market_name"}},
65 }
66
67 for _, index := range indexes {
68 _, err := db.Exec(fmt.Sprintf("CREATE UNIQUE INDEX IF NOT EXISTS %s ON %s (%s)", index.Name, index.TableName, strings.Join(index.Columns, ",")))
69 if err != nil {
70 return err
71 }
72 }
73
74 return nil
75}
diff --git a/db/migrations.go b/db/migrations.go
new file mode 100644
index 0000000..ce5caf6
--- /dev/null
+++ b/db/migrations.go
@@ -0,0 +1,41 @@
1package db
2
3type Migration struct {
4 Version int64
5 Up []string
6 Down []string
7}
8
9var migrations []Migration = []Migration{
10 {
11 Version: 1,
12 Up: []string{
13 `CREATE TABLE users (
14 id BIGSERIAL PRIMARY KEY,
15 email text NOT NULL,
16 password_hash text NOT NULL,
17 otp_secret text,
18 is_otp_setup boolean,
19 status smallint,
20 UNIQUE(email)
21 )`,
22 `CREATE TABLE market_configs (
23 id BIGSERIAL PRIMARY KEY,
24 market_name text NOT NULL,
25 user_id bigint NOT NULL REFERENCES users(id),
26 config jsonb,
27 UNIQUE(user_id, market_name)
28 )`,
29 `CREATE TABLE report_lines (
30 id BIGSERIAL PRIMARY KEY,
31 date timestamp with time zone NOT NULL,
32 report_id bigint NOT NULL,
33 type text,
34 payload jsonb
35 )`,
36 "CREATE INDEX IF NOT EXISTS report_lines_report_id ON report_lines (report_id)",
37 "CREATE INDEX IF NOT EXISTS report_lines_type ON report_lines (type)",
38 },
39 Down: []string{"DROP TABLE users", "DROP TABLE market_configs", "DROP TABLE report_lines"},
40 },
41}