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
58 59 60 61 62 63 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 58 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
41 42 43 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 41 def clear_table(table_to_clear) execute %{delete from #{table_to_clear}} end |
#create_view(view_name, sql_query_definition) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 45 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
17 18 19 20 21 22 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 17 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
52 53 54 55 56 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 52 def drop_view(view_name) execute %{ DROP VIEW #{view_name} } end |
#foreign_key(from_table, from_column, to_table, opts = {}) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 8 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
65 66 67 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 65 def raise_err(msg = '') raise ActiveRecord::IrreversibleMigration, msg end |
#remove_column_with_fk(table, column) ⇒ Object
24 25 26 27 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 24 def remove_column_with_fk(table, column) drop_foreign_key(table, column) remove_column(table, column) end |
#safe_drop_table(table_name) ⇒ Object
29 30 31 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 29 def safe_drop_table(table_name) execute "drop table if exists #{table_name}" end |
#set_auto_increment(table, number) ⇒ Object
37 38 39 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 37 def set_auto_increment(table, number) execute %{ALTER TABLE #{table} AUTO_INCREMENT = #{number}} end |
#unique_constraint(table_name, columns) ⇒ Object
33 34 35 |
# File 'lib/useful/active_record_helpers/mysql_migration_helpers.rb', line 33 def unique_constraint(table_name, columns) execute %{ALTER TABLE #{table_name} ADD CONSTRAINT uniq_#{columns.join('_')} UNIQUE KEY (#{columns.join(", ")})} end |