Skip to content

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

DEV: Allow `DROP NOT NULL` in pre-deploy migrations (#20775)

David Taylor7070f81596a1
diff --git a/lib/migration/safe_migrate.rb b/lib/migration/safe_migrate.rb
index 26e093c3036..b43bc38b029 100644
--- a/lib/migration/safe_migrate.rb
+++ b/lib/migration/safe_migrate.rb
@@ -129,7 +129,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+/i
+ elsif sql =~ /\A\s*alter\s+table.*(?:rename|drop(?!\s+not\s+null))\s+/i
$stdout.puts("", <<~TEXT)
WARNING
-------------------------------------------------------------------------------------
diff --git a/spec/fixtures/db/migrate/drop_not_null/20990309014015_drop_not_null.rb b/spec/fixtures/db/migrate/drop_not_null/20990309014015_drop_not_null.rb
new file mode 100644
index 00000000000..e6f845b94c4
--- /dev/null
+++ b/spec/fixtures/db/migrate/drop_not_null/20990309014015_drop_not_null.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+class DropNotNull < ActiveRecord::Migration[5.1]
+ def up
+ change_column_null :users, :username, true
+ 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 06c15e985b3..3ed5413330a 100644
--- a/spec/lib/migration/safe_migrate_spec.rb
+++ b/spec/lib/migration/safe_migrate_spec.rb
@@ -70,6 +70,16 @@ RSpec.describe Migration::SafeMigrate do
expect { User.first.username }.not_to raise_error
end
+ it "allows dropping NOT NULL" do
+ Migration::SafeMigrate.enable!
+
+ path = File.expand_path "#{Rails.root}/spec/fixtures/db/migrate/drop_not_null"
+
+ output = capture_stdout { migrate_up(path) }
+
+ expect(output).to include("change_column_null(:users, :username, true)")
+ end
+
it "supports being disabled" do
Migration::SafeMigrate.enable!
Migration::SafeMigrate.disable!