-
Notifications
You must be signed in to change notification settings - Fork 1
feat(migration): add migrate subcommand and opt-in --migrate flag #1152
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
895e1b1
feat(migration): add migrate subcommand and opt-in --migrate flag
cursoragent e33f0ac
fix(migration): honor log config and silence duplicate cobra errors
cursoragent 2a68e41
Merge branch 'main' into cursor/nextgen-migrate-subcommand-a3c2
adlerhurst 3a99517
fix(cli): record injected --migrate in local server status
cursoragent e1b3c24
Merge branch 'main' into cursor/nextgen-migrate-subcommand-a3c2
muhlemmer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@zitadel/server": minor | ||
| --- | ||
|
|
||
| Operators can run `nextgen migrate` to apply schema changes and exit without starting the HTTP server. `server` no longer migrates on start unless you pass `--migrate`; `zitadel start` and the published image still migrate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "github.com/zitadel/nextgen/internal/instrumentation/zlog" | ||
| "github.com/zitadel/nextgen/internal/instrumentation/zotel" | ||
| ) | ||
|
|
||
| func newMigrateCommand(configPath *string) *cobra.Command { | ||
| return &cobra.Command{ | ||
| Use: "migrate", | ||
| Short: "Apply database migrations and exit", | ||
| SilenceUsage: true, | ||
| SilenceErrors: true, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
| cfg, err := loadConfig(*configPath) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return migrateDatabase(cmd.Context(), cfg) | ||
| }, | ||
| } | ||
|
Copilot marked this conversation as resolved.
|
||
| } | ||
|
|
||
| func migrateDatabase(ctx context.Context, cfg Config) error { | ||
| metrics, err := zotel.NewOtelMetrics(ctx, zotel.MetricsConfig{ | ||
| ServiceName: cfg.Instrumentation.ServiceName, | ||
| TraceIdFraction: cfg.Instrumentation.Trace.Fraction, | ||
| TraceExporter: cfg.Instrumentation.Trace.Exporter, | ||
| MetricExporter: cfg.Instrumentation.Metric.Exporter, | ||
| LogExporter: cfg.Instrumentation.Log.Exporter, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create otel metrics: %w", err) | ||
| } | ||
| defer func() { | ||
| _ = metrics.Shutdown(context.WithoutCancel(ctx)) | ||
| }() | ||
| setUpLogging(cfg.Instrumentation.Log, metrics.LoggerProvider()) | ||
| ctx = zlog.WithLoggingContext(ctx, zlog.WithStream(slog.Default(), zlog.StreamRuntime)) | ||
|
|
||
| pool, err := startDatabase(ctx, cfg, true) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| zlog.Info(ctx, "database migrations applied") | ||
| if err := pool.Close(ctx); err != nil { | ||
| zlog.WithError(ctx, err).Debug("closing database pool after migrate") | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package server | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "database/sql" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| _ "modernc.org/sqlite" | ||
| ) | ||
|
|
||
| func TestCommandHelpListsMigrate(t *testing.T) { | ||
| cmd := NewCommand() | ||
| var out bytes.Buffer | ||
| cmd.SetOut(&out) | ||
| cmd.SetErr(&out) | ||
| cmd.SetArgs([]string{"--help"}) | ||
| require.NoError(t, cmd.Execute()) | ||
|
|
||
| var migrateCmd, serverCmd bool | ||
| for _, c := range cmd.Commands() { | ||
| switch c.Name() { | ||
| case "migrate": | ||
| migrateCmd = true | ||
| assert.Equal(t, "Apply database migrations and exit", c.Short) | ||
| case "server": | ||
| serverCmd = true | ||
| require.NotNil(t, c.Flags().Lookup("migrate"), "expected --migrate on server") | ||
| } | ||
| } | ||
| assert.True(t, migrateCmd, "expected a command named migrate") | ||
| assert.True(t, serverCmd, "expected a command named server") | ||
| require.NotNil(t, cmd.Flags().Lookup("migrate"), "expected --migrate on root") | ||
| } | ||
|
|
||
| func TestMigrateCommandAppliesSchemaIdempotently(t *testing.T) { | ||
| dataDir, configPath := tempServerConfig(t) | ||
|
|
||
| for range 2 { | ||
| cmd := NewCommand() | ||
| cmd.SetArgs([]string{"migrate", "--config", configPath}) | ||
| executed, err := cmd.ExecuteC() | ||
| require.NoError(t, err) | ||
| assert.Equal(t, "migrate", executed.Name()) | ||
| } | ||
|
|
||
| assert.True(t, sqliteHasGooseTable(t, defaultSQLitePath(dataDir))) | ||
| } | ||
|
|
||
| func TestStartDatabaseSkipsMigrationsUnlessRequested(t *testing.T) { | ||
| dataDir, configPath := tempServerConfig(t) | ||
| cfg, err := loadConfig(configPath) | ||
| require.NoError(t, err) | ||
|
|
||
| ctx := t.Context() | ||
| skipped, err := startDatabase(ctx, cfg, false) | ||
| require.NoError(t, err) | ||
| require.NoError(t, skipped.Close(ctx)) | ||
|
|
||
| dbPath := defaultSQLitePath(dataDir) | ||
| assert.False(t, sqliteHasGooseTable(t, dbPath), "goose table should be absent without --migrate") | ||
|
|
||
| applied, err := startDatabase(ctx, cfg, true) | ||
| require.NoError(t, err) | ||
| require.NoError(t, applied.Close(ctx)) | ||
| assert.True(t, sqliteHasGooseTable(t, dbPath)) | ||
| } | ||
|
|
||
| func tempServerConfig(t *testing.T) (dataDir, configPath string) { | ||
| t.Helper() | ||
| dataDir = t.TempDir() | ||
| t.Setenv("NEXTGEN_SERVER_DATA_DIR", dataDir) | ||
| configPath = filepath.Join(t.TempDir(), "nextgen.yaml") | ||
| require.NoError(t, os.WriteFile(configPath, nil, 0o600)) | ||
| return dataDir, configPath | ||
| } | ||
|
|
||
| func sqliteHasGooseTable(t *testing.T, dbPath string) bool { | ||
| t.Helper() | ||
| db, err := sql.Open("sqlite", dbPath) | ||
| require.NoError(t, err) | ||
| defer db.Close() | ||
|
|
||
| var name string | ||
| err = db.QueryRow(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'goose_db_version'`).Scan(&name) | ||
| if err == sql.ErrNoRows { | ||
| return false | ||
| } | ||
| require.NoError(t, err) | ||
| return name == "goose_db_version" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.