2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-02-19 00:23:44 +08:00
|
|
|
var goMigrations = map[string](func() error){
|
|
|
|
"20190213033530-email-notifications.sql": migrateEmails,
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:51:59 +08:00
|
|
|
func migrate() error {
|
|
|
|
return migrateFromDir(os.Getenv("STATIC") + "/db")
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 14:51:59 +08:00
|
|
|
func migrateFromDir(dir string) error {
|
2018-05-27 22:40:42 +08:00
|
|
|
files, err := ioutil.ReadDir(dir)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read directory for migrations: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 09:31:27 +08:00
|
|
|
SELECT filename
|
|
|
|
FROM migrations;
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
rows, err := db.Query(statement)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot query migrations: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
filenames := make(map[string]bool)
|
|
|
|
for rows.Next() {
|
|
|
|
var filename string
|
|
|
|
if err = rows.Scan(&filename); err != nil {
|
|
|
|
logger.Errorf("cannot scan filename: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
filenames[filename] = true
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
logger.Infof("%d migrations already installed, looking for more", len(filenames))
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
completed := 0
|
|
|
|
for _, file := range files {
|
|
|
|
if strings.HasSuffix(file.Name(), ".sql") {
|
|
|
|
if !filenames[file.Name()] {
|
|
|
|
f := dir + string(os.PathSeparator) + file.Name()
|
|
|
|
contents, err := ioutil.ReadFile(f)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot read file %s: %v", file.Name(), err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = db.Exec(string(contents)); err != nil {
|
|
|
|
logger.Errorf("cannot execute the SQL in %s: %v", f, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
statement = `
|
2019-12-28 09:31:27 +08:00
|
|
|
INSERT INTO
|
|
|
|
migrations (filename)
|
|
|
|
VALUES ($1 );
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
_, err = db.Exec(statement, file.Name())
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot insert filename into the migrations table: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-19 00:23:44 +08:00
|
|
|
if fn, ok := goMigrations[file.Name()]; ok {
|
|
|
|
if err = fn(); err != nil {
|
|
|
|
logger.Errorf("cannot execute Go migration associated with SQL %s: %v", f, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
completed++
|
2022-10-11 00:01:30 +08:00
|
|
|
}
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if completed > 0 {
|
2018-06-20 11:29:55 +08:00
|
|
|
logger.Infof("%d new migrations completed (%d total)", completed, len(filenames)+completed)
|
|
|
|
} else {
|
|
|
|
logger.Infof("none found")
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|