Skip to content

Modern Docker Management, Designed for Everyone

fix(database): enforce SQLite foreign keys and remove orphaned authorization records (#4339)

Kyle Mendell33bdd33133a3
diff --git a/backend/internal/database/database.go b/backend/internal/database/database.go
index 9845155f9..9c57638ce 100644
--- a/backend/internal/database/database.go
+++ b/backend/internal/database/database.go
@@ -7,6 +7,7 @@ import (
"fmt"
"io/fs"
"log/slog"
+ "maps"
"net/url"
"os"
"path/filepath"
@@ -36,19 +37,13 @@ type MigrationOptions struct {
}
const (
- dbProviderSQLite = "sqlite"
- dbProviderPostgres = "postgres"
- gooseVersionTable = "goose_db_version"
- legacyVersionTable = "schema_migrations"
-
- // Prepared-statement cache bounds. GORM's PrepareStmt cache is a global LRU keyed
- // by SQL text. When PrepareStmtMaxSize/PrepareStmtTTL are left at zero, GORM falls
- // back to math.MaxInt entries with a 24h TTL, i.e. effectively unbounded. Because
- // this codebase emits highly variable SQL (dynamic filter/sort/pagination and
- // GORM's IN (?,?,...) slice expansion, whose placeholder count changes the query
- // text), the cache — and the modernc.org/sqlite compiled statements it retains on
- // the Go heap — grows steadily under normal use. Bounding size and TTL keeps hot
- // queries prepared while evicting the long tail (evicted statements are closed).
+ dbProviderSQLite = "sqlite"
+ dbProviderPostgres = "postgres"
+ gooseVersionTable = "goose_db_version"
+ legacyVersionTable = "schema_migrations"
+ latestMigrationVersion int64 = -1
+
+ // Bound prepared statements so variable SQL does not retain an unbounded cache.
preparedStmtMaxSize = 256
preparedStmtTTL = 15 * time.Minute
)
@@ -60,90 +55,51 @@ func SetGormLogger(l logger.Interface) {
}
func Initialize(ctx context.Context, databaseURL string, options MigrationOptions) (database *DB, err error) {
- db, err := connectDatabaseInternal(ctx, databaseURL)
- if err != nil {
- return nil, fmt.Errorf("failed to connect to database: %w", err)
- }
- // Initialization opens a connection pool before it can fail on migrations or
- // provider detection; release it rather than leaking it into a failed startup.
- defer func() {
- if err == nil {
- return
- }
- if closeErr := db.Close(); closeErr != nil {
- slog.WarnContext(ctx, "Failed to close database after initialization failure", "error", closeErr)
- }
- }()
-
- if cancellationErr := ctx.Err(); cancellationErr != nil {
- return nil, cancellationErr
- }
-
- // Get underlying sql.DB for migrations
- sqlDB, err := db.DB.DB()
- if err != nil {
- return nil, fmt.Errorf("failed to get sql.DB: %w", err)
- }
-
+ var dialector gorm.Dialector
var dbProvider string
switch {
case strings.HasPrefix(databaseURL, "file:"):
dbProvider = dbProviderSQLite
- case strings.HasPrefix(databaseURL, "postgres"):
- dbProvider = dbProviderPostgres
- default:
- return nil, fmt.Errorf("unsupported database type in URL: %s", databaseURL)
- }
-
- if migrateDatabaseErr := migrateDatabaseInternal(ctx, sqlDB, dbProvider, options); migrateDatabaseErr != nil {
- slog.ErrorContext(ctx, "Failed to run migrations", "error", migrateDatabaseErr)
- return nil, fmt.Errorf("failed to run migrations: %w", migrateDatabaseErr)
- }
-
- // Set connection pool settings
- if db.Name() == "postgres" {
- sqlDB.SetMaxIdleConns(15)
- sqlDB.SetMaxOpenConns(50)
- } else {
- sqlDB.SetMaxIdleConns(5)
- sqlDB.SetMaxOpenConns(20)
- }
- sqlDB.SetConnMaxLifetime(5 * time.Minute)
- sqlDB.SetConnMaxIdleTime(3 * time.Minute)
-
- return db, nil
-}
-
-func connectDatabaseInternal(ctx context.Context, databaseURL string) (*DB, error) {
- var dialector gorm.Dialector
-
- switch {
- case strings.HasPrefix(databaseURL, "file:"):
- if err := sqliteutil.RegisterFunctions(); err != nil {
- return nil, fmt.Errorf("failed to register SQLite functions: %w", err)
+ if err = sqliteutil.RegisterFunctions(); err != nil {
+ err = fmt.Errorf("failed to register SQLite functions: %w", err)
+ break
}
- connString, err := ParseSQLiteConnectionString(databaseURL)
+ var connString string
+ connString, err = ParseSQLiteConnectionString(databaseURL)
if err != nil {
- return nil, fmt.Errorf("failed to parse SQLite connection string: %w", err)
+ err = fmt.Errorf("failed to parse SQLite connection string: %w", err)
+ break
+ }
+ var path string
+ path, err = kit.SQLitePathFromDSN(connString)
+ if err != nil {
+ err = fmt.Errorf("failed to prepare SQLite directory: failed to parse SQLite DSN: %w", err)
+ break
}
- if ensureSQLiteDirectoryErr := ensureSQLiteDirectoryInternal(connString); ensureSQLiteDirectoryErr != nil {
- return nil, fmt.Errorf("failed to prepare SQLite directory: %w", ensureSQLiteDirectoryErr)
+ dir := filepath.Dir(path)
+ if path != "" && !strings.HasPrefix(path, ":memory:") && dir != "" && dir != "." {
+ if err = os.MkdirAll(dir, 0o755); err != nil {
+ err = fmt.Errorf("failed to prepare SQLite directory: %w", err)
+ break
+ }
}
dialector = sqlite.Open(connString)
case strings.HasPrefix(databaseURL, "postgres"):
+ dbProvider = dbProviderPostgres
dialector = postgres.Open(databaseURL)
default:
- return nil, fmt.Errorf("unsupported database type in URL: %s", databaseURL)
+ err = fmt.Errorf("unsupported database type in URL: %s", databaseURL)
+ }
+ if err != nil {
+ return nil, fmt.Errorf("failed to connect to database: %w", err)
}
- // Retry connection up to 3 times
- var db *gorm.DB
- var err error
- for i := 1; i <= 3; i++ {
- if retryCancellationErr := ctx.Err(); retryCancellationErr != nil {
- return nil, retryCancellationErr
+ var gormDB *gorm.DB
+ for attempt := 1; attempt <= 3; attempt++ {
+ if err = ctx.Err(); err != nil {
+ return nil, fmt.Errorf("failed to connect to database: %w", err)
}
- db, err = gorm.Open(dialector, &gorm.Config{
+ gormDB, err = gorm.Open(dialector, &gorm.Config{
Logger: customGormLogger,
NowFunc: func() time.Time {
return time.Now().UTC()
@@ -154,37 +110,90 @@ func connectDatabaseInternal(ctx context.Context, databaseURL string) (*DB, erro
IgnoreRelationshipsWhenMigrating: true,
})
if err == nil {
- return &DB{db}, nil
+ break
}
-
- slog.InfoContext(ctx, "Failed to initialize database", "attempt", i)
- if i < 3 {
+ slog.InfoContext(ctx, "Failed to initialize database", "attempt", attempt)
+ if attempt < 3 {
select {
case <-time.After(3 * time.Second):
case <-ctx.Done():
- return nil, ctx.Err()
+ return nil, fmt.Errorf("failed to connect to database: %w", ctx.Err())
}
}
}
+ if err != nil {
+ return nil, fmt.Errorf("failed to connect to database: %w", err)
+ }
+ db := &DB{DB: gormDB}
+ // Close the pool if initialization fails after opening it.
+ defer func() {
+ if err == nil {
+ return
+ }
+ if closeErr := db.Close(); closeErr != nil {
+ slog.WarnContext(ctx, "Failed to close database after initialization failure", "error", closeErr)
+ }
+ }()
+ if cancellationErr := ctx.Err(); cancellationErr != nil {
+ return nil, cancellationErr
+ }
- return nil, err
-}
-
-func migrateDatabaseInternal(ctx context.Context, db *sql.DB, dbProvider string, options MigrationOptions) error {
- requiredVersion, err := getHighestEmbeddedMigrationVersionInternal(dbProvider)
+ sqlDB, err := db.SQLDB()
if err != nil {
- return fmt.Errorf("failed to determine target migration version for %s: %w", dbProvider, err)
+ return nil, fmt.Errorf("failed to get sql.DB: %w", err)
+ }
+ if migrationErr := migrateDatabase(ctx, sqlDB, dbProvider, options, latestMigrationVersion); migrationErr != nil {
+ slog.ErrorContext(ctx, "Failed to run migrations", "error", migrationErr)
+ return nil, fmt.Errorf("failed to run migrations: %w", migrationErr)
}
- return migrateDatabaseToVersionInternal(ctx, db, dbProvider, options, requiredVersion)
+ sqlDB.SetMaxIdleConns(kit.Ternary(dbProvider == dbProviderPostgres, 15, 5))
+ sqlDB.SetMaxOpenConns(kit.Ternary(dbProvider == dbProviderPostgres, 50, 20))
+ sqlDB.SetConnMaxLifetime(5 * time.Minute)
+ sqlDB.SetConnMaxIdleTime(3 * time.Minute)
+ return db, nil
}
-func migrateDatabaseToVersionInternal(ctx context.Context, db *sql.DB, dbProvider string, options MigrationOptions, requiredVersion int64) error {
- if err := adoptLegacyMigrationStateInternal(ctx, db, dbProvider, options); err != nil {
+// latestMigrationVersion selects the latest embedded migration.
+func migrateDatabase(ctx context.Context, db *sql.DB, dbProvider string, options MigrationOptions, requiredVersion int64) error {
+ if requiredVersion == latestMigrationVersion {
+ versions, err := getEmbeddedMigrationVersions(dbProvider)
+ if err != nil {
+ return fmt.Errorf("failed to determine target migration version for %s: %w", dbProvider, err)
+ }
+ if len(versions) == 0 {
+ return fmt.Errorf("failed to determine target migration version for %s: no embedded migrations found for %s", dbProvider, dbProvider)
+ }
+ requiredVersion = versions[len(versions)-1]
+ }
+
+ if err := adoptLegacyMigrationState(ctx, db, dbProvider, options); err != nil {
return err
}
- provider, err := newGooseProviderInternal(db, dbProvider)
+ migrationsFS, err := fs.Sub(resources.FS, "migrations/"+dbProvider)
+ if err != nil {
+ return fmt.Errorf("failed to create goose provider for %s: failed to load embedded migrations for %s: %w", dbProvider, dbProvider, err)
+ }
+ var dialect goose.Dialect
+ switch dbProvider {
+ case dbProviderSQLite:
+ dialect = goose.DialectSQLite3
+ case dbProviderPostgres:
+ dialect = goose.DialectPostgres
+ default:
+ return fmt.Errorf("failed to create goose provider for %s: unsupported database provider: %s", dbProvider, dbProvider)
+ }
+ // Serialize Postgres migrations across processes. SQLite already allows one writer.
+ providerOptions := []goose.ProviderOption{}
+ if dialect == goose.DialectPostgres {
+ sessionLocker, lockerErr := lock.NewPostgresSessionLocker()
+ if lockerErr != nil {
+ return fmt.Errorf("failed to create goose provider for %s: failed to create Postgres migration session locker: %w", dbProvider, lockerErr)
+ }
+ providerOptions = append(providerOptions, goose.WithSessionLocker(sessionLocker))
+ }
+ provider, err := goose.NewProvider(dialect, db, migrationsFS, providerOptions...)
if err != nil {
return fmt.Errorf("failed to create goose provider for %s: %w", dbProvider, err)
}
@@ -207,9 +216,33 @@ func migrateDatabaseToVersionInternal(ctx context.Context, db *sql.DB, dbProvide
)
}
- missingVersions, missingEmbeddedDowngradeMigrationsErr := missingEmbeddedDowngradeMigrationsInternal(ctx, db, dbProvider, requiredVersion)
- if missingEmbeddedDowngradeMigrationsErr != nil {
- return missingEmbeddedDowngradeMigrationsErr
+ embeddedVersions, versionsErr := getEmbeddedMigrationVersions(dbProvider)
+ if versionsErr != nil {
+ return versionsErr
+ }
+ applied := kit.Ternary(dbProvider == dbProviderPostgres, "true", "1")
+ placeholder := kit.Ternary(dbProvider == dbProviderPostgres, "$1", "?")
+ query := "SELECT DISTINCT version_id FROM " + gooseVersionTable +
+ " WHERE is_applied = " + applied + " AND version_id > " + placeholder + " ORDER BY version_id"
+ rows, queryErr := db.QueryContext(ctx, query, requiredVersion)
+ if queryErr != nil {
+ return fmt.Errorf("failed to read applied migration versions for %s: %w", dbProvider, queryErr)
+ }
+ defer func() {
+ _ = rows.Close()
+ }()
+ missingVersions := []int64{}
+ for rows.Next() {
+ var version int64
+ if scanErr := rows.Scan(&version); scanErr != nil {
+ return fmt.Errorf("failed to scan applied migration version for %s: %w", dbProvider, scanErr)
+ }
+ if !slices.Contains(embeddedVersions, version) {
+ missingVersions = append(missingVersions, version)
+ }
+ }
+ if iterateErr := rows.Err(); iterateErr != nil {
+ return fmt.Errorf("failed to iterate applied migration versions for %s: %w", dbProvider, iterateErr)
}
if len(missingVersions) > 0 {
return fmt.Errorf("cannot downgrade database from version %d to %d for %s: embedded Goose migrations are missing for "+
@@ -244,91 +277,71 @@ func migrateDatabaseToVersionInternal(ctx context.Context, db *sql.DB, dbProvide
return nil
}
-func newGooseProviderInternal(db *sql.DB, dbProvider string) (*goose.Provider, error) {
- migrationsFS, err := embeddedMigrationFSInternal(dbProvider)
- if err != nil {
- return nil, err
- }
-
- dialect, err := gooseDialectInternal(dbProvider)
- if err != nil {
- return nil, err
+// Remove in v3
+func adoptLegacyMigrationState(ctx context.Context, db *sql.DB, dbProvider string, options MigrationOptions) error {
+ if dbProvider != dbProviderSQLite && dbProvider != dbProviderPostgres {
+ return fmt.Errorf("unsupported database provider: %s", dbProvider)
}
-
- // Two Arcane processes pointed at the same Postgres (a rolling deploy, or a
- // replica set) would otherwise run migrations concurrently and race on the
- // same DDL. A session-level advisory lock serializes them; SQLite needs no
- // equivalent because it is single-writer by construction.
- var options []goose.ProviderOption
- if dialect == goose.DialectPostgres {
- sessionLocker, newPostgresSessionLockerErr := lock.NewPostgresSessionLocker()
- if newPostgresSessionLockerErr != nil {
- return nil, fmt.Errorf("failed to create Postgres migration session locker: %w", newPostgresSessionLockerErr)
+ tableQuery := kit.Ternary(dbProvider == dbProviderPostgres,
+ "SELECT to_regclass($1) IS NOT NULL",
+ "SELECT EXISTS(SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?)",
+ )
+ var legacyVersion int64
+ var dirty bool
+ for _, table := range []string{legacyVersionTable, gooseVersionTable} {
+ var exists bool
+ if err := db.QueryRowContext(ctx, tableQuery, table).Scan(&exists); err != nil {
+ label := kit.Ternary(table == legacyVersionTable, "legacy migration table", "Goose version table")
+ return fmt.Errorf("failed to check %s for %s: %w", label, dbProvider, err)
}
- options = append(options, goose.WithSessionLocker(sessionLocker))
- }
-
- return goose.NewProvider(dialect, db, migrationsFS, options...)
-}
-
-func embeddedMigrationFSInternal(dbProvider string) (fs.FS, error) {
- migrationsFS, err := fs.Sub(resources.FS, "migrations/"+dbProvider)
- if err != nil {
- return nil, fmt.Errorf("failed to load embedded migrations for %s: %w", dbProvider, err)
- }
-
- return migrationsFS, nil
-}
-
-func gooseDialectInternal(dbProvider string) (goose.Dialect, error) {
- switch dbProvider {
- case dbProviderSQLite:
- return goose.DialectSQLite3, nil
- case dbProviderPostgres:
- return goose.DialectPostgres, nil
- default:
- return "", fmt.Errorf("unsupported database provider: %s", dbProvider)
- }
-}
-
-func adoptLegacyMigrationStateInternal(ctx context.Context, db *sql.DB, dbProvider string, options MigrationOptions) error {
- legacyState, ok, err := legacyMigrationStateInternal(ctx, db, dbProvider)
- if err != nil {
- return err
- }
- if !ok {
- return nil
- }
-
- if legacyState.dirty {
- if !options.AllowDowngrade {
- return fmt.Errorf(
- "database schema version %d is dirty in legacy %s table; resolve it manually or set "+
- "ALLOW_DOWNGRADE=true after verifying the database state",
- legacyState.version,
- legacyVersionTable,
- )
+ if table == gooseVersionTable {
+ if !exists {
+ break
+ }
+ var version int64
+ applied := kit.Ternary(dbProvider == dbProviderPostgres, "true", "1")
+ query := fmt.Sprintf("SELECT COALESCE(MAX(version_id), 0) FROM %s WHERE is_applied = %s", table, applied)
+ if err := db.QueryRowContext(ctx, query).Scan(&version); err != nil {
+ return fmt.Errorf("failed to read Goose migration state for %s: %w", dbProvider, err)
+ }
+ if version > 0 {
+ return nil
+ }
+ continue
}
-
- if clearLegacyMigrationDirtyErr := clearLegacyMigrationDirtyInternal(ctx, db, dbProvider, legacyState.version); clearLegacyMigrationDirtyErr != nil {
- return clearLegacyMigrationDirtyErr
+ if !exists {
+ return nil
+ }
+ query := "SELECT version, dirty FROM " + table + " ORDER BY version DESC LIMIT 1"
+ err := db.QueryRowContext(ctx, query).Scan(&legacyVersion, &dirty)
+ if errors.Is(err, sql.ErrNoRows) {
+ return nil
+ }
+ if err != nil {
+ return fmt.Errorf("failed to read legacy migration state for %s: %w", dbProvider, err)
+ }
+ if dirty {
+ if !options.AllowDowngrade {
+ return fmt.Errorf(
+ "database schema version %d is dirty in legacy %s table; resolve it manually or set "+
+ "ALLOW_DOWNGRADE=true after verifying the database state",
+ legacyVersion,
+ legacyVersionTable,
+ )
+ }
+ placeholder := kit.Ternary(dbProvider == dbProviderPostgres, "$1", "?")
+ query = "UPDATE " + table + " SET dirty = false WHERE version = " + placeholder
+ if _, err = db.ExecContext(ctx, query, legacyVersion); err != nil {
+ return fmt.Errorf("failed to clear legacy dirty migration state for %s version %d: %w", dbProvider, legacyVersion, err)
+ }
+ slog.WarnContext(ctx, "Cleared dirty legacy migration state because ALLOW_DOWNGRADE=true", "provider", dbProvider, "version", legacyVersion)
}
- slog.WarnContext(ctx, "Cleared dirty legacy migration state because ALLOW_DOWNGRADE=true", "provider", dbProvider, "version", legacyState.version)
- }
-
- hasGooseState, err := gooseVersionTableHasAppliedMigrationsInternal(ctx, db, dbProvider)
- if err != nil {
- return err
- }
- if hasGooseState {
- return nil
}
- versions, err := getEmbeddedMigrationVersionsInternal(dbProvider)
+ versions, err := getEmbeddedMigrationVersions(dbProvider)
if err != nil {
return err
}
-
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("failed to start legacy migration adoption transaction for %s: %w", dbProvider, err)
@@ -336,212 +349,50 @@ func adoptLegacyMigrationStateInternal(ctx context.Context, db *sql.DB, dbProvid
defer func() {
_ = tx.Rollback()
}()
-
- if createGooseVersionTableErr := createGooseVersionTableInternal(ctx, tx, dbProvider); createGooseVersionTableErr != nil {
- return createGooseVersionTableErr
- }
-
- if clearGooseVersionTableErr := clearGooseVersionTableInternal(ctx, tx, dbProvider); clearGooseVersionTableErr != nil {
- return clearGooseVersionTableErr
- }
-
- if insertGooseMigrationVersionErr := insertGooseMigrationVersionInternal(ctx, tx, dbProvider, 0); insertGooseMigrationVersionErr != nil {
- return insertGooseMigrationVersionErr
- }
- versionApplied := legacyState.version == 0
- for _, version := range versions {
- if version > legacyState.version {
- break
- }
- if insertMigrationVersionErr := insertGooseMigrationVersionInternal(ctx, tx, dbProvider, version); insertMigrationVersionErr != nil {
- return insertMigrationVersionErr
- }
- if version == legacyState.version {
- versionApplied = true
- }
- }
- if !versionApplied {
- if insertLegacyVersionErr := insertGooseMigrationVersionInternal(ctx, tx, dbProvider, legacyState.version); insertLegacyVersionErr != nil {
- return insertLegacyVersionErr
- }
- }
-
- if commitErr := tx.Commit(); commitErr != nil {
- return fmt.Errorf("failed to commit legacy migration adoption for %s: %w", dbProvider, commitErr)
- }
-
- slog.InfoContext(ctx, "Adopted legacy migration state into Goose", "provider", dbProvider, "legacyVersion", legacyState.version)
- return nil
-}
-
-type legacyMigrationState struct {
- version int64
- dirty bool
-}
-
-func legacyMigrationStateInternal(ctx context.Context, db *sql.DB, dbProvider string) (legacyMigrationState, bool, error) {
- exists, err := legacyVersionTableExistsInternal(ctx, db, dbProvider)
- if err != nil {
- return legacyMigrationState{}, false, err
- }
- if !exists {
- return legacyMigrationState{}, false, nil
- }
-
- var state legacyMigrationState
- err = db.QueryRowContext(ctx, fmt.Sprintf("SELECT version, dirty FROM %s ORDER BY version DESC LIMIT 1", legacyVersionTable)).Scan(&state.version, &state.dirty)
- if errors.Is(err, sql.ErrNoRows) {
- return legacyMigrationState{}, false, nil
- }
- if err != nil {
- return legacyMigrationState{}, false, fmt.Errorf("failed to read legacy migration state for %s: %w", dbProvider, err)
- }
-
- return state, true, nil
-}
-
-func legacyVersionTableExistsInternal(ctx context.Context, db *sql.DB, dbProvider string) (bool, error) {
- switch dbProvider {
- case dbProviderSQLite:
- var count int
- if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?`, legacyVersionTable).Scan(&count); err != nil {
- return false, fmt.Errorf("failed to check legacy migration table for sqlite: %w", err)
- }
- return count > 0, nil
- case dbProviderPostgres:
- var exists bool
- if err := db.QueryRowContext(ctx, `SELECT to_regclass($1) IS NOT NULL`, legacyVersionTable).Scan(&exists); err != nil {
- return false, fmt.Errorf("failed to check legacy migration table for postgres: %w", err)
- }
- return exists, nil
- default:
- return false, fmt.Errorf("unsupported database provider: %s", dbProvider)
- }
-}
-
-func clearLegacyMigrationDirtyInternal(ctx context.Context, db *sql.DB, dbProvider string, version int64) error {
- queryFormat := "UPDATE " + legacyVersionTable + " SET dirty = false WHERE version = %s"
- query, args, err := sqlWithProviderPlaceholderInternal(dbProvider, queryFormat, version)
- if err != nil {
- return err
- }
- if _, execContextErr := db.ExecContext(ctx, query, args...); execContextErr != nil {
- return fmt.Errorf("failed to clear legacy dirty migration state for %s version %d: %w", dbProvider, version, execContextErr)
- }
- return nil
-}
-
-func gooseVersionTableHasAppliedMigrationsInternal(ctx context.Context, db *sql.DB, dbProvider string) (bool, error) {
- exists, err := gooseVersionTableExistsInternal(ctx, db, dbProvider)
- if err != nil || !exists {
- return false, err
- }
-
- var version int64
- applied := kit.Ternary(dbProvider == dbProviderPostgres, "true", "1")
- if scanErr := db.QueryRowContext(ctx, fmt.Sprintf("SELECT COALESCE(MAX(version_id), 0) FROM %s WHERE is_applied = %s", gooseVersionTable, applied)).Scan(&version); scanErr != nil {
- return false, fmt.Errorf("failed to read Goose migration state for %s: %w", dbProvider, scanErr)
- }
- return version > 0, nil
-}
-
-func gooseVersionTableExistsInternal(ctx context.Context, db *sql.DB, dbProvider string) (bool, error) {
- switch dbProvider {
- case dbProviderSQLite:
- var count int
- if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = ?`, gooseVersionTable).Scan(&count); err != nil {
- return false, fmt.Errorf("failed to check Goose version table for sqlite: %w", err)
- }
- return count > 0, nil
- case dbProviderPostgres:
- var exists bool
- if err := db.QueryRowContext(ctx, `SELECT to_regclass($1) IS NOT NULL`, gooseVersionTable).Scan(&exists); err != nil {
- return false, fmt.Errorf("failed to check Goose version table for postgres: %w", err)
- }
- return exists, nil
- default:
- return false, fmt.Errorf("unsupported database provider: %s", dbProvider)
- }
-}
-
-type sqlExecerInternal interface {
- ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
-}
-
-func createGooseVersionTableInternal(ctx context.Context, execer sqlExecerInternal, dbProvider string) error {
- var query string
- switch dbProvider {
- case dbProviderSQLite:
- query = fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- version_id INTEGER NOT NULL,
- is_applied INTEGER NOT NULL,
- tstamp TIMESTAMP DEFAULT (datetime('now'))
-)`, gooseVersionTable)
- case dbProviderPostgres:
- query = fmt.Sprintf(`CREATE TABLE IF NOT EXISTS %s (
+ versionDDL := fmt.Sprintf(kit.Ternary(dbProvider == dbProviderPostgres,
+ `CREATE TABLE IF NOT EXISTS %s (
id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY,
version_id bigint NOT NULL,
is_applied boolean NOT NULL,
tstamp timestamp NOT NULL DEFAULT now()
-)`, gooseVersionTable)
- default:
- return fmt.Errorf("unsupported database provider: %s", dbProvider)
- }
-
- if _, err := execer.ExecContext(ctx, query); err != nil {
- return fmt.Errorf("failed to create Goose version table for %s: %w", dbProvider, err)
+)`,
+ `CREATE TABLE IF NOT EXISTS %s (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ version_id INTEGER NOT NULL,
+ is_applied INTEGER NOT NULL,
+ tstamp TIMESTAMP DEFAULT (datetime('now'))
+)`,
+ ), gooseVersionTable)
+ if _, createErr := tx.ExecContext(ctx, versionDDL); createErr != nil {
+ return fmt.Errorf("failed to create Goose version table for %s: %w", dbProvider, createErr)
}
- return nil
-}
-
-func clearGooseVersionTableInternal(ctx context.Context, execer sqlExecerInternal, dbProvider string) error {
- if _, err := execer.ExecContext(ctx, "DELETE FROM "+gooseVersionTable); err != nil {
+ if _, err = tx.ExecContext(ctx, "DELETE FROM "+gooseVersionTable); err != nil {
return fmt.Errorf("failed to clear Goose version table for %s: %w", dbProvider, err)
}
- return nil
-}
-func insertGooseMigrationVersionInternal(ctx context.Context, execer sqlExecerInternal, dbProvider string, version int64) error {
- switch dbProvider {
- case dbProviderSQLite:
- if _, err := execer.ExecContext(ctx, fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES (?, ?)", gooseVersionTable), version, true); err != nil {
- return fmt.Errorf("failed to insert Goose migration version %d for sqlite: %w", version, err)
- }
- case dbProviderPostgres:
- if _, err := execer.ExecContext(ctx, fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES ($1, $2)", gooseVersionTable), version, true); err != nil {
- return fmt.Errorf("failed to insert Goose migration version %d for postgres: %w", version, err)
- }
- default:
- return fmt.Errorf("unsupported database provider: %s", dbProvider)
+ if end := slices.IndexFunc(versions, func(version int64) bool { return version > legacyVersion }); end >= 0 {
+ versions = versions[:end]
}
- return nil
-}
-
-func sqlWithProviderPlaceholderInternal(dbProvider, queryFormat string, arg any) (string, []any, error) {
- switch dbProvider {
- case dbProviderSQLite:
- return fmt.Sprintf(queryFormat, "?"), []any{arg}, nil
- case dbProviderPostgres:
- return fmt.Sprintf(queryFormat, "$1"), []any{arg}, nil
- default:
- return "", nil, fmt.Errorf("unsupported database provider: %s", dbProvider)
+ appliedVersions := append([]int64{0}, versions...)
+ if !slices.Contains(appliedVersions, legacyVersion) {
+ appliedVersions = append(appliedVersions, legacyVersion)
}
-}
-
-func getHighestEmbeddedMigrationVersionInternal(dbProvider string) (int64, error) {
- versions, err := getEmbeddedMigrationVersionsInternal(dbProvider)
- if err != nil {
- return 0, err
+ first := kit.Ternary(dbProvider == dbProviderPostgres, "$1", "?")
+ second := kit.Ternary(dbProvider == dbProviderPostgres, "$2", "?")
+ insertQuery := fmt.Sprintf("INSERT INTO %s (version_id, is_applied) VALUES (%s, %s)", gooseVersionTable, first, second)
+ for _, version := range appliedVersions {
+ if _, insertErr := tx.ExecContext(ctx, insertQuery, version, true); insertErr != nil {
+ return fmt.Errorf("failed to insert Goose migration version %d for %s: %w", version, dbProvider, insertErr)
+ }
}
- if len(versions) == 0 {
- return 0, fmt.Errorf("no embedded migrations found for %s", dbProvider)
+ if err = tx.Commit(); err != nil {
+ return fmt.Errorf("failed to commit legacy migration adoption for %s: %w", dbProvider, err)
}
-
- return versions[len(versions)-1], nil
+ slog.InfoContext(ctx, "Adopted legacy migration state into Goose", "provider", dbProvider, "legacyVersion", legacyVersion)
+ return nil
}
-func getEmbeddedMigrationVersionsInternal(dbProvider string) ([]int64, error) {
+func getEmbeddedMigrationVersions(dbProvider string) ([]int64, error) {
entries, err := resources.FS.ReadDir("migrations/" + dbProvider)
if err != nil {
return nil, fmt.Errorf("failed to read embedded migrations for %s: %w", dbProvider, err)
@@ -565,62 +416,7 @@ func getEmbeddedMigrationVersionsInternal(dbProvider string) ([]int64, error) {
versionsMap[version] = struct{}{}
}
- versions := make([]int64, 0, len(versionsMap))
- for version := range versionsMap {
- versions = append(versions, version)
- }
- slices.Sort(versions)
-
- return versions, nil
-}
-
-// missingEmbeddedDowngradeMigrationsInternal returns the applied migration
-// versions above requiredVersion that have no matching embedded migration file.
-// Goose can only roll back migrations whose .Down SQL is embedded in this
-// binary, so any such missing version makes an embedded-only downgrade
-// impossible and signals that a restore from backup is required.
-func missingEmbeddedDowngradeMigrationsInternal(ctx context.Context, db *sql.DB, dbProvider string, requiredVersion int64) ([]int64, error) {
- embeddedVersions, err := getEmbeddedMigrationVersionsInternal(dbProvider)
- if err != nil {
- return nil, err
- }
-
- embeddedSet := make(map[int64]struct{}, len(embeddedVersions))
- for _, version := range embeddedVersions {
- embeddedSet[version] = struct{}{}
- }
-
- queryFormat := "SELECT DISTINCT version_id FROM " + gooseVersionTable +
- " WHERE is_applied = " + kit.Ternary(dbProvider == dbProviderPostgres, "true", "1") +
- " AND version_id > %s ORDER BY version_id"
- query, args, err := sqlWithProviderPlaceholderInternal(dbProvider, queryFormat, requiredVersion)
- if err != nil {
- return nil, err
- }
-
- rows, err := db.QueryContext(ctx, query, args...)
- if err != nil {
- return nil, fmt.Errorf("failed to read applied migration versions for %s: %w", dbProvider, err)
- }
- defer func() {
- _ = rows.Close()
- }()
-
- var missing []int64
- for rows.Next() {
- var version int64
- if scanErr := rows.Scan(&version); scanErr != nil {
- return nil, fmt.Errorf("failed to scan applied migration version for %s: %w", dbProvider, scanErr)
- }
- if _, ok := embeddedSet[version]; !ok {
- missing = append(missing, version)
- }
- }
- if iterateVersionsErr := rows.Err(); iterateVersionsErr != nil {
- return nil, fmt.Errorf("failed to iterate applied migration versions for %s: %w", dbProvider, iterateVersionsErr)
- }
-
- return missing, nil
+ return slices.Sorted(maps.Keys(versionsMap)), nil
}
func ParseSQLiteConnectionString(connString string) (string, error) {
@@ -643,7 +439,8 @@ func ParseSQLiteConnectionString(connString string) (string, error) {
case "_case_sensitive_like", "_cslike":
qs.Add("_pragma", "case_sensitive_like("+v[0]+")")
case "_foreign_keys", "_fk":
- qs.Add("_pragma", "foreign_keys("+v[0]+")")
+ // Foreign-key enforcement is required on every pooled connection.
+ continue
case "_locking_mode", "_locking":
qs.Add("_pragma", "locking_mode("+v[0]+")")
case "_secure_delete":
@@ -655,12 +452,25 @@ func ParseSQLiteConnectionString(connString string) (string, error) {
case "_txlock":
qs.Add("_txlock", v[0])
case "_pragma":
- qs["_pragma"] = append(qs["_pragma"], v...)
+ for _, pragma := range v {
+ name := strings.TrimSpace(pragma)
+ if end := strings.IndexAny(name, "=( \t\r\n"); end >= 0 {
+ name = name[:end]
+ }
+ if _, unqualified, found := strings.Cut(name, "."); found {
+ name = unqualified
+ }
+ if strings.EqualFold(strings.Trim(name, "\"'`[]"), "foreign_keys") {
+ continue
+ }
+ qs.Add("_pragma", pragma)
+ }
default:
qs[k] = v
}
}
+ qs.Add("_pragma", "foreign_keys(1)")
connStringUrl.RawQuery = qs.Encode()
return connStringUrl.String(), nil
}
@@ -694,25 +504,3 @@ func (db *DB) Close() error {
func (db *DB) SQLDB() (*sql.DB, error) {
return db.DB.DB()
}
-
-// Create parent directory for file-based SQLite if needed
-func ensureSQLiteDirectoryInternal(connString string) error {
- if !strings.HasPrefix(connString, "file:") {
- return nil
- }
- pathPart, err := kit.SQLitePathFromDSN(connString)
- if err != nil {
- return fmt.Errorf("failed to parse SQLite DSN: %w", err)
- }
- if pathPart == "" || strings.HasPrefix(pathPart, ":memory:") {
- return nil
- }
-
- dir := filepath.Dir(pathPart)
- if dir == "" || dir == "." {
- return nil
- }
- // os.* rather than acfs: this creates the sqlite data directory itself,
- // which has to exist before any acfs root could be opened on it.
- return os.MkdirAll(dir, 0o755)
-}
diff --git a/backend/internal/database/database_test.go b/backend/internal/database/database_test.go
index 58835850e..bef1fde13 100644
--- a/backend/internal/database/database_test.go
+++ b/backend/internal/database/database_test.go
@@ -16,68 +16,151 @@ import (
"github.com/stretchr/testify/require"
sqliteutil "github.com/getarcaneapp/arcane/backend/v2/pkg/utils/sqlite"
+ "github.com/getarcaneapp/arcane/backend/v2/resources"
)
func TestGetEmbeddedMigrationVersions_ProvidersMatch(t *testing.T) {
- sqliteVersions, err := getEmbeddedMigrationVersionsInternal("sqlite")
+ sqliteVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
- postgresVersions, err := getEmbeddedMigrationVersionsInternal("postgres")
+ postgresVersions, err := getEmbeddedMigrationVersions("postgres")
require.NoError(t, err)
assert.Equal(t, sqliteVersions, postgresVersions)
require.NotEmpty(t, sqliteVersions)
-
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
- require.NoError(t, err)
- assert.Equal(t, sqliteVersions[len(sqliteVersions)-1], highest)
}
-func TestEnsureSQLiteDirectoryPreservesAbsoluteFilePath(t *testing.T) {
+func TestInitialize_CreatesSQLiteDirectory(t *testing.T) {
tempDir := t.TempDir()
dsn := "file:" + filepath.Join(tempDir, "nested", "arcane-test.db")
- require.NoError(t, ensureSQLiteDirectoryInternal(dsn))
+ db, err := Initialize(t.Context(), dsn, MigrationOptions{})
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, db.Close()) })
require.DirExists(t, filepath.Join(tempDir, "nested"))
require.NoDirExists(t, filepath.Join("var", "folders"))
}
func TestMigrateDatabase_BlocksDowngradeWithoutFlag(t *testing.T) {
ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-test.db")
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}))
- targetVersion := downgradeTargetVersionInternal(t)
+ rawDB, dsn := newSQLiteSQLDB(t, t.TempDir(), "arcane-test.db")
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
+ targetVersion := downgradeTargetVersion(t)
- err := migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, targetVersion)
+ err := migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, targetVersion)
require.Error(t, err)
require.ErrorContains(t, err, "ALLOW_DOWNGRADE=true")
require.ErrorContains(t, err, "newer than this Arcane binary supports")
- highestVersion, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ highestVersionVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
- assert.Equal(t, highestVersion, readGooseSQLiteVersionInternal(t, dsn))
+ require.NotEmpty(t, highestVersionVersions)
+ highestVersion := highestVersionVersions[len(highestVersionVersions)-1]
+ assert.Equal(t, highestVersion, readGooseSQLiteVersion(t, dsn))
}
func TestMigrateDatabase_DowngradesWhenAllowed(t *testing.T) {
ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-test.db")
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}))
- targetVersion := downgradeTargetVersionInternal(t)
+ rawDB, dsn := newSQLiteSQLDB(t, t.TempDir(), "arcane-test.db")
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
+ targetVersion := downgradeTargetVersion(t)
+
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, targetVersion))
+ assert.Equal(t, targetVersion, readGooseSQLiteVersion(t, dsn))
+
+ t.Run("authorization cleanup survives downgrade and re-upgrade", func(t *testing.T) {
+ cleanupCtx := t.Context()
+ orphanDB, orphanDSN := newSQLiteSQLDB(t, t.TempDir(), "arcane-orphaned-authorization.db")
+ orphanDB.SetMaxOpenConns(1)
+ require.NoError(t, migrateDatabase(cleanupCtx, orphanDB, dbProviderSQLite, MigrationOptions{}, 91))
+ _, err := orphanDB.ExecContext(cleanupCtx, `PRAGMA foreign_keys=OFF`)
+ require.NoError(t, err)
+ _, err = orphanDB.ExecContext(cleanupCtx, `
+ INSERT INTO users (id, username, password_hash, roles) VALUES ('user', 'user', 'unused', '[]');
+ INSERT INTO roles (id, name) VALUES ('role', 'Custom');
+ INSERT INTO environments (id, name, api_url) VALUES ('env', 'Env', 'http://env');
+ INSERT INTO api_keys (id, name, key_hash, key_prefix, user_id, environment_id) VALUES
+ ('personal', 'Personal', 'hash-1', 'arc_1', 'user', NULL),
+ ('system', 'System', 'hash-2', 'arc_2', NULL, NULL),
+ ('environment', 'Environment', 'hash-3', 'arc_3', NULL, 'env'),
+ ('missing-user', 'Missing user', 'hash-4', 'arc_4', 'missing', 'env'),
+ ('missing-env', 'Missing env', 'hash-5', 'arc_5', 'user', 'missing'),
+ ('missing-both', 'Missing both', 'hash-6', 'arc_6', 'missing', 'missing');
+ INSERT INTO user_role_assignments (id, user_id, role_id, environment_id, source) VALUES
+ ('global', 'user', 'role', NULL, 'manual'),
+ ('oidc', 'user', 'role', NULL, 'oidc'),
+ ('scoped', 'user', 'role', 'env', 'manual'),
+ ('missing-user', 'missing', 'role', NULL, 'manual'),
+ ('missing-role', 'user', 'missing', NULL, 'manual'),
+ ('missing-env', 'user', 'role', 'missing', 'manual');
+ INSERT INTO api_key_permissions (id, api_key_id, permission, environment_id) VALUES
+ ('global', 'personal', 'containers:read', NULL),
+ ('scoped', 'personal', 'containers:read', 'env'),
+ ('system', 'system', 'containers:read', NULL),
+ ('environment', 'environment', 'containers:read', 'env'),
+ ('missing-key', 'missing', 'containers:read', NULL),
+ ('missing-env', 'personal', 'containers:read', 'missing'),
+ ('missing-owner', 'missing-user', 'containers:read', 'env'),
+ ('missing-key-env', 'missing-env', 'containers:read', NULL),
+ ('missing-both', 'missing-both', 'containers:read', 'missing');
+ INSERT INTO oidc_role_mappings (id, claim_value, role_id, environment_id) VALUES
+ ('global', 'global', 'role', NULL),
+ ('scoped', 'scoped', 'role', 'env'),
+ ('missing-role', 'missing-role', 'missing', NULL),
+ ('missing-env', 'missing-env', 'role', 'missing');
+ `)
+ require.NoError(t, err)
+ var violations int
+ require.NoError(t, orphanDB.QueryRowContext(cleanupCtx, `SELECT COUNT(*) FROM pragma_foreign_key_check`).Scan(&violations))
+ require.Positive(t, violations)
+ require.NoError(t, orphanDB.Close())
+
+ db, err := Initialize(cleanupCtx, orphanDSN+"?_fk=0", MigrationOptions{})
+ require.NoError(t, err)
+ t.Cleanup(func() { require.NoError(t, db.Close()) })
+ assertClean := func() {
+ t.Helper()
+ for table, expectedIDs := range map[string][]string{
+ "users": {"user"},
+ "roles": {"role"},
+ "environments": {"env"},
+ "api_keys": {"environment", "personal", "system"},
+ "user_role_assignments": {"global", "oidc", "scoped"},
+ "api_key_permissions": {"environment", "global", "scoped", "system"},
+ "oidc_role_mappings": {"global", "scoped"},
+ } {
+ ids := []string{}
+ require.NoError(t, db.Table(table).Order("id").Pluck("id", &ids).Error)
+ require.Equal(t, expectedIDs, ids, table)
+ }
+ require.NoError(t, db.Raw(`SELECT COUNT(*) FROM pragma_foreign_key_check`).Scan(&violations).Error)
+ require.Zero(t, violations)
+ }
+ assertClean()
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, targetVersion))
- assert.Equal(t, targetVersion, readGooseSQLiteVersionInternal(t, dsn))
+ require.NoError(t, db.Close())
+ db, err = Initialize(cleanupCtx, orphanDSN, MigrationOptions{})
+ require.NoError(t, err)
+ assertClean()
+ sqlDB, err := db.SQLDB()
+ require.NoError(t, err)
+ require.NoError(t, migrateDatabase(cleanupCtx, sqlDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 91))
+ assertClean()
+ require.NoError(t, migrateDatabase(cleanupCtx, sqlDB, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
+ assertClean()
+ })
}
func TestMigration065_ProjectBuildImageRefs_UpAndDown(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-project-build-refs.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-project-build-refs.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 64))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 64))
var columnCount int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('projects') WHERE name = 'build_image_refs_json'`).Scan(&columnCount))
assert.Zero(t, columnCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 65))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 65))
var notNull int
require.NoError(
t,
@@ -90,21 +173,21 @@ func TestMigration065_ProjectBuildImageRefs_UpAndDown(t *testing.T) {
assert.Equal(t, 1, columnCount)
assert.Zero(t, notNull)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 64))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 64))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('projects') WHERE name = 'build_image_refs_json'`).Scan(&columnCount))
assert.Zero(t, columnCount)
}
func TestMigration066_GlobalVariables_UpAndDown(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-global-variables.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-global-variables.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 65))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 65))
var tableCount int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('global_variables', 'global_variable_environments')`).Scan(&tableCount))
assert.Zero(t, tableCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 66))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 66))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('global_variables', 'global_variable_environments')`).Scan(&tableCount))
assert.Equal(t, 2, tableCount)
@@ -116,11 +199,11 @@ func TestMigration066_GlobalVariables_UpAndDown(t *testing.T) {
_, err = rawDB.Exec(`INSERT INTO global_variable_environments (global_variable_id, environment_id) VALUES ('var-1', 'env-1')`)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 65))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 65))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('global_variables', 'global_variable_environments')`).Scan(&tableCount))
assert.Zero(t, tableCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 66))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 66))
var rowCount int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM global_variables`).Scan(&rowCount))
assert.Zero(t, rowCount)
@@ -128,9 +211,9 @@ func TestMigration066_GlobalVariables_UpAndDown(t *testing.T) {
func TestMigration088_VulnerabilityRisk_BackfillsCVSSAndDowngrades(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-vulnerability-risk.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-vulnerability-risk.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 87))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 87))
_, err := rawDB.Exec(`INSERT INTO vulnerability_scans (id, image_name, status, scan_time) VALUES ('img-1', 'nginx:latest', 'completed', CURRENT_TIMESTAMP)`)
require.NoError(t, err)
_, err = rawDB.Exec(`INSERT INTO vulnerability_scan_items (image_id, vulnerability_id, pkg_name, severity, details) VALUES
@@ -139,7 +222,7 @@ func TestMigration088_VulnerabilityRisk_BackfillsCVSSAndDowngrades(t *testing.T)
('img-1', 'CVE-NONE', 'zlib', 'LOW', NULL)`)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 88))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 88))
scores := map[string]stdsql.NullFloat64{}
rows, err := rawDB.Query(`SELECT vulnerability_id, cvss_score FROM vulnerability_scan_items`)
require.NoError(t, err)
@@ -163,7 +246,7 @@ func TestMigration088_VulnerabilityRisk_BackfillsCVSSAndDowngrades(t *testing.T)
)
assert.Equal(t, 2, tableCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 87))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 87))
require.NoError(
t,
rawDB.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('vulnerability_threat_intel', '"+
@@ -174,14 +257,14 @@ func TestMigration088_VulnerabilityRisk_BackfillsCVSSAndDowngrades(t *testing.T)
func TestMigration067_ActivityBatchID_UpAndDown(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-activity-batch-id.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-activity-batch-id.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 66))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 66))
var columnCount int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('activities') WHERE name = 'batch_id'`).Scan(&columnCount))
assert.Zero(t, columnCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 67))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 67))
var notNull int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*), COALESCE(MAX("notnull"), 0) FROM pragma_table_info('activities') WHERE name = 'batch_id'`).Scan(&columnCount, &notNull))
assert.Equal(t, 1, columnCount)
@@ -191,7 +274,7 @@ func TestMigration067_ActivityBatchID_UpAndDown(t *testing.T) {
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = 'idx_activities_environment_batch'`).Scan(&indexCount))
assert.Equal(t, 1, indexCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 66))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 66))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('activities') WHERE name = 'batch_id'`).Scan(&columnCount))
assert.Zero(t, columnCount)
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'index' AND name = 'idx_activities_environment_batch'`).Scan(&indexCount))
@@ -200,9 +283,9 @@ func TestMigration067_ActivityBatchID_UpAndDown(t *testing.T) {
func TestMigration068_UserPreferences_UpAndDown(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-user-preferences.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-user-preferences.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 67))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 67))
var columnCount int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('users') WHERE name = 'preferences'`).Scan(&columnCount))
assert.Zero(t, columnCount)
@@ -214,7 +297,7 @@ func TestMigration068_UserPreferences_UpAndDown(t *testing.T) {
_, err = rawDB.Exec(`INSERT INTO settings (key, value) VALUES ('applicationTheme', 'nord'), ('oledMode', 'true')`)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 68))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 68))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('users') WHERE name = 'preferences'`).Scan(&columnCount))
assert.Equal(t, 1, columnCount)
@@ -234,16 +317,16 @@ func TestMigration068_UserPreferences_UpAndDown(t *testing.T) {
assert.True(t, oled)
assert.False(t, iconCatalog.Valid)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 67))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 67))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('users') WHERE name = 'preferences'`).Scan(&columnCount))
assert.Zero(t, columnCount)
}
func TestMigration070_PasskeysAndMFA_UpAndDown(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-passkeys-mfa.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-passkeys-mfa.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 69))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 69))
var tableCount int
require.NoError(
t,
@@ -256,7 +339,7 @@ func TestMigration070_PasskeysAndMFA_UpAndDown(t *testing.T) {
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('users') WHERE name = 'passkey_mfa_enabled'`).Scan(&columnCount))
assert.Zero(t, columnCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 70))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 70))
require.NoError(
t,
rawDB.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('passkeys', 'auth_transactions'"+
@@ -268,7 +351,7 @@ func TestMigration070_PasskeysAndMFA_UpAndDown(t *testing.T) {
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM pragma_table_info('user_sessions') WHERE name IN ('mfa_method', 'mfa_verified_at')`).Scan(&columnCount))
assert.Equal(t, 2, columnCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 69))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 69))
require.NoError(
t,
rawDB.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name IN ('passkeys', 'auth_transactions'"+
@@ -286,13 +369,13 @@ func TestMigration070_PasskeysAndMFA_UpAndDown(t *testing.T) {
// format=archive, and that downgrading is refused while Rustic rows exist.
func TestMigration073_BackupSupport_PreservesExistingBackups(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-backup-support.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-backup-support.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 72))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 72))
_, err := rawDB.ExecContext(ctx, `INSERT INTO volume_backups (id, volume_name, size, created_at) VALUES ('legacy-1', 'app-data', 42, CURRENT_TIMESTAMP)`)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 73))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 73))
var format, status, destination string
var size int64
require.NoError(t, rawDB.QueryRow(`SELECT format, status, destination, size FROM volume_backups WHERE id = 'legacy-1'`).Scan(&format, &status, &destination, &size))
@@ -308,13 +391,13 @@ func TestMigration073_BackupSupport_PreservesExistingBackups(t *testing.T) {
"ustic-1', 'app-data', 7, CURRENT_TIMESTAMP, 'rustic', 'snap-1')",
)
require.NoError(t, err)
- err = migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 72)
+ err = migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 72)
require.Error(t, err)
// With only archive rows left, the downgrade succeeds and keeps the row.
_, err = rawDB.ExecContext(ctx, `DELETE FROM volume_backups WHERE id = 'rustic-1'`)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 72))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 72))
var count int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM volume_backups WHERE id = 'legacy-1'`).Scan(&count))
assert.Equal(t, 1, count)
@@ -322,15 +405,15 @@ func TestMigration073_BackupSupport_PreservesExistingBackups(t *testing.T) {
func TestMigration072_ProjectTags_UpDownAndCascade(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-project-tags.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-project-tags.db")
rawDB.SetMaxOpenConns(1)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 71))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 71))
var tableCount int
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'project_tags'`).Scan(&tableCount))
assert.Zero(t, tableCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 72))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 72))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'project_tags'`).Scan(&tableCount))
assert.Equal(t, 1, tableCount)
var indexCount int
@@ -354,16 +437,16 @@ func TestMigration072_ProjectTags_UpDownAndCascade(t *testing.T) {
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM project_tags WHERE project_id = 'project-1'`).Scan(&tagCount))
assert.Zero(t, tagCount)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 71))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 71))
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'project_tags'`).Scan(&tableCount))
assert.Zero(t, tableCount)
}
func TestMigration071_RenamesVolumeWorkspaceLegacyKeys(t *testing.T) {
ctx := t.Context()
- rawDB, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-volume-workspace-keys.db")
+ rawDB, _ := newSQLiteSQLDB(t, t.TempDir(), "arcane-volume-workspace-keys.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 70))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 70))
_, err := rawDB.Exec(`DELETE FROM settings WHERE key = 'volumeHelperIdleTimeout'`)
require.NoError(t, err)
_, err = rawDB.Exec(`INSERT INTO settings (key, value) VALUES ('volumeBrowserHelperIdleTimeout', '27')`)
@@ -376,7 +459,7 @@ func TestMigration071_RenamesVolumeWorkspaceLegacyKeys(t *testing.T) {
", 'volumes:browse'), ('grant-read', 'key-workspace', 'volumes:read')")
require.NoError(t, err)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 71))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, 71))
var timeout string
require.NoError(t, rawDB.QueryRow(`SELECT value FROM settings WHERE key = 'volumeHelperIdleTimeout'`).Scan(&timeout))
assert.Equal(t, "27", timeout)
@@ -392,92 +475,103 @@ func TestMigration071_RenamesVolumeWorkspaceLegacyKeys(t *testing.T) {
require.NoError(t, rawDB.QueryRow(`SELECT COUNT(*) FROM api_key_permissions WHERE permission = 'volumes:browse'`).Scan(&count))
assert.Zero(t, count)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 70))
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, 70))
require.NoError(t, rawDB.QueryRow(`SELECT value FROM settings WHERE key = 'volumeBrowserHelperIdleTimeout'`).Scan(&timeout))
assert.Equal(t, "27", timeout)
}
func TestMigrateDatabase_BlocksFutureGooseVersionWithoutFlag(t *testing.T) {
ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-future.db")
- highestVersion, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ rawDB, dsn := newSQLiteSQLDB(t, t.TempDir(), "arcane-future.db")
+ highestVersionVersions, err := getEmbeddedMigrationVersions("sqlite")
+ require.NoError(t, err)
+ require.NotEmpty(t, highestVersionVersions)
+ highestVersion := highestVersionVersions[len(highestVersionVersions)-1]
+ _, err = rawDB.ExecContext(ctx, `CREATE TABLE goose_db_version (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ version_id INTEGER NOT NULL,
+ is_applied INTEGER NOT NULL,
+ tstamp TIMESTAMP DEFAULT (datetime('now'))
+ )`)
+ require.NoError(t, err)
+ _, err = rawDB.ExecContext(ctx, `INSERT INTO goose_db_version (version_id, is_applied) VALUES (0, true), (?, true)`, highestVersion+1)
require.NoError(t, err)
- require.NoError(t, createGooseVersionTableInternal(ctx, rawDB, dbProviderSQLite))
- require.NoError(t, insertGooseMigrationVersionInternal(ctx, rawDB, dbProviderSQLite, 0))
- require.NoError(t, insertGooseMigrationVersionInternal(ctx, rawDB, dbProviderSQLite, highestVersion+1))
- err = migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{})
+ err = migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion)
require.Error(t, err)
require.ErrorContains(t, err, "newer than this Arcane binary supports")
- assert.Equal(t, highestVersion+1, readGooseSQLiteVersionInternal(t, dsn))
+ assert.Equal(t, highestVersion+1, readGooseSQLiteVersion(t, dsn))
}
func TestMigrateDatabase_BlocksDowngradeWhenEmbeddedMigrationMissing(t *testing.T) {
ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-missing-down.db")
- highestVersion, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ rawDB, dsn := newSQLiteSQLDB(t, t.TempDir(), "arcane-missing-down.db")
+ highestVersionVersions, err := getEmbeddedMigrationVersions("sqlite")
+ require.NoError(t, err)
+ require.NotEmpty(t, highestVersionVersions)
+ highestVersion := highestVersionVersions[len(highestVersionVersions)-1]
+ _, err = rawDB.ExecContext(ctx, `CREATE TABLE goose_db_version (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ version_id INTEGER NOT NULL,
+ is_applied INTEGER NOT NULL,
+ tstamp TIMESTAMP DEFAULT (datetime('now'))
+ )`)
+ require.NoError(t, err)
+ _, err = rawDB.ExecContext(ctx, `INSERT INTO goose_db_version (version_id, is_applied) VALUES (0, true), (?, true)`, highestVersion+1)
require.NoError(t, err)
- require.NoError(t, createGooseVersionTableInternal(ctx, rawDB, dbProviderSQLite))
- require.NoError(t, insertGooseMigrationVersionInternal(ctx, rawDB, dbProviderSQLite, 0))
- require.NoError(t, insertGooseMigrationVersionInternal(ctx, rawDB, dbProviderSQLite, highestVersion+1))
- err = migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true})
+ err = migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, latestMigrationVersion)
require.Error(t, err)
require.ErrorContains(t, err, "ALLOW_DOWNGRADE=true is not sufficient")
require.ErrorContains(t, err, "restore the database from a backup")
require.ErrorContains(t, err, strconv.FormatInt(highestVersion+1, 10))
- assert.Equal(t, highestVersion+1, readGooseSQLiteVersionInternal(t, dsn))
-}
-
-func TestMigrateDatabase_BlocksDirtyLegacyCurrentVersion(t *testing.T) {
- ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-legacy-current-dirty.db")
- highestVersion, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
- require.NoError(t, err)
- seedLegacyMigrationStateInternal(t, dsn, highestVersion, true)
-
- err = migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{})
- require.Error(t, err)
- require.ErrorContains(t, err, "is dirty")
- require.ErrorContains(t, err, "ALLOW_DOWNGRADE=true")
-
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}))
- assert.Equal(t, highestVersion, readGooseSQLiteVersionInternal(t, dsn))
- assertLegacyMigrationCleanInternal(t, dsn)
+ assert.Equal(t, highestVersion+1, readGooseSQLiteVersion(t, dsn))
}
-func TestMigrateDatabase_BlocksDirtyLegacyOlderVersion(t *testing.T) {
- ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-legacy-older-dirty.db")
- targetVersion := downgradeTargetVersionInternal(t)
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}))
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, targetVersion))
- require.NoError(t, clearGooseVersionTableInternal(ctx, rawDB, dbProviderSQLite))
- seedLegacyMigrationStateInternal(t, dsn, targetVersion, true)
-
- err := migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{})
- require.Error(t, err)
- require.ErrorContains(t, err, "is dirty")
- require.ErrorContains(t, err, "ALLOW_DOWNGRADE=true")
-
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}))
- highestVersion, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+func TestAdoptLegacyMigrationState_DirtyVersions(t *testing.T) {
+ highestVersions, err := getEmbeddedMigrationVersions(dbProviderSQLite)
require.NoError(t, err)
- assert.Equal(t, highestVersion, readGooseSQLiteVersionInternal(t, dsn))
- assertLegacyMigrationCleanInternal(t, dsn)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
+ for _, tc := range []struct {
+ name string
+ version int64
+ }{
+ {name: "current version", version: highest},
+ {name: "older version", version: downgradeTargetVersion(t)},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ ctx := t.Context()
+ db, dsn := newSQLiteSQLDB(t, t.TempDir(), "legacy-dirty.db")
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, tc.version))
+ _, clearErr := db.ExecContext(ctx, "DELETE FROM "+gooseVersionTable)
+ require.NoError(t, clearErr)
+ seedLegacyMigrationState(t, dsn, tc.version, true)
+
+ adoptionErr := adoptLegacyMigrationState(ctx, db, dbProviderSQLite, MigrationOptions{})
+ require.ErrorContains(t, adoptionErr, "is dirty")
+ require.ErrorContains(t, adoptionErr, "ALLOW_DOWNGRADE=true")
+ require.NoError(t, adoptLegacyMigrationState(ctx, db, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}))
+ assert.Equal(t, tc.version, readGooseSQLiteVersion(t, dsn))
+ assertLegacyMigrationClean(t, dsn)
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
+ assert.Equal(t, highest, readGooseSQLiteVersion(t, dsn))
+ })
+ }
}
-func downgradeTargetVersionInternal(t *testing.T) int64 {
+func downgradeTargetVersion(t *testing.T) int64 {
t.Helper()
- allVersions, err := getEmbeddedMigrationVersionsInternal("sqlite")
+ allVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
require.GreaterOrEqual(t, len(allVersions), 2, "need at least 2 migration versions to test downgrade")
return allVersions[len(allVersions)-2]
}
-func newSQLiteSQLDBInternal(t *testing.T, dirPath, fileName string) (*stdsql.DB, string) {
+func newSQLiteSQLDB(t *testing.T, dirPath, fileName string) (*stdsql.DB, string) {
t.Helper()
require.NoError(t, sqliteutil.RegisterFunctions())
@@ -492,6 +586,133 @@ func newSQLiteSQLDBInternal(t *testing.T, dirPath, fileName string) (*stdsql.DB,
}
func TestInitialize_AllowsMigrationOptions(t *testing.T) {
+ for _, broken := range []bool{false, true} {
+ t.Run(fmt.Sprintf("project references repair=%t", broken), func(t *testing.T) {
+ ctx := t.Context()
+ db, dsn := newSQLiteSQLDB(t, t.TempDir(), "project-references.db")
+ db.SetMaxOpenConns(1)
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, 43))
+ _, err := db.ExecContext(ctx, `
+ INSERT INTO environments (id, name, api_url) VALUES ('env', 'Environment', 'http://agent');
+ INSERT INTO projects (id, name, dir_name, path, status) VALUES ('project', 'Project', 'project', '/project', 'running');
+ INSERT INTO git_repositories (id, name, url, auth_type) VALUES ('repo', 'Repository', 'https://example.com/repo', 'none');
+ INSERT INTO gitops_syncs (id, name, environment_id, repository_id, branch, compose_path, project_name, project_id, updated_at)
+ VALUES ('saved', 'Saved', 'env', 'repo', 'main', 'compose.yaml', 'project', 'project', CURRENT_TIMESTAMP);`)
+ require.NoError(t, err)
+ targets := []int64{44, 43, 48, 47, latestMigrationVersion}
+ if broken {
+ // Reproduce the old rename with modern SQLite defaults, retaining the projects table's data and constraints.
+ _, err = db.ExecContext(ctx, `PRAGMA foreign_keys=OFF; PRAGMA legacy_alter_table=OFF;
+ ALTER TABLE projects RENAME TO projects_old;
+ PRAGMA legacy_alter_table=ON;
+ ALTER TABLE projects_old RENAME TO projects;
+ PRAGMA legacy_alter_table=OFF; PRAGMA foreign_keys=ON;`)
+ require.NoError(t, err)
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, 91))
+ _, err = db.ExecContext(ctx, `INSERT INTO gitops_syncs
+ (id, name, environment_id, repository_id, branch, compose_path, project_name, updated_at)
+ VALUES ('blocked', 'Blocked', 'env', 'repo', 'main', 'compose.yaml', 'project', CURRENT_TIMESTAMP)`)
+ require.ErrorContains(t, err, "projects_old")
+ _, err = db.ExecContext(ctx, `UPDATE gitops_syncs SET pre_deploy_script_path = 'hooks/deploy.sh',
+ pre_deploy_last_run_output = 'saved output', mode = 'backup', backup_paths = '["/data"]',
+ last_backup_snapshot = 'snapshot' WHERE id = 'saved'`)
+ require.NoError(t, err)
+ targets = []int64{latestMigrationVersion, 91, latestMigrationVersion}
+ }
+ reopenDSN, err := ParseSQLiteConnectionString(dsn)
+ require.NoError(t, err)
+ for _, target := range targets {
+ // Each step uses a fresh connection so migration 006 cannot mask rename bugs with a leaked pragma.
+ require.NoError(t, db.Close())
+ reopened, openErr := stdsql.Open("sqlite", reopenDSN)
+ require.NoError(t, openErr)
+ t.Cleanup(func() { require.NoError(t, reopened.Close()) })
+ db = reopened
+ db.SetMaxOpenConns(1)
+ migrationName := ""
+ marker := "ALTER TABLE projects_new RENAME TO projects"
+ switch {
+ case broken && target == latestMigrationVersion:
+ migrationName = "092_remove_orphaned_authorization_records.sql"
+ marker = "ALTER TABLE gitops_syncs_new RENAME TO gitops_syncs"
+ case target == 43 || target == 44:
+ migrationName = "044_allow_nested_project_paths.sql"
+ case target == 47:
+ migrationName = "048_add_compose_project_name.sql"
+ }
+ if migrationName != "" {
+ migration, readErr := resources.FS.ReadFile("migrations/sqlite/" + migrationName)
+ require.NoError(t, readErr)
+ up, down := gooseUpDownSections(string(migration))
+ section := up
+ if target == 43 || target == 47 {
+ section = down
+ }
+ beforeRename, _, found := strings.Cut(section, marker)
+ require.True(t, found)
+ _, err = db.ExecContext(ctx, beforeRename)
+ require.NoError(t, err)
+ require.NoError(t, db.Close())
+ restored, restoreErr := stdsql.Open("sqlite", reopenDSN)
+ require.NoError(t, restoreErr)
+ t.Cleanup(func() { require.NoError(t, restored.Close()) })
+ db = restored
+ db.SetMaxOpenConns(1)
+ var saved int
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM projects
+ JOIN gitops_syncs ON gitops_syncs.project_id = projects.id
+ WHERE gitops_syncs.id = 'saved'`).Scan(&saved))
+ require.Equal(t, 1, saved)
+ }
+ if target == latestMigrationVersion {
+ require.NoError(t, db.Close())
+ initialized, initErr := Initialize(ctx, reopenDSN, MigrationOptions{})
+ require.NoError(t, initErr)
+ t.Cleanup(func() { require.NoError(t, initialized.Close()) })
+ db, err = initialized.SQLDB()
+ require.NoError(t, err)
+ } else {
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, target))
+ }
+ var parent string
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT "table" FROM pragma_foreign_key_list('gitops_syncs') WHERE "from" = 'project_id'`).Scan(&parent))
+ require.Equal(t, "projects", parent)
+ var savedProject string
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT project_id FROM gitops_syncs WHERE id = 'saved'`).Scan(&savedProject))
+ require.Equal(t, "project", savedProject)
+ _, err = db.ExecContext(ctx, `INSERT INTO gitops_syncs
+ (id, name, environment_id, repository_id, branch, compose_path, project_name, project_id, updated_at)
+ VALUES ('new', 'New', 'env', 'repo', 'main', 'compose.yaml', 'project', 'project', CURRENT_TIMESTAMP);
+ UPDATE gitops_syncs SET project_id = NULL WHERE id = 'new';
+ DELETE FROM gitops_syncs WHERE id = 'new';`)
+ require.NoError(t, err)
+ var count int
+ require.NoError(t, db.QueryRowContext(ctx, `PRAGMA foreign_keys`).Scan(&count))
+ require.Equal(t, 1, count)
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM pragma_foreign_key_check`).Scan(&count))
+ require.Zero(t, count)
+ if broken {
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM gitops_syncs WHERE id = 'saved'
+ AND pre_deploy_script_path = 'hooks/deploy.sh' AND pre_deploy_last_run_output = 'saved output'
+ AND mode = 'backup' AND backup_paths = '["/data"]' AND last_backup_snapshot = 'snapshot'`).Scan(&count))
+ require.Equal(t, 1, count)
+ _, err = db.ExecContext(ctx, `INSERT INTO gitops_syncs
+ (id, name, environment_id, repository_id, branch, compose_path, project_name, project_id, mode, updated_at)
+ VALUES ('duplicate', 'Duplicate', 'env', 'repo', 'main', 'compose.yaml', 'project', 'project', 'backup', CURRENT_TIMESTAMP)`)
+ require.ErrorContains(t, err, "UNIQUE constraint failed")
+ }
+ }
+ _, err = db.ExecContext(ctx, `INSERT INTO project_tags (project_id, name, source) VALUES ('project', 'tag', 'ui');
+ DELETE FROM projects WHERE id = 'project';`)
+ require.NoError(t, err)
+ var count int
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM gitops_syncs WHERE id = 'saved' AND project_id IS NULL`).Scan(&count))
+ require.Equal(t, 1, count)
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM project_tags`).Scan(&count))
+ require.Zero(t, count)
+ })
+ }
+
ctx := t.Context()
dsn := "file:" + filepath.Join(t.TempDir(), "arcane-init.db")
@@ -516,17 +737,103 @@ func TestInitialize_RecordsGooseVersionOnFreshSQLite(t *testing.T) {
require.NoError(t, db.Close())
})
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
- require.NoError(t, err)
- assert.Equal(t, highest, readGooseSQLiteVersionInternal(t, dsn))
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
+ require.NoError(t, err)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
+ assert.Equal(t, highest, readGooseSQLiteVersion(t, dsn))
+
+ for _, phase := range []string{"fresh", "reopened"} {
+ t.Run(phase+" pool enforces foreign keys", func(t *testing.T) {
+ sqlDB, poolErr := db.SQLDB()
+ require.NoError(t, poolErr)
+ connections := make([]*stdsql.Conn, 0, 3)
+ for range 3 {
+ conn, connErr := sqlDB.Conn(ctx)
+ require.NoError(t, connErr)
+ connections = append(connections, conn)
+ t.Cleanup(func() { require.NoError(t, conn.Close()) })
+ }
+ for _, conn := range connections {
+ var enabled int
+ require.NoError(t, conn.QueryRowContext(ctx, `PRAGMA foreign_keys`).Scan(&enabled))
+ require.Equal(t, 1, enabled)
+ _, insertErr := conn.ExecContext(ctx, `INSERT INTO user_role_assignments (id, user_id, role_id)
+ VALUES ('invalid', 'missing-user', 'missing-role')`)
+ require.ErrorContains(t, insertErr, "FOREIGN KEY constraint failed")
+ }
+ })
+ if phase == "fresh" {
+ require.NoError(t, db.Close())
+ db, err = Initialize(ctx, dsn+"?_pragma=foreign_keys(0)", MigrationOptions{})
+ require.NoError(t, err)
+ }
+ }
+
+ for parent, deletedTables := range map[string][]string{
+ "users": {"users", "user_role_assignments", "api_keys", "api_key_permissions"},
+ "api_keys": {"api_keys", "api_key_permissions"},
+ "environments": {"environments", "user_role_assignments", "api_keys", "api_key_permissions", "oidc_role_mappings"},
+ "roles": {"roles", "user_role_assignments", "oidc_role_mappings"},
+ } {
+ t.Run(parent+" deletion cascades", func(t *testing.T) {
+ cascadeDB, cascadeErr := Initialize(ctx, "file:"+filepath.Join(t.TempDir(), "cascade.db"), MigrationOptions{})
+ require.NoError(t, cascadeErr)
+ t.Cleanup(func() { require.NoError(t, cascadeDB.Close()) })
+ require.NoError(t, cascadeDB.Exec(`
+ INSERT INTO users (id, username, password_hash, roles) VALUES
+ ('target', 'target', 'unused', '[]'), ('other', 'other', 'unused', '[]');
+ INSERT INTO roles (id, name) VALUES ('target', 'Target'), ('other', 'Other');
+ INSERT INTO environments (id, name, api_url) VALUES
+ ('target', 'Target', 'http://target'), ('other', 'Other', 'http://other');
+ INSERT INTO api_keys (id, name, key_hash, key_prefix, user_id, environment_id) VALUES
+ ('target', 'Target', 'hash-target', 'arc_target', 'target', 'target'),
+ ('other', 'Other', 'hash-other', 'arc_other', 'other', 'other'),
+ ('system', 'System', 'hash-system', 'arc_system', NULL, NULL);
+ UPDATE environments SET api_key_id = 'target' WHERE id = 'target';
+ INSERT INTO user_role_assignments (id, user_id, role_id, environment_id) VALUES
+ ('target', 'target', 'target', 'target'), ('other', 'other', 'other', 'other');
+ INSERT INTO api_key_permissions (id, api_key_id, permission, environment_id) VALUES
+ ('target', 'target', 'containers:read', 'target'),
+ ('other', 'other', 'containers:read', 'other'),
+ ('system', 'system', 'containers:read', NULL);
+ INSERT INTO oidc_role_mappings (id, claim_value, role_id, environment_id) VALUES
+ ('target', 'target', 'target', 'target'), ('other', 'other', 'other', 'other');
+ `).Error)
+ require.NoError(t, cascadeDB.Table(parent).Where("id = ?", "target").Delete(nil).Error)
+ for _, table := range []string{
+ "users", "api_keys", "environments", "roles",
+ "user_role_assignments", "api_key_permissions", "oidc_role_mappings",
+ } {
+ var count int64
+ require.NoError(t, cascadeDB.Table(table).Where("id = ?", "other").Count(&count).Error)
+ require.EqualValues(t, 1, count, table)
+ }
+ for _, table := range deletedTables {
+ var count int64
+ require.NoError(t, cascadeDB.Table(table).Where("id = ?", "target").Count(&count).Error)
+ require.Zero(t, count, table)
+ }
+ for _, table := range []string{"api_keys", "api_key_permissions"} {
+ var count int64
+ require.NoError(t, cascadeDB.Table(table).Where("id = ?", "system").Count(&count).Error)
+ require.EqualValues(t, 1, count, table)
+ }
+ var violations int
+ require.NoError(t, cascadeDB.Raw(`SELECT COUNT(*) FROM pragma_foreign_key_check`).Scan(&violations).Error)
+ require.Zero(t, violations)
+ })
+ }
}
func TestInitialize_AdoptsCleanLegacyMigrationState(t *testing.T) {
ctx := t.Context()
dsn := "file:" + filepath.Join(t.TempDir(), "arcane-legacy-clean.db")
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
- seedLegacyMigrationStateInternal(t, dsn, highest, false)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
+ seedLegacyMigrationState(t, dsn, highest, false)
db, err := Initialize(ctx, dsn, MigrationOptions{})
require.NoError(t, err)
@@ -535,16 +842,18 @@ func TestInitialize_AdoptsCleanLegacyMigrationState(t *testing.T) {
require.NoError(t, db.Close())
})
- assert.Equal(t, highest, readGooseSQLiteVersionInternal(t, dsn))
- assertLegacyMigrationCleanInternal(t, dsn)
+ assert.Equal(t, highest, readGooseSQLiteVersion(t, dsn))
+ assertLegacyMigrationClean(t, dsn)
}
func TestInitialize_RollsBackFailedLegacyMigrationAdoption(t *testing.T) {
ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-legacy-rollback.db")
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ rawDB, dsn := newSQLiteSQLDB(t, t.TempDir(), "arcane-legacy-rollback.db")
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
- seedLegacyMigrationStateInternal(t, dsn, highest, false)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
+ seedLegacyMigrationState(t, dsn, highest, false)
_, err = rawDB.Exec(`
CREATE TABLE goose_db_version (
@@ -557,7 +866,7 @@ CREATE TABLE goose_db_version (
_, err = rawDB.Exec(`INSERT INTO goose_db_version (version_id, is_applied) VALUES (?, ?)`, 0, 0)
require.NoError(t, err)
- err = adoptLegacyMigrationStateInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{})
+ err = adoptLegacyMigrationState(ctx, rawDB, dbProviderSQLite, MigrationOptions{})
require.Error(t, err)
require.ErrorContains(t, err, "failed to insert Goose migration version")
@@ -570,9 +879,11 @@ CREATE TABLE goose_db_version (
func TestInitialize_BlocksDirtyLegacyMigrationState(t *testing.T) {
ctx := t.Context()
dsn := "file:" + filepath.Join(t.TempDir(), "arcane-legacy-dirty.db")
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
- seedLegacyMigrationStateInternal(t, dsn, highest, true)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
+ seedLegacyMigrationState(t, dsn, highest, true)
db, err := Initialize(ctx, dsn, MigrationOptions{})
require.Error(t, err)
@@ -584,9 +895,11 @@ func TestInitialize_BlocksDirtyLegacyMigrationState(t *testing.T) {
func TestInitialize_ClearsDirtyLegacyMigrationStateWhenAllowed(t *testing.T) {
ctx := t.Context()
dsn := "file:" + filepath.Join(t.TempDir(), "arcane-legacy-dirty-allowed.db")
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
- seedLegacyMigrationStateInternal(t, dsn, highest, true)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
+ seedLegacyMigrationState(t, dsn, highest, true)
db, err := Initialize(ctx, dsn, MigrationOptions{AllowDowngrade: true})
require.NoError(t, err)
@@ -595,8 +908,8 @@ func TestInitialize_ClearsDirtyLegacyMigrationStateWhenAllowed(t *testing.T) {
require.NoError(t, db.Close())
})
- assert.Equal(t, highest, readGooseSQLiteVersionInternal(t, dsn))
- assertLegacyMigrationCleanInternal(t, dsn)
+ assert.Equal(t, highest, readGooseSQLiteVersion(t, dsn))
+ assertLegacyMigrationClean(t, dsn)
}
func TestInitialize_CreatesQueryPerformanceIndexes(t *testing.T) {
@@ -636,7 +949,7 @@ func TestInitialize_CreatesQueryPerformanceIndexes(t *testing.T) {
}
for _, indexName := range indexes {
- assertSQLiteIndexExistsInternal(t, db, indexName)
+ assertSQLiteIndexExists(t, db, indexName)
}
removedIndexes := []string{
@@ -651,11 +964,11 @@ func TestInitialize_CreatesQueryPerformanceIndexes(t *testing.T) {
}
for _, indexName := range removedIndexes {
- assertSQLiteIndexMissingInternal(t, db, indexName)
+ assertSQLiteIndexMissing(t, db, indexName)
}
}
-func assertSQLiteIndexExistsInternal(t *testing.T, db *DB, indexName string) {
+func assertSQLiteIndexExists(t *testing.T, db *DB, indexName string) {
t.Helper()
var result struct {
@@ -670,7 +983,7 @@ func assertSQLiteIndexExistsInternal(t *testing.T, db *DB, indexName string) {
assert.Equal(t, indexName, result.Name)
}
-func assertSQLiteIndexMissingInternal(t *testing.T, db *DB, indexName string) {
+func assertSQLiteIndexMissing(t *testing.T, db *DB, indexName string) {
t.Helper()
var count int64
@@ -683,7 +996,7 @@ func assertSQLiteIndexMissingInternal(t *testing.T, db *DB, indexName string) {
assert.Zero(t, count, "expected index %s to be removed", indexName)
}
-func seedLegacyMigrationStateInternal(t *testing.T, dsn string, version int64, dirty bool) {
+func seedLegacyMigrationState(t *testing.T, dsn string, version int64, dirty bool) {
t.Helper()
rawDB, err := stdsql.Open("sqlite", dsn)
@@ -703,7 +1016,7 @@ CREATE TABLE schema_migrations (
require.NoError(t, err)
}
-func readGooseSQLiteVersionInternal(t *testing.T, dsn string) int64 {
+func readGooseSQLiteVersion(t *testing.T, dsn string) int64 {
t.Helper()
rawDB, err := stdsql.Open("sqlite", dsn)
@@ -718,7 +1031,7 @@ func readGooseSQLiteVersionInternal(t *testing.T, dsn string) int64 {
return version
}
-func assertLegacyMigrationCleanInternal(t *testing.T, dsn string) {
+func assertLegacyMigrationClean(t *testing.T, dsn string) {
t.Helper()
rawDB, err := stdsql.Open("sqlite", dsn)
@@ -733,14 +1046,8 @@ func assertLegacyMigrationCleanInternal(t *testing.T, dsn string) {
assert.False(t, dirty)
}
-// TestSQLiteMigrations_ColumnAddsAreReversible guards against the historical footgun
-// where a SQLite migration adds a column but leaves a no-op '-- +goose Down' (on the
-// mistaken belief that SQLite can't DROP COLUMN). The bundled modernc SQLite supports
-// DROP COLUMN, so every column-adding migration must reverse itself — otherwise a
-// down/up round-trip fails with a duplicate-column error. See 059_add_api_key_kind.sql
-// for the expected pattern.
func TestSQLiteMigrations_ColumnAddsAreReversible(t *testing.T) {
- migrationsFS, err := embeddedMigrationFSInternal(dbProviderSQLite)
+ migrationsFS, err := fs.Sub(resources.FS, "migrations/"+dbProviderSQLite)
require.NoError(t, err)
entries, err := fs.ReadDir(migrationsFS, ".")
@@ -754,14 +1061,14 @@ func TestSQLiteMigrations_ColumnAddsAreReversible(t *testing.T) {
content, readFileErr := fs.ReadFile(migrationsFS, entry.Name())
require.NoError(t, readFileErr)
- up, down := gooseUpDownSectionsInternal(string(content))
+ up, down := gooseUpDownSections(string(content))
if !strings.Contains(strings.ToUpper(up), "ADD COLUMN") {
continue
}
assert.True(
t,
- sectionHasSQLInternal(down),
+ sectionHasSQL(down),
"migration %s adds a column but its '-- +goose Down' has no SQL; add the reversing ALTER TABLE ... DR"+
"OP COLUMN (modernc SQLite supports it). A no-op Down breaks down/up round-trips with a duplicate-col"+
"umn error.",
@@ -770,37 +1077,159 @@ func TestSQLiteMigrations_ColumnAddsAreReversible(t *testing.T) {
}
}
-// TestSQLiteMigrations_DownUpRoundTrip migrates fully up, downgrades to just below
-// 029_add_ssh_host_key_verification (whose Down was previously a no-op), then migrates
-// back up. It proves that Down block executes cleanly (DROP COLUMN) and that re-applying
-// the Up does not fail with a duplicate-column error.
-//
-// The target stops at 28 rather than below 007 because downgrading past version 25 hits
-// a separate, pre-existing problem: 025's Down drops a column from api_keys while a
-// foreign key still references it, which SQLite rejects. That is unrelated to the no-op
-// Down fixes here and is tracked separately.
func TestSQLiteMigrations_DownUpRoundTrip(t *testing.T) {
- ctx := t.Context()
- rawDB, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "arcane-roundtrip.db")
-
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}))
+ for _, version := range []int64{28, 29} {
+ t.Run(fmt.Sprintf("gitops rebuild from %d preserves legacy references", version), func(t *testing.T) {
+ ctx := t.Context()
+ db, dsn := newSQLiteSQLDB(t, t.TempDir(), "legacy-gitops.db")
+ db.SetMaxOpenConns(1)
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, version))
+ _, err := db.ExecContext(ctx, `PRAGMA foreign_keys=OFF;
+ INSERT INTO environments (id, name, api_url) VALUES ('env', 'Environment', 'http://agent');
+ INSERT INTO git_repositories (id, name, url, auth_type) VALUES ('repo', 'Repository', 'https://example.com/repo', 'none');
+ INSERT INTO gitops_syncs (id, name, repository_id, branch, compose_path, project_name, project_id, environment_id, updated_at) VALUES
+ ('valid', 'Valid', 'repo', 'main', 'compose.yaml', 'valid', NULL, 'env', NULL),
+ ('project', 'Missing project', 'repo', 'main', 'compose.yaml', 'project', 'deleted-project', 'env', CURRENT_TIMESTAMP),
+ ('environment', 'Missing environment', 'repo', 'main', 'compose.yaml', 'environment', NULL, 'deleted-env', CURRENT_TIMESTAMP),
+ ('repository', 'Missing repository', 'deleted-repo', 'main', 'compose.yaml', 'repository', NULL, 'env', CURRENT_TIMESTAMP);
+ PRAGMA foreign_keys=ON;`)
+ require.NoError(t, err)
- const belowFixedVersion = int64(28)
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, belowFixedVersion))
- assert.Equal(t, belowFixedVersion, readGooseSQLiteVersionInternal(t, dsn))
+ for _, target := range []int64{30, 29, latestMigrationVersion} {
+ if target == latestMigrationVersion {
+ require.NoError(t, db.Close())
+ initialized, initErr := Initialize(ctx, dsn, MigrationOptions{})
+ require.NoError(t, initErr)
+ t.Cleanup(func() { require.NoError(t, initialized.Close()) })
+ db, err = initialized.SQLDB()
+ require.NoError(t, err)
+ } else {
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, target))
+ }
+ var count int
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM gitops_syncs WHERE
+ (id = 'valid' AND repository_id = 'repo' AND environment_id = 'env' AND project_id IS NULL AND updated_at IS NULL) OR
+ (id = 'project' AND repository_id = 'repo' AND environment_id = 'env' AND project_id = 'deleted-project') OR
+ (id = 'environment' AND repository_id = 'repo' AND environment_id = 'deleted-env' AND project_id IS NULL) OR
+ (id = 'repository' AND repository_id = 'deleted-repo' AND environment_id = 'env' AND project_id IS NULL)`).Scan(&count))
+ require.Equal(t, 4, count, "saved syncs after migration to %d", target)
+ require.NoError(t, db.QueryRowContext(ctx, `PRAGMA foreign_keys`).Scan(&count))
+ require.Equal(t, 1, count)
+ }
+ })
+ }
- // Re-up: a no-op Down would have left the column in place, so this would fail
- // with "duplicate column name".
- require.NoError(t, migrateDatabaseInternal(ctx, rawDB, dbProviderSQLite, MigrationOptions{}))
+ t.Run("api key rebuild preserves environment pairings", func(t *testing.T) {
+ ctx := t.Context()
+ db, dsn := newSQLiteSQLDB(t, t.TempDir(), "paired-environment.db")
+ db.SetMaxOpenConns(1)
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, 39))
+ _, err := db.ExecContext(ctx, `PRAGMA foreign_keys=ON`)
+ require.NoError(t, err)
+ _, err = db.ExecContext(ctx, `
+ INSERT INTO users (id, username, password_hash) VALUES ('owner', 'owner', 'unused');
+ INSERT INTO environments (id, name, api_url) VALUES
+ ('paired', 'Paired', 'http://paired'), ('unpaired', 'Unpaired', 'http://unpaired');
+ INSERT INTO api_keys (id, name, key_hash, key_prefix, user_id, environment_id) VALUES
+ ('pairing-key', 'Pairing', 'saved-hash', 'arc_pairing', 'owner', 'paired');
+ UPDATE environments SET api_key_id = 'pairing-key' WHERE id = 'paired';
+ `)
+ require.NoError(t, err)
+
+ reopenDSN, err := ParseSQLiteConnectionString(dsn)
+ require.NoError(t, err)
+
+ for _, target := range []int64{40, 39, 46, 45, latestMigrationVersion} {
+ if target == latestMigrationVersion {
+ require.NoError(t, db.Close())
+ initialized, initErr := Initialize(ctx, dsn, MigrationOptions{})
+ require.NoError(t, initErr)
+ t.Cleanup(func() { require.NoError(t, initialized.Close()) })
+ db, err = initialized.SQLDB()
+ require.NoError(t, err)
+ } else {
+ migrationName := "040_add_api_key_managed_by.sql"
+ if target == 46 {
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, 45))
+ }
+ if target == 45 || target == 46 {
+ migrationName = "046_nullable_api_key_user_id.sql"
+ }
+ migration, readErr := resources.FS.ReadFile("migrations/sqlite/" + migrationName)
+ require.NoError(t, readErr)
+ up, down := gooseUpDownSections(string(migration))
+ section := up
+ if target == 39 || target == 45 {
+ section = down
+ }
+ beforeRename, _, found := strings.Cut(section, "ALTER TABLE api_keys_")
+ require.True(t, found)
+ _, err = db.ExecContext(ctx, beforeRename)
+ require.NoError(t, err)
+ // Closing before the rename simulates an interrupted rebuild.
+ require.NoError(t, db.Close())
+ reopened, openErr := stdsql.Open("sqlite", reopenDSN)
+ require.NoError(t, openErr)
+ t.Cleanup(func() { require.NoError(t, reopened.Close()) })
+ db = reopened
+ db.SetMaxOpenConns(1)
+ var savedKeys int
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM api_keys
+ JOIN environments ON environments.api_key_id = api_keys.id
+ WHERE api_keys.id = 'pairing-key' AND key_hash = 'saved-hash'`).Scan(&savedKeys))
+ require.Equal(t, 1, savedKeys, "pairing after interrupted migration to %d", target)
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, target))
+ }
+ var count int
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM environments
+ JOIN api_keys ON api_keys.id = environments.api_key_id
+ WHERE environments.id = 'paired' AND api_keys.id = 'pairing-key'
+ AND api_keys.key_hash = 'saved-hash' AND api_keys.user_id = 'owner'
+ AND api_keys.environment_id = 'paired'`).Scan(&count))
+ require.Equal(t, 1, count, "pairing after migration to %d", target)
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM environments
+ WHERE id = 'unpaired' AND api_key_id IS NULL`).Scan(&count))
+ require.Equal(t, 1, count)
+ require.NoError(t, db.QueryRowContext(ctx, `PRAGMA foreign_keys`).Scan(&count))
+ require.Equal(t, 1, count)
+ require.NoError(t, db.QueryRowContext(ctx, `SELECT COUNT(*) FROM pragma_foreign_key_check`).Scan(&count))
+ require.Zero(t, count)
+ }
+ })
- highestVersion, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ ctx := t.Context()
+ rawDB, dsn := newSQLiteSQLDB(t, t.TempDir(), "arcane-roundtrip.db")
+ rawDB.SetMaxOpenConns(1)
+ versions, err := getEmbeddedMigrationVersions(dbProviderSQLite)
require.NoError(t, err)
- assert.Equal(t, highestVersion, readGooseSQLiteVersionInternal(t, dsn))
+ require.NotEmpty(t, versions)
+ targets := append([]int64{}, versions...)
+ for i := len(versions) - 2; i >= 0; i-- {
+ targets = append(targets, versions[i])
+ }
+ targets = append(targets, 0)
+ targets = append(targets, versions...)
+ for step, target := range targets {
+ passed := t.Run(fmt.Sprintf("schema step %d version %d", step, target), func(t *testing.T) {
+ _, pragmaErr := rawDB.ExecContext(ctx, `PRAGMA foreign_keys=ON; PRAGMA legacy_alter_table=OFF;`)
+ require.NoError(t, pragmaErr)
+ require.NoError(t, migrateDatabase(ctx, rawDB, dbProviderSQLite, MigrationOptions{AllowDowngrade: true}, target))
+ var missing int
+ require.NoError(t, rawDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM sqlite_schema AS s
+ JOIN pragma_foreign_key_list(s.name) AS fk WHERE s.type = 'table'
+ AND NOT EXISTS (SELECT 1 FROM sqlite_schema AS parent WHERE parent.type = 'table' AND parent.name = fk."table")`).Scan(&missing))
+ require.Zero(t, missing, "foreign keys must reference existing tables")
+ require.NoError(t, rawDB.QueryRowContext(ctx, `SELECT COUNT(*) FROM pragma_foreign_key_check`).Scan(&missing))
+ require.Zero(t, missing)
+ })
+ require.True(t, passed)
+ }
+ assert.Equal(t, versions[len(versions)-1], readGooseSQLiteVersion(t, dsn))
}
-// gooseUpDownSectionsInternal splits a goose migration into the text before the
+// gooseUpDownSections splits a goose migration into the text before the
// '-- +goose Down' marker (the Up section) and the text after it (the Down section).
-func gooseUpDownSectionsInternal(content string) (up, down string) {
+func gooseUpDownSections(content string) (up, down string) {
const downMarker = "-- +goose Down"
before, after, ok := strings.Cut(content, downMarker)
if !ok {
@@ -809,9 +1238,9 @@ func gooseUpDownSectionsInternal(content string) (up, down string) {
return before, after
}
-// sectionHasSQLInternal reports whether a migration section contains at least one
+// sectionHasSQL reports whether a migration section contains at least one
// non-comment, non-blank line (i.e. an actual statement rather than only comments).
-func sectionHasSQLInternal(section string) bool {
+func sectionHasSQL(section string) bool {
for line := range strings.SplitSeq(section, "\n") {
trimmed := strings.TrimSpace(line)
if trimmed == "" || strings.HasPrefix(trimmed, "--") {
@@ -835,8 +1264,8 @@ func TestIdentityNormalizationMigration(t *testing.T) {
} {
t.Run(tc.name, func(t *testing.T) {
ctx := t.Context()
- db, _ := newSQLiteSQLDBInternal(t, t.TempDir(), "identities.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, db, dbProviderSQLite, MigrationOptions{}, 80))
+ db, _ := newSQLiteSQLDB(t, t.TempDir(), "identities.db")
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, 80))
const displayName = "\t\n\v\f\r \u0085\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000Jose\u0301\u3000"
_, err := db.Exec(
"INSERT INTO users (id, username, email, display_name, password_hash) VALUES (?, ?, ?, ?, 'unchanged'"+
@@ -851,8 +1280,8 @@ func TestIdentityNormalizationMigration(t *testing.T) {
nil,
)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseInternal(ctx, db, dbProviderSQLite, MigrationOptions{}))
- require.NoError(t, migrateDatabaseInternal(ctx, db, dbProviderSQLite, MigrationOptions{}))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
for _, expected := range []struct {
id, username, email string
displayName stdsql.NullString
@@ -883,8 +1312,10 @@ func TestIdentityNormalizationMigration(t *testing.T) {
}
var version int64
require.NoError(t, db.QueryRow("SELECT MAX(version_id) FROM goose_db_version WHERE is_applied = 1").Scan(&version))
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
require.Equal(t, highest, version)
})
}
@@ -892,8 +1323,8 @@ func TestIdentityNormalizationMigration(t *testing.T) {
func TestIdentityNormalizationGooseUpgrade(t *testing.T) {
ctx := t.Context()
- db, dsn := newSQLiteSQLDBInternal(t, t.TempDir(), "upgrade.db")
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, db, dbProviderSQLite, MigrationOptions{}, 80))
+ db, dsn := newSQLiteSQLDB(t, t.TempDir(), "upgrade.db")
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, 80))
_, err := db.Exec(
"INSERT INTO users (id, username, display_name, password_hash) VALUES (?, ?, ?, ?), (?, ?, ?, ?)",
"first",
@@ -911,7 +1342,7 @@ func TestIdentityNormalizationGooseUpgrade(t *testing.T) {
initializedSQL, err := initialized.DB.DB()
require.NoError(t, err)
require.NoError(t, initializedSQL.Close())
- require.NoError(t, migrateDatabaseInternal(ctx, db, dbProviderSQLite, MigrationOptions{}))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderSQLite, MigrationOptions{}, latestMigrationVersion))
var id, username, password string
var email, displayName stdsql.NullString
require.NoError(t, db.QueryRow("SELECT id, username, email, display_name, password_hash FROM users WHERE id = 'first'").Scan(&id, &username, &email, &displayName, &password))
@@ -922,8 +1353,10 @@ func TestIdentityNormalizationGooseUpgrade(t *testing.T) {
require.Equal(t, "unchanged", password)
var version int64
require.NoError(t, db.QueryRow("SELECT MAX(version_id) FROM goose_db_version WHERE is_applied = 1").Scan(&version))
- highest, err := getHighestEmbeddedMigrationVersionInternal("sqlite")
+ highestVersions, err := getEmbeddedMigrationVersions("sqlite")
require.NoError(t, err)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
require.Equal(t, highest, version)
}
@@ -952,7 +1385,7 @@ func TestIdentityNormalizationPostgresUpgrade(t *testing.T) {
db, err := stdsql.Open("pgx", parsed.String())
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, db.Close()) })
- require.NoError(t, migrateDatabaseToVersionInternal(ctx, db, dbProviderPostgres, MigrationOptions{}, 80))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderPostgres, MigrationOptions{}, 80))
const whitespace = "\t\n\v\f\r \u0085\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u2028\u2029\u202f\u205f\u3000"
_, err = db.ExecContext(
ctx,
@@ -970,8 +1403,8 @@ func TestIdentityNormalizationPostgresUpgrade(t *testing.T) {
"unchanged",
)
require.NoError(t, err)
- require.NoError(t, migrateDatabaseInternal(ctx, db, dbProviderPostgres, MigrationOptions{}))
- require.NoError(t, migrateDatabaseInternal(ctx, db, dbProviderPostgres, MigrationOptions{}))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderPostgres, MigrationOptions{}, latestMigrationVersion))
+ require.NoError(t, migrateDatabase(ctx, db, dbProviderPostgres, MigrationOptions{}, latestMigrationVersion))
for _, expected := range []struct {
id, username string
email, displayName stdsql.NullString
@@ -1003,18 +1436,38 @@ func TestIdentityNormalizationPostgresUpgrade(t *testing.T) {
}
var version int64
require.NoError(t, db.QueryRowContext(ctx, "SELECT MAX(version_id) FROM goose_db_version WHERE is_applied").Scan(&version))
- highest, err := getHighestEmbeddedMigrationVersionInternal("postgres")
+ highestVersions, err := getEmbeddedMigrationVersions("postgres")
require.NoError(t, err)
+ require.NotEmpty(t, highestVersions)
+ highest := highestVersions[len(highestVersions)-1]
require.Equal(t, highest, version)
}
func TestSQLiteDSN(t *testing.T) {
- original := "file:arcane.db?_fk=0&_pragma=foreign_keys%3D1&_pragma=synchronous(NORMAL)&_txlock=immediate"
- dsn, err := ParseSQLiteConnectionString(original)
- require.NoError(t, err)
- parsed, err := url.Parse(dsn)
- require.NoError(t, err)
- require.Equal(t, "immediate", parsed.Query().Get("_txlock"))
- require.ElementsMatch(t, []string{"foreign_keys(0)", "foreign_keys=1", "synchronous(NORMAL)"}, parsed.Query()["_pragma"])
- require.Empty(t, parsed.Query().Get("_fk"))
+ for name, foreignKeys := range map[string]string{
+ "missing": "",
+ "short alias": "&_fk=0",
+ "long alias": "&_foreign_keys=off",
+ "function syntax": "&_pragma=foreign_keys(0)",
+ "equals syntax": "&_pragma=foreign_keys%3DOFF",
+ "case and spaces": "&_pragma=%20FoReIgN_KeYs%20%3D%200",
+ "qualified pragma": "&_pragma=main.foreign_keys(0)",
+ "quoted pragma": "&_pragma=%22foreign_keys%22%3D0",
+ "duplicates": "&_pragma=foreign_keys(1)&_pragma=foreign_keys(1)",
+ "conflicting": "&_fk=0&_foreign_keys=1&_pragma=foreign_keys%3D1&_pragma=foreign_keys(0)",
+ } {
+ t.Run(name, func(t *testing.T) {
+ original := "file:arcane.db?_pragma=synchronous(NORMAL)&_busy_timeout=2500&_txlock=immediate&cache=shared" + foreignKeys
+ dsn, err := ParseSQLiteConnectionString(original)
+ require.NoError(t, err)
+ parsed, err := url.Parse(dsn)
+ require.NoError(t, err)
+ require.Equal(t, "immediate", parsed.Query().Get("_txlock"))
+ require.Equal(t, "shared", parsed.Query().Get("cache"))
+ require.ElementsMatch(t, []string{"foreign_keys(1)", "synchronous(NORMAL)", "busy_timeout(2500)"}, parsed.Query()["_pragma"])
+ require.Empty(t, parsed.Query().Get("_fk"))
+ require.Empty(t, parsed.Query().Get("_foreign_keys"))
+ require.Empty(t, parsed.Query().Get("_busy_timeout"))
+ })
+ }
}
diff --git a/backend/resources/migrations/postgres/092_remove_orphaned_authorization_records.sql b/backend/resources/migrations/postgres/092_remove_orphaned_authorization_records.sql
new file mode 100644
index 000000000..d01f17a92
--- /dev/null
+++ b/backend/resources/migrations/postgres/092_remove_orphaned_authorization_records.sql
@@ -0,0 +1,30 @@
+-- +goose Up
+DELETE FROM user_role_assignments
+WHERE NOT EXISTS (SELECT 1 FROM users WHERE users.id = user_role_assignments.user_id)
+ OR NOT EXISTS (SELECT 1 FROM roles WHERE roles.id = user_role_assignments.role_id)
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = user_role_assignments.environment_id
+ ));
+
+DELETE FROM api_key_permissions
+WHERE NOT EXISTS (SELECT 1 FROM api_keys WHERE api_keys.id = api_key_permissions.api_key_id)
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = api_key_permissions.environment_id
+ ));
+
+DELETE FROM oidc_role_mappings
+WHERE NOT EXISTS (SELECT 1 FROM roles WHERE roles.id = oidc_role_mappings.role_id)
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = oidc_role_mappings.environment_id
+ ));
+
+DELETE FROM api_keys
+WHERE (user_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM users WHERE users.id = api_keys.user_id
+ ))
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = api_keys.environment_id
+ ));
+
+-- +goose Down
+-- Deleted orphaned records cannot be reconstructed; restore them from a pre-upgrade backup.
diff --git a/backend/resources/migrations/sqlite/003_remove_events_environment_fk.sql b/backend/resources/migrations/sqlite/003_remove_events_environment_fk.sql
index 38d7e9305..2da438e21 100644
--- a/backend/resources/migrations/sqlite/003_remove_events_environment_fk.sql
+++ b/backend/resources/migrations/sqlite/003_remove_events_environment_fk.sql
@@ -4,6 +4,7 @@
-- We need to recreate the table without the constraint
PRAGMA foreign_keys=off;
+BEGIN;
CREATE TABLE events_new (
id TEXT PRIMARY KEY,
@@ -36,6 +37,7 @@ CREATE INDEX IF NOT EXISTS idx_events_user_id ON events(user_id);
CREATE INDEX IF NOT EXISTS idx_events_environment_id ON events(environment_id);
CREATE INDEX IF NOT EXISTS idx_events_timestamp ON events(timestamp);
+COMMIT;
PRAGMA foreign_keys=on;
-- +goose Down
diff --git a/backend/resources/migrations/sqlite/011_add_environment_name.sql b/backend/resources/migrations/sqlite/011_add_environment_name.sql
index e719b6895..125a14499 100644
--- a/backend/resources/migrations/sqlite/011_add_environment_name.sql
+++ b/backend/resources/migrations/sqlite/011_add_environment_name.sql
@@ -3,5 +3,5 @@ ALTER TABLE environments ADD COLUMN name TEXT DEFAULT '';
CREATE INDEX IF NOT EXISTS idx_environments_name ON environments(name);
-- +goose Down
-ALTER TABLE environments DROP COLUMN name;
DROP INDEX IF EXISTS idx_environments_name;
+ALTER TABLE environments DROP COLUMN name;
diff --git a/backend/resources/migrations/sqlite/016_drop_image_table.sql b/backend/resources/migrations/sqlite/016_drop_image_table.sql
index ee4747d1e..4ba38c90e 100644
--- a/backend/resources/migrations/sqlite/016_drop_image_table.sql
+++ b/backend/resources/migrations/sqlite/016_drop_image_table.sql
@@ -2,7 +2,7 @@
DROP TABLE IF EXISTS images_table;
-- +goose Down
-CREATE TABLE IF NOT EXISTS images_table (
+CREATE TABLE IF NOT EXISTS images (
id TEXT PRIMARY KEY,
repo_tags TEXT,
repo_digests TEXT,
diff --git a/backend/resources/migrations/sqlite/025_add_environment_api_key_pairing.sql b/backend/resources/migrations/sqlite/025_add_environment_api_key_pairing.sql
index f77f826ff..235b32e8c 100644
--- a/backend/resources/migrations/sqlite/025_add_environment_api_key_pairing.sql
+++ b/backend/resources/migrations/sqlite/025_add_environment_api_key_pairing.sql
@@ -6,8 +6,29 @@ ALTER TABLE environments ADD COLUMN api_key_id TEXT REFERENCES api_keys(id) ON D
ALTER TABLE api_keys ADD COLUMN environment_id TEXT REFERENCES environments(id) ON DELETE CASCADE;
-- +goose Down
--- Remove environment_id column from api_keys table
-ALTER TABLE api_keys DROP COLUMN environment_id;
-
--- Remove api_key_id column from environments table
+-- Rebuilding removes the table-level environment FK introduced by later migrations.
ALTER TABLE environments DROP COLUMN api_key_id;
+
+CREATE TABLE api_keys_new (
+ id TEXT PRIMARY KEY,
+ name TEXT NOT NULL,
+ description TEXT,
+ key_hash TEXT NOT NULL,
+ key_prefix TEXT NOT NULL,
+ user_id TEXT NOT NULL,
+ expires_at DATETIME,
+ last_used_at DATETIME,
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME,
+ FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
+);
+
+INSERT INTO api_keys_new (id, name, description, key_hash, key_prefix, user_id, expires_at, last_used_at, created_at, updated_at)
+SELECT id, name, description, key_hash, key_prefix, user_id, expires_at, last_used_at, created_at, updated_at FROM api_keys;
+
+DROP TABLE api_keys;
+ALTER TABLE api_keys_new RENAME TO api_keys;
+
+CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
+CREATE INDEX IF NOT EXISTS idx_api_keys_key_hash ON api_keys(key_hash);
+CREATE INDEX IF NOT EXISTS idx_api_keys_key_prefix ON api_keys(key_prefix);
diff --git a/backend/resources/migrations/sqlite/026_add_notification_sent_to_image_updates.sql b/backend/resources/migrations/sqlite/026_add_notification_sent_to_image_updates.sql
index a9868a22f..1b36f8373 100644
--- a/backend/resources/migrations/sqlite/026_add_notification_sent_to_image_updates.sql
+++ b/backend/resources/migrations/sqlite/026_add_notification_sent_to_image_updates.sql
@@ -4,19 +4,4 @@
ALTER TABLE image_updates ADD COLUMN notification_sent INTEGER DEFAULT 0;
-- +goose Down
--- SQLite doesn't support DROP COLUMN directly, so we need to recreate the table
--- Create backup without notification_sent column
-CREATE TABLE image_updates_backup AS SELECT
- id, repository, tag, has_update, update_type, current_version,
- latest_version, current_digest, latest_digest, check_time,
- response_time_ms, last_error, auth_method, auth_username,
- auth_registry, used_credential, created_at, updated_at
-FROM image_updates;
-
-DROP TABLE image_updates;
-
-ALTER TABLE image_updates_backup RENAME TO image_updates;
-
--- Recreate indexes that were lost during table recreation
-CREATE INDEX IF NOT EXISTS idx_image_update_repository ON image_updates(repository);
-CREATE INDEX IF NOT EXISTS idx_image_update_tag ON image_updates(tag);
+ALTER TABLE image_updates DROP COLUMN notification_sent;
diff --git a/backend/resources/migrations/sqlite/028_add_gitops.sql b/backend/resources/migrations/sqlite/028_add_gitops.sql
index cd1c748ee..b1ef6c788 100644
--- a/backend/resources/migrations/sqlite/028_add_gitops.sql
+++ b/backend/resources/migrations/sqlite/028_add_gitops.sql
@@ -51,9 +51,7 @@ ALTER TABLE projects ADD COLUMN gitops_managed_by TEXT;
CREATE INDEX IF NOT EXISTS idx_projects_gitops_managed_by ON projects(gitops_managed_by);
-- +goose Down
--- Remove gitops_managed_by column from projects table
--- Note: SQLite doesn't support DROP COLUMN directly, so we'll leave it
--- or need to recreate the table if this is critical
-
DROP TABLE IF EXISTS gitops_syncs;
DROP TABLE IF EXISTS git_repositories;
+DROP INDEX IF EXISTS idx_projects_gitops_managed_by;
+ALTER TABLE projects DROP COLUMN gitops_managed_by;
diff --git a/backend/resources/migrations/sqlite/030_remove_gitops_sync_enabled.sql b/backend/resources/migrations/sqlite/030_remove_gitops_sync_enabled.sql
index 4f0f4afb6..5f0202f7d 100644
--- a/backend/resources/migrations/sqlite/030_remove_gitops_sync_enabled.sql
+++ b/backend/resources/migrations/sqlite/030_remove_gitops_sync_enabled.sql
@@ -1,8 +1,13 @@
+-- +goose NO TRANSACTION
-- +goose Up
-- Remove the enabled column from gitops_syncs table
-- SQLite doesn't support DROP COLUMN directly, so we need to recreate the table
-- Only autoSync field will control automatic syncing behavior
+-- Preserve legacy syncs with dangling references during the atomic rebuild.
+PRAGMA foreign_keys=OFF;
+BEGIN;
+
-- Create new table without enabled column
CREATE TABLE gitops_syncs_new (
id TEXT PRIMARY KEY,
@@ -19,8 +24,8 @@ CREATE TABLE gitops_syncs_new (
last_sync_status TEXT,
last_sync_error TEXT,
last_sync_commit TEXT,
- created_at DATETIME NOT NULL,
- updated_at DATETIME NOT NULL,
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME,
FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE,
FOREIGN KEY (repository_id) REFERENCES git_repositories(id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
@@ -49,10 +54,16 @@ CREATE INDEX IF NOT EXISTS idx_gitops_syncs_environment_id ON gitops_syncs(envir
CREATE INDEX IF NOT EXISTS idx_gitops_syncs_repository_id ON gitops_syncs(repository_id);
CREATE INDEX IF NOT EXISTS idx_gitops_syncs_project_id ON gitops_syncs(project_id);
+COMMIT;
+PRAGMA foreign_keys=ON;
+
-- +goose Down
-- Re-add the enabled column to gitops_syncs table
-- SQLite doesn't support ADD COLUMN with constraints directly, so we need to recreate the table
+PRAGMA foreign_keys=OFF;
+BEGIN;
+
-- Create new table with enabled column
CREATE TABLE gitops_syncs_new (
id TEXT PRIMARY KEY,
@@ -70,8 +81,8 @@ CREATE TABLE gitops_syncs_new (
last_sync_error TEXT,
last_sync_commit TEXT,
enabled BOOLEAN NOT NULL DEFAULT true,
- created_at DATETIME NOT NULL,
- updated_at DATETIME NOT NULL,
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME,
FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE,
FOREIGN KEY (repository_id) REFERENCES git_repositories(id) ON DELETE CASCADE,
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
@@ -100,3 +111,6 @@ CREATE INDEX IF NOT EXISTS idx_gitops_syncs_environment_id ON gitops_syncs(envir
CREATE INDEX IF NOT EXISTS idx_gitops_syncs_repository_id ON gitops_syncs(repository_id);
CREATE INDEX IF NOT EXISTS idx_gitops_syncs_project_id ON gitops_syncs(project_id);
CREATE INDEX IF NOT EXISTS idx_gitops_syncs_enabled ON gitops_syncs(enabled);
+
+COMMIT;
+PRAGMA foreign_keys=ON;
diff --git a/backend/resources/migrations/sqlite/040_add_api_key_managed_by.sql b/backend/resources/migrations/sqlite/040_add_api_key_managed_by.sql
index af54248b8..f5a0fd8ad 100644
--- a/backend/resources/migrations/sqlite/040_add_api_key_managed_by.sql
+++ b/backend/resources/migrations/sqlite/040_add_api_key_managed_by.sql
@@ -1,6 +1,12 @@
+-- +goose NO TRANSACTION
-- +goose Up
-- Use create-copy-drop-rename for idempotent column additions in SQLite
-- (SQLite lacks ADD COLUMN IF NOT EXISTS).
+-- Preserve environment pairings while rebuilding the referenced table.
+PRAGMA foreign_keys=OFF;
+
+BEGIN;
+
DROP TABLE IF EXISTS api_keys_new;
CREATE TABLE api_keys_new (
@@ -54,7 +60,15 @@ CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
CREATE INDEX IF NOT EXISTS idx_api_keys_key_hash ON api_keys(key_hash);
CREATE INDEX IF NOT EXISTS idx_api_keys_key_prefix ON api_keys(key_prefix);
+COMMIT;
+
+PRAGMA foreign_keys=ON;
+
-- +goose Down
+PRAGMA foreign_keys=OFF;
+
+BEGIN;
+
DROP TABLE IF EXISTS api_keys_old;
CREATE TABLE api_keys_old (
@@ -106,3 +120,7 @@ ALTER TABLE api_keys_old RENAME TO api_keys;
CREATE INDEX IF NOT EXISTS idx_api_keys_user_id ON api_keys(user_id);
CREATE INDEX IF NOT EXISTS idx_api_keys_key_hash ON api_keys(key_hash);
CREATE INDEX IF NOT EXISTS idx_api_keys_key_prefix ON api_keys(key_prefix);
+
+COMMIT;
+
+PRAGMA foreign_keys=ON;
diff --git a/backend/resources/migrations/sqlite/042_add_directory_sync.sql b/backend/resources/migrations/sqlite/042_add_directory_sync.sql
index 45e0d1a0d..02163b144 100644
--- a/backend/resources/migrations/sqlite/042_add_directory_sync.sql
+++ b/backend/resources/migrations/sqlite/042_add_directory_sync.sql
@@ -15,58 +15,9 @@ ALTER TABLE gitops_syncs ADD COLUMN max_sync_binary_size INTEGER NOT NULL DEFAUL
CREATE INDEX IF NOT EXISTS idx_gitops_syncs_sync_directory ON gitops_syncs(sync_directory);
-- +goose Down
--- Remove directory sync columns from gitops_syncs table
--- SQLite doesn't support DROP COLUMN in older versions, so we recreate the table
-
DROP INDEX IF EXISTS idx_gitops_syncs_sync_directory;
-
-CREATE TABLE gitops_syncs_backup AS SELECT
- id,
- name,
- environment_id,
- repository_id,
- branch,
- compose_path,
- project_name,
- project_id,
- auto_sync,
- sync_interval,
- last_sync_at,
- last_sync_status,
- last_sync_error,
- last_sync_commit,
- created_at,
- updated_at
-FROM gitops_syncs;
-
-DROP TABLE gitops_syncs;
-
-CREATE TABLE gitops_syncs (
- id TEXT PRIMARY KEY,
- name TEXT NOT NULL,
- environment_id TEXT NOT NULL,
- repository_id TEXT NOT NULL,
- branch TEXT NOT NULL,
- compose_path TEXT NOT NULL,
- project_name TEXT NOT NULL,
- project_id TEXT,
- auto_sync INTEGER NOT NULL DEFAULT 0,
- sync_interval INTEGER NOT NULL DEFAULT 60,
- last_sync_at DATETIME,
- last_sync_status TEXT,
- last_sync_error TEXT,
- last_sync_commit TEXT,
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- updated_at DATETIME,
- FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE,
- FOREIGN KEY (repository_id) REFERENCES git_repositories(id) ON DELETE CASCADE,
- FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
-);
-
-INSERT INTO gitops_syncs SELECT * FROM gitops_syncs_backup;
-DROP TABLE gitops_syncs_backup;
-
-CREATE INDEX IF NOT EXISTS idx_gitops_syncs_environment_id ON gitops_syncs(environment_id);
-CREATE INDEX IF NOT EXISTS idx_gitops_syncs_repository_id ON gitops_syncs(repository_id);
-CREATE INDEX IF NOT EXISTS idx_gitops_syncs_project_id ON gitops_syncs(project_id);
-CREATE INDEX IF NOT EXISTS idx_gitops_syncs_auto_sync ON gitops_syncs(auto_sync);
+ALTER TABLE gitops_syncs DROP COLUMN max_sync_binary_size;
+ALTER TABLE gitops_syncs DROP COLUMN max_sync_total_size;
+ALTER TABLE gitops_syncs DROP COLUMN max_sync_files;
+ALTER TABLE gitops_syncs DROP COLUMN synced_files;
+ALTER TABLE gitops_syncs DROP COLUMN sync_directory;
diff --git a/backend/resources/migrations/sqlite/044_allow_nested_project_paths.sql b/backend/resources/migrations/sqlite/044_allow_nested_project_paths.sql
index 6d52d3b38..6b086413b 100644
--- a/backend/resources/migrations/sqlite/044_allow_nested_project_paths.sql
+++ b/backend/resources/migrations/sqlite/044_allow_nested_project_paths.sql
@@ -2,9 +2,9 @@
-- +goose Up
PRAGMA foreign_keys=OFF;
-ALTER TABLE projects RENAME TO projects_old;
+BEGIN;
-CREATE TABLE projects (
+CREATE TABLE projects_new (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
dir_name TEXT,
@@ -18,7 +18,7 @@ CREATE TABLE projects (
gitops_managed_by TEXT
);
-INSERT INTO projects (
+INSERT INTO projects_new (
id,
name,
dir_name,
@@ -43,9 +43,10 @@ SELECT
updated_at,
status_reason,
gitops_managed_by
-FROM projects_old;
+FROM projects;
-DROP TABLE projects_old;
+DROP TABLE projects;
+ALTER TABLE projects_new RENAME TO projects;
CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
CREATE INDEX IF NOT EXISTS idx_projects_name ON projects(name);
@@ -53,6 +54,7 @@ CREATE INDEX IF NOT EXISTS idx_projects_gitops_managed_by ON projects(gitops_man
CREATE INDEX IF NOT EXISTS idx_projects_dir_name_not_null ON projects(dir_name) WHERE dir_name IS NOT NULL;
CREATE UNIQUE INDEX IF NOT EXISTS idx_projects_path_unique ON projects(path);
+COMMIT;
PRAGMA foreign_keys=ON;
-- +goose Down
@@ -62,9 +64,9 @@ PRAGMA foreign_keys=OFF;
-- If nested projects introduced duplicate leaf directory names after the up migration,
-- recreating UNIQUE(dir_name) below will fail and the rollback must be handled manually.
-ALTER TABLE projects RENAME TO projects_old;
+BEGIN;
-CREATE TABLE projects (
+CREATE TABLE projects_new (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
dir_name TEXT UNIQUE,
@@ -78,7 +80,7 @@ CREATE TABLE projects (
gitops_managed_by TEXT
);
-INSERT INTO projects (
+INSERT INTO projects_new (
id,
name,
dir_name,
@@ -103,9 +105,10 @@ SELECT
updated_at,
status_reason,
gitops_managed_by
-FROM projects_old;
+FROM projects;
-DROP TABLE projects_old;
+DROP TABLE projects;
+ALTER TABLE projects_new RENAME TO projects;
CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
CREATE INDEX IF NOT EXISTS idx_projects_name ON projects(name);
@@ -113,4 +116,5 @@ CREATE INDEX IF NOT EXISTS idx_projects_gitops_managed_by ON projects(gitops_man
CREATE INDEX IF NOT EXISTS idx_projects_path ON projects(path);
CREATE INDEX IF NOT EXISTS idx_projects_dir_name_not_null ON projects(dir_name) WHERE dir_name IS NOT NULL;
+COMMIT;
PRAGMA foreign_keys=ON;
diff --git a/backend/resources/migrations/sqlite/046_nullable_api_key_user_id.sql b/backend/resources/migrations/sqlite/046_nullable_api_key_user_id.sql
index dccec3d60..27e9d96ab 100644
--- a/backend/resources/migrations/sqlite/046_nullable_api_key_user_id.sql
+++ b/backend/resources/migrations/sqlite/046_nullable_api_key_user_id.sql
@@ -3,6 +3,7 @@
-- Environment bootstrap keys are owned by the system, not a user.
-- Allow user_id to be NULL so agent-side key creation doesn't violate the FK constraint.
PRAGMA foreign_keys=OFF;
+BEGIN;
DROP TABLE IF EXISTS api_keys_new;
@@ -60,11 +61,13 @@ CREATE INDEX IF NOT EXISTS idx_api_keys_key_prefix ON api_keys(key_prefix);
CREATE INDEX IF NOT EXISTS idx_api_keys_expires_at_not_null ON api_keys(expires_at) WHERE expires_at IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_api_keys_user_managed_by_created_at ON api_keys(user_id, managed_by, created_at, id);
+COMMIT;
PRAGMA foreign_keys=ON;
-- +goose Down
-- Remove environment bootstrap keys (user_id IS NULL) before restoring the NOT NULL constraint.
PRAGMA foreign_keys=OFF;
+BEGIN;
DROP TABLE IF EXISTS api_keys_old;
@@ -123,4 +126,5 @@ CREATE INDEX IF NOT EXISTS idx_api_keys_key_prefix ON api_keys(key_prefix);
CREATE INDEX IF NOT EXISTS idx_api_keys_expires_at_not_null ON api_keys(expires_at) WHERE expires_at IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_api_keys_user_managed_by_created_at ON api_keys(user_id, managed_by, created_at, id);
+COMMIT;
PRAGMA foreign_keys=ON;
diff --git a/backend/resources/migrations/sqlite/048_add_compose_project_name.sql b/backend/resources/migrations/sqlite/048_add_compose_project_name.sql
index cefe74696..4be72fba6 100644
--- a/backend/resources/migrations/sqlite/048_add_compose_project_name.sql
+++ b/backend/resources/migrations/sqlite/048_add_compose_project_name.sql
@@ -5,9 +5,9 @@ ALTER TABLE projects ADD COLUMN compose_project_name TEXT;
-- +goose Down
PRAGMA foreign_keys=OFF;
-ALTER TABLE projects RENAME TO projects_old;
+BEGIN;
-CREATE TABLE projects (
+CREATE TABLE projects_new (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
dir_name TEXT,
@@ -21,16 +21,17 @@ CREATE TABLE projects (
gitops_managed_by TEXT
);
-INSERT INTO projects (
+INSERT INTO projects_new (
id, name, dir_name, path, status, service_count, running_count,
created_at, updated_at, status_reason, gitops_managed_by
)
SELECT
id, name, dir_name, path, status, service_count, running_count,
created_at, updated_at, status_reason, gitops_managed_by
-FROM projects_old;
+FROM projects;
-DROP TABLE projects_old;
+DROP TABLE projects;
+ALTER TABLE projects_new RENAME TO projects;
CREATE INDEX IF NOT EXISTS idx_projects_status ON projects(status);
CREATE INDEX IF NOT EXISTS idx_projects_name ON projects(name);
@@ -38,4 +39,5 @@ CREATE INDEX IF NOT EXISTS idx_projects_gitops_managed_by ON projects(gitops_man
CREATE INDEX IF NOT EXISTS idx_projects_dir_name_not_null ON projects(dir_name) WHERE dir_name IS NOT NULL;
CREATE UNIQUE INDEX IF NOT EXISTS idx_projects_path_unique ON projects(path);
+COMMIT;
PRAGMA foreign_keys=ON;
diff --git a/backend/resources/migrations/sqlite/092_remove_orphaned_authorization_records.sql b/backend/resources/migrations/sqlite/092_remove_orphaned_authorization_records.sql
new file mode 100644
index 000000000..9fc5ac496
--- /dev/null
+++ b/backend/resources/migrations/sqlite/092_remove_orphaned_authorization_records.sql
@@ -0,0 +1,204 @@
+-- +goose NO TRANSACTION
+-- +goose Up
+-- Rebuild stale project references left by historical project-table renames.
+PRAGMA foreign_keys=OFF;
+BEGIN;
+
+CREATE TABLE gitops_syncs_new (
+ id TEXT PRIMARY KEY,
+ name TEXT NOT NULL,
+ environment_id TEXT NOT NULL,
+ repository_id TEXT NOT NULL,
+ branch TEXT NOT NULL,
+ compose_path TEXT NOT NULL,
+ project_name TEXT NOT NULL,
+ project_id TEXT,
+ auto_sync BOOLEAN NOT NULL DEFAULT false,
+ sync_interval INTEGER NOT NULL DEFAULT 60,
+ last_sync_at DATETIME,
+ last_sync_status TEXT,
+ last_sync_error TEXT,
+ last_sync_commit TEXT,
+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ updated_at DATETIME,
+ sync_directory INTEGER NOT NULL DEFAULT 1,
+ synced_files TEXT,
+ max_sync_files INTEGER NOT NULL DEFAULT 500,
+ max_sync_total_size INTEGER NOT NULL DEFAULT 52428800,
+ max_sync_binary_size INTEGER NOT NULL DEFAULT 10485760,
+ target_type TEXT NOT NULL DEFAULT 'project',
+ pre_deploy_script_path TEXT,
+ pre_deploy_runner_image TEXT,
+ pre_deploy_env TEXT,
+ pre_deploy_extra_mounts TEXT,
+ pre_deploy_timeout_sec INTEGER NOT NULL DEFAULT 60,
+ pre_deploy_network_mode TEXT NOT NULL DEFAULT 'none',
+ pre_deploy_last_run_at DATETIME,
+ pre_deploy_last_run_status TEXT,
+ pre_deploy_last_run_output TEXT,
+ pull_image_after_sync BOOLEAN NOT NULL DEFAULT false,
+ redeploy_after_sync BOOLEAN NOT NULL DEFAULT false,
+ mode TEXT NOT NULL DEFAULT 'deploy',
+ backup_directory TEXT NOT NULL DEFAULT '',
+ backup_paths TEXT,
+ backup_on_save BOOLEAN NOT NULL DEFAULT true,
+ backup_pending BOOLEAN NOT NULL DEFAULT false,
+ backup_pending_since DATETIME,
+ backup_conflict BOOLEAN NOT NULL DEFAULT false,
+ backup_failure_reason TEXT,
+ last_backup_at DATETIME,
+ last_backup_snapshot TEXT,
+ FOREIGN KEY (environment_id) REFERENCES environments(id) ON DELETE CASCADE,
+ FOREIGN KEY (repository_id) REFERENCES git_repositories(id) ON DELETE CASCADE,
+ FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE SET NULL
+);
+
+INSERT INTO gitops_syncs_new (
+ id,
+ name,
+ environment_id,
+ repository_id,
+ branch,
+ compose_path,
+ project_name,
+ project_id,
+ auto_sync,
+ sync_interval,
+ last_sync_at,
+ last_sync_status,
+ last_sync_error,
+ last_sync_commit,
+ created_at,
+ updated_at,
+ sync_directory,
+ synced_files,
+ max_sync_files,
+ max_sync_total_size,
+ max_sync_binary_size,
+ target_type,
+ pre_deploy_script_path,
+ pre_deploy_runner_image,
+ pre_deploy_env,
+ pre_deploy_extra_mounts,
+ pre_deploy_timeout_sec,
+ pre_deploy_network_mode,
+ pre_deploy_last_run_at,
+ pre_deploy_last_run_status,
+ pre_deploy_last_run_output,
+ pull_image_after_sync,
+ redeploy_after_sync,
+ mode,
+ backup_directory,
+ backup_paths,
+ backup_on_save,
+ backup_pending,
+ backup_pending_since,
+ backup_conflict,
+ backup_failure_reason,
+ last_backup_at,
+ last_backup_snapshot
+)
+SELECT
+ id,
+ name,
+ environment_id,
+ repository_id,
+ branch,
+ compose_path,
+ project_name,
+ project_id,
+ auto_sync,
+ sync_interval,
+ last_sync_at,
+ last_sync_status,
+ last_sync_error,
+ last_sync_commit,
+ created_at,
+ updated_at,
+ sync_directory,
+ synced_files,
+ max_sync_files,
+ max_sync_total_size,
+ max_sync_binary_size,
+ target_type,
+ pre_deploy_script_path,
+ pre_deploy_runner_image,
+ pre_deploy_env,
+ pre_deploy_extra_mounts,
+ pre_deploy_timeout_sec,
+ pre_deploy_network_mode,
+ pre_deploy_last_run_at,
+ pre_deploy_last_run_status,
+ pre_deploy_last_run_output,
+ pull_image_after_sync,
+ redeploy_after_sync,
+ mode,
+ backup_directory,
+ backup_paths,
+ backup_on_save,
+ backup_pending,
+ backup_pending_since,
+ backup_conflict,
+ backup_failure_reason,
+ last_backup_at,
+ last_backup_snapshot
+FROM gitops_syncs;
+
+DROP TABLE gitops_syncs;
+ALTER TABLE gitops_syncs_new RENAME TO gitops_syncs;
+
+CREATE INDEX idx_gitops_syncs_auto_sync_true
+ON gitops_syncs(id, environment_id, sync_interval, last_sync_at)
+WHERE auto_sync = 1;
+CREATE UNIQUE INDEX idx_gitops_syncs_backup_project ON gitops_syncs(project_id) WHERE mode = 'backup';
+CREATE INDEX idx_gitops_syncs_environment_auto_sync
+ON gitops_syncs(environment_id, auto_sync);
+CREATE INDEX idx_gitops_syncs_environment_id ON gitops_syncs(environment_id);
+CREATE INDEX idx_gitops_syncs_environment_last_sync_status
+ON gitops_syncs(environment_id, last_sync_status);
+CREATE INDEX idx_gitops_syncs_environment_project_id
+ON gitops_syncs(environment_id, project_id);
+CREATE INDEX idx_gitops_syncs_environment_repository_id
+ON gitops_syncs(environment_id, repository_id);
+CREATE INDEX idx_gitops_syncs_project_id ON gitops_syncs(project_id);
+CREATE INDEX idx_gitops_syncs_repository_id ON gitops_syncs(repository_id);
+CREATE INDEX idx_gitops_syncs_sync_directory ON gitops_syncs(sync_directory);
+
+CREATE INDEX idx_gitops_syncs_auto_sync ON gitops_syncs(auto_sync);
+CREATE INDEX idx_gitops_syncs_last_sync_commit ON gitops_syncs(last_sync_commit);
+
+COMMIT;
+PRAGMA foreign_keys=ON;
+
+-- Keep cascades enabled while repairing authorization records.
+BEGIN;
+DELETE FROM user_role_assignments
+WHERE NOT EXISTS (SELECT 1 FROM users WHERE users.id = user_role_assignments.user_id)
+ OR NOT EXISTS (SELECT 1 FROM roles WHERE roles.id = user_role_assignments.role_id)
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = user_role_assignments.environment_id
+ ));
+
+DELETE FROM api_key_permissions
+WHERE NOT EXISTS (SELECT 1 FROM api_keys WHERE api_keys.id = api_key_permissions.api_key_id)
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = api_key_permissions.environment_id
+ ));
+
+DELETE FROM oidc_role_mappings
+WHERE NOT EXISTS (SELECT 1 FROM roles WHERE roles.id = oidc_role_mappings.role_id)
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = oidc_role_mappings.environment_id
+ ));
+
+DELETE FROM api_keys
+WHERE (user_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM users WHERE users.id = api_keys.user_id
+ ))
+ OR (environment_id IS NOT NULL AND NOT EXISTS (
+ SELECT 1 FROM environments WHERE environments.id = api_keys.environment_id
+ ));
+COMMIT;
+
+-- +goose Down
+-- Keep repaired references; deleted orphaned records cannot be reconstructed.