diff --git a/app/models/user.rb b/app/models/user.rb index 1bde1ea1b8d..e86f5966a30 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1123,7 +1123,6 @@ end # name :string # seen_notification_id :integer default(0), not null # last_posted_at :datetime -# email :string(513) # password_hash :string(64) # salt :string(32) # active :boolean default(FALSE), not null diff --git a/db/fixtures/009_users.rb b/db/fixtures/009_users.rb index 937faa20cf7..d0ec2a498ac 100644 --- a/db/fixtures/009_users.rb +++ b/db/fixtures/009_users.rb @@ -35,8 +35,9 @@ Group.user_trust_level_change!(-1, TrustLevel[4]) ColumnDropper.drop( table: 'users', - after_migration: 'AddUserAuthTokens', + after_migration: 'CreateUserEmails', columns: %w[ + email email_always mailing_list_mode email_digests diff --git a/lib/column_dropper.rb b/lib/column_dropper.rb index 7c77a25331c..ff001ca46da 100644 --- a/lib/column_dropper.rb +++ b/lib/column_dropper.rb @@ -30,13 +30,13 @@ class ColumnDropper on_drop&.call columns.each do |column| + ActiveRecord::Base.exec_sql <<~SQL + DROP TRIGGER IF EXISTS #{readonly_trigger_name(table, column)} ON #{table}; + DROP FUNCTION IF EXISTS #{readonly_function_name(table, column)}; + SQL + # safe cause it is protected on method entry, can not be passed in params ActiveRecord::Base.exec_sql("ALTER TABLE #{table} DROP COLUMN IF EXISTS #{column}") - - ActiveRecord::Base.exec_sql <<~SQL - DROP FUNCTION IF EXISTS #{readonly_function_name(table, column)}; - DROP TRIGGER IF EXISTS #{readonly_trigger_name(table, column)} ON #{table}; - SQL end Discourse.reset_active_record_cache diff --git a/spec/components/column_dropper_spec.rb b/spec/components/column_dropper_spec.rb index 46db479925d..12e01fb9fd7 100644 --- a/spec/components/column_dropper_spec.rb +++ b/spec/components/column_dropper_spec.rb @@ -72,6 +72,23 @@ RSpec.describe ColumnDropper do SQL end + it 'should be droppable' do + name = Topic + .exec_sql("SELECT name FROM schema_migration_details LIMIT 1") + .getvalue(0, 0) + + dropped_proc_called = false + ColumnDropper.drop( + table: table_name, + after_migration: name, + columns: ['email'], + delay: 0.minutes, + on_drop: ->() { dropped_proc_called = true } + ) + + expect(dropped_proc_called).to eq(true) + + end it 'should prevent updates to the readonly column' do expect do ActiveRecord::Base.connection.raw_connection.exec <<~SQL