Skip to content

Commit

Permalink
Fix nullable descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEssem committed Dec 18, 2024
1 parent 645cbdb commit ea1fe21
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@
# requested_review_at :datetime
# indexable :boolean default(FALSE), not null
# attribution_domains :string default([]), is an Array
# avatar_description :text
# header_description :text
# avatar_description :text default(""), not null
# header_description :text default(""), not null
#

class Account < ApplicationRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class ChangeAvatarAndHeaderDescriptionNonNullable < ActiveRecord::Migration[7.2]
def change
change_column_default :accounts, :avatar_description, from: nil, to: ''
add_check_constraint :accounts, 'avatar_description IS NOT NULL', name: 'accounts_avatar_description_null', validate: false

change_column_default :accounts, :header_description, from: nil, to: ''
add_check_constraint :accounts, 'header_description IS NOT NULL', name: 'accounts_header_description_null', validate: false

safety_assured do
execute "UPDATE accounts SET avatar_description = COALESCE(avatar_description, ''), header_description = COALESCE(header_description, '') WHERE avatar_description IS NULL OR header_description IS NULL"
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class ValidateChangeAvatarDescriptionNonNullable < ActiveRecord::Migration[7.2]
def up
validate_check_constraint :accounts, name: 'accounts_avatar_description_null'
change_column_null :accounts, :avatar_description, false
remove_check_constraint :accounts, name: 'accounts_avatar_description_null'
end

def down
add_check_constraint :accounts, 'avatar_description IS NOT NULL', name: 'accounts_avatar_description_null', validate: false
change_column_null :accounts, :avatar_description, true
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

class ValidateChangeHeaderDescriptionNonNullable < ActiveRecord::Migration[7.2]
def up
validate_check_constraint :accounts, name: 'accounts_header_description_null'
change_column_null :accounts, :header_description, false
remove_check_constraint :accounts, name: 'accounts_header_description_null'
end

def down
add_check_constraint :accounts, 'header_description IS NOT NULL', name: 'accounts_header_description_null', validate: false
change_column_null :accounts, :header_description, true
end
end
6 changes: 3 additions & 3 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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_16_214211) do
ActiveRecord::Schema[7.2].define(version: 2024_12_18_050032) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -198,8 +198,8 @@
t.datetime "requested_review_at", precision: nil
t.boolean "indexable", default: false, null: false
t.string "attribution_domains", default: [], array: true
t.text "avatar_description"
t.text "header_description"
t.text "avatar_description", default: "", null: false
t.text "header_description", default: "", null: false
t.index "(((setweight(to_tsvector('simple'::regconfig, (display_name)::text), 'A'::\"char\") || setweight(to_tsvector('simple'::regconfig, (username)::text), 'B'::\"char\")) || setweight(to_tsvector('simple'::regconfig, (COALESCE(domain, ''::character varying))::text), 'C'::\"char\")))", name: "search_index", using: :gin
t.index "lower((username)::text), COALESCE(lower((domain)::text), ''::text)", name: "index_accounts_on_username_and_domain_lower", unique: true
t.index ["domain", "id"], name: "index_accounts_on_domain_and_id"
Expand Down

0 comments on commit ea1fe21

Please sign in to comment.