Module: AbAdmin::Utils::Mysql::ClassMethods

Defined in:
lib/ab_admin/utils/mysql.rb

Instance Method Summary collapse

Instance Method Details

#disable_keysObject

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_keysObject

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) ⇒ 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)
  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 < 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_disabledObject

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