lib/strong_migrations/checker.rb in strong_migrations-0.7.9 vs lib/strong_migrations/checker.rb in strong_migrations-0.8.0

- old
+ new

@@ -84,12 +84,11 @@ when :add_column table, column, type, options = args options ||= {} default = options[:default] - if !default.nil? && !((postgresql? && postgresql_version >= Gem::Version.new("11")) || (mysql? && mysql_version >= Gem::Version.new("8.0.12")) || (mariadb? && mariadb_version >= Gem::Version.new("10.3.2"))) - + if !default.nil? && !add_column_default_safe? if options[:null] == false options = options.except(:null) append = " Then add the NOT NULL constraint in separate migrations." @@ -228,33 +227,35 @@ add_code = constraint_str("ALTER TABLE %s ADD CONSTRAINT %s CHECK (%s IS NOT NULL) NOT VALID", [table, constraint_name, column]) validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [table, constraint_name]) remove_code = constraint_str("ALTER TABLE %s DROP CONSTRAINT %s", [table, constraint_name]) + constraint_methods = ar_version >= 6.1 + validate_constraint_code = - if ar_version >= 6.1 + if constraint_methods String.new(command_str(:validate_check_constraint, [table, {name: constraint_name}])) else String.new(safety_assured_str(validate_code)) end if postgresql_version >= Gem::Version.new("12") change_args = [table, column, null] validate_constraint_code << "\n #{command_str(:change_column_null, change_args)}" - if ar_version >= 6.1 + if constraint_methods validate_constraint_code << "\n #{command_str(:remove_check_constraint, [table, {name: constraint_name}])}" else validate_constraint_code << "\n #{safety_assured_str(remove_code)}" end end return safe_change_column_null(add_code, validate_code, change_args, remove_code) if StrongMigrations.safe_by_default add_constraint_code = - if ar_version >= 6.1 + if constraint_methods # only quote when needed expr_column = column.to_s =~ /\A[a-z0-9_]+\z/ ? column : connection.quote_column_name(column) command_str(:add_check_constraint, [table, "#{expr_column} IS NOT NULL", {name: constraint_name, validate: false}]) else safety_assured_str(add_code) @@ -273,36 +274,18 @@ end when :add_foreign_key from_table, to_table, options = args options ||= {} - # always validated before 5.2 - validate = options.fetch(:validate, true) || ar_version < 5.2 + validate = options.fetch(:validate, true) if postgresql? && validate - if ar_version < 5.2 - # fk name logic from rails - primary_key = options[:primary_key] || "id" - column = options[:column] || "#{to_table.to_s.singularize}_id" - hashed_identifier = Digest::SHA256.hexdigest("#{from_table}_#{column}_fk").first(10) - fk_name = options[:name] || "fk_rails_#{hashed_identifier}" + return safe_add_foreign_key(from_table, to_table, options) if StrongMigrations.safe_by_default - add_code = constraint_str("ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s) NOT VALID", [from_table, fk_name, column, to_table, primary_key]) - validate_code = constraint_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [from_table, fk_name]) - - return safe_add_foreign_key_code(from_table, to_table, add_code, validate_code) if StrongMigrations.safe_by_default - - raise_error :add_foreign_key, - add_foreign_key_code: safety_assured_str(add_code), - validate_foreign_key_code: safety_assured_str(validate_code) - else - return safe_add_foreign_key(from_table, to_table, options) if StrongMigrations.safe_by_default - - raise_error :add_foreign_key, - add_foreign_key_code: command_str("add_foreign_key", [from_table, to_table, options.merge(validate: false)]), - validate_foreign_key_code: command_str("validate_foreign_key", [from_table, to_table]) - end + raise_error :add_foreign_key, + add_foreign_key_code: command_str("add_foreign_key", [from_table, to_table, options.merge(validate: false)]), + validate_foreign_key_code: command_str("validate_foreign_key", [from_table, to_table]) end when :validate_foreign_key if postgresql? && writes_blocked? raise_error :validate_foreign_key end @@ -595,8 +578,20 @@ "#{model}.unscoped.in_batches do |relation| \n relation.update_all #{column}: #{default.inspect}\n sleep(0.01)\n end" end def new_table?(table) @new_tables.include?(table.to_s) + end + + def add_column_default_safe? + if postgresql? + postgresql_version >= Gem::Version.new("11") + elsif mysql? + mysql_version >= Gem::Version.new("8.0.12") + elsif mariadb? + mariadb_version >= Gem::Version.new("10.3.2") + else + false + end end end end