Class: DatabaseCleaner::Sequel::Truncation

Inherits:
Base
  • Object
show all
Defined in:
lib/database_cleaner/sequel/truncation.rb

Direct Known Subclasses

Deletion

Instance Method Summary collapse

Methods inherited from Base

#db

Constructor Details

#initialize(opts = {}) ⇒ Truncation

Returns a new instance of Truncation.



6
7
8
9
10
# File 'lib/database_cleaner/sequel/truncation.rb', line 6

def initialize(opts = {})
  @only = opts[:only] || []
  @except = opts[:except] || []
  @pre_count = opts[:pre_count] || false
end

Instance Method Details

#cleanObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/database_cleaner/sequel/truncation.rb', line 16

def clean
  return unless dirty?

  tables = tables_to_truncate(db)

  # Count rows before truncating
  if pre_count?
    tables = pre_count_tables(tables)
  end

  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.empty?
      tables_sql = tables.map { |t| %("#{t}") }.join(',')
      db.run "TRUNCATE TABLE #{tables_sql} RESTART IDENTITY;"
    end
  else
    truncate_tables(db, tables)
  end
end

#startObject



12
13
14
# File 'lib/database_cleaner/sequel/truncation.rb', line 12

def start
  @last_txid = txid
end