Module: Useful::ActiveRecordHelpers::MysqlMigrationHelpers::ClassMethods
- Defined in:
- lib/useful/active_record_helpers/mysql_migration_helpers.rb
Instance Method Summary collapse
- #alter_view(view_name, sql_query_definition) ⇒ Object
- #clear_table(table_to_clear) ⇒ Object
- #create_view(view_name, sql_query_definition) ⇒ Object
- #drop_foreign_key(from_table, *from_columns) ⇒ Object
- #drop_view(view_name) ⇒ Object
- #foreign_key(from_table, from_column, to_table, opts = {}) ⇒ Object
- #raise_err(msg = '') ⇒ Object
- #remove_column_with_fk(table, column) ⇒ Object
- #safe_drop_table(table_name) ⇒ Object
- #set_auto_increment(table, number) ⇒ Object
- #unique_constraint(table_name, columns) ⇒ Object
Instance Method Details
#alter_view(view_name, sql_query_definition) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 57 def alter_view(view_name,sql_query_definition) execute %{ ALTER SQL SECURITY INVOKER VIEW #{view_name.to_s} AS #{sql_query_definition} } end |
#clear_table(table_to_clear) ⇒ Object
40 41 42 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 40 def clear_table(table_to_clear) execute %{delete from #{table_to_clear}} end |
#create_view(view_name, sql_query_definition) ⇒ Object
44 45 46 47 48 49 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 44 def create_view(view_name,sql_query_definition) execute %{ CREATE SQL SECURITY INVOKER VIEW #{view_name.to_s} AS #{sql_query_definition} } end |
#drop_foreign_key(from_table, *from_columns) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 16 def drop_foreign_key(from_table, *from_columns) from_columns.each { |from_column| constraint_name = "fk_#{from_table}_#{from_column}" execute %{alter table #{from_table} drop FOREIGN KEY #{constraint_name}} } end |
#drop_view(view_name) ⇒ Object
51 52 53 54 55 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 51 def drop_view(view_name) execute %{ DROP VIEW #{view_name} } end |
#foreign_key(from_table, from_column, to_table, opts = {}) ⇒ Object
7 8 9 10 11 12 13 14 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 7 def foreign_key(from_table, from_column, to_table, opts={}) opts[:destination_key] = 'id' unless opts[:destination_key] constraint_name = "fk_#{from_table}_#{from_column}" execute %{alter table #{from_table} add constraint #{constraint_name} foreign key (#{from_column}) references #{to_table}(#{opts[:destination_key]})} end |
#raise_err(msg = '') ⇒ Object
64 65 66 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 64 def raise_err(msg = '') raise ActiveRecord::IrreversibleMigration, msg end |
#remove_column_with_fk(table, column) ⇒ Object
23 24 25 26 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 23 def remove_column_with_fk(table, column) drop_foreign_key(table, column) remove_column(table, column) end |
#safe_drop_table(table_name) ⇒ Object
28 29 30 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 28 def safe_drop_table(table_name) execute "drop table if exists #{table_name}" end |
#set_auto_increment(table, number) ⇒ Object
36 37 38 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 36 def set_auto_increment(table, number) execute %{ALTER TABLE #{table} AUTO_INCREMENT = #{number}} end |
#unique_constraint(table_name, columns) ⇒ Object
32 33 34 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 32 def unique_constraint(table_name, columns) execute %{ALTER TABLE #{table_name} ADD CONSTRAINT uniq_#{columns.join('_')} UNIQUE KEY (#{columns.join(", ")})} end |