Module: SchemaPlus::ForeignKeys::Middleware::Dumper::Tables

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

Overview

index and foreign key constraint definitions are dumped inline in the create_table block. (This is done for elegance, but also because Sqlite3 does not allow foreign key constraints to be added to a table after it has been defined.)

Defined Under Namespace

Modules: SQLite3

Instance Method Summary collapse

Instance Method Details

#after(env) ⇒ Object

Ignore the foreign key dumps at the end of the schema; we'll put them in/near their tables


39
40
41
# File 'lib/schema_plus/foreign_keys/middleware/dumper.rb', line 39

def after(env)
  env.dump.final.reject!(&it =~/foreign_key/)
end

#before(env) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/schema_plus/foreign_keys/middleware/dumper.rb', line 12

def before(env)

  @inline_fks = Hash.new{ |h, k| h[k] = [] }
  @backref_fks = Hash.new{ |h, k| h[k] = [] }

  env.connection.tables_only.each do |table|
    if (fks = env.connection.foreign_keys(table)).any?
      env.dump.data.has_fks = true
      @inline_fks[table] = fks
      env.dump.depends(table, fks.collect(&:to_table))
    end
  end

  # Normally we dump foreign key constraints inline in the table
  # definitions, both for visual cleanliness and because sqlite3
  # doesn't allow foreign key constraints to be added afterwards.
  # But in case there's a cycle in the constraint references, some
  # constraints will need to be broken out then added later.  (Adding
  # constraints later won't work with sqlite3, but that means sqlite3
  # won't let you create cycles in the first place.)
  break_fk_cycles(env) while env.dump.strongly_connected_components.any?{|component| component.size > 1}

  env.dump.data.inline_fks = @inline_fks
  env.dump.data.backref_fks = @backref_fks
end