package db
import (
- "fmt"
- "strings"
-
+ migrate "github.com/go-pg/migrations"
"github.com/go-pg/pg"
"github.com/go-pg/pg/orm"
"github.com/jloup/utils"
DB = connect(config)
- err = createSchema(DB)
+ err = migratedb()
if err != nil {
- log.Errorf("cannot create schemas %v\n", err)
+ log.Fatalf("cannot migratedb '%v'\n", err)
}
+}
- err = createIndexes(DB)
+func migratedb() error {
+ /* Remove after first MEP */
+ version, err := migrate.Version(DB)
if err != nil {
- log.Errorf("cannot create indexes %v\n", err)
+ return err
+ }
+
+ if version == 0 {
+ return migrate.SetVersion(DB, 1)
}
+ /***/
+
+ mig := make([]migrate.Migration, 0)
+
+ for _, migration := range migrations {
+ mig = append(mig, migrate.Migration{
+ Version: migration.Version,
+ Up: func(db orm.DB) error {
+ for _, query := range migration.Up {
+ _, err := db.Exec(query)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+ },
+ Down: func(db orm.DB) error {
+ for _, query := range migration.Down {
+ _, err := db.Exec(query)
+ if err != nil {
+ return err
+ }
+ }
+
+ return nil
+ },
+ })
+ }
+
+ oldVersion, newVersion, err := migrate.RunMigrations(DB, mig, "up")
+
+ if oldVersion != newVersion {
+ log.Infof("Migrate DB: %v -> %v", oldVersion, newVersion)
+ } else {
+ log.Infof("DB up-to-date: version '%v'", newVersion)
+ }
+ return err
}
func connect(config DBConfig) *pg.DB {
Addr: config.Address,
})
}
-
-func createSchema(db *pg.DB) error {
- for _, model := range []interface{}{&User{}, &MarketConfig{}} {
- err := db.CreateTable(model, &orm.CreateTableOptions{IfNotExists: true})
- if err != nil {
- return err
- }
- }
- return nil
-}
-
-func createIndexes(db *pg.DB) error {
- indexes := []struct {
- TableName string
- Name string
- Columns []string
- }{
- {"market_configs", "market_name_user_id_idx", []string{"user_id", "market_name"}},
- }
-
- for _, index := range indexes {
- _, err := db.Exec(fmt.Sprintf("CREATE UNIQUE INDEX IF NOT EXISTS %s ON %s (%s)", index.Name, index.TableName, strings.Join(index.Columns, ",")))
- if err != nil {
- return err
- }
- }
-
- return nil
-}
--- /dev/null
+package db
+
+type Migration struct {
+ Version int64
+ Up []string
+ Down []string
+}
+
+var migrations []Migration = []Migration{
+ {
+ Version: 1,
+ Up: []string{
+ `CREATE TABLE users (
+ id BIGSERIAL PRIMARY KEY,
+ email text NOT NULL,
+ password_hash text NOT NULL,
+ otp_secret text,
+ is_otp_setup boolean,
+ status smallint,
+ UNIQUE(email)
+ )`,
+ `CREATE TABLE market_configs (
+ id BIGSERIAL PRIMARY KEY,
+ market_name text NOT NULL,
+ user_id bigint NOT NULL REFERENCES users(id),
+ config jsonb,
+ UNIQUE(user_id, market_name)
+ )`,
+ `CREATE TABLE report_lines (
+ id BIGSERIAL PRIMARY KEY,
+ date timestamp with time zone NOT NULL,
+ report_id bigint NOT NULL,
+ type text,
+ payload jsonb
+ )`,
+ "CREATE INDEX IF NOT EXISTS report_lines_report_id ON report_lines (report_id)",
+ "CREATE INDEX IF NOT EXISTS report_lines_type ON report_lines (type)",
+ },
+ Down: []string{"DROP TABLE users", "DROP TABLE market_configs", "DROP TABLE report_lines"},
+ },
+}