Module: StrongMigrations::SafeMethods
- Included in:
- Checker
- Defined in:
- lib/strong_migrations/safe_methods.rb
Instance Method Summary collapse
-
#disable_transaction ⇒ Object
hard to commit at right time when reverting so just commit at start.
- #in_transaction? ⇒ Boolean
- #safe_add_check_constraint(table, expression, *args, add_options, validate_options) ⇒ Object
- #safe_add_foreign_key(from_table, to_table, *args, **options) ⇒ Object
- #safe_add_index(*args, **options) ⇒ Object
- #safe_add_reference(table, reference, *args, **options) ⇒ Object
- #safe_by_default_method?(method) ⇒ Boolean
- #safe_change_column_null(add_args, validate_args, change_args, remove_args, default, constraints) ⇒ Object
- #safe_remove_index(*args, **options) ⇒ Object
Instance Method Details
#disable_transaction ⇒ Object
hard to commit at right time when reverting so just commit at start
112 113 114 115 116 117 |
# File 'lib/strong_migrations/safe_methods.rb', line 112 def disable_transaction if in_transaction? && !transaction_disabled connection.commit_db_transaction self.transaction_disabled = true end end |
#in_transaction? ⇒ Boolean
119 120 121 |
# File 'lib/strong_migrations/safe_methods.rb', line 119 def in_transaction? connection.open_transactions > 0 end |
#safe_add_check_constraint(table, expression, *args, add_options, validate_options) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/strong_migrations/safe_methods.rb', line 63 def safe_add_check_constraint(table, expression, *args, , ) @migration.reversible do |dir| dir.up do # only skip invalid constraints unless connection.check_constraints(table).any? { |c| c.[:name] == [:name] && !c.[:validate] } @migration.add_check_constraint(table, expression, *args, **) end disable_transaction @migration.validate_check_constraint(table, **) end dir.down do @migration.remove_check_constraint(table, expression, **.except(:validate)) end end end |
#safe_add_foreign_key(from_table, to_table, *args, **options) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/strong_migrations/safe_methods.rb', line 46 def safe_add_foreign_key(from_table, to_table, *args, **) @migration.reversible do |dir| dir.up do # validate option is unintentionally ignored for Active Record < 7.1 # https://github.com/rails/rails/pull/45896 if !connection.foreign_key_exists?(from_table, to_table, **.merge(validate: false)) @migration.add_foreign_key(from_table, to_table, *args, **.merge(validate: false)) end disable_transaction @migration.validate_foreign_key(from_table, to_table, **.slice(:column, :name)) end dir.down do @migration.remove_foreign_key(from_table, to_table, **.slice(:column, :name)) end end end |
#safe_add_index(*args, **options) ⇒ Object
7 8 9 10 |
# File 'lib/strong_migrations/safe_methods.rb', line 7 def safe_add_index(*args, **) disable_transaction @migration.add_index(*args, **.merge(algorithm: :concurrently)) end |
#safe_add_reference(table, reference, *args, **options) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/strong_migrations/safe_methods.rb', line 17 def safe_add_reference(table, reference, *args, **) @migration.reversible do |dir| dir.up do disable_transaction foreign_key = .delete(:foreign_key) @migration.add_reference(table, reference, *args, **) if foreign_key # same as Active Record name = if foreign_key.is_a?(Hash) && foreign_key[:to_table] foreign_key[:to_table] else (ActiveRecord::Base.pluralize_table_names ? reference.to_s.pluralize : reference).to_sym end foreign_key_opts = foreign_key.is_a?(Hash) ? foreign_key.except(:to_table) : {} if reference @migration.add_foreign_key(table, name, column: "#{reference}_id", **foreign_key_opts) else @migration.add_foreign_key(table, name, **foreign_key_opts) end end end dir.down do @migration.remove_reference(table, reference) end end end |
#safe_by_default_method?(method) ⇒ Boolean
3 4 5 |
# File 'lib/strong_migrations/safe_methods.rb', line 3 def safe_by_default_method?(method) StrongMigrations.safe_by_default && [:add_index, :add_belongs_to, :add_reference, :remove_index, :add_foreign_key, :add_check_constraint, :change_column_null].include?(method) end |
#safe_change_column_null(add_args, validate_args, change_args, remove_args, default, constraints) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/strong_migrations/safe_methods.rb', line 79 def safe_change_column_null(add_args, validate_args, change_args, remove_args, default, constraints) @migration.reversible do |dir| dir.up do unless default.nil? raise Error, "default value not supported yet with safe_by_default" end = add_args. = validate_args. = remove_args. # only skip invalid constraints unless constraints.any? { |c| c.[:name] == [:name] && !c.[:validate] } @migration.add_check_constraint(*add_args, **) end disable_transaction connection.begin_db_transaction @migration.validate_check_constraint(*validate_args, **) @migration.change_column_null(*change_args) @migration.remove_check_constraint(*remove_args, **) connection.commit_db_transaction end dir.down do down_args = change_args.dup down_args[2] = true @migration.change_column_null(*down_args) end end end |
#safe_remove_index(*args, **options) ⇒ Object
12 13 14 15 |
# File 'lib/strong_migrations/safe_methods.rb', line 12 def safe_remove_index(*args, **) disable_transaction @migration.remove_index(*args, **.merge(algorithm: :concurrently)) end |