discourse/db/migrate/20241224191732_change_full_name_required_setting.rb
Martin Brennan 19105bfd32
FIX: ChangeFullNameRequiredSetting could fail if setting was already in DB (#30605)
Followup 3187606d34

Fix full_name_requirement INSERT by adding ON CONFLICT DO NOTHING,
on sites that already have this setting this migration will fail.
2025-01-07 13:08:36 +10:00

33 lines
880 B
Ruby

# frozen_string_literal: true
class ChangeFullNameRequiredSetting < ActiveRecord::Migration[7.2]
def up
old_setting = DB.query_single(<<~SQL).first
SELECT value
FROM site_settings
WHERE name = 'full_name_required'
SQL
new_setting = nil
if old_setting
new_setting = old_setting == "t" ? "required_at_signup" : "optional_at_signup"
elsif Migration::Helpers.existing_site?
new_setting = "optional_at_signup"
end
DB.exec(<<~SQL)
DELETE FROM site_settings WHERE name = 'full_name_required'
SQL
DB.exec(<<~SQL, value: new_setting) if new_setting
INSERT INTO site_settings(name, data_type, value, created_at, updated_at)
VALUES ('full_name_requirement', 7, :value, NOW(), NOW())
ON CONFLICT (name) DO NOTHING
SQL
end
def down
raise ActiveRecord::IrreversibleMigration
end
end