Module: SchemaPlus::ForeignKeys::Middleware::Migration::RenameTable

Defined in:
lib/schema_plus/foreign_keys/middleware/migration.rb

Instance Method Summary collapse

Instance Method Details

#after(env) ⇒ Object


19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/schema_plus/foreign_keys/middleware/migration.rb', line 19

def after(env)
  oldname = env.table_name
  newname = env.new_name
  env.connection.foreign_keys(newname).each do |fk|
    begin
      env.connection.remove_foreign_key(newname, name: fk.name)
      env.connection.add_foreign_key(newname, fk.to_table, :column => fk.column, :primary_key => fk.primary_key, :name => fk.name.sub(/#{oldname}/, newname), :on_update => fk.on_update, :on_delete => fk.on_delete, :deferrable => fk.deferrable)
    rescue NotImplementedError
      # sqlite3 can't remote or add foreign keys, so just skip it
    end
  end
end