Skip to content

Commit

Permalink
stores: get rid of migrate bool
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjan committed Feb 16, 2024
1 parent a52b3a7 commit 9051e47
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 38 deletions.
6 changes: 3 additions & 3 deletions stores/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func performMigrations(db *gorm.DB, logger *zap.SugaredLogger) error {
{
ID: "00001_object_metadata",
Migrate: func(tx *gorm.DB) error {
return performMigration(tx, "00001_object_metadata", false, logger)
return performMigration(tx, "main", "00001_object_metadata", logger)
},
},
{
ID: "00002_prune_slabs_trigger",
Migrate: func(tx *gorm.DB) error {
err := performMigration(tx, "00002_prune_slabs_trigger", false, logger)
err := performMigration(tx, "main", "00002_prune_slabs_trigger", logger)
if err != nil && strings.Contains(err.Error(), errMySQLNoSuperPrivilege.Error()) {
logger.Warn("migration 00002_prune_slabs_trigger requires the user to have the SUPER privilege to register triggers")
}
Expand All @@ -43,7 +43,7 @@ func performMigrations(db *gorm.DB, logger *zap.SugaredLogger) error {
m := gormigrate.New(db, gormigrate.DefaultOptions, migrations)

// Set init function.
m.InitSchema(initSchema(db, false, logger))
m.InitSchema(initSchema(db, "main", logger))

// Perform migrations.
if err := m.Migrate(); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion stores/migrations/mysql/main/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,7 @@ AND NOT EXISTS (
SELECT 1
FROM slices
WHERE slices.db_slab_id = OLD.db_slab_id
);
);

-- create default bucket
INSERT INTO buckets (created_at, name) VALUES (CURRENT_TIMESTAMP, 'default');
5 changes: 4 additions & 1 deletion stores/migrations/sqlite/main/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,7 @@ BEGIN
FROM slices
WHERE slices.db_slab_id = OLD.db_slab_id
);
END;
END;

-- create default bucket
INSERT INTO buckets (created_at, name) VALUES (CURRENT_TIMESTAMP, 'default');
2 changes: 1 addition & 1 deletion stores/migrations_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func performMetricsMigrations(tx *gorm.DB, logger *zap.SugaredLogger) error {
m := gormigrate.New(tx, gormigrate.DefaultOptions, migrations)

// Set init function.
m.InitSchema(initSchema(tx, true, logger))
m.InitSchema(initSchema(tx, "metrics", logger))

// Perform migrations.
if err := m.Migrate(); err != nil {
Expand Down
42 changes: 10 additions & 32 deletions stores/migrations_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,42 @@ import (
"fmt"

gormigrate "github.com/go-gormigrate/gormigrate/v2"
"go.sia.tech/renterd/api"
"go.uber.org/zap"
"gorm.io/gorm"
)

// initSchema is executed only on a clean database. Otherwise the individual
// migrations are executed.
func initSchema(db *gorm.DB, metrics bool, logger *zap.SugaredLogger) gormigrate.InitSchemaFunc {
func initSchema(db *gorm.DB, name string, logger *zap.SugaredLogger) gormigrate.InitSchemaFunc {
return func(tx *gorm.DB) error {
if metrics {
logger.Info("initializing metrics schema")
} else {
logger.Info("initializing schema")
}
logger.Infof("initializing '%s' schema", name)

// build filename
filename := "schema"
err := execSQLFile(tx, metrics, filename)
// init schema
err := execSQLFile(tx, name, "schema")
if err != nil {
return fmt.Errorf("failed to init schema: %w", err)
}

// add default bucket.
if !metrics {
if err := tx.Create(&dbBucket{
Name: api.DefaultBucketName,
}).Error; err != nil {
return fmt.Errorf("failed to create default bucket: %v", err)
}
}

logger.Info("initialization complete")
return nil
}
}

func performMigration(db *gorm.DB, name string, metrics bool, logger *zap.SugaredLogger) error {
logger.Infof("performing migration %s", name)

// build filename
filename := fmt.Sprintf("migration_%s", name)
func performMigration(db *gorm.DB, kind, migration string, logger *zap.SugaredLogger) error {
logger.Infof("performing %s migration '%s'", kind, migration)

// execute migration
err := execSQLFile(db, metrics, filename)
err := execSQLFile(db, kind, fmt.Sprintf("migration_%s", migration))
if err != nil {
return fmt.Errorf("migration %s failed: %w", name, err)
return fmt.Errorf("migration '%s' failed: %w", migration, err)
}

logger.Infof("migration %s complete", name)
logger.Infof("migration '%s' complete", migration)
return nil
}

func execSQLFile(db *gorm.DB, metrics bool, filename string) error {
func execSQLFile(db *gorm.DB, folder, filename string) error {
// build path
folder := "main"
if metrics {
folder = "metrics"
}
protocol := "mysql"
if isSQLite(db) {
protocol = "sqlite"
Expand Down

0 comments on commit 9051e47

Please sign in to comment.