database_cleaner 1.7.0 → 2.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/database_cleaner.rb +1 -12
- metadata +24 -418
- data/CONTRIBUTE.markdown +0 -28
- data/Gemfile.lock +0 -286
- data/History.rdoc +0 -453
- data/LICENSE +0 -20
- data/README.markdown +0 -541
- data/Rakefile +0 -40
- data/TODO +0 -3
- data/VERSION.yml +0 -4
- data/cucumber.yml +0 -1
- data/lib/database_cleaner/active_record/base.rb +0 -92
- data/lib/database_cleaner/active_record/deletion.rb +0 -108
- data/lib/database_cleaner/active_record/transaction.rb +0 -59
- data/lib/database_cleaner/active_record/truncation.rb +0 -275
- data/lib/database_cleaner/base.rb +0 -166
- data/lib/database_cleaner/configuration.rb +0 -131
- data/lib/database_cleaner/couch_potato/base.rb +0 -7
- data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
- data/lib/database_cleaner/cucumber.rb +0 -3
- data/lib/database_cleaner/data_mapper/base.rb +0 -21
- data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
- data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
- data/lib/database_cleaner/generic/base.rb +0 -29
- data/lib/database_cleaner/generic/transaction.rb +0 -11
- data/lib/database_cleaner/generic/truncation.rb +0 -40
- data/lib/database_cleaner/mongo/base.rb +0 -16
- data/lib/database_cleaner/mongo/truncation.rb +0 -62
- data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
- data/lib/database_cleaner/mongo2/base.rb +0 -16
- data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
- data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
- data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
- data/lib/database_cleaner/mongoid/base.rb +0 -20
- data/lib/database_cleaner/mongoid/truncation.rb +0 -49
- data/lib/database_cleaner/moped/base.rb +0 -39
- data/lib/database_cleaner/moped/truncation.rb +0 -9
- data/lib/database_cleaner/moped/truncation_base.rb +0 -40
- data/lib/database_cleaner/neo4j/base.rb +0 -62
- data/lib/database_cleaner/neo4j/deletion.rb +0 -16
- data/lib/database_cleaner/neo4j/transaction.rb +0 -35
- data/lib/database_cleaner/neo4j/truncation.rb +0 -9
- data/lib/database_cleaner/null_strategy.rb +0 -20
- data/lib/database_cleaner/ohm/truncation.rb +0 -15
- data/lib/database_cleaner/redis/base.rb +0 -37
- data/lib/database_cleaner/redis/truncation.rb +0 -26
- data/lib/database_cleaner/safeguard.rb +0 -72
- data/lib/database_cleaner/sequel/base.rb +0 -22
- data/lib/database_cleaner/sequel/deletion.rb +0 -47
- data/lib/database_cleaner/sequel/transaction.rb +0 -40
- data/lib/database_cleaner/sequel/truncation.rb +0 -78
@@ -1,20 +0,0 @@
|
|
1
|
-
require 'database_cleaner/generic/base'
|
2
|
-
module DatabaseCleaner
|
3
|
-
module Mongoid
|
4
|
-
def self.available_strategies
|
5
|
-
%w[truncation]
|
6
|
-
end
|
7
|
-
|
8
|
-
module Base
|
9
|
-
include ::DatabaseCleaner::Generic::Base
|
10
|
-
|
11
|
-
def db=(desired_db)
|
12
|
-
@db = desired_db
|
13
|
-
end
|
14
|
-
|
15
|
-
def db
|
16
|
-
@db || :default
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'database_cleaner/mongoid/base'
|
2
|
-
require 'database_cleaner/generic/truncation'
|
3
|
-
require 'database_cleaner/mongo/truncation_mixin'
|
4
|
-
require 'database_cleaner/mongo2/truncation_mixin'
|
5
|
-
require 'database_cleaner/moped/truncation_base'
|
6
|
-
require 'mongoid/version'
|
7
|
-
|
8
|
-
module DatabaseCleaner
|
9
|
-
module Mongoid
|
10
|
-
class Truncation
|
11
|
-
include ::DatabaseCleaner::Mongoid::Base
|
12
|
-
include ::DatabaseCleaner::Generic::Truncation
|
13
|
-
|
14
|
-
if ::Mongoid::VERSION < '3'
|
15
|
-
|
16
|
-
include ::DatabaseCleaner::Mongo::TruncationMixin
|
17
|
-
|
18
|
-
private
|
19
|
-
|
20
|
-
def database
|
21
|
-
::Mongoid.database
|
22
|
-
end
|
23
|
-
|
24
|
-
elsif ::Mongoid::VERSION < '5'
|
25
|
-
|
26
|
-
include ::DatabaseCleaner::Moped::TruncationBase
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def session
|
31
|
-
::Mongoid::VERSION > "5.0.0" ? ::Mongoid.default_client : ::Mongoid.default_session
|
32
|
-
end
|
33
|
-
|
34
|
-
def database
|
35
|
-
if not(@db.nil? or @db == :default)
|
36
|
-
::Mongoid.databases[@db]
|
37
|
-
else
|
38
|
-
::Mongoid.database
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
else
|
43
|
-
|
44
|
-
include ::DatabaseCleaner::Mongo2::TruncationMixin
|
45
|
-
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'database_cleaner/generic/base'
|
2
|
-
|
3
|
-
module DatabaseCleaner
|
4
|
-
module Moped
|
5
|
-
def self.available_strategies
|
6
|
-
%w[truncation]
|
7
|
-
end
|
8
|
-
|
9
|
-
module Base
|
10
|
-
include ::DatabaseCleaner::Generic::Base
|
11
|
-
|
12
|
-
def db=(desired_db)
|
13
|
-
@db = desired_db
|
14
|
-
end
|
15
|
-
|
16
|
-
def db
|
17
|
-
@db ||= :default
|
18
|
-
end
|
19
|
-
|
20
|
-
def host_port=(desired_host)
|
21
|
-
@host = desired_host
|
22
|
-
end
|
23
|
-
|
24
|
-
def host
|
25
|
-
@host ||= '127.0.0.1:27017'
|
26
|
-
end
|
27
|
-
|
28
|
-
def db_version
|
29
|
-
@db_version ||= session.command('buildinfo' => 1)['version']
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def session
|
35
|
-
::Moped::Session.new([host], database: db)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'database_cleaner/moped/base'
|
2
|
-
require 'database_cleaner/generic/truncation'
|
3
|
-
|
4
|
-
module DatabaseCleaner
|
5
|
-
module Moped
|
6
|
-
module TruncationBase
|
7
|
-
include ::DatabaseCleaner::Moped::Base
|
8
|
-
include ::DatabaseCleaner::Generic::Truncation
|
9
|
-
|
10
|
-
def clean
|
11
|
-
if @only
|
12
|
-
collections.each { |c| session[c].find.remove_all if @only.include?(c) }
|
13
|
-
else
|
14
|
-
collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
|
15
|
-
end
|
16
|
-
true
|
17
|
-
end
|
18
|
-
|
19
|
-
private
|
20
|
-
|
21
|
-
def collections
|
22
|
-
if db != :default
|
23
|
-
session.use(db)
|
24
|
-
end
|
25
|
-
|
26
|
-
if db_version.split('.').first.to_i >= 3
|
27
|
-
session.command(listCollections: 1, filter: { 'name' => { '$not' => /.?system\.|\$/ } })['cursor']['firstBatch'].map do |collection|
|
28
|
-
collection['name']
|
29
|
-
end
|
30
|
-
else
|
31
|
-
session['system.namespaces'].find(name: { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
|
32
|
-
_, name = collection['name'].split('.', 2)
|
33
|
-
name
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'database_cleaner/generic/base'
|
2
|
-
module DatabaseCleaner
|
3
|
-
module Neo4j
|
4
|
-
def self.available_strategies
|
5
|
-
%w[transaction truncation deletion]
|
6
|
-
end
|
7
|
-
|
8
|
-
module Base
|
9
|
-
include ::DatabaseCleaner::Generic::Base
|
10
|
-
|
11
|
-
def db=(desired_db)
|
12
|
-
@db = desired_db == :default ? nil : desired_db
|
13
|
-
end
|
14
|
-
|
15
|
-
def db
|
16
|
-
@db ||= nil
|
17
|
-
end
|
18
|
-
|
19
|
-
def start
|
20
|
-
if db_type == :embedded_db and not session.running?
|
21
|
-
session.start
|
22
|
-
else
|
23
|
-
session
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def database
|
28
|
-
db && default_db.merge(db) || default_db
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
def default_db
|
34
|
-
{:type => default_db_type, :path => default_db_path}
|
35
|
-
end
|
36
|
-
|
37
|
-
def default_db_type
|
38
|
-
:server_db
|
39
|
-
end
|
40
|
-
|
41
|
-
def default_db_path(type = default_db_type)
|
42
|
-
type == :server_db ? 'http://localhost:7475/' : './db/test'
|
43
|
-
end
|
44
|
-
|
45
|
-
def db_type
|
46
|
-
database[:type]
|
47
|
-
end
|
48
|
-
|
49
|
-
def db_path
|
50
|
-
database[:path]
|
51
|
-
end
|
52
|
-
|
53
|
-
def db_params
|
54
|
-
database.reject!{|key, value| [:type, :path].include? key }
|
55
|
-
end
|
56
|
-
|
57
|
-
def session
|
58
|
-
@session ||= ::Neo4j::Session.open(db_type, db_path, db_params)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
@@ -1,16 +0,0 @@
|
|
1
|
-
require 'database_cleaner/neo4j/base'
|
2
|
-
require 'neo4j-core'
|
3
|
-
|
4
|
-
module DatabaseCleaner
|
5
|
-
module Neo4j
|
6
|
-
class Deletion
|
7
|
-
include ::DatabaseCleaner::Neo4j::Base
|
8
|
-
|
9
|
-
def clean
|
10
|
-
::Neo4j::Transaction.run do
|
11
|
-
session._query('MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r')
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'database_cleaner/neo4j/base'
|
2
|
-
require 'database_cleaner/generic/transaction'
|
3
|
-
require 'neo4j-core'
|
4
|
-
|
5
|
-
module DatabaseCleaner
|
6
|
-
module Neo4j
|
7
|
-
class Transaction
|
8
|
-
include ::DatabaseCleaner::Generic::Transaction
|
9
|
-
include ::DatabaseCleaner::Neo4j::Base
|
10
|
-
|
11
|
-
attr_accessor :tx
|
12
|
-
|
13
|
-
def start
|
14
|
-
super
|
15
|
-
rollback
|
16
|
-
self.tx = ::Neo4j::Transaction.new
|
17
|
-
end
|
18
|
-
|
19
|
-
def clean
|
20
|
-
rollback
|
21
|
-
end
|
22
|
-
|
23
|
-
private
|
24
|
-
|
25
|
-
def rollback
|
26
|
-
if tx
|
27
|
-
tx.failure
|
28
|
-
tx.close
|
29
|
-
end
|
30
|
-
ensure
|
31
|
-
self.tx = nil
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
require 'database_cleaner/generic/base'
|
2
|
-
|
3
|
-
module DatabaseCleaner
|
4
|
-
module Redis
|
5
|
-
def self.available_strategies
|
6
|
-
%w{truncation}
|
7
|
-
end
|
8
|
-
|
9
|
-
module Base
|
10
|
-
include ::DatabaseCleaner::Generic::Base
|
11
|
-
|
12
|
-
def db=(desired_db)
|
13
|
-
@db = desired_db
|
14
|
-
end
|
15
|
-
|
16
|
-
def db
|
17
|
-
@db ||= :default
|
18
|
-
end
|
19
|
-
|
20
|
-
alias url db
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def connection
|
25
|
-
@connection ||= begin
|
26
|
-
if url == :default
|
27
|
-
::Redis.new
|
28
|
-
elsif db.is_a?(::Redis) # pass directly the connection
|
29
|
-
db
|
30
|
-
else
|
31
|
-
::Redis.new(:url => url)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'database_cleaner/redis/base'
|
2
|
-
require 'database_cleaner/generic/truncation'
|
3
|
-
|
4
|
-
module DatabaseCleaner
|
5
|
-
module Redis
|
6
|
-
class Truncation
|
7
|
-
include ::DatabaseCleaner::Redis::Base
|
8
|
-
include ::DatabaseCleaner::Generic::Truncation
|
9
|
-
|
10
|
-
def clean
|
11
|
-
if @only
|
12
|
-
@only.each do |term|
|
13
|
-
connection.keys(term).each { |k| connection.del k }
|
14
|
-
end
|
15
|
-
elsif @tables_to_exclude
|
16
|
-
keys_except = []
|
17
|
-
@tables_to_exclude.each { |term| keys_except += connection.keys(term) }
|
18
|
-
connection.keys.each { |k| connection.del(k) unless keys_except.include?(k) }
|
19
|
-
else
|
20
|
-
connection.flushdb
|
21
|
-
end
|
22
|
-
connection.quit unless url == :default
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,72 +0,0 @@
|
|
1
|
-
module DatabaseCleaner
|
2
|
-
class Safeguard
|
3
|
-
class Error < Exception
|
4
|
-
class RemoteDatabaseUrl < Error
|
5
|
-
def initialize
|
6
|
-
super("ENV['DATABASE_URL'] is set to a remote URL. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards")
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
class ProductionEnv < Error
|
11
|
-
def initialize(env)
|
12
|
-
super("ENV['#{env}'] is set to production. Please refer to https://github.com/DatabaseCleaner/database_cleaner#safeguards")
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class RemoteDatabaseUrl
|
18
|
-
LOCAL = %w(localhost 127.0.0.1)
|
19
|
-
|
20
|
-
def run
|
21
|
-
raise Error::RemoteDatabaseUrl if !skip? && given?
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def given?
|
27
|
-
remote?(ENV['DATABASE_URL'])
|
28
|
-
end
|
29
|
-
|
30
|
-
def remote?(url)
|
31
|
-
url && !LOCAL.any? { |str| url.include?(str) }
|
32
|
-
end
|
33
|
-
|
34
|
-
def skip?
|
35
|
-
ENV['DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL'] ||
|
36
|
-
DatabaseCleaner.allow_remote_database_url
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
class Production
|
41
|
-
KEYS = %w(ENV RACK_ENV RAILS_ENV)
|
42
|
-
|
43
|
-
def run
|
44
|
-
raise Error::ProductionEnv.new(key) if !skip? && given?
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def given?
|
50
|
-
!!key
|
51
|
-
end
|
52
|
-
|
53
|
-
def key
|
54
|
-
@key ||= KEYS.detect { |key| ENV[key] == 'production' }
|
55
|
-
end
|
56
|
-
|
57
|
-
def skip?
|
58
|
-
ENV['DATABASE_CLEANER_ALLOW_PRODUCTION'] ||
|
59
|
-
DatabaseCleaner.allow_production
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
CHECKS = [
|
64
|
-
RemoteDatabaseUrl,
|
65
|
-
Production
|
66
|
-
]
|
67
|
-
|
68
|
-
def run
|
69
|
-
CHECKS.each { |const| const.new.run }
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
@@ -1,22 +0,0 @@
|
|
1
|
-
require 'database_cleaner/generic/base'
|
2
|
-
module DatabaseCleaner
|
3
|
-
module Sequel
|
4
|
-
def self.available_strategies
|
5
|
-
%w(truncation transaction deletion)
|
6
|
-
end
|
7
|
-
|
8
|
-
module Base
|
9
|
-
include ::DatabaseCleaner::Generic::Base
|
10
|
-
|
11
|
-
def db=(desired_db)
|
12
|
-
@db = desired_db
|
13
|
-
end
|
14
|
-
|
15
|
-
def db
|
16
|
-
return @db if @db && @db != :default
|
17
|
-
raise "As you have more than one active sequel database you have to specify the one to use manually!" if ::Sequel::DATABASES.count > 1
|
18
|
-
::Sequel::DATABASES.first || :default
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
require 'database_cleaner/sequel/base'
|
2
|
-
require 'database_cleaner/generic/truncation'
|
3
|
-
require 'database_cleaner/sequel/truncation'
|
4
|
-
|
5
|
-
module DatabaseCleaner::Sequel
|
6
|
-
class Deletion < Truncation
|
7
|
-
def disable_referential_integrity(tables)
|
8
|
-
case db.database_type
|
9
|
-
when :postgres
|
10
|
-
db.run('SET CONSTRAINTS ALL DEFERRED')
|
11
|
-
tables_to_truncate(db).each do |table|
|
12
|
-
db.run("ALTER TABLE \"#{table}\" DISABLE TRIGGER ALL")
|
13
|
-
end
|
14
|
-
when :mysql
|
15
|
-
old = db.fetch('SELECT @@FOREIGN_KEY_CHECKS').first[:@@FOREIGN_KEY_CHECKS]
|
16
|
-
db.run('SET FOREIGN_KEY_CHECKS = 0')
|
17
|
-
end
|
18
|
-
yield
|
19
|
-
ensure
|
20
|
-
case db.database_type
|
21
|
-
when :postgres
|
22
|
-
tables.each do |table|
|
23
|
-
db.run("ALTER TABLE \"#{table}\" ENABLE TRIGGER ALL")
|
24
|
-
end
|
25
|
-
when :mysql
|
26
|
-
db.run("SET FOREIGN_KEY_CHECKS = #{old}")
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def delete_tables(db, tables)
|
31
|
-
tables.each do |table|
|
32
|
-
db[table.to_sym].delete
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def clean
|
37
|
-
return unless dirty?
|
38
|
-
|
39
|
-
tables = tables_to_truncate(db)
|
40
|
-
db.transaction do
|
41
|
-
disable_referential_integrity(tables) do
|
42
|
-
delete_tables(db, tables)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
require 'database_cleaner/sequel/base'
|
2
|
-
module DatabaseCleaner
|
3
|
-
module Sequel
|
4
|
-
class Transaction
|
5
|
-
include ::DatabaseCleaner::Sequel::Base
|
6
|
-
|
7
|
-
def self.check_fiber_brokenness
|
8
|
-
if !@checked_fiber_brokenness && Fiber.new { Thread.current }.resume != Thread.current
|
9
|
-
raise RuntimeError, "This ruby engine's Fibers are not compatible with Sequel's connection pool. " +
|
10
|
-
"To work around this, please use DatabaseCleaner.cleaning with a block instead of " +
|
11
|
-
"DatabaseCleaner.start and DatabaseCleaner.clean"
|
12
|
-
end
|
13
|
-
@checked_fiber_brokenness = true
|
14
|
-
end
|
15
|
-
|
16
|
-
def start
|
17
|
-
self.class.check_fiber_brokenness
|
18
|
-
|
19
|
-
@fibers ||= []
|
20
|
-
db = self.db
|
21
|
-
f = Fiber.new do
|
22
|
-
db.transaction(:rollback => :always, :savepoint => true) do
|
23
|
-
Fiber.yield
|
24
|
-
end
|
25
|
-
end
|
26
|
-
f.resume
|
27
|
-
@fibers << f
|
28
|
-
end
|
29
|
-
|
30
|
-
def clean
|
31
|
-
f = @fibers.pop
|
32
|
-
f.resume
|
33
|
-
end
|
34
|
-
|
35
|
-
def cleaning
|
36
|
-
self.db.transaction(:rollback => :always, :savepoint => true) { yield }
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
@@ -1,78 +0,0 @@
|
|
1
|
-
require 'database_cleaner/generic/truncation'
|
2
|
-
require 'database_cleaner/sequel/base'
|
3
|
-
|
4
|
-
module DatabaseCleaner
|
5
|
-
module Sequel
|
6
|
-
class Truncation
|
7
|
-
include ::DatabaseCleaner::Sequel::Base
|
8
|
-
include ::DatabaseCleaner::Generic::Truncation
|
9
|
-
|
10
|
-
def start
|
11
|
-
@last_txid = txid
|
12
|
-
end
|
13
|
-
|
14
|
-
def clean
|
15
|
-
return unless dirty?
|
16
|
-
|
17
|
-
case db.database_type
|
18
|
-
when :postgres
|
19
|
-
# PostgreSQL requires all tables with FKs to be truncates in the same command, or have the CASCADE keyword
|
20
|
-
# appended. Bulk truncation without CASCADE is:
|
21
|
-
# * Safer. Tables outside of tables_to_truncate won't be affected.
|
22
|
-
# * Faster. Less roundtrips to the db.
|
23
|
-
unless (tables = tables_to_truncate(db)).empty?
|
24
|
-
all_tables = tables.map { |t| %("#{t}") }.join(',')
|
25
|
-
db.run "TRUNCATE TABLE #{all_tables};"
|
26
|
-
end
|
27
|
-
else
|
28
|
-
tables = tables_to_truncate(db)
|
29
|
-
|
30
|
-
if pre_count?
|
31
|
-
# Count rows before truncating
|
32
|
-
pre_count_truncate_tables(db, tables)
|
33
|
-
else
|
34
|
-
# Truncate each table normally
|
35
|
-
truncate_tables(db, tables)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
private
|
41
|
-
|
42
|
-
def pre_count_truncate_tables(db, tables)
|
43
|
-
tables = tables.reject { |table| db[table.to_sym].count == 0 }
|
44
|
-
truncate_tables(db, tables)
|
45
|
-
end
|
46
|
-
|
47
|
-
def truncate_tables(db, tables)
|
48
|
-
tables.each do |table|
|
49
|
-
db[table.to_sym].truncate
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
def dirty?
|
54
|
-
@last_txid != txid || @last_txid.nil?
|
55
|
-
end
|
56
|
-
|
57
|
-
def txid
|
58
|
-
case db.database_type
|
59
|
-
when :postgres
|
60
|
-
db.fetch('SELECT txid_snapshot_xmax(txid_current_snapshot()) AS txid').first[:txid]
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def tables_to_truncate(db)
|
65
|
-
(@only || db.tables.map(&:to_s)) - @tables_to_exclude
|
66
|
-
end
|
67
|
-
|
68
|
-
# overwritten
|
69
|
-
def migration_storage_names
|
70
|
-
%w(schema_info schema_migrations)
|
71
|
-
end
|
72
|
-
|
73
|
-
def pre_count?
|
74
|
-
@pre_count == true
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|