Module: AbAdmin::Utils::Mysql::ClassMethods
- Defined in:
- lib/ab_admin/utils/mysql.rb
Instance Method Summary collapse
-
#disable_keys ⇒ Object
Disables key updates for model table.
-
#enable_keys ⇒ Object
Enables key updates for model table.
-
#remove_duplicates(*cols, deleted_id_order: '<') ⇒ Object
remove duplicate records by columns.
-
#truncate! ⇒ Object
Deletes all rows in table very fast, but without calling
destroy
method nor any hooks. -
#with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
Instance Method Details
#disable_keys ⇒ Object
Disables key updates for model table
26 27 28 |
# File 'lib/ab_admin/utils/mysql.rb', line 26 def disable_keys connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS") end |
#enable_keys ⇒ Object
Enables key updates for model table
31 32 33 |
# File 'lib/ab_admin/utils/mysql.rb', line 31 def enable_keys connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS") end |
#remove_duplicates(*cols, deleted_id_order: '<') ⇒ Object
remove duplicate records by columns
16 17 18 19 20 21 22 |
# File 'lib/ab_admin/utils/mysql.rb', line 16 def remove_duplicates(*cols, deleted_id_order: '<') conds = cols.map { |col| "#{table_name}.#{col} IS NOT NULL AND #{table_name}.#{col} = t.#{col}" }.join(' AND ') query = <<-SQL DELETE FROM #{table_name} USING #{table_name}, #{table_name} AS t WHERE #{table_name}.id #{deleted_id_order} t.id AND #{conds} SQL connection.execute(query) end |
#truncate! ⇒ Object
Deletes all rows in table very fast, but without calling destroy
method nor any hooks.
11 12 13 |
# File 'lib/ab_admin/utils/mysql.rb', line 11 def truncate! transaction { connection.execute("TRUNCATE TABLE #{quoted_table_name};") } end |
#with_keys_disabled ⇒ Object
Disables keys, yields block, enables keys.
36 37 38 39 40 |
# File 'lib/ab_admin/utils/mysql.rb', line 36 def with_keys_disabled disable_keys yield enable_keys end |