Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion acceleration/internal/store/connector_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type ConnectorDefinition struct {
Description string `bun:"description,notnull"`
Manifest core.Manifest `bun:"manifest,type:jsonb,notnull"`
// SyncHash is a fingerprint of the file the CLI last synced onto this revision
// (20270201001200_connector_router_sync_hash.sql). Empty for a built-in, and for a revision
// (20270202000100_connector_router_sync_hash.sql). Empty for a built-in, and for a revision
// that was never synced or was written since by anything that is not a sync.
SyncHash string `bun:"sync_hash,notnull"`
CreatedAt time.Time `bun:"created_at,notnull"`
Expand Down
48 changes: 48 additions & 0 deletions acceleration/internal/store/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,54 @@ func (s *StoreSuite) TestARolledBackVersionWithNoFileIsNotRefused() {
s.Require().NoError(db.Migrate(s.ctx))
}

// Staging's global version was 20270202000000 before the sync_hash columns merged, and a build refuses to start
// on a database with a migration older than its newest applied one. So the migration sorts after that version,
// and a database that ran it under its first version, 20270201001200, applies it again as a no-op.
func (s *StoreSuite) TestTheSyncHashMigrationSortsAfterAGlobalVersionStagingHad() {
db := s.syncHashDatabase("sync_hash_slot")
defer db.Close()
for _, table := range []string{"connector_definitions", "router_configs"} {
_, err := db.global.ExecContext(s.ctx, "ALTER TABLE ? ADD COLUMN sync_hash TEXT NOT NULL DEFAULT ''", bun.Ident(table))
s.Require().NoError(err)
}
_, err := db.global.ExecContext(s.ctx, "INSERT INTO ? (version_id, is_applied) VALUES (20270201001200, true)", bun.Ident(globalSet.table))
s.Require().NoError(err)

s.Require().NoError(db.Migrate(s.ctx))
s.True(s.syncHashRecorded(db), "the migration is recorded under its new version")
}

// Staging's own path: at 20270202000000 with neither column and no row of the first version, Migrate adds both.
func (s *StoreSuite) TestTheSyncHashMigrationAddsTheColumnsToAGlobalDatabaseAt20270202000000() {
db := s.syncHashDatabase("sync_hash_add")
defer db.Close()

s.Require().NoError(db.Migrate(s.ctx))
s.True(s.syncHashColumns(db), "Migrate adds sync_hash to connector_definitions and router_configs")
s.True(s.syncHashRecorded(db), "the migration is recorded under its new version")
}

// syncHashDatabase opens an empty database with the global set applied up to 20270202000000.
func (s *StoreSuite) syncHashDatabase(name string) *Store {
db, err := Open(s.emptyDatabase(name))
s.Require().NoError(err)
s.upTo(db, globalSet, 20270202000000)
s.False(s.syncHashColumns(db), "a database at 20270202000000 has not run the sync_hash migration")
return db
}

func (s *StoreSuite) syncHashColumns(db *Store) (hasColumns bool) {
s.Require().NoError(db.global.QueryRowContext(s.ctx, `SELECT count(*) = 2 FROM information_schema.columns
WHERE column_name = 'sync_hash' AND table_name IN ('connector_definitions', 'router_configs')`).Scan(&hasColumns))
return hasColumns
}

func (s *StoreSuite) syncHashRecorded(db *Store) (recorded bool) {
s.Require().NoError(db.global.QueryRowContext(s.ctx, "SELECT EXISTS (SELECT 1 FROM ? WHERE version_id = 20270202000100 AND is_applied)",
bun.Ident(globalSet.table)).Scan(&recorded))
return recorded
}

// A database a later release migrated holds versions this one has no file for, after the
// squashed history and before any of it is recorded once the cleanup has run: rolling back to
// this release starts on it.
Expand Down
2 changes: 1 addition & 1 deletion acceleration/internal/store/router_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type RouterConfig struct {
UpdatedAt time.Time `bun:"updated_at,notnull"`
DeletedAt *time.Time `bun:"deleted_at"`
// SyncHash is a fingerprint of the file the CLI last synced onto this config
// (20270201001200_connector_router_sync_hash.sql), as AgentConfig.SyncHash is for a
// (20270202000100_connector_router_sync_hash.sql), as AgentConfig.SyncHash is for a
// directory. Empty if it was never synced, or was written since by anything that is not a
// sync.
SyncHash string `bun:"sync_hash,notnull"`
Expand Down