experimental-cli/project/migrations.go

51 lines
904 B
Go
Raw Normal View History

2020-09-11 12:44:10 +00:00
package main
import (
"database/sql"
)
type migration struct {
up string
down string
}
var migrations = []migration{
migration{
up: `CREATE TABLE IF NOT EXISTS configs (
key string NOT NULL,
value string NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_configs_key ON configs (key);
`,
down: `DROP TABLE IF EXISTS configs`,
},
migration{
up: `CREATE TABLE IF NOT EXISTS peers (
2020-09-11 12:44:10 +00:00
mhash string NOT NULL,
status string NOT NULL
);
CREATE UNIQUE INDEX IF NOT EXISTS unique_peers_mhash ON peers (mhash);
2020-09-11 12:44:10 +00:00
`,
down: `DROP TABLE IF EXISTS peers`,
2020-09-11 12:44:10 +00:00
},
}
func migrateUp(db *sql.DB) {
tx, err := db.Begin()
if err != nil {
2020-09-24 23:43:11 +00:00
panicf("Failed to start transaction: %s", err)
2020-09-11 12:44:10 +00:00
}
for i, migration := range migrations {
_, err := tx.Exec(migration.up)
if err != nil {
2020-09-24 23:43:11 +00:00
panicf("Migration failure(%d): %s", i, err)
2020-09-11 12:44:10 +00:00
}
}
if tx.Commit() != nil {
2020-09-24 23:43:11 +00:00
panic(err)
2020-09-11 12:44:10 +00:00
}
}