checkForUnknownMigrations checks if the database has migrations that this binary doesn't know about, which indicates the database was created by a newer version. This produces a clear error instead of cryptic SQL failures from schema mismatches.
(ctx context.Context)
| 88 | // doesn't know about, which indicates the database was created by a newer version. |
| 89 | // This produces a clear error instead of cryptic SQL failures from schema mismatches. |
| 90 | func (m *MigrationManager) checkForUnknownMigrations(ctx context.Context) error { |
| 91 | if len(m.migrations) == 0 { |
| 92 | return nil |
| 93 | } |
| 94 | maxKnownID := m.migrations[len(m.migrations)-1].ID |
| 95 | |
| 96 | var maxAppliedID int |
| 97 | err := m.db.QueryRowContext(ctx, "SELECT COALESCE(MAX(id), 0) FROM migrations").Scan(&maxAppliedID) |
| 98 | if err != nil { |
| 99 | return fmt.Errorf("failed to check applied migrations: %w", err) |
| 100 | } |
| 101 | |
| 102 | if maxAppliedID > maxKnownID { |
| 103 | return fmt.Errorf( |
| 104 | "%w: you are running docker-agent %s which supports migrations up to %d, "+ |
| 105 | "but the session database has migration %d from a newer version; "+ |
| 106 | "please upgrade docker-agent to the latest version", |
| 107 | ErrNewerDatabase, version.Version, maxKnownID, maxAppliedID) |
| 108 | } |
| 109 | |
| 110 | return nil |
| 111 | } |
| 112 | |
| 113 | // RunPendingMigrations executes all migrations that haven't been applied yet |
| 114 | func (m *MigrationManager) RunPendingMigrations(ctx context.Context) error { |