lib/strong_migrations/checks.rb in strong_migrations-1.5.0 vs lib/strong_migrations/checks.rb in strong_migrations-1.6.0
- old
+ new
@@ -30,10 +30,13 @@
def check_add_column(*args)
options = args.extract_options!
table, column, type = args
default = options[:default]
+ # keep track of new columns of change_column_default check
+ @new_columns << [table.to_s, column.to_s]
+
# Check key since DEFAULT NULL behaves differently from no default
#
# Also, Active Record has special case for uuid columns that allows function default values
# https://github.com/rails/rails/blob/v7.0.3.1/activerecord/lib/active_record/connection_adapters/postgresql/quoting.rb#L92-L93
if options.key?(:default) && (!adapter.add_column_default_safe? || (volatile = (postgresql? && type.to_s == "uuid" && default.to_s.include?("()") && adapter.default_volatile?(default))))
@@ -196,10 +199,22 @@
end
raise_error :change_column, rewrite_blocks: adapter.rewrite_blocks unless safe
end
+ def check_change_column_default(*args)
+ table, column, _default_or_changes = args
+
+ # just check ActiveRecord::Base, even though can override on model
+ partial_inserts = ar_version >= 7 ? ActiveRecord::Base.partial_inserts : ActiveRecord::Base.partial_writes
+
+ if partial_inserts && !new_column?(table, column)
+ raise_error :change_column_default,
+ config: ar_version >= 7 ? "partial_inserts" : "partial_writes"
+ end
+ end
+
def check_change_column_null(*args)
table, column, null, default = args
if !null
if postgresql?
safe = false
@@ -457,8 +472,12 @@
column.to_s =~ /\A[a-z0-9_]+\z/ ? column : connection.quote_column_name(column)
end
def new_table?(table)
@new_tables.include?(table.to_s)
+ end
+
+ def new_column?(table, column)
+ new_table?(table) || @new_columns.include?([table.to_s, column.to_s])
end
end
end