chore: fix sqlite migrator

This commit is contained in:
Steven 2023-10-06 00:34:06 +08:00
parent 7791fb10d8
commit c0422dea5b
2 changed files with 22 additions and 13 deletions

View File

@ -15,17 +15,17 @@ func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
var err error
// As mysql driver is not fully implemented, we use sqlite for now in prod mode.
if profile.Mode == "prod" {
if profile.Mode == "prod" && profile.Driver != "sqlite" {
return nil, errors.New("Only SQLite is supported in prod mode")
}
switch profile.Driver {
case "sqlite":
driver, err = sqlite.NewDB(profile)
} else {
switch profile.Driver {
case "sqlite":
driver, err = sqlite.NewDB(profile)
case "mysql":
driver, err = mysql.NewDB(profile)
default:
return nil, errors.New("unknown db driver")
}
case "mysql":
driver, err = mysql.NewDB(profile)
default:
return nil, errors.New("unknown db driver")
}
if err != nil {
return nil, errors.Wrap(err, "failed to create db driver")

View File

@ -23,6 +23,7 @@ var seedFS embed.FS
// Migrate applies the latest schema to the database.
func (d *DB) Migrate(ctx context.Context) error {
currentVersion := version.GetCurrentVersion(d.profile.Mode)
if d.profile.Mode == "prod" {
_, err := os.Stat(d.profile.DSN)
if err != nil {
@ -31,17 +32,27 @@ func (d *DB) Migrate(ctx context.Context) error {
if err := d.applyLatestSchema(ctx); err != nil {
return errors.Wrap(err, "failed to apply latest schema")
}
// Upsert the newest version to migration_history.
if _, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
Version: currentVersion,
}); err != nil {
return errors.Wrap(err, "failed to upsert migration history")
}
} else {
return errors.Wrap(err, "failed to get db file stat")
}
} else {
// If db file exists, we should check if we need to migrate the database.
currentVersion := version.GetCurrentVersion(d.profile.Mode)
migrationHistoryList, err := d.FindMigrationHistoryList(ctx, &MigrationHistoryFind{})
if err != nil {
return errors.Wrap(err, "failed to find migration history")
}
// If no migration history, we should apply the latest version migration and upsert the migration history.
if len(migrationHistoryList) == 0 {
minorVersion := version.GetMinorVersion(currentVersion)
if err := d.applyMigrationForMinorVersion(ctx, minorVersion); err != nil {
return errors.Wrapf(err, "failed to apply version %s migration", minorVersion)
}
_, err := d.UpsertMigrationHistory(ctx, &MigrationHistoryUpsert{
Version: currentVersion,
})
@ -60,7 +71,6 @@ func (d *DB) Migrate(ctx context.Context) error {
if version.IsVersionGreaterThan(version.GetSchemaVersion(currentVersion), latestMigrationHistoryVersion) {
minorVersionList := getMinorVersionList()
// backup the raw database file before migration
rawBytes, err := os.ReadFile(d.profile.DSN)
if err != nil {
@ -71,7 +81,6 @@ func (d *DB) Migrate(ctx context.Context) error {
return errors.Wrap(err, "failed to write raw database file")
}
println("succeed to copy a backup database file")
println("start migrate")
for _, minorVersion := range minorVersionList {
normalizedVersion := minorVersion + ".0"