From b648c64e2ecc3b56f977a20bf725ecef1b7881dd Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 20 Dec 2024 09:33:48 -0500 Subject: [PATCH] Add `NOT NULL` requirement to columns on `polls` (#33374) --- app/models/poll.rb | 16 ++++++++-------- ...507_add_not_null_to_poll_account_column.rb | 7 +++++++ ...alidate_not_null_to_poll_account_column.rb | 19 +++++++++++++++++++ ...4520_add_not_null_to_poll_status_column.rb | 7 +++++++ ...validate_not_null_to_poll_status_column.rb | 19 +++++++++++++++++++ db/schema.rb | 4 ++-- 6 files changed, 62 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20241216224507_add_not_null_to_poll_account_column.rb create mode 100644 db/migrate/20241216224514_validate_not_null_to_poll_account_column.rb create mode 100644 db/migrate/20241216224520_add_not_null_to_poll_status_column.rb create mode 100644 db/migrate/20241216224530_validate_not_null_to_poll_status_column.rb diff --git a/app/models/poll.rb b/app/models/poll.rb index 4844d2d23c..c2aa4d0172 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -5,19 +5,19 @@ # Table name: polls # # id :bigint(8) not null, primary key -# account_id :bigint(8) -# status_id :bigint(8) -# expires_at :datetime -# options :string default([]), not null, is an Array # cached_tallies :bigint(8) default([]), not null, is an Array -# multiple :boolean default(FALSE), not null +# expires_at :datetime # hide_totals :boolean default(FALSE), not null -# votes_count :bigint(8) default(0), not null # last_fetched_at :datetime +# lock_version :integer default(0), not null +# multiple :boolean default(FALSE), not null +# options :string default([]), not null, is an Array +# voters_count :bigint(8) +# votes_count :bigint(8) default(0), not null # created_at :datetime not null # updated_at :datetime not null -# lock_version :integer default(0), not null -# voters_count :bigint(8) +# account_id :bigint(8) not null +# status_id :bigint(8) not null # class Poll < ApplicationRecord diff --git a/db/migrate/20241216224507_add_not_null_to_poll_account_column.rb b/db/migrate/20241216224507_add_not_null_to_poll_account_column.rb new file mode 100644 index 0000000000..9ff5c2d8f3 --- /dev/null +++ b/db/migrate/20241216224507_add_not_null_to_poll_account_column.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddNotNullToPollAccountColumn < ActiveRecord::Migration[7.2] + def change + add_check_constraint :polls, 'account_id IS NOT NULL', name: 'polls_account_id_null', validate: false + end +end diff --git a/db/migrate/20241216224514_validate_not_null_to_poll_account_column.rb b/db/migrate/20241216224514_validate_not_null_to_poll_account_column.rb new file mode 100644 index 0000000000..91e835359c --- /dev/null +++ b/db/migrate/20241216224514_validate_not_null_to_poll_account_column.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ValidateNotNullToPollAccountColumn < ActiveRecord::Migration[7.2] + def up + connection.execute(<<~SQL.squish) + DELETE FROM polls + WHERE account_id IS NULL + SQL + + validate_check_constraint :polls, name: 'polls_account_id_null' + change_column_null :polls, :account_id, false + remove_check_constraint :polls, name: 'polls_account_id_null' + end + + def down + add_check_constraint :polls, 'account_id IS NOT NULL', name: 'polls_account_id_null', validate: false + change_column_null :polls, :account_id, true + end +end diff --git a/db/migrate/20241216224520_add_not_null_to_poll_status_column.rb b/db/migrate/20241216224520_add_not_null_to_poll_status_column.rb new file mode 100644 index 0000000000..5c638c6af0 --- /dev/null +++ b/db/migrate/20241216224520_add_not_null_to_poll_status_column.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class AddNotNullToPollStatusColumn < ActiveRecord::Migration[7.2] + def change + add_check_constraint :polls, 'status_id IS NOT NULL', name: 'polls_status_id_null', validate: false + end +end diff --git a/db/migrate/20241216224530_validate_not_null_to_poll_status_column.rb b/db/migrate/20241216224530_validate_not_null_to_poll_status_column.rb new file mode 100644 index 0000000000..b5985ce062 --- /dev/null +++ b/db/migrate/20241216224530_validate_not_null_to_poll_status_column.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ValidateNotNullToPollStatusColumn < ActiveRecord::Migration[7.2] + def up + connection.execute(<<~SQL.squish) + DELETE FROM polls + WHERE status_id IS NULL + SQL + + validate_check_constraint :polls, name: 'polls_status_id_null' + change_column_null :polls, :status_id, false + remove_check_constraint :polls, name: 'polls_status_id_null' + end + + def down + add_check_constraint :polls, 'status_id IS NOT NULL', name: 'polls_status_id_null', validate: false + change_column_null :polls, :status_id, true + end +end diff --git a/db/schema.rb b/db/schema.rb index 87d42a6051..49b10cb7bd 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -778,8 +778,8 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_16_224825) do end create_table "polls", force: :cascade do |t| - t.bigint "account_id" - t.bigint "status_id" + t.bigint "account_id", null: false + t.bigint "status_id", null: false t.datetime "expires_at", precision: nil t.string "options", default: [], null: false, array: true t.bigint "cached_tallies", default: [], null: false, array: true