Class: HQ::Structure::Inserts

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/hq/structure/inserts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_pool:, tables:) ⇒ Inserts

Returns a new instance of Inserts.



11
12
13
14
15
16
17
18
19
# File 'lib/hq/structure/inserts.rb', line 11

def initialize(connection_pool:, tables:)
  @connection_pool = connection_pool
  @dependencies = {}
  @tables = Set.new

  tables.select { |table_name| connection_pool.with_connection { |c| c.table_exists?(table_name) } }.each do |t|
    build_dependencies!(t)
  end
end

Instance Attribute Details

#connection_poolObject (readonly)

Returns the value of attribute connection_pool.



9
10
11
# File 'lib/hq/structure/inserts.rb', line 9

def connection_pool
  @connection_pool
end

#dependenciesObject (readonly)

Returns the value of attribute dependencies.



9
10
11
# File 'lib/hq/structure/inserts.rb', line 9

def dependencies
  @dependencies
end

#tablesObject (readonly)

Returns the value of attribute tables.



9
10
11
# File 'lib/hq/structure/inserts.rb', line 9

def tables
  @tables
end

Instance Method Details

#buildObject



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hq/structure/inserts.rb', line 21

def build
  tsort.lazy.map do |table_name|
    sql = build_sql(table_name)
    sequence = sequence_sql(table_name) if sql

    [
      *sql,
      *sequence
    ].join("\n").presence
  end.reject(&:nil?)
end