Skip to content

Commit

Permalink
single step migration
Browse files Browse the repository at this point in the history
cropped previously inherited migrations into a single step, add custom (software) migrations template
  • Loading branch information
Alexey Shtykov committed Jan 23, 2025
1 parent 9d1ba82 commit f82d1ef
Show file tree
Hide file tree
Showing 282 changed files with 1,772 additions and 2,926 deletions.
1 change: 1 addition & 0 deletions qubership-apihub-service/Service.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func main() {
time.Sleep(time.Second * 10) // Give a chance to read the unrecoverable error
panic("Failed perform DB migration: " + err.Error())
}
// to perform migrations, which could not be implemented with "pure" SQL
err = dbMigrationService.SoftMigrateDb(currentVersion, newVersion, migrationRequired)
if err != nil {
log.Errorf("Failed to perform db migrations: %v", err.Error())
Expand Down
35 changes: 17 additions & 18 deletions qubership-apihub-service/migration/service/DBMigrationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"os"
"path/filepath"
"sort"
"strings"

Expand Down Expand Up @@ -53,7 +54,7 @@ func NewDBMigrationService(cp db.ConnectionProvider, mRRepo mRepository.Migratio
repo: mRRepo,
buildCleanupRepository: bCRepo,
transitionRepository: transitionRepository,
migrationsFolder: systemInfoService.GetBasePath() + "/resources/migrations",
migrationsFolder: filepath.Join(systemInfoService.GetBasePath(), "resources", "migrations"),
minioStorageService: minioStorageService,
}
upMigrations, downMigrations, err := service.getMigrationFilenamesMap()
Expand All @@ -77,37 +78,35 @@ type dbMigrationServiceImpl struct {
minioStorageService service.MinioStorageService
}

const storedMigrationsTableMigrationVersion = 84
const storedMigrationsTableMigrationVersion = 1 // migration table will be created at first migration

func (d *dbMigrationServiceImpl) createSchemaMigrationsTable() error {
_, err := d.cp.GetConnection().Exec(`
create table if not exists schema_migrations
(
version integer not null,
dirty boolean not null,
PRIMARY KEY(version)
CREATE TABLE IF NOT EXISTS public.schema_migrations (
"version" int4 NOT NULL,
dirty bool NOT NULL,
CONSTRAINT schema_migrations_pkey PRIMARY KEY (version)
)`)
return err
}

func (d *dbMigrationServiceImpl) createStoredMigrationsTable() error {
_, err := d.cp.GetConnection().Exec(`
create table if not exists stored_schema_migration
(
num integer not null,
up_hash varchar not null,
sql_up varchar not null,
down_hash varchar null,
sql_down varchar null,
PRIMARY KEY(num)
CREATE TABLE IF NOT EXISTS public.stored_schema_migration (
num int4 NOT NULL,
up_hash varchar NOT NULL,
sql_up varchar NOT NULL,
down_hash varchar NULL,
sql_down varchar NULL,
CONSTRAINT stored_schema_migration_pkey PRIMARY KEY (num)
)`)
return err
}

func (d *dbMigrationServiceImpl) Migrate(basePath string) (currentMigrationNum int, newMigrationNum int, migrationRequired bool, err error) {
log.Infof("Schema Migration: start")

var currentMigrationNumber int
currentMigrationNumber := 0
_, err = d.cp.GetConnection().QueryOne(pg.Scan(&currentMigrationNumber), `SELECT version FROM schema_migrations`)
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
Expand Down Expand Up @@ -225,12 +224,12 @@ func (d *dbMigrationServiceImpl) applyRequiredMigrations(upMigrations []mEntity.
Where("version is not null").
Delete()
if err != nil {
return fmt.Errorf("failed to update schema_migrations table with latest migration version %v", latestMigrationNum)
return fmt.Errorf("failed to delete schema_migrations table with latest migration version %v", latestMigrationNum)
}
_, err = tx.Model(&migrationEntity).
Insert()
if err != nil {
return fmt.Errorf("failed to update schema_migrations table with latest migration version %v", latestMigrationNum)
return fmt.Errorf("failed to insert schema_migrations table with latest migration version %v", latestMigrationNum)
}
return nil
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ func (d *dbMigrationServiceImpl) getMigrationFilenamesMap() (map[int]string, map
if _, exists := upMigrations[num]; exists {
return nil, nil, fmt.Errorf("found duplicate migration number, migration is not possible: %v", file)
}
upMigrations[num] = d.migrationsFolder + "/" + file
upMigrations[num] = filepath.Join(d.migrationsFolder, file)
if maxUpMigrationNumber < num {
maxUpMigrationNumber = num
}
Expand All @@ -635,7 +635,7 @@ func (d *dbMigrationServiceImpl) getMigrationFilenamesMap() (map[int]string, map
if _, exists := downMigrations[num]; exists {
return nil, nil, fmt.Errorf("found duplicate migration number, migration is not possible: %v", file)
}
downMigrations[num] = d.migrationsFolder + "/" + file
downMigrations[num] = filepath.Join(d.migrationsFolder, file)
}
}
if maxUpMigrationNumber != len(upMigrations) {
Expand Down
Loading

0 comments on commit f82d1ef

Please sign in to comment.