Skip to content

Commit

Permalink
Add support for configuring automatic migration (and default to false) (
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored Apr 18, 2023
1 parent 9327a94 commit 342c090
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
48 changes: 30 additions & 18 deletions cmd/warrant/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ func (env *ServiceEnv) InitDB(config config.Config) error {
return err
}

err = db.Migrate(ctx, MySQLDatastoreMigrationVersion)
if err != nil {
return err
if config.AutoMigrate {
err = db.Migrate(ctx, MySQLDatastoreMigrationVersion)
if err != nil {
return err
}
}

env.Datastore = db
Expand All @@ -73,9 +75,11 @@ func (env *ServiceEnv) InitDB(config config.Config) error {
return err
}

err = db.Migrate(ctx, PostgresDatastoreMigrationVersion)
if err != nil {
return err
if config.AutoMigrate {
err = db.Migrate(ctx, PostgresDatastoreMigrationVersion)
if err != nil {
return err
}
}

env.Datastore = db
Expand All @@ -89,9 +93,11 @@ func (env *ServiceEnv) InitDB(config config.Config) error {
return err
}

err = db.Migrate(ctx, SQLiteDatastoreMigrationVersion)
if err != nil {
return err
if config.AutoMigrate {
err = db.Migrate(ctx, SQLiteDatastoreMigrationVersion)
if err != nil {
return err
}
}

env.Datastore = db
Expand All @@ -112,9 +118,11 @@ func (env *ServiceEnv) InitEventDB(config config.Config) error {
return err
}

err = db.Migrate(ctx, MySQLEventstoreMigrationVersion)
if err != nil {
return err
if config.AutoMigrate {
err = db.Migrate(ctx, MySQLEventstoreMigrationVersion)
if err != nil {
return err
}
}

env.Eventstore = db
Expand All @@ -128,9 +136,11 @@ func (env *ServiceEnv) InitEventDB(config config.Config) error {
return err
}

err = db.Migrate(ctx, PostgresEventstoreMigrationVersion)
if err != nil {
return err
if config.AutoMigrate {
err = db.Migrate(ctx, PostgresEventstoreMigrationVersion)
if err != nil {
return err
}
}

env.Eventstore = db
Expand All @@ -144,9 +154,11 @@ func (env *ServiceEnv) InitEventDB(config config.Config) error {
return err
}

err = db.Migrate(ctx, SQLiteEventstoreMigrationVersion)
if err != nil {
return err
if config.AutoMigrate {
err = db.Migrate(ctx, SQLiteEventstoreMigrationVersion)
if err != nil {
return err
}
}

env.Eventstore = db
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
Port int `mapstructure:"port"`
LogLevel int8 `mapstructure:"logLevel"`
EnableAccessLog bool `mapstructure:"enableAccessLog"`
AutoMigrate bool `mapstructure:"autoMigrate"`
Datastore *DatastoreConfig `mapstructure:"datastore"`
Eventstore *EventstoreConfig `mapstructure:"eventstore"`
ApiKey string `mapstructure:"apiKey"`
Expand Down Expand Up @@ -89,6 +90,7 @@ func NewConfig() Config {
viper.SetDefault("port", 8000)
viper.SetDefault("levelLevel", zerolog.DebugLevel)
viper.SetDefault("enableAccessLog", true)
viper.SetDefault("autoMigrate", false)
viper.SetDefault("datastore.mysql.migrationSource", DefaultMySQLDatastoreMigrationSource)
viper.SetDefault("datastore.postgres.migrationSource", DefaultPostgresDatastoreMigrationSource)
viper.SetDefault("datastore.sqlite.migrationSource", DefaultSQLiteDatastoreMigrationSource)
Expand Down

0 comments on commit 342c090

Please sign in to comment.