diff --git a/db/post_migrate/20240327145507_backfill_account_deleted_at.rb b/db/post_migrate/20240327145507_backfill_account_deleted_at.rb new file mode 100644 index 00000000000..f5bd5fc20fe --- /dev/null +++ b/db/post_migrate/20240327145507_backfill_account_deleted_at.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class BackfillAccountDeletedAt < ActiveRecord::Migration[6.1] + def up + safety_assured do + execute 'UPDATE accounts SET deleted_at = suspended_at, suspended_at = NULL WHERE domain IS NULL AND suspended_at IS NOT NULL AND NOT EXISTS (SELECT 1 FROM users WHERE account_id = accounts.id)' + end + end + + def down + safety_assured do + 'UPDATE accounts SET suspended_at = deleted_at WHERE domain is NULL AND suspended_at IS NULL AND deleted_at IS NOT NULL' + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b05fb61be1c..7d21d3abca0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_03_27_145403) do +ActiveRecord::Schema[7.1].define(version: 2024_03_27_145507) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql"