sso/cmd/migrator/main.go

45 lines
1019 B
Go

package main
import (
"errors"
"flag"
"fmt"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/golang-migrate/migrate/v4/source/file"
)
func main() {
var storagePath, migrationsPath, migtrationsTable string
flag.StringVar(&storagePath, "storage-path", "", "path to db file")
flag.StringVar(&migrationsPath, "migrations-path", "", "path to migrations")
flag.StringVar(&migtrationsTable, "migrations-table", "migrations", "name of migrations table")
flag.Parse()
if storagePath == "" {
panic("storage-path is required")
}
if migrationsPath == "" {
panic("migrations-path")
}
m, err := migrate.New("file://"+migrationsPath, fmt.Sprintf("sqlite3://%s?x-migrations-table=%s", storagePath, migtrationsTable))
if err != nil {
panic(err)
}
if err := m.Up(); err != nil {
if errors.Is(err, migrate.ErrNoChange) {
fmt.Println("no migrations apply")
return
}
panic(err)
}
fmt.Println("migrations applied successfully")
}