class DatabaseCleaner::Sequel::Truncation

Public Instance Methods

clean() click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 14
def clean
  return unless dirty?

  case db.database_type
  when :postgres
    # PostgreSQL requires all tables with FKs to be truncates in the same command, or have the CASCADE keyword
    # appended. Bulk truncation without CASCADE is:
    # * Safer. Tables outside of tables_to_truncate won't be affected.
    # * Faster. Less roundtrips to the db.
    unless (tables = tables_to_truncate(db)).empty?
      all_tables = tables.map { |t| %Q("#{t}") }.join(',')
      db.run "TRUNCATE TABLE #{all_tables};"
    end
  else
    tables = tables_to_truncate(db)

    if pre_count?
      # Count rows before truncating
      pre_count_truncate_tables(db, tables)
    else
      # Truncate each table normally
      truncate_tables(db, tables)
    end
  end
end
start() click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 10
def start
  @last_txid = txid
end

Private Instance Methods

dirty?() click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 53
def dirty?
  @last_txid != txid || @last_txid.nil?
end
migration_storage_names() click to toggle source

overwritten

# File lib/database_cleaner/sequel/truncation.rb, line 69
def migration_storage_names
  %w(schema_info schema_migrations)
end
pre_count?() click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 73
def pre_count?
  @pre_count == true
end
pre_count_truncate_tables(db, tables) click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 42
def pre_count_truncate_tables(db, tables)
  tables = tables.reject { |table| db[table.to_sym].count == 0 }
  truncate_tables(db, tables)
end
tables_to_truncate(db) click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 64
def tables_to_truncate(db)
  (@only || db.tables.map(&:to_s)) - @tables_to_exclude
end
truncate_tables(db, tables) click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 47
def truncate_tables(db, tables)
  tables.each do |table|
    db[table.to_sym].truncate
  end
end
txid() click to toggle source
# File lib/database_cleaner/sequel/truncation.rb, line 57
def txid
  case db.database_type
  when :postgres
    db.fetch('SELECT txid_snapshot_xmax(txid_current_snapshot()) AS txid').first[:txid]
  end
end