From e4e35ab1348234d3910f8f24f0c3e67c303f3967 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 10 Dec 2024 10:50:49 -0500 Subject: [PATCH] Add NOT NULL requirement to account columns on `AccountPin` (#33244) --- app/models/account_pin.rb | 4 ++-- ...not_null_to_account_pin_account_columns.rb | 23 +++++++++++++++++++ db/schema.rb | 6 ++--- 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20241210140838_add_not_null_to_account_pin_account_columns.rb diff --git a/app/models/account_pin.rb b/app/models/account_pin.rb index 2dc32809b8..dc05d3cd25 100644 --- a/app/models/account_pin.rb +++ b/app/models/account_pin.rb @@ -5,10 +5,10 @@ # Table name: account_pins # # id :bigint(8) not null, primary key -# account_id :bigint(8) -# target_account_id :bigint(8) # created_at :datetime not null # updated_at :datetime not null +# account_id :bigint(8) not null +# target_account_id :bigint(8) not null # class AccountPin < ApplicationRecord diff --git a/db/migrate/20241210140838_add_not_null_to_account_pin_account_columns.rb b/db/migrate/20241210140838_add_not_null_to_account_pin_account_columns.rb new file mode 100644 index 0000000000..69b5b4a025 --- /dev/null +++ b/db/migrate/20241210140838_add_not_null_to_account_pin_account_columns.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +class AddNotNullToAccountPinAccountColumns < ActiveRecord::Migration[7.2] + def up + connection.execute(<<~SQL.squish) + DELETE FROM account_pins + WHERE account_id IS NULL + OR target_account_id IS NULL + SQL + + safety_assured do + change_column_null :account_pins, :account_id, false + change_column_null :account_pins, :target_account_id, false + end + end + + def down + safety_assured do + change_column_null :account_pins, :account_id, true + change_column_null :account_pins, :target_account_id, true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index d28bc5efca..f7f6cf63b3 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.2].define(version: 2024_12_05_163118) do +ActiveRecord::Schema[7.2].define(version: 2024_12_10_140838) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -82,8 +82,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_05_163118) do end create_table "account_pins", force: :cascade do |t| - t.bigint "account_id" - t.bigint "target_account_id" + t.bigint "account_id", null: false + t.bigint "target_account_id", null: false t.datetime "created_at", precision: nil, null: false t.datetime "updated_at", precision: nil, null: false t.index ["account_id", "target_account_id"], name: "index_account_pins_on_account_id_and_target_account_id", unique: true