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

IS migration improvement #7354

Merged
merged 5 commits into from
Oct 23, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ For details about compatibility between different releases, see the **Commitment

- Potential leak of end devices of other (owned) applications in the top end devices panel in the application overview of the Console.
- Fix reversed Join Server dev nonce metrics.
- Identity Server's store runs each migration within a transaction, so a migration's changes are only applied if all of its queries are successful.
- Identity Server's store now marks a migration as successful after all its operations are finished. Previously it was possible to have a successful migration which not all of its queries were processed.

### Security

Expand Down
38 changes: 35 additions & 3 deletions cmd/ttn-lw-stack/commands/is_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
bunstore "go.thethings.network/lorawan-stack/v3/pkg/identityserver/bunstore"
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/store"
ismigrations "go.thethings.network/lorawan-stack/v3/pkg/identityserver/store/migrations"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
ttntypes "go.thethings.network/lorawan-stack/v3/pkg/types"
storeutil "go.thethings.network/lorawan-stack/v3/pkg/util/store"
Expand All @@ -42,6 +43,36 @@ var (
return fmt.Errorf("init command deprecated, use migrate instead")
},
}
isDBStatusCommand = &cobra.Command{
Use: "status",
Short: "Check the migration status of the Identity Server database",
RunE: func(cmd *cobra.Command, _ []string) error {
logger.WithField("URI", config.IS.DatabaseURI).Info("Connecting to Identity Server database...")

sqlDB, err := storeutil.OpenDB(cmd.Context(), config.IS.DatabaseURI)
if err != nil {
return err
}
defer sqlDB.Close()

bunDB := bun.NewDB(sqlDB, pgdialect.New())

migrator := migrate.NewMigrator(bunDB, ismigrations.Migrations, migrate.WithMarkAppliedOnSuccess(true))

group, err := migrator.MigrationsWithStatus(ctx)
if err != nil {
return err
}

log.FromContext(ctx).
WithField("migrations", group).
WithField("unapplied_migrations", group.Unapplied()).
WithField("applied_migrations", group.Applied()).
Info("Status fetched")

return nil
},
}
isDBMigrateCommand = &cobra.Command{
Use: "migrate",
Short: "Migrate the Identity Server database",
Expand All @@ -56,7 +87,7 @@ var (

bunDB := bun.NewDB(sqlDB, pgdialect.New())

migrator := migrate.NewMigrator(bunDB, ismigrations.Migrations)
migrator := migrate.NewMigrator(bunDB, ismigrations.Migrations, migrate.WithMarkAppliedOnSuccess(true))

err = migrator.Init(cmd.Context())
if err != nil {
Expand All @@ -68,10 +99,10 @@ var (
return err
}
if migrations := status.Applied(); len(migrations) > 0 {
logger.Infof("Applied: %s", status)
logger.Infof("Applied: %s", migrations)
}
if migrations := status.Unapplied(); len(migrations) > 0 {
logger.Infof("Unapplied: %s", status)
logger.Infof("Unapplied: %s", migrations)
}

var group *migrate.MigrationGroup
Expand Down Expand Up @@ -373,6 +404,7 @@ func init() {
isDBCommand.AddCommand(isDBInitCommand)
isDBMigrateCommand.Flags().Bool("rollback", false, "Rollback most recent migration group")
isDBCommand.AddCommand(isDBMigrateCommand)
isDBCommand.AddCommand(isDBStatusCommand)
isDBCleanupCommand.Flags().Bool("dry-run", false, "Dry run")
isDBCommand.AddCommand(isDBCleanupCommand)
isDBEUIBlockCreationCommand.Flags().Bool("use-config", false, "Create block using values from config")
Expand Down
Loading