Module: Polymorpheus::ConnectionAdapters::MysqlAdapter

Defined in:
lib/polymorpheus/mysql_adapter.rb

Constant Summary collapse

INSERT =
'INSERT'
UPDATE =
'UPDATE'

Instance Method Summary collapse

Instance Method Details

#add_polymorphic_constraints(table, columns, options = {}) ⇒ Object

See the README for explanations regarding the use of these methods

table: a string equal to the name of the db table

columns: a hash, with keys equal to the column names in the table we

are operating on, and values indicating the foreign key
association through the form "table.column". so,
  { 'employee_id' => 'employees.ssn',
    'product_id' => 'products.id' }
indicates that the `employee_id` column in `table` should have
a foreign key constraint connecting it to the `ssn` column
in the `employees` table, and the `product_id` column should
have a foreign key constraint with the `id` column in the
`products` table

options: a hash, corrently only accepts one option that allows us to

add an additional uniqueness constraint.
if the columns hash was specified as above, and we supplied
options of
  { :unique => true }
then this would create a uniqueness constraint in the database
that would ensure that any given employee_id could only be in
the table once, and that any given product_id could only be in
the table once.

alternatively, the user can also supply a column name or array
of column names to the :unique option:
  { :unique => 'picture_url' }
This would allow an employee_id (or product_id) to appear
multiple times in the table, but no two employee ids would
be able to have the same picture_url.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/polymorpheus/mysql_adapter.rb', line 40

def add_polymorphic_constraints(table, columns, options={})
  column_names = columns.keys.sort
  add_polymorphic_triggers(table, column_names)
  options.symbolize_keys!
  if options[:unique].present?
    poly_create_indexes(table, column_names, Array(options[:unique]))
  end
  column_names.each do |col_name|
    ref_table, ref_col = columns[col_name].to_s.split('.')
    add_foreign_key table, ref_table,
                           :column => col_name,
                           :primary_key => (ref_col || 'id')
  end
end

#add_polymorphic_triggers(table, column_names) ⇒ Object

DO NOT USE THIS METHOD DIRECTLY

it will not create the foreign key relationships you want. the only reason it is here is because it is used by the schema dumper, since the schema dump will contains separate statements for foreign keys, and we don’t want to duplicate those



76
77
78
79
80
# File 'lib/polymorpheus/mysql_adapter.rb', line 76

def add_polymorphic_triggers(table, column_names)
  column_names.sort!
  poly_drop_triggers(table, column_names)
  poly_create_triggers(table, column_names)
end

#remove_polymorphic_constraints(table, columns, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
# File 'lib/polymorpheus/mysql_adapter.rb', line 55

def remove_polymorphic_constraints(table, columns, options = {})
  poly_drop_triggers(table, columns.keys.sort)
  columns.each do |(col, reference)|
    remove_foreign_key table, :column => col
  end
  if options[:unique].present?
    poly_remove_indexes(table, columns.keys, Array(options[:unique]))
  end
end

#triggersObject



65
66
67
# File 'lib/polymorpheus/mysql_adapter.rb', line 65

def triggers
  execute("show triggers").collect {|t| Trigger.new(t) }
end