Class: Wal::Migration::PublicationDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/wal/migration.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ PublicationDefinition

Returns a new instance of PublicationDefinition.



6
7
8
9
# File 'lib/wal/migration.rb', line 6

def initialize(name)
  @name = name
  @tables = {}
end

Instance Method Details

#table(name, columns: []) ⇒ Object



11
12
13
14
# File 'lib/wal/migration.rb', line 11

def table(name, columns: [])
  @tables[name] ||= [].to_set
  @tables[name].merge(columns.map(&:to_s))
end

#tablesObject



16
17
18
# File 'lib/wal/migration.rb', line 16

def tables
  @tables.keys
end

#to_sqlObject



20
21
22
23
24
25
26
27
# File 'lib/wal/migration.rb', line 20

def to_sql
  tables_sql = @tables.reduce([]) do |sql, (table, columns)|
    table_sql = "TABLE #{table}"
    table_sql << " (#{columns.join(", ")})" unless columns.empty?
    sql << table_sql
  end
  "ALTER PUBLICATION #{@name} SET #{tables_sql.join(", ")}"
end