Class: ActiveRecord::SchemaDumper

Inherits:
Object
  • Object
show all
Defined in:
lib/rmre/active_record/schema_dumper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dump_with_fk(connection = ActiveRecord::Base.connection, foreign_keys = [], stream = STDOUT) ⇒ Object



7
8
9
10
# File 'lib/rmre/active_record/schema_dumper.rb', line 7

def self.dump_with_fk(connection=ActiveRecord::Base.connection, foreign_keys=[], stream=STDOUT)
  new(connection).dump_with_fk(foreign_keys, stream)
  stream
end

Instance Method Details

#dump_with_fk(foreign_keys, stream) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rmre/active_record/schema_dumper.rb', line 12

def dump_with_fk(foreign_keys, stream)
  header(stream)
  tables(stream)

  foreign_keys.each do |fk|
stream.puts <<-SQL
  execute "ALTER TABLE #{fk['from_table']} ADD CONSTRAINT fk_#{fk['from_table']}_#{fk['to_table']}
       FOREIGN KEY (#{fk['from_column']}) REFERENCES #{fk['to_table']}(#{fk['to_column']})"
SQL
  end

  trailer(stream)
  stream
end