Sha256: 1d10be8e8b30018d2b80bc3d5153f845242cbb36b43119286d66131aaececde2

Contents?: true

Size: 1.31 KB

Versions: 5

Compression:

Stored size: 1.31 KB

Contents

module AbAdmin
  module Utils
    module Mysql
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        # Deletes all rows in table very fast, but without calling +destroy+ method
        # nor any hooks.
        def truncate!
          transaction { connection.execute("TRUNCATE TABLE #{quoted_table_name};") }
        end

        # remove duplicate records by columns
        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


        # Disables key updates for model table
        def disable_keys
          connection.execute("ALTER TABLE #{quoted_table_name} DISABLE KEYS")
        end

        # Enables key updates for model table
        def enable_keys
          connection.execute("ALTER TABLE #{quoted_table_name} ENABLE KEYS")
        end

        # Disables keys, yields block, enables keys.
        def with_keys_disabled
          disable_keys
          yield
          enable_keys
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ab_admin-0.9.0 lib/ab_admin/utils/mysql.rb
ab_admin-0.8.3 lib/ab_admin/utils/mysql.rb
ab_admin-0.8.2 lib/ab_admin/utils/mysql.rb
ab_admin-0.8.1 lib/ab_admin/utils/mysql.rb
ab_admin-0.8.0 lib/ab_admin/utils/mysql.rb