Check during startup if the database schema is up to date
This commit is contained in:
parent
05b88c8add
commit
e494d6e381
2 changed files with 16 additions and 2 deletions
|
@ -115,6 +115,10 @@ func Parse() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := database.IsSchemaUpToDate(db); err != nil {
|
||||||
|
logger.Fatal(`You must run the SQL migrations, %v`, err)
|
||||||
|
}
|
||||||
|
|
||||||
store := storage.NewStorage(db)
|
store := storage.NewStorage(db)
|
||||||
|
|
||||||
if flagResetFeedErrors {
|
if flagResetFeedErrors {
|
||||||
|
|
|
@ -17,7 +17,7 @@ const schemaVersion = 28
|
||||||
// Migrate executes database migrations.
|
// Migrate executes database migrations.
|
||||||
func Migrate(db *sql.DB) {
|
func Migrate(db *sql.DB) {
|
||||||
var currentVersion int
|
var currentVersion int
|
||||||
db.QueryRow(`select version from schema_version`).Scan(¤tVersion)
|
db.QueryRow(`SELECT version FROM schema_version`).Scan(¤tVersion)
|
||||||
|
|
||||||
fmt.Println("Current schema version:", currentVersion)
|
fmt.Println("Current schema version:", currentVersion)
|
||||||
fmt.Println("Latest schema version:", schemaVersion)
|
fmt.Println("Latest schema version:", schemaVersion)
|
||||||
|
@ -43,7 +43,7 @@ func Migrate(db *sql.DB) {
|
||||||
logger.Fatal("[Migrate] %v", err)
|
logger.Fatal("[Migrate] %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := tx.Exec(`insert into schema_version (version) values($1)`, version); err != nil {
|
if _, err := tx.Exec(`INSERT INTO schema_version (version) VALUES ($1)`, version); err != nil {
|
||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
logger.Fatal("[Migrate] %v", err)
|
logger.Fatal("[Migrate] %v", err)
|
||||||
}
|
}
|
||||||
|
@ -53,3 +53,13 @@ func Migrate(db *sql.DB) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsSchemaUpToDate checks if the database schema is up to date.
|
||||||
|
func IsSchemaUpToDate(db *sql.DB) error {
|
||||||
|
var currentVersion int
|
||||||
|
db.QueryRow(`SELECT version FROM schema_version`).Scan(¤tVersion)
|
||||||
|
if currentVersion != schemaVersion {
|
||||||
|
return fmt.Errorf(`database schema is not up to date: current=v%d expected=v%d`, currentVersion, schemaVersion)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue