Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some logging around forced migration #2938

Merged
merged 2 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions components/authz-service/storage/postgres/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Config) Migrate(dataMigConf datamigration.Config) error {
migrationsTable := ""
ctx := context.Background()

l.Infof("Running db migrations from %q", migrationsPath)
l.Info("Initializing DB migrations...")
purl, err := addMigrationsTable(pgURL, migrationsTable)
if err != nil {
return errors.Wrap(err, "parse PG URL")
Expand All @@ -63,9 +63,10 @@ func (c *Config) Migrate(dataMigConf datamigration.Config) error {
}

if version < PreForceUpgradeMigration {
l.Infof("Migrating schema version %v to %v...", version, PreForceUpgradeMigration)
err = m.Migrate(PreForceUpgradeMigration)
if err != nil && err != migrate.ErrNoChange {
return errors.Wrap(err, "migration up to IAM-V2-force-upgrade failed")
return errors.Wrapf(err, "migration up to version %v failed", PreForceUpgradeMigration)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review sentiment seemed to favor removing v1 and v2 references, so updated a few error messages in that regard.

}
}

Expand All @@ -90,12 +91,13 @@ func (c *Config) Migrate(dataMigConf datamigration.Config) error {
if isOnV1 {
if isDirty { // we've attempted to migrate and not finished
// get IAM db in original, clean state to avoid conflicts
l.Infof("Detected unfinished migration; resetting IAM DB to clean state")
err = legacy.ResetIAMDb(ctx, db)
if err != nil {
return errors.Wrap(err, "reset IAM V2 database")
return errors.Wrap(err, "reset database")
}
if err := dataMigConf.Reset(); err != nil {
return errors.Wrap(err, "reset v2 data migrations")
return errors.Wrap(err, "reset data migrations")
}
if version != 0 {
migrateV1Policies = true
Expand All @@ -106,33 +108,41 @@ func (c *Config) Migrate(dataMigConf datamigration.Config) error {
if err != nil {
return errors.Wrapf(err, "failed to set IAM v2 migration_status to %s", constants_v2.EnumInProgress)
}
l.Info("Setting up IAM data basics...")
err = legacy.MigrateToV2(ctx, db, migrateV1Policies)
if err != nil {
statusErr := legacy.RecordMigrationStatus(ctx, constants_v2.EnumFailed, db)
if statusErr != nil {
return errors.Wrapf(statusErr, "failed to set IAM v2 migration_status to %s:%s", constants_v2.EnumFailed, err.Error())
return errors.Wrapf(statusErr, "failed to set IAM migration_status to %s:%s", constants_v2.EnumFailed, err.Error())
}
return errors.Wrap(err, "IAM v2 force-upgrade failed")
return errors.Wrap(err, "IAM data basics failed")
}
err = legacy.RecordMigrationStatus(ctx, constants_v2.EnumSuccessful, db)
if err != nil {
return errors.Wrapf(err, "failed to set IAM v2 migration_status to %s", constants_v2.EnumSuccessful)
return errors.Wrapf(err, "failed to set IAM migration_status to %s", constants_v2.EnumSuccessful)
}
}

// idempotent
l.Info("Checking for data migrations...")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😎

err = dataMigConf.Migrate()
if err != nil {
return errors.Wrap(err, "IAM data migrations failed")
}

// perform remaining migrations
l.Infof("Checking for remaining schema migrations...")
err = m.Up()
if err != nil && err != migrate.ErrNoChange {
return errors.Wrap(err, "migrations failed")
}

l.Infof("Completed db migrations")
version, _, err = m.Version()
if err != nil {
return errors.Wrap(err, "failed to get version post-migration")
}

l.Infof("DB initialization complete at version %v", version)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thought it would be a bonus to actually show the final migration level in the log, too.


err = db.Close()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion components/authz-service/storage/v1/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func (m *policyMap) Scan(src interface{}) error {

// New instantiates the postgres IAM v1 storage backend.
func New(ctx context.Context, l logger.Logger, migConf migration.Config, dataMigConf datamigration.Config) (storage.Storage, error) {
l.Infof("applying database migrations from %s", migConf.Path)
l.Infof("[v1] applying database migrations from %s", migConf.Path)

db, err := postgres.New(ctx, migConf, dataMigConf)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion components/authz-service/storage/v2/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func GetInstance() *pg {
return singletonInstance
}

// New instantiates the singleton postgres storage backend.
// Initialize instantiates the singleton postgres storage backend.
// Will only initialize once. Will simply return nil if already initialized.
func Initialize(ctx context.Context, e engine.Engine, l logger.Logger, migConf migration.Config,
dataMigConf datamigration.Config, projectLimit int) error {
Expand Down