From cf4595967b1167491bca31cedc21213b458fbe1b Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Mon, 16 Dec 2024 03:20:08 -0500 Subject: [PATCH] Add `NOT NULL` requirement to columns on `account_conversations` (#33308) --- app/models/account_conversation.rb | 8 ++++---- ..._to_account_conversation_account_column.rb | 7 +++++++ ..._to_account_conversation_account_column.rb | 19 +++++++++++++++++++ ...ccount_conversation_conversation_column.rb | 7 +++++++ ...ccount_conversation_conversation_column.rb | 19 +++++++++++++++++++ db/schema.rb | 6 +++--- 6 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 db/migrate/20241213170027_add_not_null_to_account_conversation_account_column.rb create mode 100644 db/migrate/20241213170036_validate_not_null_to_account_conversation_account_column.rb create mode 100644 db/migrate/20241213170043_add_not_null_to_account_conversation_conversation_column.rb create mode 100644 db/migrate/20241213170053_validate_not_null_to_account_conversation_conversation_column.rb diff --git a/app/models/account_conversation.rb b/app/models/account_conversation.rb index 25a75d8a61..ccf8f36966 100644 --- a/app/models/account_conversation.rb +++ b/app/models/account_conversation.rb @@ -5,13 +5,13 @@ # Table name: account_conversations # # id :bigint(8) not null, primary key -# account_id :bigint(8) -# conversation_id :bigint(8) +# lock_version :integer default(0), not null # participant_account_ids :bigint(8) default([]), not null, is an Array # status_ids :bigint(8) default([]), not null, is an Array -# last_status_id :bigint(8) -# lock_version :integer default(0), not null # unread :boolean default(FALSE), not null +# account_id :bigint(8) not null +# conversation_id :bigint(8) not null +# last_status_id :bigint(8) # class AccountConversation < ApplicationRecord diff --git a/db/migrate/20241213170027_add_not_null_to_account_conversation_account_column.rb b/db/migrate/20241213170027_add_not_null_to_account_conversation_account_column.rb new file mode 100644 index 0000000000..5bfa55bcc6 --- /dev/null +++ b/db/migrate/20241213170027_add_not_null_to_account_conversation_account_column.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddNotNullToAccountConversationAccountColumn < ActiveRecord::Migration[7.2] + def change + add_check_constraint :account_conversations, 'account_id IS NOT NULL', name: 'account_conversations_account_id_null', validate: false + end +end diff --git a/db/migrate/20241213170036_validate_not_null_to_account_conversation_account_column.rb b/db/migrate/20241213170036_validate_not_null_to_account_conversation_account_column.rb new file mode 100644 index 0000000000..0186559dc3 --- /dev/null +++ b/db/migrate/20241213170036_validate_not_null_to_account_conversation_account_column.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ValidateNotNullToAccountConversationAccountColumn < ActiveRecord::Migration[7.2] + def up + connection.execute(<<~SQL.squish) + DELETE FROM account_conversations + WHERE account_id IS NULL + SQL + + validate_check_constraint :account_conversations, name: 'account_conversations_account_id_null' + change_column_null :account_conversations, :account_id, false + remove_check_constraint :account_conversations, name: 'account_conversations_account_id_null' + end + + def down + add_check_constraint :account_conversations, 'account_id IS NOT NULL', name: 'account_conversations_account_id_null', validate: false + change_column_null :account_conversations, :account_id, true + end +end diff --git a/db/migrate/20241213170043_add_not_null_to_account_conversation_conversation_column.rb b/db/migrate/20241213170043_add_not_null_to_account_conversation_conversation_column.rb new file mode 100644 index 0000000000..e253a1b3e1 --- /dev/null +++ b/db/migrate/20241213170043_add_not_null_to_account_conversation_conversation_column.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddNotNullToAccountConversationConversationColumn < ActiveRecord::Migration[7.2] + def change + add_check_constraint :account_conversations, 'conversation_id IS NOT NULL', name: 'account_conversations_conversation_id_null', validate: false + end +end diff --git a/db/migrate/20241213170053_validate_not_null_to_account_conversation_conversation_column.rb b/db/migrate/20241213170053_validate_not_null_to_account_conversation_conversation_column.rb new file mode 100644 index 0000000000..324e9180e2 --- /dev/null +++ b/db/migrate/20241213170053_validate_not_null_to_account_conversation_conversation_column.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ValidateNotNullToAccountConversationConversationColumn < ActiveRecord::Migration[7.2] + def up + connection.execute(<<~SQL.squish) + DELETE FROM account_conversations + WHERE conversation_id IS NULL + SQL + + validate_check_constraint :account_conversations, name: 'account_conversations_conversation_id_null' + change_column_null :account_conversations, :conversation_id, false + remove_check_constraint :account_conversations, name: 'account_conversations_conversation_id_null' + end + + def down + add_check_constraint :account_conversations, 'conversation_id IS NOT NULL', name: 'account_conversations_conversation_id_null', validate: false + change_column_null :account_conversations, :conversation_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 2a7684d5df..eb203039d5 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_12_154346) do +ActiveRecord::Schema[7.2].define(version: 2024_12_13_170053) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -24,8 +24,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_12_154346) do end create_table "account_conversations", force: :cascade do |t| - t.bigint "account_id" - t.bigint "conversation_id" + t.bigint "account_id", null: false + t.bigint "conversation_id", null: false t.bigint "participant_account_ids", default: [], null: false, array: true t.bigint "status_ids", default: [], null: false, array: true t.bigint "last_status_id"