Skip to content

Commit

Permalink
chore: verify if kratos tables exist before running migration
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra authored and moshloop committed Dec 1, 2024
1 parent 7e7a7fb commit 95a1e0a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ type Config struct {
SkipMigrations bool
SkipMigrationFiles []string
MigrationMode MigrationMode

// If we are using Kratos auth, some migrations
// depend on kratos migrations being ran or not and
// can cause problems if mission-control mirations run
// before kratos migrations
KratosAuth bool
}

func (t *Config) Migrate() bool {
Expand Down
21 changes: 21 additions & 0 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ func SetupDB(config api.Config) (gormDB *gorm.DB, pgxpool *pgxpool.Pool, err err
}

if config.Migrate() {

// Some triggers are dependent on kratos tables
if config.KratosAuth {
if err = verifyKratosMigration(gormDB); err != nil {
return nil, nil, err
}
}

if err = Migrate(config); err != nil {
return
}
Expand All @@ -230,6 +238,19 @@ func SetupDB(config api.Config) (gormDB *gorm.DB, pgxpool *pgxpool.Pool, err err
return
}

func verifyKratosMigration(db *gorm.DB) error {
var exists bool
err := db.Raw(`SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'identities')`).Scan(&exists).Error
if err != nil {
return fmt.Errorf("error confirming if kratos migration ran: %w", err)
}
if !exists {
return fmt.Errorf("kratos created tables[identities] not found")
}

return nil
}

func setStatementTimeouts(ctx dutyContext.Context, config api.Config) {
postgrestTimeout := ctx.Properties().Duration("db.postgrest.timeout", 1*time.Minute)

Expand Down
5 changes: 5 additions & 0 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ var DisableKubernetes = func(config Config) Config {
return config
}

var KratosAuth = func(config Config) Config {
config.KratosAuth = true
return config
}

func Start(name string, opts ...StartOption) (context.Context, func(), error) {
config := DefaultConfig
for _, opt := range opts {
Expand Down

0 comments on commit 95a1e0a

Please sign in to comment.