From b49ee44a4fc6eb4c30ff77c80b361ea599b99be4 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 30 Nov 2023 22:22:56 +0000 Subject: [PATCH 1/9] feat: run all migrations in a transaction --- coderd/database/dump.sql | 2 +- .../000030_template_version_created_by.up.sql | 1 - .../migrations/000035_linked_user_id.down.sql | 4 - .../migrations/000057_api_key_token.up.sql | 23 ++- .../000144_user_status_dormant.down.sql | 2 +- .../000144_user_status_dormant.up.sql | 16 +- coderd/database/migrations/migrate.go | 13 +- coderd/database/migrations/txnmigrator.go | 184 ++++++++++++++++++ coderd/database/models.go | 2 +- coderd/database/postgres/postgres.go | 2 +- 10 files changed, 231 insertions(+), 18 deletions(-) create mode 100644 coderd/database/migrations/txnmigrator.go diff --git a/coderd/database/dump.sql b/coderd/database/dump.sql index f4200f7ea3109..9d79ae7c348da 100644 --- a/coderd/database/dump.sql +++ b/coderd/database/dump.sql @@ -153,7 +153,7 @@ CREATE TYPE user_status AS ENUM ( 'dormant' ); -COMMENT ON TYPE user_status IS 'Defines the user status: active, dormant, or suspended.'; +COMMENT ON TYPE user_status IS 'Defines the users status: active, dormant, or suspended.'; CREATE TYPE workspace_agent_lifecycle_state AS ENUM ( 'created', diff --git a/coderd/database/migrations/000030_template_version_created_by.up.sql b/coderd/database/migrations/000030_template_version_created_by.up.sql index 36cdff6f88188..41fcec4afdbbc 100644 --- a/coderd/database/migrations/000030_template_version_created_by.up.sql +++ b/coderd/database/migrations/000030_template_version_created_by.up.sql @@ -1,4 +1,3 @@ -BEGIN; ALTER TABLE ONLY template_versions ADD COLUMN IF NOT EXISTS created_by uuid REFERENCES users (id) ON DELETE RESTRICT; diff --git a/coderd/database/migrations/000035_linked_user_id.down.sql b/coderd/database/migrations/000035_linked_user_id.down.sql index 4b75aad6abd7f..b1b6066854fc1 100644 --- a/coderd/database/migrations/000035_linked_user_id.down.sql +++ b/coderd/database/migrations/000035_linked_user_id.down.sql @@ -2,8 +2,6 @@ -- the oauth_access_token, oauth_refresh_token, and oauth_expiry -- columns of api_key rows with the values from the dropped user_links -- table. -BEGIN; - DROP TABLE IF EXISTS user_links; ALTER TABLE @@ -19,5 +17,3 @@ ALTER TABLE ADD COLUMN oauth_expiry timestamp with time zone DEFAULT '0001-01-01 00:00:00+00'::timestamp with time zone NOT NULL; ALTER TABLE users DROP COLUMN login_type; - -COMMIT; diff --git a/coderd/database/migrations/000057_api_key_token.up.sql b/coderd/database/migrations/000057_api_key_token.up.sql index bb03e2f70950b..1537154819eb7 100644 --- a/coderd/database/migrations/000057_api_key_token.up.sql +++ b/coderd/database/migrations/000057_api_key_token.up.sql @@ -1 +1,22 @@ -ALTER TYPE login_type ADD VALUE IF NOT EXISTS 'token'; +-- ALTER TYPE login_type ADD VALUE IF NOT EXISTS 'token'; + +CREATE TYPE new_logintype AS ENUM ( + 'password', + 'github', + 'oidc', + 'token' +); + +ALTER TABLE users + ALTER COLUMN login_type DROP DEFAULT, + ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype), + ALTER COLUMN login_type SET DEFAULT 'password'::new_logintype; + +ALTER TABLE user_links + ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype); + +ALTER TABLE api_keys + ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype); + +DROP TYPE login_type; +ALTER TYPE new_logintype RENAME TO login_type; diff --git a/coderd/database/migrations/000144_user_status_dormant.down.sql b/coderd/database/migrations/000144_user_status_dormant.down.sql index 55504e0938064..12af9940908e7 100644 --- a/coderd/database/migrations/000144_user_status_dormant.down.sql +++ b/coderd/database/migrations/000144_user_status_dormant.down.sql @@ -1,3 +1,3 @@ -- It's not possible to drop enum values from enum types, so the UP has "IF NOT EXISTS" -UPDATE users SET status = 'active'::user_status WHERE status = 'dormant'::user_status; +UPDATE users SET status = 'active'::user_status WHERE status::text = 'dormant'; diff --git a/coderd/database/migrations/000144_user_status_dormant.up.sql b/coderd/database/migrations/000144_user_status_dormant.up.sql index 106cc10b53b62..abad786d3791c 100644 --- a/coderd/database/migrations/000144_user_status_dormant.up.sql +++ b/coderd/database/migrations/000144_user_status_dormant.up.sql @@ -1,2 +1,14 @@ -ALTER TYPE user_status ADD VALUE IF NOT EXISTS 'dormant'; -COMMENT ON TYPE user_status IS 'Defines the user status: active, dormant, or suspended.'; +CREATE TYPE new_user_status AS ENUM ( + 'active', + 'suspended', + 'dormant' +); +COMMENT ON TYPE new_user_status IS 'Defines the users status: active, dormant, or suspended.'; + +ALTER TABLE users + ALTER COLUMN status DROP DEFAULT, + ALTER COLUMN status TYPE new_user_status USING (status::text::new_user_status), + ALTER COLUMN status SET DEFAULT 'active'::new_user_status; + +DROP TYPE user_status; +ALTER TYPE new_user_status RENAME TO user_status; diff --git a/coderd/database/migrations/migrate.go b/coderd/database/migrations/migrate.go index 4103513751e72..ab07f83ffed9c 100644 --- a/coderd/database/migrations/migrate.go +++ b/coderd/database/migrations/migrate.go @@ -27,8 +27,9 @@ func setup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { // migration_cursor is a v1 migration table. If this exists, we're on v1. // Do no run v2 migrations on a v1 database! - row := db.QueryRowContext(ctx, "SELECT * FROM migration_cursor;") - if row.Err() == nil { + row := db.QueryRowContext(ctx, "SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = 'migration_cursor';") + var v1Exists int + if row.Scan(&v1Exists) == nil { return nil, nil, xerrors.Errorf("currently connected to a Coder v1 database, aborting database setup") } @@ -55,7 +56,7 @@ func setup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { // Up runs SQL migrations to ensure the database schema is up-to-date. func Up(db *sql.DB) (retErr error) { - _, m, err := setup(db) + _, m, err := betterSetup(db) if err != nil { return xerrors.Errorf("migrate setup: %w", err) } @@ -86,7 +87,7 @@ func Up(db *sql.DB) (retErr error) { // Down runs all down SQL migrations. func Down(db *sql.DB) error { - _, m, err := setup(db) + _, m, err := betterSetup(db) if err != nil { return xerrors.Errorf("migrate setup: %w", err) } @@ -108,7 +109,7 @@ func Down(db *sql.DB) error { // applied, without making any changes to the database. If not, returns a // non-nil error. func EnsureClean(db *sql.DB) error { - sourceDriver, m, err := setup(db) + sourceDriver, m, err := betterSetup(db) if err != nil { return xerrors.Errorf("migrate setup: %w", err) } @@ -174,7 +175,7 @@ func CheckLatestVersion(sourceDriver source.Driver, currentVersion uint) error { // Stepper cannot be closed pre-emptively, it must be run to completion // (or until an error is encountered). func Stepper(db *sql.DB) (next func() (version uint, more bool, err error), err error) { - _, m, err := setup(db) + _, m, err := betterSetup(db) if err != nil { return nil, xerrors.Errorf("migrate setup: %w", err) } diff --git a/coderd/database/migrations/txnmigrator.go b/coderd/database/migrations/txnmigrator.go new file mode 100644 index 0000000000000..b1e4bab0abcf4 --- /dev/null +++ b/coderd/database/migrations/txnmigrator.go @@ -0,0 +1,184 @@ +package migrations + +import ( + "bytes" + "context" + "database/sql" + "fmt" + "io" + "strings" + + "github.com/golang-migrate/migrate/v4" + "github.com/golang-migrate/migrate/v4/database" + "github.com/golang-migrate/migrate/v4/source" + "github.com/golang-migrate/migrate/v4/source/iofs" + "github.com/lib/pq" + "golang.org/x/xerrors" +) + +const ( + lockID = int64(1037453835920848937) + migrationsTableName = "schema_migrations" +) + +func betterSetup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { + ctx := context.Background() + sourceDriver, err := iofs.New(migrations, ".") + if err != nil { + return nil, nil, xerrors.Errorf("create iofs: %w", err) + } + + // migration_cursor is a v1 migration table. If this exists, we're on v1. + // Do no run v2 migrations on a v1 database! + row := db.QueryRowContext(ctx, "SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = 'migration_cursor';") + var v1Exists int + if row.Scan(&v1Exists) == nil { + return nil, nil, xerrors.New("currently connected to a Coder v1 database, aborting database setup") + } + + dbDriver := &pgTxnDriver{ctx: context.Background(), db: db} + err = dbDriver.ensureVersionTable() + if err != nil { + return nil, nil, xerrors.Errorf("ensure version table: %w", err) + } + + m, err := migrate.NewWithInstance("", sourceDriver, "", dbDriver) + if err != nil { + return nil, nil, xerrors.Errorf("new migrate instance: %w", err) + } + + return sourceDriver, m, nil +} + +type pgTxnDriver struct { + ctx context.Context + db *sql.DB + tx *sql.Tx +} + +func (d *pgTxnDriver) Open(url string) (database.Driver, error) { + panic("not implemented") +} + +func (d *pgTxnDriver) Close() error { + return nil +} + +func (d *pgTxnDriver) Lock() error { + var err error + + d.tx, err = d.db.BeginTx(d.ctx, nil) + if err != nil { + return err + } + const q = ` +SELECT pg_advisory_xact_lock($1) +` + + _, err = d.tx.ExecContext(d.ctx, q, lockID) + if err != nil { + return xerrors.Errorf("exec select: %w", err) + } + return nil +} + +func (d *pgTxnDriver) Unlock() error { + err := d.tx.Commit() + d.tx = nil + if err != nil { + return xerrors.Errorf("commit tx on unlock: %w", err) + } + return nil +} + +// Run applies a migration to the database. migration is guaranteed to be not nil. +func (d *pgTxnDriver) Run(migration io.Reader) error { + migr, err := io.ReadAll(migration) + if err != nil { + return xerrors.Errorf("read migration: %w", err) + } + migr = bytes.ReplaceAll(migr, []byte("BEGIN;"), []byte{}) + migr = bytes.ReplaceAll(migr, []byte("COMMIT;"), []byte{}) + err = d.runStatement(migr) + if err != nil { + return xerrors.Errorf("run statement: %w", err) + } + return nil +} + +func (d *pgTxnDriver) runStatement(statement []byte) error { + ctx := context.Background() + query := string(statement) + if strings.TrimSpace(query) == "" { + return nil + } + if _, err := d.tx.ExecContext(ctx, query); err != nil { + if pgErr, ok := err.(*pq.Error); ok { + var line uint + message := fmt.Sprintf("migration failed: %s", pgErr.Message) + if pgErr.Detail != "" { + message = fmt.Sprintf("%s, %s", message, pgErr.Detail) + } + return database.Error{OrigErr: err, Err: message, Query: statement, Line: line} + } + return database.Error{OrigErr: err, Err: "migration failed", Query: statement} + } + return nil +} + +//nolint:revive +func (d *pgTxnDriver) SetVersion(version int, dirty bool) error { + query := `TRUNCATE ` + migrationsTableName + if _, err := d.tx.Exec(query); err != nil { + return &database.Error{OrigErr: err, Query: []byte(query)} + } + + if version >= 0 { + query = `INSERT INTO ` + migrationsTableName + ` (version, dirty) VALUES ($1, $2)` + if _, err := d.tx.Exec(query, version, dirty); err != nil { + return &database.Error{OrigErr: err, Query: []byte(query)} + } + } + + return nil +} + +func (d *pgTxnDriver) Version() (version int, dirty bool, err error) { + var q interface { + QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row + } = d.tx + if d.tx == nil { + q = d.db + } + + query := `SELECT version, dirty FROM ` + migrationsTableName + ` LIMIT 1` + err = q.QueryRowContext(context.Background(), query).Scan(&version, &dirty) + switch { + case err == sql.ErrNoRows: + return database.NilVersion, false, nil + + case err != nil: + if e, ok := err.(*pq.Error); ok { + if e.Code.Name() == "undefined_table" { + return database.NilVersion, false, nil + } + } + return 0, false, &database.Error{OrigErr: err, Query: []byte(query)} + + default: + return version, dirty, nil + } +} + +func (d *pgTxnDriver) Drop() error { + panic("not implemented") +} + +func (d *pgTxnDriver) ensureVersionTable() error { + const query = `CREATE TABLE IF NOT EXISTS ` + migrationsTableName + ` (version bigint not null primary key, dirty boolean not null)` + if _, err := d.db.ExecContext(context.Background(), query); err != nil { + return &database.Error{OrigErr: err, Query: []byte(query)} + } + + return nil +} diff --git a/coderd/database/models.go b/coderd/database/models.go index bd7625657b7bd..c3e4fdf42fddb 100644 --- a/coderd/database/models.go +++ b/coderd/database/models.go @@ -1350,7 +1350,7 @@ func AllTailnetStatusValues() []TailnetStatus { } } -// Defines the user status: active, dormant, or suspended. +// Defines the users status: active, dormant, or suspended. type UserStatus string const ( diff --git a/coderd/database/postgres/postgres.go b/coderd/database/postgres/postgres.go index 8a7d0209ba4e0..94524cbdc35d5 100644 --- a/coderd/database/postgres/postgres.go +++ b/coderd/database/postgres/postgres.go @@ -36,7 +36,7 @@ func Open() (string, func(), error) { } dbName = "ci" + dbName - _, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM")) + _, err = db.Exec("CREATE DATABASE " + dbName) if err != nil { return "", nil, xerrors.Errorf("create db with template: %w", err) } From 31524f37847612447e6b5ce7a7be98395b50ff31 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 30 Nov 2023 22:59:13 +0000 Subject: [PATCH 2/9] fix migrations --- .../000030_template_version_created_by.up.sql | 2 -- .../migrations/000035_linked_user_id.up.sql | 4 --- .../migrations/000058_template_acl.down.sql | 4 --- .../migrations/000058_template_acl.up.sql | 4 --- .../migrations/000059_file_id.down.sql | 6 +--- .../database/migrations/000059_file_id.up.sql | 4 --- .../migrations/000062_group_avatars.down.sql | 4 --- .../migrations/000062_group_avatars.up.sql | 4 --- .../000063_resource_type_group.up.sql | 6 +--- .../migrations/000066_app_slug.up.sql | 4 --- .../000067_app_display_name.down.sql | 4 --- .../migrations/000067_app_display_name.up.sql | 4 --- ..._update_template_version_created_by.up.sql | 4 --- ...n_timeout_and_troubleshooting_url.down.sql | 4 --- ...ion_timeout_and_troubleshooting_url.up.sql | 4 --- ..._sqlc_upgrade_fix_nullable_values.down.sql | 2 -- ...90_sqlc_upgrade_fix_nullable_values.up.sql | 2 -- ...until_ready_to_login_before_ready.down.sql | 2 -- ...n_until_ready_to_login_before_ready.up.sql | 2 -- .../000096_agent_resolved_directory.down.sql | 4 --- .../000096_agent_resolved_directory.up.sql | 4 --- .../000097_license_not_null_uuid.up.sql | 4 --- .../000103_add_apikey_name.down.sql | 4 --- .../migrations/000103_add_apikey_name.up.sql | 4 --- .../migrations/000110_add_startup_logs.up.sql | 4 --- .../000114_workspace_proxy.down.sql | 3 -- .../migrations/000114_workspace_proxy.up.sql | 3 -- .../000118_workspace_proxy_token.down.sql | 4 --- .../000118_workspace_proxy_token.up.sql | 4 --- .../000119_workspace_proxy_name_idx.down.sql | 4 --- .../000119_workspace_proxy_name_idx.up.sql | 4 --- ...000120_trigger_delete_user_apikey.down.sql | 4 --- .../000120_trigger_delete_user_apikey.up.sql | 4 --- .../000122_add_template_cleanup_ttls.down.sql | 2 -- .../000122_add_template_cleanup_ttls.up.sql | 2 -- .../000123_workspace_agent_subsystem.down.sql | 2 -- .../000123_workspace_agent_subsystem.up.sql | 2 -- ...00124_validation_min_max_nullable.down.sql | 2 -- .../000124_validation_min_max_nullable.up.sql | 2 -- ..._ready_to_startup_script_behavior.down.sql | 4 --- ...re_ready_to_startup_script_behavior.up.sql | 4 --- .../000128_template_locked_ttl.down.sql | 2 -- .../000128_template_locked_ttl.up.sql | 2 -- ...artup_logs_eof_and_add_completion.down.sql | 4 --- ...startup_logs_eof_and_add_completion.up.sql | 4 --- .../migrations/000130_ha_coordinator.down.sql | 4 --- .../migrations/000130_ha_coordinator.up.sql | 4 --- .../000131_workspace_locked.down.sql | 2 -- .../migrations/000131_workspace_locked.up.sql | 2 -- .../000134_workspace_build_reason.up.sql | 2 -- .../migrations/000138_join_users.down.sql | 4 --- .../migrations/000138_join_users.up.sql | 4 --- ...0139_template_restart_requirement.down.sql | 4 --- ...000139_template_restart_requirement.up.sql | 4 --- .../000141_join_users_build_version.down.sql | 4 --- .../000141_join_users_build_version.up.sql | 4 --- .../migrations/000142_proxy_derp.down.sql | 4 --- .../migrations/000142_proxy_derp.up.sql | 4 --- .../000143_workspace_agent_logs.down.sql | 2 -- .../000143_workspace_agent_logs.up.sql | 2 -- .../migrations/000146_proxy_derp_only.up.sql | 4 --- .../000147_group_display_name.down.sql | 4 --- .../000147_group_display_name.up.sql | 4 --- .../migrations/000148_group_source.down.sql | 4 --- .../migrations/000148_group_source.up.sql | 4 --- .../000149_agent_multiple_subsystems.down.sql | 4 --- .../000149_agent_multiple_subsystems.up.sql | 4 --- .../migrations/000151_rename_locked.down.sql | 4 --- .../migrations/000151_rename_locked.up.sql | 3 -- ...name_template_restart_requirement.down.sql | 4 --- ...rename_template_restart_requirement.up.sql | 4 --- .../000153_agent_default_apps.down.sql | 5 +-- .../000153_agent_default_apps.up.sql | 2 -- .../000154_dbcrypt_key_ids.down.sql | 4 --- ...156_pg_coordinator_single_tailnet.down.sql | 4 --- ...00156_pg_coordinator_single_tailnet.up.sql | 4 --- .../000157_workspace_agent_script.down.sql | 4 --- .../000157_workspace_agent_script.up.sql | 2 -- .../migrations/000158_external_auth.down.sql | 4 --- .../migrations/000158_external_auth.up.sql | 4 --- .../000160_provisioner_job_status.down.sql | 4 --- .../000160_provisioner_job_status.up.sql | 4 --- ...00162_workspace_automatic_updates.down.sql | 2 -- .../000162_workspace_automatic_updates.up.sql | 2 -- .../000164_archive_template_versions.down.sql | 4 --- .../000164_archive_template_versions.up.sql | 4 --- .../000165_prevent_autostart_days.down.sql | 4 --- .../000165_prevent_autostart_days.up.sql | 4 --- .../000166_template_active_version.down.sql | 4 --- .../000166_template_active_version.up.sql | 4 --- ...00167_workspace_agent_api_version.down.sql | 2 -- .../000167_workspace_agent_api_version.up.sql | 2 -- .../000168_pg_coord_tailnet_v2_api.down.sql | 4 --- .../000168_pg_coord_tailnet_v2_api.up.sql | 4 --- .../000169_deprecate_template.down.sql | 4 --- .../000169_deprecate_template.up.sql | 4 --- .../000170_workspaceproxy_version.down.sql | 2 -- .../000170_workspaceproxy_version.up.sql | 2 -- .../000171_oidc_debug_claims.down.sql | 4 --- .../000171_oidc_debug_claims.up.sql | 4 --- .../database/migrations/create_migration.sh | 11 +++++++ coderd/database/migrations/migrate.go | 24 +++++--------- coderd/database/migrations/txnmigrator.go | 32 ------------------- 103 files changed, 22 insertions(+), 395 deletions(-) diff --git a/coderd/database/migrations/000030_template_version_created_by.up.sql b/coderd/database/migrations/000030_template_version_created_by.up.sql index 41fcec4afdbbc..00bd00650ca52 100644 --- a/coderd/database/migrations/000030_template_version_created_by.up.sql +++ b/coderd/database/migrations/000030_template_version_created_by.up.sql @@ -11,5 +11,3 @@ SET ) WHERE created_by IS NULL; - -COMMIT; diff --git a/coderd/database/migrations/000035_linked_user_id.up.sql b/coderd/database/migrations/000035_linked_user_id.up.sql index d86d5771165e6..aa68a8e85526a 100644 --- a/coderd/database/migrations/000035_linked_user_id.up.sql +++ b/coderd/database/migrations/000035_linked_user_id.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TABLE IF NOT EXISTS user_links ( user_id uuid NOT NULL, login_type login_type NOT NULL, @@ -70,5 +68,3 @@ FROM user_links WHERE user_links.user_id = users.id; - -COMMIT; diff --git a/coderd/database/migrations/000058_template_acl.down.sql b/coderd/database/migrations/000058_template_acl.down.sql index 6b34ddf33119b..5320786a6e79b 100644 --- a/coderd/database/migrations/000058_template_acl.down.sql +++ b/coderd/database/migrations/000058_template_acl.down.sql @@ -1,8 +1,4 @@ -BEGIN; - DROP TABLE group_members; DROP TABLE groups; ALTER TABLE templates DROP COLUMN group_acl; ALTER TABLE templates DROP COLUMN user_acl; - -COMMIT; diff --git a/coderd/database/migrations/000058_template_acl.up.sql b/coderd/database/migrations/000058_template_acl.up.sql index f87cd759f9e94..f60a227da6966 100644 --- a/coderd/database/migrations/000058_template_acl.up.sql +++ b/coderd/database/migrations/000058_template_acl.up.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE templates ADD COLUMN user_acl jsonb NOT NULL default '{}'; ALTER TABLE templates ADD COLUMN group_acl jsonb NOT NULL default '{}'; @@ -44,5 +42,3 @@ SET WHERE templates.organization_id = organizations.id ); - -COMMIT; diff --git a/coderd/database/migrations/000059_file_id.down.sql b/coderd/database/migrations/000059_file_id.down.sql index 56dbb13eeb504..99dc7a0f63479 100644 --- a/coderd/database/migrations/000059_file_id.down.sql +++ b/coderd/database/migrations/000059_file_id.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Add back the storage_source column. This must be nullable temporarily. ALTER TABLE provisioner_jobs ADD COLUMN storage_source text; @@ -30,12 +28,10 @@ AND a.hash = b.hash; -- Drop the primary key on files.id. -ALTER TABLE files DROP CONSTRAINT files_pkey; +ALTER TABLE files DROP CONSTRAINT files_pkey; -- Drop the id column. ALTER TABLE files DROP COLUMN id; -- Drop the unique constraint on hash + owner. ALTER TABLE files DROP CONSTRAINT files_hash_created_by_key; -- Set the primary key back to hash. ALTER TABLE files ADD PRIMARY KEY (hash); - -COMMIT; diff --git a/coderd/database/migrations/000059_file_id.up.sql b/coderd/database/migrations/000059_file_id.up.sql index f1b6f96edd6a9..03da21df8518f 100644 --- a/coderd/database/migrations/000059_file_id.up.sql +++ b/coderd/database/migrations/000059_file_id.up.sql @@ -9,8 +9,6 @@ -- This migration also adds a 'files.id' column as the primary -- key. As a side effect the provisioner_jobs must now reference -- the files.id column since the 'hash' column is now ambiguous. -BEGIN; - -- Drop the primary key on hash. ALTER TABLE files DROP CONSTRAINT files_pkey; @@ -38,5 +36,3 @@ WHERE ALTER TABLE provisioner_jobs ALTER COLUMN file_id SET NOT NULL; -- Drop storage_source since it is no longer useful for anything. ALTER TABLE provisioner_jobs DROP COLUMN storage_source; - -COMMIT; diff --git a/coderd/database/migrations/000062_group_avatars.down.sql b/coderd/database/migrations/000062_group_avatars.down.sql index eb15f354383fc..1885a1e26e932 100644 --- a/coderd/database/migrations/000062_group_avatars.down.sql +++ b/coderd/database/migrations/000062_group_avatars.down.sql @@ -1,5 +1 @@ -BEGIN; - ALTER TABLE groups DROP COLUMN avatar_url; - -COMMIT; diff --git a/coderd/database/migrations/000062_group_avatars.up.sql b/coderd/database/migrations/000062_group_avatars.up.sql index b7f033874ba68..1b8e50df663fb 100644 --- a/coderd/database/migrations/000062_group_avatars.up.sql +++ b/coderd/database/migrations/000062_group_avatars.up.sql @@ -1,5 +1 @@ -BEGIN; - ALTER TABLE groups ADD COLUMN avatar_url text NOT NULL DEFAULT ''; - -COMMIT; diff --git a/coderd/database/migrations/000063_resource_type_group.up.sql b/coderd/database/migrations/000063_resource_type_group.up.sql index 3234c61bb7ca1..c129924e3753b 100644 --- a/coderd/database/migrations/000063_resource_type_group.up.sql +++ b/coderd/database/migrations/000063_resource_type_group.up.sql @@ -1,5 +1 @@ -BEGIN; - -ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'group'; - -COMMIT; +ALTER TYPE resource_type ADD VALUE IF NOT EXISTS 'group'; diff --git a/coderd/database/migrations/000066_app_slug.up.sql b/coderd/database/migrations/000066_app_slug.up.sql index 6f67451f2796e..a2fe4f7bf2e11 100644 --- a/coderd/database/migrations/000066_app_slug.up.sql +++ b/coderd/database/migrations/000066_app_slug.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- add "slug" column to "workspace_apps" table ALTER TABLE "workspace_apps" ADD COLUMN "slug" text DEFAULT ''; @@ -12,5 +10,3 @@ ALTER TABLE "workspace_apps" ALTER COLUMN "slug" DROP DEFAULT; -- add unique index on "slug" column ALTER TABLE "workspace_apps" ADD CONSTRAINT "workspace_apps_agent_id_slug_idx" UNIQUE ("agent_id", "slug"); - -COMMIT; diff --git a/coderd/database/migrations/000067_app_display_name.down.sql b/coderd/database/migrations/000067_app_display_name.down.sql index 1b6fe06a0e25b..cd75693ce0e98 100644 --- a/coderd/database/migrations/000067_app_display_name.down.sql +++ b/coderd/database/migrations/000067_app_display_name.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Select all apps with an extra "row_number" column that determines the "rank" -- of the display name against other display names in the same agent. WITH row_numbers AS ( @@ -30,5 +28,3 @@ ALTER TABLE "workspace_apps" RENAME COLUMN "display_name" TO "name"; -- restore unique index on "workspace_apps" table ALTER TABLE workspace_apps ADD CONSTRAINT workspace_apps_agent_id_name_key UNIQUE ("agent_id", "name"); - -COMMIT; diff --git a/coderd/database/migrations/000067_app_display_name.up.sql b/coderd/database/migrations/000067_app_display_name.up.sql index 8d210b35a71bc..4c543573a2606 100644 --- a/coderd/database/migrations/000067_app_display_name.up.sql +++ b/coderd/database/migrations/000067_app_display_name.up.sql @@ -1,9 +1,5 @@ -BEGIN; - -- rename column "name" to "display_name" on "workspace_apps" ALTER TABLE "workspace_apps" RENAME COLUMN "name" TO "display_name"; -- drop constraint "workspace_apps_agent_id_name_key" on "workspace_apps". ALTER TABLE ONLY workspace_apps DROP CONSTRAINT IF EXISTS workspace_apps_agent_id_name_key; - -COMMIT; diff --git a/coderd/database/migrations/000068_update_template_version_created_by.up.sql b/coderd/database/migrations/000068_update_template_version_created_by.up.sql index faeeb1bea637b..704e0fc823738 100644 --- a/coderd/database/migrations/000068_update_template_version_created_by.up.sql +++ b/coderd/database/migrations/000068_update_template_version_created_by.up.sql @@ -1,5 +1,3 @@ -BEGIN; - UPDATE template_versions SET @@ -14,5 +12,3 @@ WHERE created_by IS NULL; ALTER TABLE template_versions ALTER COLUMN created_by SET NOT NULL; - -COMMIT; diff --git a/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.down.sql b/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.down.sql index 7486a8688d911..2fa439edb11b8 100644 --- a/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.down.sql +++ b/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.down.sql @@ -1,9 +1,5 @@ -BEGIN; - ALTER TABLE workspace_agents DROP COLUMN connection_timeout_seconds; ALTER TABLE workspace_agents DROP COLUMN troubleshooting_url; - -COMMIT; diff --git a/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.up.sql b/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.up.sql index 3208ef8ece987..1bbe6c93d9132 100644 --- a/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.up.sql +++ b/coderd/database/migrations/000072_add_agent_connection_timeout_and_troubleshooting_url.up.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE workspace_agents ADD COLUMN connection_timeout_seconds integer NOT NULL DEFAULT 0; @@ -9,5 +7,3 @@ ALTER TABLE workspace_agents ADD COLUMN troubleshooting_url text NOT NULL DEFAULT ''; COMMENT ON COLUMN workspace_agents.troubleshooting_url IS 'URL for troubleshooting the agent.'; - -COMMIT; diff --git a/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.down.sql b/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.down.sql index 889a43c964eb1..f6d5d46a19eae 100644 --- a/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.down.sql +++ b/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.down.sql @@ -1,5 +1,3 @@ -BEGIN; ALTER TABLE parameter_schemas ALTER COLUMN default_source_scheme DROP NOT NULL; ALTER TABLE parameter_schemas ALTER COLUMN default_destination_scheme DROP NOT NULL; -COMMIT; diff --git a/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.up.sql b/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.up.sql index fbceb2ca3d0f9..7da3466acf63a 100644 --- a/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.up.sql +++ b/coderd/database/migrations/000090_sqlc_upgrade_fix_nullable_values.up.sql @@ -1,7 +1,5 @@ -BEGIN; UPDATE parameter_schemas SET default_source_scheme = 'none' WHERE default_source_scheme IS NULL; ALTER TABLE parameter_schemas ALTER COLUMN default_source_scheme SET NOT NULL; UPDATE parameter_schemas SET default_destination_scheme = 'none' WHERE default_destination_scheme IS NULL; ALTER TABLE parameter_schemas ALTER COLUMN default_destination_scheme SET NOT NULL; -COMMIT; diff --git a/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.down.sql b/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.down.sql index 9fec239936190..b071485772874 100644 --- a/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.down.sql +++ b/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.down.sql @@ -1,8 +1,6 @@ -BEGIN; ALTER TABLE workspace_agents RENAME COLUMN login_before_ready TO delay_login_until_ready; ALTER TABLE workspace_agents ALTER COLUMN delay_login_until_ready SET DEFAULT false; UPDATE workspace_agents SET delay_login_until_ready = NOT delay_login_until_ready; COMMENT ON COLUMN workspace_agents.delay_login_until_ready IS 'If true, the agent will delay logins until it is ready (e.g. executing startup script has ended).'; -COMMIT; diff --git a/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.up.sql b/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.up.sql index 2f49830da4a11..df8a731f5dc65 100644 --- a/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.up.sql +++ b/coderd/database/migrations/000093_add_workspace_agent_invert_delay_login_until_ready_to_login_before_ready.up.sql @@ -1,8 +1,6 @@ -BEGIN; ALTER TABLE workspace_agents RENAME COLUMN delay_login_until_ready TO login_before_ready; ALTER TABLE workspace_agents ALTER COLUMN login_before_ready SET DEFAULT true; UPDATE workspace_agents SET login_before_ready = NOT login_before_ready; COMMENT ON COLUMN workspace_agents.login_before_ready IS 'If true, the agent will not prevent login before it is ready (e.g. startup script is still executing).'; -COMMIT; diff --git a/coderd/database/migrations/000096_agent_resolved_directory.down.sql b/coderd/database/migrations/000096_agent_resolved_directory.down.sql index e54c206b26418..b898645020be4 100644 --- a/coderd/database/migrations/000096_agent_resolved_directory.down.sql +++ b/coderd/database/migrations/000096_agent_resolved_directory.down.sql @@ -1,6 +1,2 @@ -BEGIN; - ALTER TABLE ONLY workspace_agents DROP COLUMN IF EXISTS expanded_directory; - -COMMIT; diff --git a/coderd/database/migrations/000096_agent_resolved_directory.up.sql b/coderd/database/migrations/000096_agent_resolved_directory.up.sql index 94e65b051f5b9..a97f3f12222ae 100644 --- a/coderd/database/migrations/000096_agent_resolved_directory.up.sql +++ b/coderd/database/migrations/000096_agent_resolved_directory.up.sql @@ -1,9 +1,5 @@ -BEGIN; - ALTER TABLE ONLY workspace_agents ADD COLUMN IF NOT EXISTS expanded_directory varchar(4096) DEFAULT '' NOT NULL; COMMENT ON COLUMN workspace_agents.expanded_directory IS 'The resolved path of a user-specified directory. e.g. ~/coder -> /home/coder/coder'; - -COMMIT; diff --git a/coderd/database/migrations/000097_license_not_null_uuid.up.sql b/coderd/database/migrations/000097_license_not_null_uuid.up.sql index ca64a6850b021..31c9f4f7bd068 100644 --- a/coderd/database/migrations/000097_license_not_null_uuid.up.sql +++ b/coderd/database/migrations/000097_license_not_null_uuid.up.sql @@ -1,8 +1,4 @@ -BEGIN; - -- We need to assign uuids to any existing licenses that don't have them. UPDATE licenses SET uuid = gen_random_uuid() WHERE uuid IS NULL; -- Assert no licenses have null uuids. ALTER TABLE ONLY licenses ALTER COLUMN uuid SET NOT NULL; - -COMMIT; diff --git a/coderd/database/migrations/000103_add_apikey_name.down.sql b/coderd/database/migrations/000103_add_apikey_name.down.sql index f7070bd3637e9..e1b50394d1506 100644 --- a/coderd/database/migrations/000103_add_apikey_name.down.sql +++ b/coderd/database/migrations/000103_add_apikey_name.down.sql @@ -1,8 +1,4 @@ -BEGIN; - DROP INDEX idx_api_key_name; ALTER TABLE ONLY api_keys DROP COLUMN IF EXISTS token_name; - -COMMIT; diff --git a/coderd/database/migrations/000103_add_apikey_name.up.sql b/coderd/database/migrations/000103_add_apikey_name.up.sql index f1ba24ae0935b..b9b60c89a0630 100644 --- a/coderd/database/migrations/000103_add_apikey_name.up.sql +++ b/coderd/database/migrations/000103_add_apikey_name.up.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE ONLY api_keys ADD COLUMN IF NOT EXISTS token_name text NOT NULL DEFAULT ''; @@ -13,5 +11,3 @@ WHERE CREATE UNIQUE INDEX idx_api_key_name ON api_keys USING btree (user_id, token_name) WHERE (login_type = 'token'); - -COMMIT; diff --git a/coderd/database/migrations/000110_add_startup_logs.up.sql b/coderd/database/migrations/000110_add_startup_logs.up.sql index f74c014dd55bc..847358c405f37 100644 --- a/coderd/database/migrations/000110_add_startup_logs.up.sql +++ b/coderd/database/migrations/000110_add_startup_logs.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TABLE IF NOT EXISTS workspace_agent_startup_logs ( agent_id uuid NOT NULL REFERENCES workspace_agents (id) ON DELETE CASCADE, created_at timestamptz NOT NULL, @@ -14,5 +12,3 @@ ALTER TABLE workspace_agents ADD COLUMN startup_logs_overflowed boolean NOT NULL COMMENT ON COLUMN workspace_agents.startup_logs_length IS 'Total length of startup logs'; COMMENT ON COLUMN workspace_agents.startup_logs_overflowed IS 'Whether the startup logs overflowed in length'; - -COMMIT; diff --git a/coderd/database/migrations/000114_workspace_proxy.down.sql b/coderd/database/migrations/000114_workspace_proxy.down.sql index 8d428817f4ad1..5c289e5770ea9 100644 --- a/coderd/database/migrations/000114_workspace_proxy.down.sql +++ b/coderd/database/migrations/000114_workspace_proxy.down.sql @@ -1,4 +1 @@ -BEGIN; DROP TABLE workspace_proxies; - -COMMIT; diff --git a/coderd/database/migrations/000114_workspace_proxy.up.sql b/coderd/database/migrations/000114_workspace_proxy.up.sql index 5030a5da79523..33c22f766146e 100644 --- a/coderd/database/migrations/000114_workspace_proxy.up.sql +++ b/coderd/database/migrations/000114_workspace_proxy.up.sql @@ -1,4 +1,3 @@ -BEGIN; CREATE TABLE workspace_proxies ( id uuid NOT NULL, name text NOT NULL, @@ -19,5 +18,3 @@ COMMENT ON COLUMN workspace_proxies.wildcard_hostname IS 'Hostname with the wild -- Enforces no active proxies have the same name. CREATE UNIQUE INDEX ON workspace_proxies (name) WHERE deleted = FALSE; - -COMMIT; diff --git a/coderd/database/migrations/000118_workspace_proxy_token.down.sql b/coderd/database/migrations/000118_workspace_proxy_token.down.sql index eb698ce6e34d4..47914a5afd4c9 100644 --- a/coderd/database/migrations/000118_workspace_proxy_token.down.sql +++ b/coderd/database/migrations/000118_workspace_proxy_token.down.sql @@ -1,6 +1,2 @@ -BEGIN; - ALTER TABLE workspace_proxies DROP COLUMN token_hashed_secret; - -COMMIT; diff --git a/coderd/database/migrations/000118_workspace_proxy_token.up.sql b/coderd/database/migrations/000118_workspace_proxy_token.up.sql index f4f1a66c2384a..b514a4a41b761 100644 --- a/coderd/database/migrations/000118_workspace_proxy_token.up.sql +++ b/coderd/database/migrations/000118_workspace_proxy_token.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- It's difficult to generate tokens for existing proxies, so we'll just delete -- them if they exist. -- @@ -18,5 +16,3 @@ COMMENT ON COLUMN workspace_proxies.deleted COMMENT ON COLUMN workspace_proxies.icon IS 'Expects an emoji character. (/emojis/1f1fa-1f1f8.png)'; - -COMMIT; diff --git a/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql b/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql index 3311a6cd7ce8c..9f5b4f1829747 100644 --- a/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql +++ b/coderd/database/migrations/000119_workspace_proxy_name_idx.down.sql @@ -1,8 +1,4 @@ -BEGIN; - DROP INDEX IF EXISTS workspace_proxies_lower_name_idx; -- Enforces no active proxies have the same name. CREATE UNIQUE INDEX ON workspace_proxies (name) WHERE deleted = FALSE; - -COMMIT; diff --git a/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql b/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql index 0101905491672..bc227d68d264c 100644 --- a/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql +++ b/coderd/database/migrations/000119_workspace_proxy_name_idx.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- No one is using this feature yet as of writing this migration, so this is -- fine. Just delete all workspace proxies to prevent the new index from having -- conflicts. @@ -7,5 +5,3 @@ DELETE FROM workspace_proxies; DROP INDEX IF EXISTS workspace_proxies_name_idx; CREATE UNIQUE INDEX workspace_proxies_lower_name_idx ON workspace_proxies USING btree (lower(name)) WHERE deleted = FALSE; - -COMMIT; diff --git a/coderd/database/migrations/000120_trigger_delete_user_apikey.down.sql b/coderd/database/migrations/000120_trigger_delete_user_apikey.down.sql index f5c8592c44948..66178aa1e2d32 100644 --- a/coderd/database/migrations/000120_trigger_delete_user_apikey.down.sql +++ b/coderd/database/migrations/000120_trigger_delete_user_apikey.down.sql @@ -1,9 +1,5 @@ -BEGIN; - DROP TRIGGER IF EXISTS trigger_update_users ON users; DROP FUNCTION IF EXISTS delete_deleted_user_api_keys; DROP TRIGGER IF EXISTS trigger_insert_apikeys ON api_keys; DROP FUNCTION IF EXISTS insert_apikey_fail_if_user_deleted; - -COMMIT; diff --git a/coderd/database/migrations/000120_trigger_delete_user_apikey.up.sql b/coderd/database/migrations/000120_trigger_delete_user_apikey.up.sql index 9ea208bef4b51..4d2536f929cb5 100644 --- a/coderd/database/migrations/000120_trigger_delete_user_apikey.up.sql +++ b/coderd/database/migrations/000120_trigger_delete_user_apikey.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- We need to delete all existing API keys for soft-deleted users. DELETE FROM api_keys @@ -51,5 +49,3 @@ CREATE TRIGGER trigger_insert_apikeys BEFORE INSERT ON api_keys FOR EACH ROW EXECUTE PROCEDURE insert_apikey_fail_if_user_deleted(); - -COMMIT; diff --git a/coderd/database/migrations/000122_add_template_cleanup_ttls.down.sql b/coderd/database/migrations/000122_add_template_cleanup_ttls.down.sql index 78a04e961eea5..70fd50d21a66d 100644 --- a/coderd/database/migrations/000122_add_template_cleanup_ttls.down.sql +++ b/coderd/database/migrations/000122_add_template_cleanup_ttls.down.sql @@ -1,4 +1,2 @@ -BEGIN; ALTER TABLE ONLY templates DROP COLUMN IF EXISTS failure_ttl; ALTER TABLE ONLY templates DROP COLUMN IF EXISTS inactivity_ttl; -COMMIT; diff --git a/coderd/database/migrations/000122_add_template_cleanup_ttls.up.sql b/coderd/database/migrations/000122_add_template_cleanup_ttls.up.sql index f043356375c99..980588e269d45 100644 --- a/coderd/database/migrations/000122_add_template_cleanup_ttls.up.sql +++ b/coderd/database/migrations/000122_add_template_cleanup_ttls.up.sql @@ -1,4 +1,2 @@ -BEGIN; ALTER TABLE ONLY templates ADD COLUMN IF NOT EXISTS failure_ttl BIGINT NOT NULL DEFAULT 0; ALTER TABLE ONLY templates ADD COLUMN IF NOT EXISTS inactivity_ttl BIGINT NOT NULL DEFAULT 0; -COMMIT; diff --git a/coderd/database/migrations/000123_workspace_agent_subsystem.down.sql b/coderd/database/migrations/000123_workspace_agent_subsystem.down.sql index ec1fc4aa26c7f..9bd39b003435d 100644 --- a/coderd/database/migrations/000123_workspace_agent_subsystem.down.sql +++ b/coderd/database/migrations/000123_workspace_agent_subsystem.down.sql @@ -1,4 +1,2 @@ -BEGIN; ALTER TABLE workspace_agents DROP COLUMN subsystem; DROP TYPE workspace_agent_subsystem; -COMMIT; diff --git a/coderd/database/migrations/000123_workspace_agent_subsystem.up.sql b/coderd/database/migrations/000123_workspace_agent_subsystem.up.sql index 747c5d362fe5d..35d4389beccab 100644 --- a/coderd/database/migrations/000123_workspace_agent_subsystem.up.sql +++ b/coderd/database/migrations/000123_workspace_agent_subsystem.up.sql @@ -1,4 +1,2 @@ -BEGIN; CREATE TYPE workspace_agent_subsystem AS ENUM ('envbuilder', 'envbox', 'none'); ALTER TABLE workspace_agents ADD COLUMN subsystem workspace_agent_subsystem NOT NULL default 'none'; -COMMIT; diff --git a/coderd/database/migrations/000124_validation_min_max_nullable.down.sql b/coderd/database/migrations/000124_validation_min_max_nullable.down.sql index 39a8eb69a33a5..3345d22dc1f83 100644 --- a/coderd/database/migrations/000124_validation_min_max_nullable.down.sql +++ b/coderd/database/migrations/000124_validation_min_max_nullable.down.sql @@ -1,6 +1,4 @@ -BEGIN; UPDATE template_version_parameters SET validation_min = 0 WHERE validation_min = NULL; UPDATE template_version_parameters SET validation_max = 0 WHERE validation_max = NULL; ALTER TABLE template_version_parameters ALTER COLUMN validation_min SET NOT NULL; ALTER TABLE template_version_parameters ALTER COLUMN validation_max SET NOT NULL; -COMMIT; diff --git a/coderd/database/migrations/000124_validation_min_max_nullable.up.sql b/coderd/database/migrations/000124_validation_min_max_nullable.up.sql index ea3160747e55e..be91df47c1f30 100644 --- a/coderd/database/migrations/000124_validation_min_max_nullable.up.sql +++ b/coderd/database/migrations/000124_validation_min_max_nullable.up.sql @@ -1,6 +1,4 @@ -BEGIN; ALTER TABLE template_version_parameters ALTER COLUMN validation_min DROP NOT NULL; ALTER TABLE template_version_parameters ALTER COLUMN validation_max DROP NOT NULL; UPDATE template_version_parameters SET validation_min = NULL WHERE validation_min = 0; UPDATE template_version_parameters SET validation_max = NULL WHERE validation_max = 0; -COMMIT; diff --git a/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.down.sql b/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.down.sql index 3c93e6e92b2eb..755fb52c7ffe0 100644 --- a/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.down.sql +++ b/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.down.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE workspace_agents ADD COLUMN login_before_ready boolean NOT NULL DEFAULT TRUE; UPDATE workspace_agents SET login_before_ready = CASE WHEN startup_script_behavior = 'non-blocking' THEN TRUE ELSE FALSE END; @@ -8,5 +6,3 @@ ALTER TABLE workspace_agents DROP COLUMN startup_script_behavior; DROP TYPE startup_script_behavior; COMMENT ON COLUMN workspace_agents.login_before_ready IS 'If true, the agent will delay logins until it is ready (e.g. executing startup script has ended).'; - -COMMIT; diff --git a/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.up.sql b/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.up.sql index 408cd854de262..1091c9711e10d 100644 --- a/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.up.sql +++ b/coderd/database/migrations/000125_rename_login_before_ready_to_startup_script_behavior.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TYPE startup_script_behavior AS ENUM ('blocking', 'non-blocking'); ALTER TABLE workspace_agents ADD COLUMN startup_script_behavior startup_script_behavior NOT NULL DEFAULT 'non-blocking'; @@ -8,5 +6,3 @@ UPDATE workspace_agents SET startup_script_behavior = (CASE WHEN login_before_re ALTER TABLE workspace_agents DROP COLUMN login_before_ready; COMMENT ON COLUMN workspace_agents.startup_script_behavior IS 'When startup script behavior is non-blocking, the workspace will be ready and accessible upon agent connection, when it is blocking, workspace will wait for the startup script to complete before becoming ready and accessible.'; - -COMMIT; diff --git a/coderd/database/migrations/000128_template_locked_ttl.down.sql b/coderd/database/migrations/000128_template_locked_ttl.down.sql index 71beb28ebe2f9..72b2ae64d4e51 100644 --- a/coderd/database/migrations/000128_template_locked_ttl.down.sql +++ b/coderd/database/migrations/000128_template_locked_ttl.down.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE templates DROP COLUMN locked_ttl; -COMMIT; diff --git a/coderd/database/migrations/000128_template_locked_ttl.up.sql b/coderd/database/migrations/000128_template_locked_ttl.up.sql index 0f51a424fe115..24d53033cf9a0 100644 --- a/coderd/database/migrations/000128_template_locked_ttl.up.sql +++ b/coderd/database/migrations/000128_template_locked_ttl.up.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE templates ADD COLUMN locked_ttl BIGINT NOT NULL DEFAULT 0; -COMMIT; diff --git a/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.down.sql b/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.down.sql index 9d57ded80bb7c..9f2c4878ec771 100644 --- a/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.down.sql +++ b/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.down.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE workspace_agents DROP COLUMN started_at, DROP COLUMN ready_at; @@ -9,5 +7,3 @@ ALTER TABLE workspace_agents ALTER TABLE workspace_agent_startup_logs ADD COLUMN eof boolean NOT NULL DEFAULT false; COMMENT ON COLUMN workspace_agent_startup_logs.eof IS 'End of file reached'; - -COMMIT; diff --git a/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.up.sql b/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.up.sql index 7a11298c834f6..7d521be4af14d 100644 --- a/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.up.sql +++ b/coderd/database/migrations/000129_drop_startup_logs_eof_and_add_completion.up.sql @@ -1,5 +1,3 @@ -BEGIN; - DELETE FROM workspace_agent_startup_logs WHERE eof IS TRUE; ALTER TABLE workspace_agent_startup_logs DROP COLUMN eof; @@ -10,5 +8,3 @@ ALTER TABLE workspace_agents COMMENT ON COLUMN workspace_agents.started_at IS 'The time the agent entered the starting lifecycle state'; COMMENT ON COLUMN workspace_agents.ready_at IS 'The time the agent entered the ready or start_error lifecycle state'; - -COMMIT; diff --git a/coderd/database/migrations/000130_ha_coordinator.down.sql b/coderd/database/migrations/000130_ha_coordinator.down.sql index 54c8b0253902b..a1e4633600f35 100644 --- a/coderd/database/migrations/000130_ha_coordinator.down.sql +++ b/coderd/database/migrations/000130_ha_coordinator.down.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP TRIGGER IF EXISTS tailnet_notify_client_change ON tailnet_clients; DROP FUNCTION IF EXISTS tailnet_notify_client_change; DROP INDEX IF EXISTS idx_tailnet_clients_agent; @@ -14,5 +12,3 @@ DROP TABLE IF EXISTS tailnet_agents; DROP TRIGGER IF EXISTS tailnet_notify_coordinator_heartbeat ON tailnet_coordinators; DROP FUNCTION IF EXISTS tailnet_notify_coordinator_heartbeat; DROP TABLE IF EXISTS tailnet_coordinators; - -COMMIT; diff --git a/coderd/database/migrations/000130_ha_coordinator.up.sql b/coderd/database/migrations/000130_ha_coordinator.up.sql index f30bd077c798b..b444520e82f10 100644 --- a/coderd/database/migrations/000130_ha_coordinator.up.sql +++ b/coderd/database/migrations/000130_ha_coordinator.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TABLE tailnet_coordinators ( id uuid NOT NULL PRIMARY KEY, heartbeat_at timestamp with time zone NOT NULL @@ -93,5 +91,3 @@ CREATE TRIGGER tailnet_notify_coordinator_heartbeat AFTER INSERT OR UPDATE ON tailnet_coordinators FOR EACH ROW EXECUTE PROCEDURE tailnet_notify_coordinator_heartbeat(); - -COMMIT; diff --git a/coderd/database/migrations/000131_workspace_locked.down.sql b/coderd/database/migrations/000131_workspace_locked.down.sql index d622787938738..78361c7e9ed7e 100644 --- a/coderd/database/migrations/000131_workspace_locked.down.sql +++ b/coderd/database/migrations/000131_workspace_locked.down.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE workspaces DROP COLUMN locked_at; -COMMIT; diff --git a/coderd/database/migrations/000131_workspace_locked.up.sql b/coderd/database/migrations/000131_workspace_locked.up.sql index e62a6a351d92a..945180df5d769 100644 --- a/coderd/database/migrations/000131_workspace_locked.up.sql +++ b/coderd/database/migrations/000131_workspace_locked.up.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE workspaces ADD COLUMN locked_at timestamptz NULL; -COMMIT; diff --git a/coderd/database/migrations/000134_workspace_build_reason.up.sql b/coderd/database/migrations/000134_workspace_build_reason.up.sql index ae9d30fae9861..80914cfa2aa6f 100644 --- a/coderd/database/migrations/000134_workspace_build_reason.up.sql +++ b/coderd/database/migrations/000134_workspace_build_reason.up.sql @@ -1,5 +1,3 @@ -BEGIN; ALTER TYPE build_reason ADD VALUE IF NOT EXISTS 'autolock'; ALTER TYPE build_reason ADD VALUE IF NOT EXISTS 'failedstop'; ALTER TYPE build_reason ADD VALUE IF NOT EXISTS 'autodelete'; -COMMIT; diff --git a/coderd/database/migrations/000138_join_users.down.sql b/coderd/database/migrations/000138_join_users.down.sql index 754574f7b5abd..b70115d3d6b20 100644 --- a/coderd/database/migrations/000138_join_users.down.sql +++ b/coderd/database/migrations/000138_join_users.down.sql @@ -1,6 +1,2 @@ -BEGIN; - DROP VIEW template_with_users; DROP VIEW visible_users; - -COMMIT; diff --git a/coderd/database/migrations/000138_join_users.up.sql b/coderd/database/migrations/000138_join_users.up.sql index 198dd55edf1d2..ed4312140c106 100644 --- a/coderd/database/migrations/000138_join_users.up.sql +++ b/coderd/database/migrations/000138_join_users.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE VIEW visible_users AS @@ -26,5 +24,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000139_template_restart_requirement.down.sql b/coderd/database/migrations/000139_template_restart_requirement.down.sql index f882ada1fd1c1..bd2bddf5178e7 100644 --- a/coderd/database/migrations/000139_template_restart_requirement.down.sql +++ b/coderd/database/migrations/000139_template_restart_requirement.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Delete the new version of the template_with_users view to remove the column -- dependency. DROP VIEW template_with_users; @@ -25,5 +23,3 @@ AS ON templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000139_template_restart_requirement.up.sql b/coderd/database/migrations/000139_template_restart_requirement.up.sql index ec8f2f520aed5..7cfc3dafe88da 100644 --- a/coderd/database/migrations/000139_template_restart_requirement.up.sql +++ b/coderd/database/migrations/000139_template_restart_requirement.up.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE templates -- The max_ttl column will be dropped eventually when the new "restart -- requirement" feature flag is fully rolled out. @@ -31,5 +29,3 @@ AS ON templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000141_join_users_build_version.down.sql b/coderd/database/migrations/000141_join_users_build_version.down.sql index 0c27698e8c1e9..2eb57c5a970a3 100644 --- a/coderd/database/migrations/000141_join_users_build_version.down.sql +++ b/coderd/database/migrations/000141_join_users_build_version.down.sql @@ -1,6 +1,2 @@ -BEGIN; - DROP VIEW workspace_build_with_user; DROP VIEW template_version_with_user; - -COMMIT; diff --git a/coderd/database/migrations/000141_join_users_build_version.up.sql b/coderd/database/migrations/000141_join_users_build_version.up.sql index eed74c09b03ce..1e865c0ffacb8 100644 --- a/coderd/database/migrations/000141_join_users_build_version.up.sql +++ b/coderd/database/migrations/000141_join_users_build_version.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- If you need to update this view, put 'DROP VIEW workspace_build_with_user;' before this. CREATE VIEW workspace_build_with_user @@ -34,5 +32,3 @@ FROM template_versions.created_by = visible_users.id; COMMENT ON VIEW template_version_with_user IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000142_proxy_derp.down.sql b/coderd/database/migrations/000142_proxy_derp.down.sql index 9937e47591ce5..c7d48617c52f6 100644 --- a/coderd/database/migrations/000142_proxy_derp.down.sql +++ b/coderd/database/migrations/000142_proxy_derp.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- drop any rows that aren't primary replicas DELETE FROM replicas WHERE "primary" = false; @@ -11,5 +9,3 @@ ALTER TABLE workspace_proxies DROP CONSTRAINT workspace_proxies_region_id_unique, DROP COLUMN region_id, DROP COLUMN derp_enabled; - -COMMIT; diff --git a/coderd/database/migrations/000142_proxy_derp.up.sql b/coderd/database/migrations/000142_proxy_derp.up.sql index e214fe50fc366..fa9598c790b8f 100644 --- a/coderd/database/migrations/000142_proxy_derp.up.sql +++ b/coderd/database/migrations/000142_proxy_derp.up.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE replicas ADD COLUMN "primary" boolean NOT NULL DEFAULT true; @@ -9,5 +7,3 @@ ALTER TABLE workspace_proxies ADD COLUMN region_id serial NOT NULL, ADD COLUMN derp_enabled boolean NOT NULL DEFAULT true, ADD CONSTRAINT workspace_proxies_region_id_unique UNIQUE (region_id); - -COMMIT; diff --git a/coderd/database/migrations/000143_workspace_agent_logs.down.sql b/coderd/database/migrations/000143_workspace_agent_logs.down.sql index 53b0d03f44cfd..5fe6e3b8e6e39 100644 --- a/coderd/database/migrations/000143_workspace_agent_logs.down.sql +++ b/coderd/database/migrations/000143_workspace_agent_logs.down.sql @@ -1,8 +1,6 @@ -BEGIN; ALTER TABLE workspace_agent_logs RENAME TO workspace_agent_startup_logs; ALTER TABLE workspace_agent_startup_logs DROP COLUMN source; DROP TYPE workspace_agent_log_source; ALTER TABLE workspace_agents RENAME COLUMN logs_overflowed TO startup_logs_overflowed; ALTER TABLE workspace_agents RENAME COLUMN logs_length TO startup_logs_length; ALTER TABLE workspace_agents RENAME CONSTRAINT max_logs_length TO max_startup_logs_length; -COMMIT; diff --git a/coderd/database/migrations/000143_workspace_agent_logs.up.sql b/coderd/database/migrations/000143_workspace_agent_logs.up.sql index 7de9cf07aa5da..079d5e42e94ee 100644 --- a/coderd/database/migrations/000143_workspace_agent_logs.up.sql +++ b/coderd/database/migrations/000143_workspace_agent_logs.up.sql @@ -1,8 +1,6 @@ -BEGIN; CREATE TYPE workspace_agent_log_source AS ENUM ('startup_script', 'shutdown_script', 'kubernetes_logs', 'envbox', 'envbuilder', 'external'); ALTER TABLE workspace_agent_startup_logs RENAME TO workspace_agent_logs; ALTER TABLE workspace_agent_logs ADD COLUMN source workspace_agent_log_source NOT NULL DEFAULT 'startup_script'; ALTER TABLE workspace_agents RENAME COLUMN startup_logs_overflowed TO logs_overflowed; ALTER TABLE workspace_agents RENAME COLUMN startup_logs_length TO logs_length; ALTER TABLE workspace_agents RENAME CONSTRAINT max_startup_logs_length TO max_logs_length; -COMMIT; diff --git a/coderd/database/migrations/000146_proxy_derp_only.up.sql b/coderd/database/migrations/000146_proxy_derp_only.up.sql index d63c602f78b70..fb46ca00f03d2 100644 --- a/coderd/database/migrations/000146_proxy_derp_only.up.sql +++ b/coderd/database/migrations/000146_proxy_derp_only.up.sql @@ -1,8 +1,4 @@ -BEGIN; - ALTER TABLE workspace_proxies ADD COLUMN "derp_only" BOOLEAN NOT NULL DEFAULT false; COMMENT ON COLUMN workspace_proxies.derp_only IS 'Disables app/terminal proxying for this proxy and only acts as a DERP relay.'; - -COMMIT; diff --git a/coderd/database/migrations/000147_group_display_name.down.sql b/coderd/database/migrations/000147_group_display_name.down.sql index b04850449fedc..7e9af6c21cc1e 100644 --- a/coderd/database/migrations/000147_group_display_name.down.sql +++ b/coderd/database/migrations/000147_group_display_name.down.sql @@ -1,6 +1,2 @@ -BEGIN; - ALTER TABLE groups DROP COLUMN display_name; - -COMMIT; diff --git a/coderd/database/migrations/000147_group_display_name.up.sql b/coderd/database/migrations/000147_group_display_name.up.sql index a812ad8aa34c3..a7448ffb23001 100644 --- a/coderd/database/migrations/000147_group_display_name.up.sql +++ b/coderd/database/migrations/000147_group_display_name.up.sql @@ -1,8 +1,4 @@ -BEGIN; - ALTER TABLE groups ADD COLUMN display_name TEXT NOT NULL DEFAULT ''; COMMENT ON COLUMN groups.display_name IS 'Display name is a custom, human-friendly group name that user can set. This is not required to be unique and can be the empty string.'; - -COMMIT; diff --git a/coderd/database/migrations/000148_group_source.down.sql b/coderd/database/migrations/000148_group_source.down.sql index 504c227d186bb..1bfef7ea49297 100644 --- a/coderd/database/migrations/000148_group_source.down.sql +++ b/coderd/database/migrations/000148_group_source.down.sql @@ -1,8 +1,4 @@ -BEGIN; - ALTER TABLE groups DROP COLUMN source; DROP TYPE group_source; - -COMMIT; diff --git a/coderd/database/migrations/000148_group_source.up.sql b/coderd/database/migrations/000148_group_source.up.sql index d06e89ca2b1d6..d4b7140ebcddd 100644 --- a/coderd/database/migrations/000148_group_source.up.sql +++ b/coderd/database/migrations/000148_group_source.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TYPE group_source AS ENUM ( -- User created groups 'user', @@ -11,5 +9,3 @@ ALTER TABLE groups ADD COLUMN source group_source NOT NULL DEFAULT 'user'; COMMENT ON COLUMN groups.source IS 'Source indicates how the group was created. It can be created by a user manually, or through some system process like OIDC group sync.'; - -COMMIT; diff --git a/coderd/database/migrations/000149_agent_multiple_subsystems.down.sql b/coderd/database/migrations/000149_agent_multiple_subsystems.down.sql index 05bea6c620502..be5d71f14d7a1 100644 --- a/coderd/database/migrations/000149_agent_multiple_subsystems.down.sql +++ b/coderd/database/migrations/000149_agent_multiple_subsystems.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Bring back the subsystem column. ALTER TABLE workspace_agents ADD COLUMN subsystem workspace_agent_subsystem NOT NULL DEFAULT 'none'; @@ -13,5 +11,3 @@ ALTER TABLE workspace_agents DROP COLUMN subsystems; -- We cannot drop the "exectrace" value from the workspace_agent_subsystem type -- because you cannot drop values from an enum type. UPDATE workspace_agents SET subsystem = 'none' WHERE subsystem = 'exectrace'; - -COMMIT; diff --git a/coderd/database/migrations/000149_agent_multiple_subsystems.up.sql b/coderd/database/migrations/000149_agent_multiple_subsystems.up.sql index 9ebb71d5bdf5e..f39cf5ab06352 100644 --- a/coderd/database/migrations/000149_agent_multiple_subsystems.up.sql +++ b/coderd/database/migrations/000149_agent_multiple_subsystems.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Add "exectrace" to workspace_agent_subsystem type. ALTER TYPE workspace_agent_subsystem ADD VALUE 'exectrace'; @@ -17,5 +15,3 @@ UPDATE workspace_agents SET subsystems = ARRAY[subsystem] WHERE subsystem != 'no -- Drop the subsystem column from workspace_agents. ALTER TABLE workspace_agents DROP COLUMN subsystem; - -COMMIT; diff --git a/coderd/database/migrations/000151_rename_locked.down.sql b/coderd/database/migrations/000151_rename_locked.down.sql index 4dfb254268fa2..6be23ffdfc18a 100644 --- a/coderd/database/migrations/000151_rename_locked.down.sql +++ b/coderd/database/migrations/000151_rename_locked.down.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE templates RENAME COLUMN time_til_dormant TO inactivity_ttl; ALTER TABLE templates RENAME COLUMN time_til_dormant_autodelete TO locked_ttl; ALTER TABLE workspaces RENAME COLUMN dormant_at TO locked_at; @@ -22,5 +20,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000151_rename_locked.up.sql b/coderd/database/migrations/000151_rename_locked.up.sql index ae72c7efa98cb..957c45ff4eeb5 100644 --- a/coderd/database/migrations/000151_rename_locked.up.sql +++ b/coderd/database/migrations/000151_rename_locked.up.sql @@ -1,4 +1,3 @@ -BEGIN; ALTER TABLE templates RENAME COLUMN inactivity_ttl TO time_til_dormant; ALTER TABLE templates RENAME COLUMN locked_ttl TO time_til_dormant_autodelete; ALTER TABLE workspaces RENAME COLUMN locked_at TO dormant_at; @@ -21,5 +20,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000152_rename_template_restart_requirement.down.sql b/coderd/database/migrations/000152_rename_template_restart_requirement.down.sql index 1dc90e708db1a..92841b2c0d1f1 100644 --- a/coderd/database/migrations/000152_rename_template_restart_requirement.down.sql +++ b/coderd/database/migrations/000152_rename_template_restart_requirement.down.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP VIEW template_with_users; ALTER TABLE templates RENAME COLUMN autostop_requirement_days_of_week TO restart_requirement_days_of_week; @@ -21,5 +19,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000152_rename_template_restart_requirement.up.sql b/coderd/database/migrations/000152_rename_template_restart_requirement.up.sql index 67323287511b3..0e006d9c6e5cc 100644 --- a/coderd/database/migrations/000152_rename_template_restart_requirement.up.sql +++ b/coderd/database/migrations/000152_rename_template_restart_requirement.up.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP VIEW template_with_users; ALTER TABLE templates RENAME COLUMN restart_requirement_days_of_week TO autostop_requirement_days_of_week; @@ -21,5 +19,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000153_agent_default_apps.down.sql b/coderd/database/migrations/000153_agent_default_apps.down.sql index 34c8f46551680..234dc2c2f46fb 100644 --- a/coderd/database/migrations/000153_agent_default_apps.down.sql +++ b/coderd/database/migrations/000153_agent_default_apps.down.sql @@ -1,5 +1,2 @@ -BEGIN; ALTER TABLE workspace_agents DROP COLUMN display_apps; -DROP TYPE display_app; -COMMIT; - +DROP TYPE display_app; diff --git a/coderd/database/migrations/000153_agent_default_apps.up.sql b/coderd/database/migrations/000153_agent_default_apps.up.sql index 7b51b77d8661e..a269d4d5c7e99 100644 --- a/coderd/database/migrations/000153_agent_default_apps.up.sql +++ b/coderd/database/migrations/000153_agent_default_apps.up.sql @@ -1,4 +1,2 @@ -BEGIN; CREATE TYPE display_app AS ENUM ('vscode', 'vscode_insiders', 'web_terminal', 'ssh_helper', 'port_forwarding_helper'); ALTER TABLE workspace_agents ADD column display_apps display_app[] DEFAULT '{vscode, vscode_insiders, web_terminal, ssh_helper, port_forwarding_helper}'; -COMMIT; diff --git a/coderd/database/migrations/000154_dbcrypt_key_ids.down.sql b/coderd/database/migrations/000154_dbcrypt_key_ids.down.sql index 7dea0a1909227..01e200ccc8c58 100644 --- a/coderd/database/migrations/000154_dbcrypt_key_ids.down.sql +++ b/coderd/database/migrations/000154_dbcrypt_key_ids.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Before dropping this table, we need to check if there exist any -- foreign key references to it. We do this by checking the following: -- user_links.oauth_access_token_key_id @@ -39,5 +37,3 @@ ALTER TABLE user_links -- Finally, drop the table. DROP TABLE IF EXISTS dbcrypt_keys; - -COMMIT; diff --git a/coderd/database/migrations/000156_pg_coordinator_single_tailnet.down.sql b/coderd/database/migrations/000156_pg_coordinator_single_tailnet.down.sql index 7cc418489f59a..3b35e0015a0a3 100644 --- a/coderd/database/migrations/000156_pg_coordinator_single_tailnet.down.sql +++ b/coderd/database/migrations/000156_pg_coordinator_single_tailnet.down.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE tailnet_clients ADD COLUMN @@ -35,5 +33,3 @@ BEGIN END IF; END; $$; - -COMMIT; diff --git a/coderd/database/migrations/000156_pg_coordinator_single_tailnet.up.sql b/coderd/database/migrations/000156_pg_coordinator_single_tailnet.up.sql index 4ca218248ef4a..4bb76f8e28bdf 100644 --- a/coderd/database/migrations/000156_pg_coordinator_single_tailnet.up.sql +++ b/coderd/database/migrations/000156_pg_coordinator_single_tailnet.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TABLE tailnet_client_subscriptions ( client_id uuid NOT NULL, coordinator_id uuid NOT NULL, @@ -84,5 +82,3 @@ ALTER TABLE tailnet_clients DROP COLUMN agent_id; - -COMMIT; diff --git a/coderd/database/migrations/000157_workspace_agent_script.down.sql b/coderd/database/migrations/000157_workspace_agent_script.down.sql index 013c1097dda3e..21356654709b4 100644 --- a/coderd/database/migrations/000157_workspace_agent_script.down.sql +++ b/coderd/database/migrations/000157_workspace_agent_script.down.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE workspace_agent_logs SET LOGGED; -- Revert the workspace_agents table to its former state @@ -19,5 +17,3 @@ ALTER TABLE workspace_agent_logs DROP COLUMN log_source_id; -- Drop the newly created tables DROP TABLE workspace_agent_scripts; DROP TABLE workspace_agent_log_sources; - -COMMIT; diff --git a/coderd/database/migrations/000157_workspace_agent_script.up.sql b/coderd/database/migrations/000157_workspace_agent_script.up.sql index c073318c5b804..3b7244951f71e 100644 --- a/coderd/database/migrations/000157_workspace_agent_script.up.sql +++ b/coderd/database/migrations/000157_workspace_agent_script.up.sql @@ -1,4 +1,3 @@ -BEGIN; CREATE TABLE workspace_agent_log_sources ( workspace_agent_id uuid NOT NULL REFERENCES workspace_agents(id) ON DELETE CASCADE, id uuid NOT NULL, @@ -33,4 +32,3 @@ ALTER TABLE workspace_agents DROP COLUMN startup_script; -- Set the table to unlogged to speed up the inserts ALTER TABLE workspace_agent_logs SET UNLOGGED; -COMMIT; diff --git a/coderd/database/migrations/000158_external_auth.down.sql b/coderd/database/migrations/000158_external_auth.down.sql index 427de53c95fb2..a2f48a2c64c6b 100644 --- a/coderd/database/migrations/000158_external_auth.down.sql +++ b/coderd/database/migrations/000158_external_auth.down.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE template_versions RENAME COLUMN external_auth_providers TO git_auth_providers; ALTER TABLE external_auth_links RENAME TO git_auth_links; @@ -21,5 +19,3 @@ FROM template_versions.created_by = visible_users.id; COMMENT ON VIEW template_version_with_user IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000158_external_auth.up.sql b/coderd/database/migrations/000158_external_auth.up.sql index 52fc1977e376c..3ae295bb246e1 100644 --- a/coderd/database/migrations/000158_external_auth.up.sql +++ b/coderd/database/migrations/000158_external_auth.up.sql @@ -1,5 +1,3 @@ -BEGIN; - ALTER TABLE template_versions RENAME COLUMN git_auth_providers TO external_auth_providers; ALTER TABLE git_auth_links RENAME TO external_auth_links; @@ -23,5 +21,3 @@ FROM COMMENT ON VIEW template_version_with_user IS 'Joins in the username + avatar url of the created by user.'; COMMENT ON COLUMN template_versions.external_auth_providers IS 'IDs of External auth providers for a specific template version'; - -COMMIT; diff --git a/coderd/database/migrations/000160_provisioner_job_status.down.sql b/coderd/database/migrations/000160_provisioner_job_status.down.sql index 3f04c8dd11dfc..db71dfa84eb96 100644 --- a/coderd/database/migrations/000160_provisioner_job_status.down.sql +++ b/coderd/database/migrations/000160_provisioner_job_status.down.sql @@ -1,6 +1,2 @@ -BEGIN; - ALTER TABLE provisioner_jobs DROP COLUMN job_status; DROP TYPE provisioner_job_status; - -COMMIT; diff --git a/coderd/database/migrations/000160_provisioner_job_status.up.sql b/coderd/database/migrations/000160_provisioner_job_status.up.sql index 9cfea7fbfb140..d6b310d7a9f81 100644 --- a/coderd/database/migrations/000160_provisioner_job_status.up.sql +++ b/coderd/database/migrations/000160_provisioner_job_status.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TYPE provisioner_job_status AS ENUM ('pending', 'running', 'succeeded', 'canceling', 'canceled', 'failed', 'unknown'); COMMENT ON TYPE provisioner_job_status IS 'Computed status of a provisioner job. Jobs could be stuck in a hung state, these states do not guarantee any transition to another state.'; @@ -34,5 +32,3 @@ ALTER TABLE provisioner_jobs ADD COLUMN COMMENT ON COLUMN provisioner_jobs.job_status IS 'Computed column to track the status of the job.'; - -COMMIT; diff --git a/coderd/database/migrations/000162_workspace_automatic_updates.down.sql b/coderd/database/migrations/000162_workspace_automatic_updates.down.sql index d2f050b4afb75..57ce2c1cd7f5b 100644 --- a/coderd/database/migrations/000162_workspace_automatic_updates.down.sql +++ b/coderd/database/migrations/000162_workspace_automatic_updates.down.sql @@ -1,4 +1,2 @@ -BEGIN; ALTER TABLE workspaces DROP COLUMN IF EXISTS automatic_updates; DROP TYPE IF EXISTS automatic_updates; -COMMIT; diff --git a/coderd/database/migrations/000162_workspace_automatic_updates.up.sql b/coderd/database/migrations/000162_workspace_automatic_updates.up.sql index b034437007b54..0e773421a62df 100644 --- a/coderd/database/migrations/000162_workspace_automatic_updates.up.sql +++ b/coderd/database/migrations/000162_workspace_automatic_updates.up.sql @@ -1,8 +1,6 @@ -BEGIN; -- making this an enum in case we want to later add other options, like 'if_compatible_vars' CREATE TYPE automatic_updates AS ENUM ( 'always', 'never' ); ALTER TABLE workspaces ADD COLUMN IF NOT EXISTS automatic_updates automatic_updates NOT NULL DEFAULT 'never'::automatic_updates; -COMMIT; diff --git a/coderd/database/migrations/000164_archive_template_versions.down.sql b/coderd/database/migrations/000164_archive_template_versions.down.sql index 2c89f985aa225..0a0b82b972901 100644 --- a/coderd/database/migrations/000164_archive_template_versions.down.sql +++ b/coderd/database/migrations/000164_archive_template_versions.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- The view will be rebuilt with the new column DROP VIEW template_version_with_user; @@ -22,5 +20,3 @@ FROM template_versions.created_by = visible_users.id; COMMENT ON VIEW template_version_with_user IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000164_archive_template_versions.up.sql b/coderd/database/migrations/000164_archive_template_versions.up.sql index d18d4cdfe47e4..0be61ebde0547 100644 --- a/coderd/database/migrations/000164_archive_template_versions.up.sql +++ b/coderd/database/migrations/000164_archive_template_versions.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- The view will be rebuilt with the new column DROP VIEW template_version_with_user; @@ -23,5 +21,3 @@ FROM template_versions.created_by = visible_users.id; COMMENT ON VIEW template_version_with_user IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000165_prevent_autostart_days.down.sql b/coderd/database/migrations/000165_prevent_autostart_days.down.sql index c4f3351eeafa2..698b6edfeab6b 100644 --- a/coderd/database/migrations/000165_prevent_autostart_days.down.sql +++ b/coderd/database/migrations/000165_prevent_autostart_days.down.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP VIEW template_with_users; ALTER TABLE templates @@ -21,5 +19,3 @@ FROM templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000165_prevent_autostart_days.up.sql b/coderd/database/migrations/000165_prevent_autostart_days.up.sql index 79c2c2a5e5c22..3302b4f4910d1 100644 --- a/coderd/database/migrations/000165_prevent_autostart_days.up.sql +++ b/coderd/database/migrations/000165_prevent_autostart_days.up.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP VIEW template_with_users; ALTER TABLE templates @@ -23,5 +21,3 @@ FROM templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000166_template_active_version.down.sql b/coderd/database/migrations/000166_template_active_version.down.sql index d3b4bba305e02..21c2cfd026f61 100644 --- a/coderd/database/migrations/000166_template_active_version.down.sql +++ b/coderd/database/migrations/000166_template_active_version.down.sql @@ -1,5 +1,3 @@ -BEGIN; - -- Update the template_with_users view; DROP VIEW template_with_users; @@ -21,5 +19,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000166_template_active_version.up.sql b/coderd/database/migrations/000166_template_active_version.up.sql index a9255505eede7..726a72cf24d1b 100644 --- a/coderd/database/migrations/000166_template_active_version.up.sql +++ b/coderd/database/migrations/000166_template_active_version.up.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP VIEW template_with_users; ALTER TABLE templates ADD COLUMN require_active_version boolean NOT NULL DEFAULT 'f'; @@ -19,5 +17,3 @@ AS templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000167_workspace_agent_api_version.down.sql b/coderd/database/migrations/000167_workspace_agent_api_version.down.sql index 53c2b3417a983..1d2e66159616f 100644 --- a/coderd/database/migrations/000167_workspace_agent_api_version.down.sql +++ b/coderd/database/migrations/000167_workspace_agent_api_version.down.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE workspace_agents DROP COLUMN api_version; -COMMIT; diff --git a/coderd/database/migrations/000167_workspace_agent_api_version.up.sql b/coderd/database/migrations/000167_workspace_agent_api_version.up.sql index b4187e51efa93..143ac0f5c71f1 100644 --- a/coderd/database/migrations/000167_workspace_agent_api_version.up.sql +++ b/coderd/database/migrations/000167_workspace_agent_api_version.up.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE workspace_agents ADD COLUMN api_version TEXT DEFAULT '' NOT NULL; -COMMIT; diff --git a/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.down.sql b/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.down.sql index 084c00e922986..6b8c844157452 100644 --- a/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.down.sql +++ b/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.down.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP TRIGGER IF EXISTS tailnet_notify_tunnel_change ON tailnet_tunnels; DROP FUNCTION IF EXISTS tailnet_notify_tunnel_change; DROP TABLE IF EXISTS tailnet_tunnels; @@ -10,5 +8,3 @@ DROP INDEX IF EXISTS idx_tailnet_peers_coordinator; DROP TABLE IF EXISTS tailnet_peers; DROP TYPE IF EXISTS tailnet_status; - -COMMIT; diff --git a/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.up.sql b/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.up.sql index 9839c59a3350f..6d9029543a73f 100644 --- a/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.up.sql +++ b/coderd/database/migrations/000168_pg_coord_tailnet_v2_api.up.sql @@ -1,5 +1,3 @@ -BEGIN; - CREATE TYPE tailnet_status AS ENUM ( 'ok', 'lost' @@ -68,5 +66,3 @@ CREATE TRIGGER tailnet_notify_tunnel_change AFTER INSERT OR UPDATE OR DELETE ON tailnet_tunnels FOR EACH ROW EXECUTE PROCEDURE tailnet_notify_tunnel_change(); - -COMMIT; diff --git a/coderd/database/migrations/000169_deprecate_template.down.sql b/coderd/database/migrations/000169_deprecate_template.down.sql index 3d944135ae30b..c7e719efae567 100644 --- a/coderd/database/migrations/000169_deprecate_template.down.sql +++ b/coderd/database/migrations/000169_deprecate_template.down.sql @@ -1,5 +1,3 @@ -BEGIN; - DROP VIEW template_with_users; ALTER TABLE templates @@ -20,5 +18,3 @@ FROM templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000169_deprecate_template.up.sql b/coderd/database/migrations/000169_deprecate_template.up.sql index 3e688a6bef2a1..d09e1a3d77d69 100644 --- a/coderd/database/migrations/000169_deprecate_template.up.sql +++ b/coderd/database/migrations/000169_deprecate_template.up.sql @@ -1,5 +1,3 @@ -BEGIN; - -- The view will be rebuilt with the new column DROP VIEW template_with_users; @@ -24,5 +22,3 @@ FROM templates.created_by = visible_users.id; COMMENT ON VIEW template_with_users IS 'Joins in the username + avatar url of the created by user.'; - -COMMIT; diff --git a/coderd/database/migrations/000170_workspaceproxy_version.down.sql b/coderd/database/migrations/000170_workspaceproxy_version.down.sql index 55810705b0152..7e5afd3441664 100644 --- a/coderd/database/migrations/000170_workspaceproxy_version.down.sql +++ b/coderd/database/migrations/000170_workspaceproxy_version.down.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE workspace_proxies DROP COLUMN version; -COMMIT; diff --git a/coderd/database/migrations/000170_workspaceproxy_version.up.sql b/coderd/database/migrations/000170_workspaceproxy_version.up.sql index a7e3519126c2e..6d88df8c08dcd 100644 --- a/coderd/database/migrations/000170_workspaceproxy_version.up.sql +++ b/coderd/database/migrations/000170_workspaceproxy_version.up.sql @@ -1,3 +1 @@ -BEGIN; ALTER TABLE workspace_proxies ADD COLUMN version TEXT DEFAULT ''::TEXT NOT NULL; -COMMIT; diff --git a/coderd/database/migrations/000171_oidc_debug_claims.down.sql b/coderd/database/migrations/000171_oidc_debug_claims.down.sql index 60952cd3513eb..de9d3b72b2885 100644 --- a/coderd/database/migrations/000171_oidc_debug_claims.down.sql +++ b/coderd/database/migrations/000171_oidc_debug_claims.down.sql @@ -1,5 +1 @@ -BEGIN; - ALTER TABLE user_links DROP COLUMN debug_context; - -COMMIT; diff --git a/coderd/database/migrations/000171_oidc_debug_claims.up.sql b/coderd/database/migrations/000171_oidc_debug_claims.up.sql index c46be457678a0..81afead9b2ac2 100644 --- a/coderd/database/migrations/000171_oidc_debug_claims.up.sql +++ b/coderd/database/migrations/000171_oidc_debug_claims.up.sql @@ -1,6 +1,2 @@ -BEGIN; - ALTER TABLE user_links ADD COLUMN debug_context jsonb DEFAULT '{}' NOT NULL; COMMENT ON COLUMN user_links.debug_context IS 'Debug information includes information like id_token and userinfo claims.'; - -COMMIT; diff --git a/coderd/database/migrations/create_migration.sh b/coderd/database/migrations/create_migration.sh index 3046e875e3b9d..a9aa4b78ce5fa 100755 --- a/coderd/database/migrations/create_migration.sh +++ b/coderd/database/migrations/create_migration.sh @@ -7,6 +7,17 @@ set -euo pipefail +cat << EOF + +WARNING: Migrations now all run in a single transaction. This makes upgrades +safer, but means that 'ALTER TYPE resource_type ADD VALUE' cannot be used if the +enum value needs to be referenced in another migration. + +This also means you should not use "BEGIN;" and "COMMIT;" in your migrations, as +everything is already in a migration. + +EOF + SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") ( cd "$SCRIPT_DIR" diff --git a/coderd/database/migrations/migrate.go b/coderd/database/migrations/migrate.go index ab07f83ffed9c..adcb1d4c220b3 100644 --- a/coderd/database/migrations/migrate.go +++ b/coderd/database/migrations/migrate.go @@ -9,7 +9,6 @@ import ( "os" "github.com/golang-migrate/migrate/v4" - "github.com/golang-migrate/migrate/v4/database/postgres" "github.com/golang-migrate/migrate/v4/source" "github.com/golang-migrate/migrate/v4/source/iofs" "golang.org/x/xerrors" @@ -30,20 +29,13 @@ func setup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { row := db.QueryRowContext(ctx, "SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = 'migration_cursor';") var v1Exists int if row.Scan(&v1Exists) == nil { - return nil, nil, xerrors.Errorf("currently connected to a Coder v1 database, aborting database setup") + return nil, nil, xerrors.New("currently connected to a Coder v1 database, aborting database setup") } - // there is a postgres.WithInstance() method that takes the DB instance, - // but, when you close the resulting Migrate, it closes the DB, which - // we don't want. Instead, create just a connection that will get closed - // when migration is done. - conn, err := db.Conn(ctx) + dbDriver := &pgTxnDriver{ctx: context.Background(), db: db} + err = dbDriver.ensureVersionTable() if err != nil { - return nil, nil, xerrors.Errorf("postgres connection: %w", err) - } - dbDriver, err := postgres.WithConnection(ctx, conn, &postgres.Config{}) - if err != nil { - return nil, nil, xerrors.Errorf("wrap postgres connection: %w", err) + return nil, nil, xerrors.Errorf("ensure version table: %w", err) } m, err := migrate.NewWithInstance("", sourceDriver, "", dbDriver) @@ -56,7 +48,7 @@ func setup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { // Up runs SQL migrations to ensure the database schema is up-to-date. func Up(db *sql.DB) (retErr error) { - _, m, err := betterSetup(db) + _, m, err := setup(db) if err != nil { return xerrors.Errorf("migrate setup: %w", err) } @@ -87,7 +79,7 @@ func Up(db *sql.DB) (retErr error) { // Down runs all down SQL migrations. func Down(db *sql.DB) error { - _, m, err := betterSetup(db) + _, m, err := setup(db) if err != nil { return xerrors.Errorf("migrate setup: %w", err) } @@ -109,7 +101,7 @@ func Down(db *sql.DB) error { // applied, without making any changes to the database. If not, returns a // non-nil error. func EnsureClean(db *sql.DB) error { - sourceDriver, m, err := betterSetup(db) + sourceDriver, m, err := setup(db) if err != nil { return xerrors.Errorf("migrate setup: %w", err) } @@ -175,7 +167,7 @@ func CheckLatestVersion(sourceDriver source.Driver, currentVersion uint) error { // Stepper cannot be closed pre-emptively, it must be run to completion // (or until an error is encountered). func Stepper(db *sql.DB) (next func() (version uint, more bool, err error), err error) { - _, m, err := betterSetup(db) + _, m, err := setup(db) if err != nil { return nil, xerrors.Errorf("migrate setup: %w", err) } diff --git a/coderd/database/migrations/txnmigrator.go b/coderd/database/migrations/txnmigrator.go index b1e4bab0abcf4..b1cdcd496994b 100644 --- a/coderd/database/migrations/txnmigrator.go +++ b/coderd/database/migrations/txnmigrator.go @@ -8,10 +8,7 @@ import ( "io" "strings" - "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database" - "github.com/golang-migrate/migrate/v4/source" - "github.com/golang-migrate/migrate/v4/source/iofs" "github.com/lib/pq" "golang.org/x/xerrors" ) @@ -21,35 +18,6 @@ const ( migrationsTableName = "schema_migrations" ) -func betterSetup(db *sql.DB) (source.Driver, *migrate.Migrate, error) { - ctx := context.Background() - sourceDriver, err := iofs.New(migrations, ".") - if err != nil { - return nil, nil, xerrors.Errorf("create iofs: %w", err) - } - - // migration_cursor is a v1 migration table. If this exists, we're on v1. - // Do no run v2 migrations on a v1 database! - row := db.QueryRowContext(ctx, "SELECT 1 FROM information_schema.tables WHERE table_schema = current_schema() AND table_name = 'migration_cursor';") - var v1Exists int - if row.Scan(&v1Exists) == nil { - return nil, nil, xerrors.New("currently connected to a Coder v1 database, aborting database setup") - } - - dbDriver := &pgTxnDriver{ctx: context.Background(), db: db} - err = dbDriver.ensureVersionTable() - if err != nil { - return nil, nil, xerrors.Errorf("ensure version table: %w", err) - } - - m, err := migrate.NewWithInstance("", sourceDriver, "", dbDriver) - if err != nil { - return nil, nil, xerrors.Errorf("new migrate instance: %w", err) - } - - return sourceDriver, m, nil -} - type pgTxnDriver struct { ctx context.Context db *sql.DB From 785ff59a713bd75849fde67f032c625d6002230c Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Thu, 30 Nov 2023 23:08:29 +0000 Subject: [PATCH 3/9] lint --- coderd/database/migrations/txnmigrator.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/coderd/database/migrations/txnmigrator.go b/coderd/database/migrations/txnmigrator.go index b1cdcd496994b..34b9786887e86 100644 --- a/coderd/database/migrations/txnmigrator.go +++ b/coderd/database/migrations/txnmigrator.go @@ -24,11 +24,11 @@ type pgTxnDriver struct { tx *sql.Tx } -func (d *pgTxnDriver) Open(url string) (database.Driver, error) { +func (*pgTxnDriver) Open(string) (database.Driver, error) { panic("not implemented") } -func (d *pgTxnDriver) Close() error { +func (*pgTxnDriver) Close() error { return nil } @@ -81,7 +81,7 @@ func (d *pgTxnDriver) runStatement(statement []byte) error { return nil } if _, err := d.tx.ExecContext(ctx, query); err != nil { - if pgErr, ok := err.(*pq.Error); ok { + if pgErr, ok := err.(*pq.Error); ok { //nolint var line uint message := fmt.Sprintf("migration failed: %s", pgErr.Message) if pgErr.Detail != "" { @@ -126,7 +126,7 @@ func (d *pgTxnDriver) Version() (version int, dirty bool, err error) { return database.NilVersion, false, nil case err != nil: - if e, ok := err.(*pq.Error); ok { + if e, ok := err.(*pq.Error); ok { //nolint if e.Code.Name() == "undefined_table" { return database.NilVersion, false, nil } @@ -138,7 +138,7 @@ func (d *pgTxnDriver) Version() (version int, dirty bool, err error) { } } -func (d *pgTxnDriver) Drop() error { +func (*pgTxnDriver) Drop() error { panic("not implemented") } From 9a87f47d0e2dc0331c42a8d165b15f61ce4634c9 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Fri, 1 Dec 2023 05:29:56 +0000 Subject: [PATCH 4/9] fmt --- coderd/database/migrations/create_migration.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/migrations/create_migration.sh b/coderd/database/migrations/create_migration.sh index a9aa4b78ce5fa..2be2aca9068ff 100755 --- a/coderd/database/migrations/create_migration.sh +++ b/coderd/database/migrations/create_migration.sh @@ -7,7 +7,7 @@ set -euo pipefail -cat << EOF +cat < Date: Fri, 1 Dec 2023 05:36:01 +0000 Subject: [PATCH 5/9] revert --- coderd/database/postgres/postgres.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coderd/database/postgres/postgres.go b/coderd/database/postgres/postgres.go index 94524cbdc35d5..8a7d0209ba4e0 100644 --- a/coderd/database/postgres/postgres.go +++ b/coderd/database/postgres/postgres.go @@ -36,7 +36,7 @@ func Open() (string, func(), error) { } dbName = "ci" + dbName - _, err = db.Exec("CREATE DATABASE " + dbName) + _, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM")) if err != nil { return "", nil, xerrors.Errorf("create db with template: %w", err) } From 307b99938bd23ee5f4899ace894d6fff5a113cef Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Fri, 1 Dec 2023 05:48:22 +0000 Subject: [PATCH 6/9] add comments --- coderd/database/migrations/txnmigrator.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/coderd/database/migrations/txnmigrator.go b/coderd/database/migrations/txnmigrator.go index 34b9786887e86..73c46aa388e92 100644 --- a/coderd/database/migrations/txnmigrator.go +++ b/coderd/database/migrations/txnmigrator.go @@ -1,7 +1,6 @@ package migrations import ( - "bytes" "context" "database/sql" "fmt" @@ -18,6 +17,10 @@ const ( migrationsTableName = "schema_migrations" ) +// pgTxnDriver is a Postgres migration driver that runs all migrations in a +// single transaction. This is done to prevent users from being locked out of +// their deployment if a migration fails, since the schema will simply revert +// back to the previous version. type pgTxnDriver struct { ctx context.Context db *sql.DB @@ -59,14 +62,11 @@ func (d *pgTxnDriver) Unlock() error { return nil } -// Run applies a migration to the database. migration is guaranteed to be not nil. func (d *pgTxnDriver) Run(migration io.Reader) error { migr, err := io.ReadAll(migration) if err != nil { return xerrors.Errorf("read migration: %w", err) } - migr = bytes.ReplaceAll(migr, []byte("BEGIN;"), []byte{}) - migr = bytes.ReplaceAll(migr, []byte("COMMIT;"), []byte{}) err = d.runStatement(migr) if err != nil { return xerrors.Errorf("run statement: %w", err) @@ -81,11 +81,12 @@ func (d *pgTxnDriver) runStatement(statement []byte) error { return nil } if _, err := d.tx.ExecContext(ctx, query); err != nil { - if pgErr, ok := err.(*pq.Error); ok { //nolint + var pgErr *pq.Error + if xerrors.As(err, &pgErr) { var line uint message := fmt.Sprintf("migration failed: %s", pgErr.Message) if pgErr.Detail != "" { - message = fmt.Sprintf("%s, %s", message, pgErr.Detail) + message += ", " + pgErr.Detail } return database.Error{OrigErr: err, Err: message, Query: statement, Line: line} } @@ -112,9 +113,13 @@ func (d *pgTxnDriver) SetVersion(version int, dirty bool) error { } func (d *pgTxnDriver) Version() (version int, dirty bool, err error) { + // If the transaction is valid (we hold the exclusive lock), use the txn for + // the query. var q interface { QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row } = d.tx + // If we don't hold the lock just use the database. This only happens in the + // `Stepper` function and is only used in tests. if d.tx == nil { q = d.db } @@ -126,8 +131,9 @@ func (d *pgTxnDriver) Version() (version int, dirty bool, err error) { return database.NilVersion, false, nil case err != nil: - if e, ok := err.(*pq.Error); ok { //nolint - if e.Code.Name() == "undefined_table" { + var pgErr *pq.Error + if xerrors.As(err, &pgErr) { + if pgErr.Code.Name() == "undefined_table" { return database.NilVersion, false, nil } } From 0cb53fd621bc68b3725b98b929747cc7c53d1de2 Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Fri, 1 Dec 2023 05:56:29 +0000 Subject: [PATCH 7/9] hold migration lock on migration table creation --- coderd/database/migrations/txnmigrator.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/coderd/database/migrations/txnmigrator.go b/coderd/database/migrations/txnmigrator.go index 73c46aa388e92..c284136192c8a 100644 --- a/coderd/database/migrations/txnmigrator.go +++ b/coderd/database/migrations/txnmigrator.go @@ -149,10 +149,20 @@ func (*pgTxnDriver) Drop() error { } func (d *pgTxnDriver) ensureVersionTable() error { + err := d.Lock() + if err != nil { + return xerrors.Errorf("acquire migration lock: %w", err) + } + const query = `CREATE TABLE IF NOT EXISTS ` + migrationsTableName + ` (version bigint not null primary key, dirty boolean not null)` - if _, err := d.db.ExecContext(context.Background(), query); err != nil { + if _, err := d.tx.ExecContext(context.Background(), query); err != nil { return &database.Error{OrigErr: err, Query: []byte(query)} } + err = d.Unlock() + if err != nil { + return xerrors.Errorf("release migration lock: %w", err) + } + return nil } From 480b99d2630978b354a57572152fa3286893923f Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Fri, 1 Dec 2023 21:17:39 +0000 Subject: [PATCH 8/9] remove comment --- coderd/database/migrations/000057_api_key_token.up.sql | 2 -- 1 file changed, 2 deletions(-) diff --git a/coderd/database/migrations/000057_api_key_token.up.sql b/coderd/database/migrations/000057_api_key_token.up.sql index 1537154819eb7..5593baabdb650 100644 --- a/coderd/database/migrations/000057_api_key_token.up.sql +++ b/coderd/database/migrations/000057_api_key_token.up.sql @@ -1,5 +1,3 @@ --- ALTER TYPE login_type ADD VALUE IF NOT EXISTS 'token'; - CREATE TYPE new_logintype AS ENUM ( 'password', 'github', From 3ca5ebf4155d4e8d96404f3863fc17e55aeb5abe Mon Sep 17 00:00:00 2001 From: Colin Adler Date: Fri, 1 Dec 2023 21:31:41 +0000 Subject: [PATCH 9/9] document the correct way to create a new enum value --- coderd/database/migrations/create_migration.sh | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/coderd/database/migrations/create_migration.sh b/coderd/database/migrations/create_migration.sh index 2be2aca9068ff..aac112c4292ca 100755 --- a/coderd/database/migrations/create_migration.sh +++ b/coderd/database/migrations/create_migration.sh @@ -16,6 +16,23 @@ enum value needs to be referenced in another migration. This also means you should not use "BEGIN;" and "COMMIT;" in your migrations, as everything is already in a migration. +An example way of the proper way to add an enum value: + +CREATE TYPE new_logintype AS ENUM ( + 'password', + 'github', + 'oidc', + 'token' -- this is our new value +); + +ALTER TABLE users + ALTER COLUMN login_type DROP DEFAULT, -- if the column has a default, it must be dropped first + ALTER COLUMN login_type TYPE new_logintype USING (login_type::text::new_logintype), -- converts the old enum until the new enum using text as an intermediary + ALTER COLUMN login_type SET DEFAULT 'password'::new_logintype; -- re-add the default using the new enum + +DROP TYPE login_type; +ALTER TYPE new_logintype RENAME TO login_type; + EOF SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")