Class: StrongMigrations::Checker
- Inherits:
-
Object
- Object
- StrongMigrations::Checker
- Defined in:
- lib/strong_migrations/checker.rb
Instance Attribute Summary collapse
-
#direction ⇒ Object
Returns the value of attribute direction.
Instance Method Summary collapse
-
#initialize(migration) ⇒ Checker
constructor
A new instance of Checker.
- #perform(method, *args) ⇒ Object
- #safety_assured ⇒ Object
Constructor Details
#initialize(migration) ⇒ Checker
Returns a new instance of Checker.
5 6 7 8 9 |
# File 'lib/strong_migrations/checker.rb', line 5 def initialize(migration) @migration = migration @new_tables = [] @safe = false end |
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
3 4 5 |
# File 'lib/strong_migrations/checker.rb', line 3 def direction @direction end |
Instance Method Details
#perform(method, *args) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/strong_migrations/checker.rb', line 21 def perform(method, *args) unless safe? case method when :remove_column, :remove_columns, :remove_timestamps, :remove_reference, :remove_belongs_to columns = case method when :remove_timestamps ["created_at", "updated_at"] when :remove_column [args[1].to_s] when :remove_columns args[1..-1].map(&:to_s) else = args[2] || {} reference = args[1] cols = [] cols << "#{reference}_type" if [:polymorphic] cols << "#{reference}_id" cols end code = "self.ignored_columns = #{columns.inspect}" raise_error :remove_column, model: args[0].to_s.classify, code: code, command: command_str(method, args), column_suffix: columns.size > 1 ? "s" : "" when :change_table raise_error :change_table, header: "Possibly dangerous operation" when :rename_table raise_error :rename_table when :rename_column raise_error :rename_column when :add_index table, columns, = args ||= {} if columns.is_a?(Array) && columns.size > 3 && ![:unique] raise_error :add_index_columns, header: "Best practice" end if postgresql? && [:algorithm] != :concurrently && !@new_tables.include?(table.to_s) raise_error :add_index, command: command_str("add_index", [table, columns, .merge(algorithm: :concurrently)]) end when :add_column table, column, type, = args ||= {} default = [:default] if !default.nil? && !(postgresql? && postgresql_version >= 110000) if [:null] == false = .except(:null) append = " Then add the NOT NULL constraint. class %{migration_name}NotNull < ActiveRecord::Migration%{migration_suffix} def change #{command_str("change_column_null", [table, column, false])} end end" end raise_error :add_column_default, add_command: command_str("add_column", [table, column, type, .except(:default)]), change_command: command_str("change_column_default", [table, column, default]), remove_command: command_str("remove_column", [table, column]), code: backfill_code(table, column, default), append: append end if type.to_s == "json" && postgresql? raise_error :add_column_json end when :change_column table, column, type = args safe = false # assume Postgres 9.1+ since previous versions are EOL if postgresql? && type.to_s == "text" found_column = connection.columns(table).find { |c| c.name.to_s == column.to_s } safe = found_column && found_column.type == :string end raise_error :change_column unless safe when :create_table table, = args ||= {} raise_error :create_table if [:force] # keep track of new tables of add_index check @new_tables << table.to_s when :add_reference, :add_belongs_to table, reference, = args ||= {} index_value = .fetch(:index, true) concurrently_set = index_value.is_a?(Hash) && index_value[:algorithm] == :concurrently if postgresql? && index_value && !concurrently_set columns = [:polymorphic] ? [:"#{reference}_type", :"#{reference}_id"] : :"#{reference}_id" if index_value.is_a?(Hash) [:index] = [:index].merge(algorithm: :concurrently) else = .merge(index: {algorithm: :concurrently}) end raise_error :add_reference, command: command_str(method, [table, reference, ]) end when :execute raise_error :execute, header: "Possibly dangerous operation" when :change_column_null table, column, null, default = args if !null && !default.nil? raise_error :change_column_null, code: backfill_code(table, column, default) end when :add_foreign_key from_table, to_table, = args ||= {} validate = .fetch(:validate, true) if postgresql? if ActiveRecord::VERSION::STRING >= "5.2" if validate raise_error :add_foreign_key, add_foreign_key_code: command_str("add_foreign_key", [from_table, to_table, .merge(validate: false)]), validate_foreign_key_code: command_str("validate_foreign_key", [from_table, to_table]) end else # always validated before 5.2 # fk name logic from rails primary_key = [:primary_key] || "id" column = [:column] || "#{to_table.to_s.singularize}_id" hashed_identifier = Digest::SHA256.hexdigest("#{from_table}_#{column}_fk").first(10) fk_name = [:name] || "fk_rails_#{hashed_identifier}" raise_error :add_foreign_key, add_foreign_key_code: foreign_key_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_foreign_key_code: foreign_key_str("ALTER TABLE %s VALIDATE CONSTRAINT %s", [from_table, fk_name]) end end end StrongMigrations.checks.each do |check| @migration.instance_exec(method, args, &check) end end result = yield if StrongMigrations.auto_analyze && direction == :up && postgresql? && method == :add_index connection.execute "ANALYZE VERBOSE #{connection.quote_table_name(args[0].to_s)}" end result end |
#safety_assured ⇒ Object
11 12 13 14 15 16 17 18 19 |
# File 'lib/strong_migrations/checker.rb', line 11 def safety_assured previous_value = @safe begin @safe = true yield ensure @safe = previous_value end end |