embed schema migration scripts
This commit is contained in:
parent
e5dcc4b884
commit
1999ab7d2c
6 changed files with 15 additions and 6 deletions
|
@ -1,14 +1,15 @@
|
||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"embed"
|
||||||
"os"
|
"fmt"
|
||||||
"time"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DB_VERSION int = 4
|
const DB_VERSION int = 5
|
||||||
|
|
||||||
func CheckDBVersionAndMigrate(db *sqlx.DB) {
|
func CheckDBVersionAndMigrate(db *sqlx.DB) {
|
||||||
db.MustExec("CREATE SCHEMA IF NOT EXISTS arimelody")
|
db.MustExec("CREATE SCHEMA IF NOT EXISTS arimelody")
|
||||||
|
@ -49,16 +50,23 @@ func CheckDBVersionAndMigrate(db *sqlx.DB) {
|
||||||
ApplyMigration(db, "003-fail-lock")
|
ApplyMigration(db, "003-fail-lock")
|
||||||
oldDBVersion = 4
|
oldDBVersion = 4
|
||||||
|
|
||||||
|
case 4:
|
||||||
|
ApplyMigration(db, "004-test")
|
||||||
|
oldDBVersion = 5
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("Database schema up to date.\n")
|
fmt.Printf("Database schema up to date.\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed "schema-migration"
|
||||||
|
var schemaFS embed.FS
|
||||||
|
|
||||||
func ApplyMigration(db *sqlx.DB, scriptFile string) {
|
func ApplyMigration(db *sqlx.DB, scriptFile string) {
|
||||||
fmt.Printf("Applying schema migration %s...\n", scriptFile)
|
fmt.Printf("Applying schema migration %s...\n", scriptFile)
|
||||||
|
|
||||||
bytes, err := os.ReadFile("schema-migration/" + scriptFile + ".sql")
|
bytes, err := schemaFS.ReadFile("schema-migration/" + scriptFile + ".sql")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "FATAL: Failed to open schema file \"%s\": %v\n", scriptFile, err)
|
fmt.Fprintf(os.Stderr, "FATAL: Failed to open schema file \"%s\": %v\n", scriptFile, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
1
controller/schema-migration/004-test.sql
Normal file
1
controller/schema-migration/004-test.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
INSERT INTO arimelody.auditlog (level, type, content) VALUES (0, "test", "this is a db schema migration test!");
|
Loading…
Add table
Add a link
Reference in a new issue