class EnterpriseMti::Migration::SqlFactory::SqlFactory

Attributes

subclass_tables[RW]
superclass_table[RW]

Public Instance Methods

add_column(column, opts={}) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 107
def add_column(column, opts={})
  "ADD COLUMN #{column} #{options_parser opts}"
end
add_constraint(name, opts={}) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 117
def add_constraint(name, opts={})
  case opts[:type]
  when :check
    type = 'CHECK ('
    suffix = ')'
  when :foreign_key
    type = "FOREIGN KEY(#{opts[:column]})"
  end
  "ADD CONSTRAINT #{name} #{type} #{yield} #{suffix}"
end
alter_column(column, opts={}) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 110
def alter_column(column, opts={})
  "ALTER COLUMN #{column} SET #{options_parser opts}"
end
alter_table(table) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 106
def alter_table(table);              "ALTER TABLE #{table} #{yield};"; end
defer() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 128
def defer;                           "DEFERRABLE INITIALLY DEFERRED"; end
drop_column(column) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 113
def drop_column(column);             "DROP COLUMN #{column}"; end
drop_constraint(name) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 127
def drop_constraint(name);           "DROP CONSTRAINT #{name}"; end
not_null() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 114
def not_null;                        "NOT NULL"; end
options_parser(opts={}) click to toggle source

DSL ##

# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 97
def options_parser(opts={})
  sql = []
  sql.push opts[:type]
  sql.push not_null if opts[:nullable] == false
  sql.push unique if opts[:unique]
  sql.push references table: opts[:references][:table], column: opts[:references][:column] if opts[:references]
  sql.push defer if opts[:defer]
  sql.join(' ')
end
references(opts={}) click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 116
def references(opts={});             "REFERENCES #{opts[:table]}(#{opts[:column]})"; end
sql_for_down() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 12
def sql_for_down
  [subclass_tables_down, superclass_table_down].join
end
sql_for_up() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 8
def sql_for_up
  [superclass_table_up, subclass_tables_up].join
end
subclass_tables_down() click to toggle source

Down methods (subclass) ##

# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 83
def subclass_tables_down
  subclass_tables_id_alterations_down
end
subclass_tables_id_alterations_down() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 87
def subclass_tables_id_alterations_down
  subclass_tables.map do |subclass_table|
    alter_table subclass_table do
      drop_constraint 'id_fkey'
    end
  end
end
subclass_tables_id_alterations_up() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 51
def subclass_tables_id_alterations_up
  subclass_tables.map { |subclass_table|
    alter_table subclass_table do
      add_constraint "id_fkey", type: :foreign_key, column: 'id' do
        options_parser references: { table: superclass_table, column: "#{subclass_table}_id" }, defer: true
      end
    end
  }
end
subclass_tables_up() click to toggle source

Up methods (subclass) ##

# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 47
def subclass_tables_up
  subclass_tables_id_alterations_up
end
superclass_table_down() click to toggle source

Down methods (superclass) ##

# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 63
def superclass_table_down
  [superclass_table_xor_constraint_down, superclass_table_foreign_keys_down]
end
superclass_table_foreign_keys_down() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 73
def superclass_table_foreign_keys_down
  subclass_tables.map do |subclass_table|
    alter_table superclass_table do
      drop_column("#{subclass_table}_id")
    end
  end
end
superclass_table_foreign_keys_up() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 22
def superclass_table_foreign_keys_up
  subclass_tables.map { |subclass_table|
    alter_table superclass_table do
      add_column "#{subclass_table}_id",
      type: id_type,
      nullable: false,
      unique: true,
      references: { table: subclass_table, column: "id" },
      defer: true
    end
  }
end
superclass_table_up() click to toggle source

Up methods (superclass) ##

# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 18
def superclass_table_up
  [superclass_table_foreign_keys_up, superclass_table_xor_constraint_up]
end
superclass_table_xor_constraint_down() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 67
def superclass_table_xor_constraint_down
  alter_table superclass_table do
    drop_constraint "#{superclass_table}_xor"
  end
end
superclass_table_xor_constraint_up() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 35
def superclass_table_xor_constraint_up
  alter_table superclass_table do
    add_constraint "#{superclass_table}_xor", type: :check do
      subclass_tables.map { |subclass_table|
        "(#{subclass_table}_id IS #{not_null})::#{integer}"
      }.join(' + ') << ' = 1'
    end
  end
end
unique() click to toggle source
# File lib/enterprise_mti/migration/sql_factory/sql_factory.rb, line 115
def unique;                          "UNIQUE"; end