Skip to content

Commit

Permalink
Merge pull request #7354 from TheThingsNetwork/is/migration-improvement
Browse files Browse the repository at this point in the history
IS migration improvement
  • Loading branch information
johanstokking authored Oct 23, 2024
2 parents 4203826 + 42c1d87 commit 6045a69
Show file tree
Hide file tree
Showing 27 changed files with 37 additions and 3 deletions.
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

0 comments on commit 6045a69

Please sign in to comment.