Add NOT NULL requirement to columns on account_conversations (#33308)

This commit is contained in:
Matt Jankowski 2024-12-16 03:20:08 -05:00 committed by GitHub
parent a596f3479b
commit cf4595967b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 59 additions and 7 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"