fix: schema migration for minor version

This commit is contained in:
boojack 2022-07-09 13:34:14 +08:00
parent 7c94db0ca0
commit de7058532a
5 changed files with 22 additions and 49 deletions

View file

@ -36,11 +36,10 @@ func (m *Main) Run() error {
storeInstance := store.New(db.Db, m.profile)
s.Store = storeInstance
if err := s.Run(); err != nil {
return err
}
println(greetingBanner)
fmt.Printf("Version %s has started at :%d\n", m.profile.Version, m.profile.Port)
return nil
return s.Run()
}
func Execute() {
@ -56,8 +55,6 @@ func Execute() {
println("dsn:", profile.DSN)
println("version:", profile.Version)
println("---")
println(greetingBanner)
fmt.Printf("Version %s has started at :%d\n", profile.Version, profile.Port)
if err := m.Run(); err != nil {
fmt.Printf("error: %+v\n", err)

View file

@ -80,9 +80,8 @@ func (db *DB) Open() (err error) {
return err
}
if migrationHistory == nil {
migrationHistory, err = upsertMigrationHistory(db.Db, &MigrationHistoryCreate{
Version: currentVersion,
Statement: "",
migrationHistory, err = upsertMigrationHistory(db.Db, &MigrationHistoryUpsert{
Version: currentVersion,
})
if err != nil {
return err
@ -91,16 +90,18 @@ func (db *DB) Open() (err error) {
if common.IsVersionGreaterThan(currentVersion, migrationHistory.Version) {
minorVersionList := getMinorVersionList()
println("start migrate")
for _, minorVersion := range minorVersionList {
normalizedVersion := minorVersion + ".0"
if common.IsVersionGreaterThan(normalizedVersion, migrationHistory.Version) && common.IsVersionGreaterOrEqualThan(currentVersion, normalizedVersion) {
println("applying migration for", normalizedVersion)
err := db.applyMigrationForMinorVersion(minorVersion)
if err != nil {
return fmt.Errorf("failed to apply minor version migration: %w", err)
}
}
}
println("end migrate")
}
}
}
@ -148,9 +149,8 @@ func (db *DB) applyMigrationForMinorVersion(minorVersion string) error {
}
// upsert the newest version to migration_history
if _, err = upsertMigrationHistory(db.Db, &MigrationHistoryCreate{
Version: minorVersion + ".0",
Statement: migrationStmt,
if _, err = upsertMigrationHistory(db.Db, &MigrationHistoryUpsert{
Version: minorVersion + ".0",
}); err != nil {
return err
}
@ -221,29 +221,12 @@ func getMinorVersionList() []string {
// createMigrationHistoryTable creates the migration_history table if it doesn't exist.
func (db *DB) createMigrationHistoryTable() error {
table, err := findTable(db.Db, "migration_history")
if err != nil {
return err
}
// TODO(steven): Drop the migration_history table if it exists temporarily.
if table != nil {
err = db.execute(`
DROP TABLE IF EXISTS migration_history;
`)
if err != nil {
return err
}
}
err = createTable(db.Db, `
CREATE TABLE migration_history (
if err := createTable(db.Db, `
CREATE TABLE IF NOT EXISTS migration_history (
version TEXT NOT NULL PRIMARY KEY,
statement TEXT NOT NULL DEFAULT '',
created_ts BIGINT NOT NULL DEFAULT (strftime('%s', 'now'))
);
`)
if err != nil {
`); err != nil {
return err
}

View file

@ -1,7 +1,6 @@
-- change user role field from "OWNER"/"USER" to "HOST"/"USER".
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;
DROP TABLE IF EXISTS _user_old;
@ -52,5 +51,4 @@ WHERE
DROP TABLE IF EXISTS _user_old;
COMMIT;
PRAGMA foreign_keys = on;

View file

@ -7,13 +7,11 @@ import (
type MigrationHistory struct {
Version string
Statement string
CreatedTs int64
}
type MigrationHistoryCreate struct {
Version string
Statement string
type MigrationHistoryUpsert struct {
Version string
}
type MigrationHistoryFind struct {
@ -71,21 +69,18 @@ func findMigrationHistory(db *sql.DB, find *MigrationHistoryFind) (*MigrationHis
}
}
func upsertMigrationHistory(db *sql.DB, create *MigrationHistoryCreate) (*MigrationHistory, error) {
func upsertMigrationHistory(db *sql.DB, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
row, err := db.Query(`
INSERT INTO migration_history (
version,
statement
version
)
VALUES (?, ?)
VALUES (?)
ON CONFLICT(version) DO UPDATE
SET
version=EXCLUDED.version,
statement=EXCLUDED.statement
RETURNING version, statement, created_ts
version=EXCLUDED.version
RETURNING version, created_ts
`,
create.Version,
create.Statement,
upsert.Version,
)
if err != nil {
return nil, err
@ -96,7 +91,6 @@ func upsertMigrationHistory(db *sql.DB, create *MigrationHistoryCreate) (*Migrat
var migrationHistory MigrationHistory
if err := row.Scan(
&migrationHistory.Version,
&migrationHistory.Statement,
&migrationHistory.CreatedTs,
); err != nil {
return nil, err

View file

@ -10,6 +10,7 @@ type Table struct {
SQL string
}
//lint:ignore U1000 Ignore unused function temporarily for debugging
func findTable(db *sql.DB, tableName string) (*Table, error) {
where, args := []string{"1 = 1"}, []interface{}{}