updated logic to check if migration should run for fresh install

pull/4094/head
RoccoSmit 8 months ago
parent 1f8da374cb
commit b44dca2194

@ -35,7 +35,8 @@ const (
// Migrate applies the latest schema to the database. // Migrate applies the latest schema to the database.
func (s *Store) Migrate(ctx context.Context) error { func (s *Store) Migrate(ctx context.Context) error {
if err := s.preMigrate(ctx); err != nil { isFirstMigration, err := s.preMigrate(ctx)
if err != nil {
return errors.Wrap(err, "failed to pre-migrate") return errors.Wrap(err, "failed to pre-migrate")
} }
@ -59,7 +60,7 @@ func (s *Store) Migrate(ctx context.Context) error {
return errors.Wrap(err, "failed to get current schema version") return errors.Wrap(err, "failed to get current schema version")
} }
if version.IsVersionGreaterThan(schemaVersion, latestMigrationHistoryVersion) { if isFirstMigration || version.IsVersionGreaterThan(schemaVersion, latestMigrationHistoryVersion) {
filePaths, err := fs.Glob(migrationFS, fmt.Sprintf("%s*/*.sql", s.getMigrationBasePath())) filePaths, err := fs.Glob(migrationFS, fmt.Sprintf("%s*/*.sql", s.getMigrationBasePath()))
if err != nil { if err != nil {
return errors.Wrap(err, "failed to read migration files") return errors.Wrap(err, "failed to read migration files")
@ -79,7 +80,7 @@ func (s *Store) Migrate(ctx context.Context) error {
if err != nil { if err != nil {
return errors.Wrap(err, "failed to get schema version of migrate script") return errors.Wrap(err, "failed to get schema version of migrate script")
} }
if version.IsVersionGreaterThan(fileSchemaVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(schemaVersion, fileSchemaVersion) { if version.IsVersionGreaterOrEqualThan(fileSchemaVersion, latestMigrationHistoryVersion) && version.IsVersionGreaterOrEqualThan(schemaVersion, fileSchemaVersion) {
bytes, err := migrationFS.ReadFile(filePath) bytes, err := migrationFS.ReadFile(filePath)
if err != nil { if err != nil {
return errors.Wrapf(err, "failed to read minor version migration file: %s", filePath) return errors.Wrapf(err, "failed to read minor version migration file: %s", filePath)
@ -116,53 +117,55 @@ func (s *Store) Migrate(ctx context.Context) error {
return nil return nil
} }
func (s *Store) preMigrate(ctx context.Context) error { func (s *Store) preMigrate(ctx context.Context) (isFirstMigration bool, err error) {
// TODO: using schema version in basic setting instead of migration history. // TODO: using schema version in basic setting instead of migration history.
migrationHistoryList, err := s.driver.FindMigrationHistoryList(ctx, &FindMigrationHistory{}) migrationHistoryList, err := s.driver.FindMigrationHistoryList(ctx, &FindMigrationHistory{})
// If any error occurs or no migration history found, apply the latest schema. // If any error occurs or no migration history found, apply the latest schema.
if err != nil || len(migrationHistoryList) == 0 { if err != nil || len(migrationHistoryList) == 0 {
isFirstMigration = true
if err != nil { if err != nil {
slog.Warn("failed to find migration history in pre-migrate", slog.String("error", err.Error())) slog.Warn("failed to find migration history in pre-migrate", slog.String("error", err.Error()))
} }
filePath := s.getMigrationBasePath() + LatestSchemaFileName filePath := s.getMigrationBasePath() + LatestSchemaFileName
bytes, err := migrationFS.ReadFile(filePath) bytes, err := migrationFS.ReadFile(filePath)
if err != nil { if err != nil {
return errors.Errorf("failed to read latest schema file: %s", err) return isFirstMigration, errors.Errorf("failed to read latest schema file: %s", err)
} }
schemaVersion, err := s.GetCurrentSchemaVersion() schemaVersion, err := s.GetCurrentSchemaVersion()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to get current schema version") return isFirstMigration, errors.Wrap(err, "failed to get current schema version")
} }
// Start a transaction to apply the latest schema. // Start a transaction to apply the latest schema.
tx, err := s.driver.GetDB().Begin() tx, err := s.driver.GetDB().Begin()
if err != nil { if err != nil {
return errors.Wrap(err, "failed to start transaction") return isFirstMigration, errors.Wrap(err, "failed to start transaction")
} }
defer tx.Rollback() defer tx.Rollback()
if err := s.execute(ctx, tx, string(bytes)); err != nil { if err := s.execute(ctx, tx, string(bytes)); err != nil {
return errors.Errorf("failed to execute SQL file %s, err %s", filePath, err) return isFirstMigration, errors.Errorf("failed to execute SQL file %s, err %s", filePath, err)
} }
if err := tx.Commit(); err != nil { if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit transaction") return isFirstMigration, errors.Wrap(err, "failed to commit transaction")
} }
// TODO: using schema version in basic setting instead of migration history. // TODO: using schema version in basic setting instead of migration history.
if _, err := s.driver.UpsertMigrationHistory(ctx, &UpsertMigrationHistory{ if _, err := s.driver.UpsertMigrationHistory(ctx, &UpsertMigrationHistory{
Version: schemaVersion, Version: schemaVersion,
}); err != nil { }); err != nil {
return errors.Wrap(err, "failed to upsert migration history") return isFirstMigration, errors.Wrap(err, "failed to upsert migration history")
} }
if err := s.updateCurrentSchemaVersion(ctx, schemaVersion); err != nil { if err := s.updateCurrentSchemaVersion(ctx, schemaVersion); err != nil {
return errors.Wrap(err, "failed to update current schema version") return isFirstMigration, errors.Wrap(err, "failed to update current schema version")
} }
} }
if s.Profile.Mode == "prod" { if s.Profile.Mode == "prod" {
if err := s.normalizedMigrationHistoryList(ctx); err != nil { if err := s.normalizedMigrationHistoryList(ctx); err != nil {
return errors.Wrap(err, "failed to normalize migration history list") return isFirstMigration, errors.Wrap(err, "failed to normalize migration history list")
} }
} }
return nil return isFirstMigration, nil
} }
func (s *Store) getMigrationBasePath() string { func (s *Store) getMigrationBasePath() string {

Loading…
Cancel
Save