Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: less useless columns for instance change history (#15495)
Danny Xu699c507f1ddf
diff --git a/backend/migrator/migration/LATEST.sql b/backend/migrator/migration/LATEST.sqlindex 97a663bba2..c1e1cdc049 100644--- a/backend/migrator/migration/LATEST.sql+++ b/backend/migrator/migration/LATEST.sql@@ -338,9 +338,7 @@ CREATE INDEX idx_issue_subscriber_subscriber_id ON issue_subscriber(subscriber_i-- instance change history records the changes an instance and its databases.CREATE TABLE instance_change_history (id bigserial PRIMARY KEY,- status text NOT NULL CONSTRAINT instance_change_history_status_check CHECK (status IN ('PENDING', 'DONE', 'FAILED')),- version text NOT NULL,- execution_duration_ns bigint NOT NULL+ version text NOT NULL);CREATE UNIQUE INDEX IF NOT EXISTS idx_instance_change_history_unique_version ON instance_change_history (version);diff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.goindex 0e7fb156ad..38cba11121 100644--- a/backend/migrator/migrator.go+++ b/backend/migrator/migrator.go@@ -12,7 +12,6 @@ import ("sort""strconv""strings"- "time""github.com/blang/semver/v4""github.com/pkg/errors"@@ -29,10 +28,6 @@ func MigrateSchema(ctx context.Context, db *sql.DB) (*semver.Version, error) {}defer conn.Close()- if err := backfillSchemaObjectOwner(ctx, conn); err != nil {- return nil, err- }-// Calculate prod cutoffSchemaVersion.cutoffSchemaVersion, err := getProdCutoffVersion()if err != nil {@@ -42,6 +37,10 @@ func MigrateSchema(ctx context.Context, db *sql.DB) (*semver.Version, error) {if err := initializeSchema(ctx, conn, cutoffSchemaVersion); err != nil {return nil, err}++ if err := backfillSchemaObjectOwner(ctx, conn); err != nil {+ return nil, err+ }var c interr = conn.QueryRowContext(ctx, "SELECT count(1) FROM instance_change_history WHERE database_id IS NOT NULL").Scan(&c)if err == nil && c > 0 {@@ -71,7 +70,12 @@ func MigrateSchema(ctx context.Context, db *sql.DB) (*semver.Version, error) {CREATE UNIQUE INDEX IF NOT EXISTS idx_instance_change_history_unique_version ON instance_change_history (version);`); err != nil {return nil, err}- if _, err := conn.ExecContext(ctx, `+ var hasStatus bool+ if err := conn.QueryRowContext(ctx, "SELECT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = 'instance_change_history' AND column_name = 'status')").Scan(&hasStatus); err != nil {+ return nil, err+ }+ if hasStatus {+ if _, err := conn.ExecContext(ctx, `DELETE FROM instance_change_history WHERE status = 'FAILED';UPDATE instance_change_historySET@@ -83,6 +87,13 @@ func MigrateSchema(ctx context.Context, db *sql.DB) (*semver.Version, error) {'.')WHERE version LIKE '%-%';`); err != nil {+ return nil, err+ }+ }+ if _, err := conn.ExecContext(ctx, `+ ALTER TABLE instance_change_history+ DROP COLUMN IF EXISTS status,+ DROP COLUMN IF EXISTS execution_duration_ns;`); err != nil {return nil, err}@@ -356,8 +367,6 @@ func getMinorVersions(names []string) ([]semver.Version, error) {}func executeMigration(ctx context.Context, conn *sql.Conn, statement string, version string) error {- startedNs := time.Now().UnixNano()-txn, err := conn.BeginTx(ctx, nil)if err != nil {return err@@ -367,11 +376,9 @@ func executeMigration(ctx context.Context, conn *sql.Conn, statement string, verif _, err := txn.ExecContext(ctx, statement); err != nil {return err}- durationNano := time.Now().UnixNano() - startedNsif _, err := txn.ExecContext(ctx,- `INSERT INTO instance_change_history (status, version, execution_duration_ns) VALUES ($1, $2, $3)`, "DONE",+ `INSERT INTO instance_change_history (version) VALUES ($1)`,version,- durationNano,); err != nil {return err}@@ -380,7 +387,7 @@ func executeMigration(ctx context.Context, conn *sql.Conn, statement string, ver}func getLatestMigrationVersion(ctx context.Context, conn *sql.Conn) (*semver.Version, error) {- query := `SELECT version FROM instance_change_history WHERE status = 'DONE' ORDER BY id DESC`+ query := `SELECT version FROM instance_change_history ORDER BY id DESC`var v stringif err := conn.QueryRowContext(ctx, query).Scan(&v); err != nil {