Skip to content

A platform for community discussion. Free, open, simple.

DEV: Allow `DROP DEFAULT` in pre-deploy migrations (#34838)

Lincaa3e173c5f14c
diff --git a/lib/migration/safe_migrate.rb b/lib/migration/safe_migrate.rb
index b85c9d89d1c..8dd2d480d28 100644
--- a/lib/migration/safe_migrate.rb
+++ b/lib/migration/safe_migrate.rb
@@ -156,7 +156,7 @@ class Migration::SafeMigrate
in use by live applications.
TEXT
raise Discourse::InvalidMigration, "Attempt was made to drop a table"
- elsif sql =~ /\A\s*alter\s+table.*(?:rename|drop(?!\s+not\s+null))\s+/i
+ elsif sql =~ /\A\s*alter\s+table.*(?:rename|drop(?!(\s+not\s+null)|(\s+default)))\s+/i
$stdout.puts("", <<~TEXT)
WARNING
-------------------------------------------------------------------------------------
diff --git a/spec/fixtures/db/migrate/drop_default/20250917071036_drop_default.rb b/spec/fixtures/db/migrate/drop_default/20250917071036_drop_default.rb
new file mode 100644
index 00000000000..7560c60792f
--- /dev/null
+++ b/spec/fixtures/db/migrate/drop_default/20250917071036_drop_default.rb
@@ -0,0 +1,10 @@
+# frozen_string_literal: true
+class DropDefault < ActiveRecord::Migration[8.0]
+ def up
+ change_column_default :posts, :like_count, nil
+ end
+
+ def down
+ raise "not tested"
+ end
+end
diff --git a/spec/lib/migration/safe_migrate_spec.rb b/spec/lib/migration/safe_migrate_spec.rb
index e5e7796eef2..0b61efceafc 100644
--- a/spec/lib/migration/safe_migrate_spec.rb
+++ b/spec/lib/migration/safe_migrate_spec.rb
@@ -140,6 +140,16 @@ RSpec.describe Migration::SafeMigrate do
expect(output).to include("change_column_null(:users, :username, true, nil)")
end
+ it "allows dropping default" do
+ Migration::SafeMigrate.enable!
+
+ path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_default"
+
+ output = capture_stdout { migrate_up(path) }
+
+ expect(output).to include("change_column_default(:posts, :like_count, nil)")
+ end
+
it "supports being disabled" do
Migration::SafeMigrate.enable!
Migration::SafeMigrate.disable!