database_cleaner 1.6.2 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +5 -5
  2. data/lib/database_cleaner.rb +1 -8
  3. metadata +25 -499
  4. data/CONTRIBUTE.markdown +0 -28
  5. data/Gemfile.lock +0 -286
  6. data/History.rdoc +0 -430
  7. data/LICENSE +0 -20
  8. data/README.markdown +0 -518
  9. data/Rakefile +0 -40
  10. data/TODO +0 -3
  11. data/VERSION.yml +0 -4
  12. data/cucumber.yml +0 -1
  13. data/examples/Gemfile +0 -1
  14. data/examples/Gemfile.lock +0 -1
  15. data/examples/config/database.yml.example +0 -8
  16. data/examples/config/redis.yml +0 -8
  17. data/examples/db/sqlite_databases_go_here +0 -0
  18. data/examples/features/example.feature +0 -11
  19. data/examples/features/example_multiple_db.feature +0 -23
  20. data/examples/features/example_multiple_orm.feature +0 -22
  21. data/examples/features/step_definitions/activerecord_steps.rb +0 -31
  22. data/examples/features/step_definitions/couchpotato_steps.rb +0 -31
  23. data/examples/features/step_definitions/datamapper_steps.rb +0 -37
  24. data/examples/features/step_definitions/mongoid_steps.rb +0 -23
  25. data/examples/features/step_definitions/mongomapper_steps.rb +0 -31
  26. data/examples/features/step_definitions/neo4j_steps.rb +0 -23
  27. data/examples/features/step_definitions/ohm_steps.rb +0 -31
  28. data/examples/features/step_definitions/redis_steps.rb +0 -31
  29. data/examples/features/step_definitions/translation_steps.rb +0 -55
  30. data/examples/features/support/env.rb +0 -61
  31. data/examples/lib/activerecord_models.rb +0 -41
  32. data/examples/lib/couchpotato_models.rb +0 -61
  33. data/examples/lib/datamapper_models.rb +0 -50
  34. data/examples/lib/mongoid_models.rb +0 -44
  35. data/examples/lib/mongomapper_models.rb +0 -51
  36. data/examples/lib/neo4j_models.rb +0 -17
  37. data/examples/lib/ohm_models.rb +0 -43
  38. data/examples/lib/redis_models.rb +0 -65
  39. data/examples/lib/sequel_models.rb +0 -9
  40. data/features/cleaning.feature +0 -30
  41. data/features/cleaning_default_strategy.feature +0 -23
  42. data/features/cleaning_multiple_dbs.feature +0 -22
  43. data/features/cleaning_multiple_orms.feature +0 -67
  44. data/features/step_definitions/database_cleaner_steps.rb +0 -33
  45. data/features/support/env.rb +0 -4
  46. data/features/support/feature_runner.rb +0 -39
  47. data/lib/database_cleaner/active_record/base.rb +0 -92
  48. data/lib/database_cleaner/active_record/deletion.rb +0 -106
  49. data/lib/database_cleaner/active_record/transaction.rb +0 -59
  50. data/lib/database_cleaner/active_record/truncation.rb +0 -275
  51. data/lib/database_cleaner/base.rb +0 -164
  52. data/lib/database_cleaner/configuration.rb +0 -131
  53. data/lib/database_cleaner/couch_potato/base.rb +0 -7
  54. data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
  55. data/lib/database_cleaner/cucumber.rb +0 -3
  56. data/lib/database_cleaner/data_mapper/base.rb +0 -21
  57. data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
  58. data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
  59. data/lib/database_cleaner/generic/base.rb +0 -29
  60. data/lib/database_cleaner/generic/transaction.rb +0 -11
  61. data/lib/database_cleaner/generic/truncation.rb +0 -40
  62. data/lib/database_cleaner/mongo/base.rb +0 -16
  63. data/lib/database_cleaner/mongo/truncation.rb +0 -62
  64. data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
  65. data/lib/database_cleaner/mongo2/base.rb +0 -16
  66. data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
  67. data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
  68. data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
  69. data/lib/database_cleaner/mongoid/base.rb +0 -20
  70. data/lib/database_cleaner/mongoid/truncation.rb +0 -49
  71. data/lib/database_cleaner/moped/base.rb +0 -39
  72. data/lib/database_cleaner/moped/truncation.rb +0 -9
  73. data/lib/database_cleaner/moped/truncation_base.rb +0 -40
  74. data/lib/database_cleaner/neo4j/base.rb +0 -62
  75. data/lib/database_cleaner/neo4j/deletion.rb +0 -16
  76. data/lib/database_cleaner/neo4j/transaction.rb +0 -35
  77. data/lib/database_cleaner/neo4j/truncation.rb +0 -9
  78. data/lib/database_cleaner/null_strategy.rb +0 -20
  79. data/lib/database_cleaner/ohm/truncation.rb +0 -15
  80. data/lib/database_cleaner/redis/base.rb +0 -37
  81. data/lib/database_cleaner/redis/truncation.rb +0 -26
  82. data/lib/database_cleaner/sequel/base.rb +0 -22
  83. data/lib/database_cleaner/sequel/deletion.rb +0 -47
  84. data/lib/database_cleaner/sequel/transaction.rb +0 -40
  85. data/lib/database_cleaner/sequel/truncation.rb +0 -78
  86. data/spec/database_cleaner/active_record/base_spec.rb +0 -188
  87. data/spec/database_cleaner/active_record/transaction_spec.rb +0 -176
  88. data/spec/database_cleaner/active_record/truncation/mysql2_spec.rb +0 -38
  89. data/spec/database_cleaner/active_record/truncation/mysql_spec.rb +0 -38
  90. data/spec/database_cleaner/active_record/truncation/postgresql_spec.rb +0 -75
  91. data/spec/database_cleaner/active_record/truncation/shared_fast_truncation.rb +0 -40
  92. data/spec/database_cleaner/active_record/truncation/sqlite3_spec.rb +0 -40
  93. data/spec/database_cleaner/active_record/truncation_spec.rb +0 -180
  94. data/spec/database_cleaner/base_spec.rb +0 -617
  95. data/spec/database_cleaner/configuration_spec.rb +0 -345
  96. data/spec/database_cleaner/couch_potato/truncation_spec.rb +0 -41
  97. data/spec/database_cleaner/data_mapper/base_spec.rb +0 -30
  98. data/spec/database_cleaner/data_mapper/transaction_spec.rb +0 -23
  99. data/spec/database_cleaner/data_mapper/truncation/sqlite3_spec.rb +0 -41
  100. data/spec/database_cleaner/data_mapper/truncation_spec.rb +0 -11
  101. data/spec/database_cleaner/generic/base_spec.rb +0 -61
  102. data/spec/database_cleaner/generic/truncation_spec.rb +0 -118
  103. data/spec/database_cleaner/mongo/mongo_examples.rb +0 -26
  104. data/spec/database_cleaner/mongo/truncation_spec.rb +0 -72
  105. data/spec/database_cleaner/mongo_mapper/base_spec.rb +0 -33
  106. data/spec/database_cleaner/mongo_mapper/mongo_examples.rb +0 -8
  107. data/spec/database_cleaner/mongo_mapper/truncation_spec.rb +0 -74
  108. data/spec/database_cleaner/moped/moped_examples.rb +0 -32
  109. data/spec/database_cleaner/moped/truncation_spec.rb +0 -80
  110. data/spec/database_cleaner/neo4j/base_spec.rb +0 -43
  111. data/spec/database_cleaner/neo4j/transaction_spec.rb +0 -25
  112. data/spec/database_cleaner/null_strategy_spec.rb +0 -28
  113. data/spec/database_cleaner/ohm/truncation_spec.rb +0 -70
  114. data/spec/database_cleaner/redis/base_spec.rb +0 -43
  115. data/spec/database_cleaner/redis/truncation_spec.rb +0 -63
  116. data/spec/database_cleaner/sequel/base_spec.rb +0 -32
  117. data/spec/database_cleaner/sequel/deletion_spec.rb +0 -58
  118. data/spec/database_cleaner/sequel/transaction_spec.rb +0 -21
  119. data/spec/database_cleaner/sequel/truncation/sqlite3_spec.rb +0 -0
  120. data/spec/database_cleaner/sequel/truncation_spec.rb +0 -182
  121. data/spec/database_cleaner/shared_strategy.rb +0 -15
  122. data/spec/rcov.opts +0 -1
  123. data/spec/spec_helper.rb +0 -21
  124. data/spec/support/active_record/database_setup.rb +0 -6
  125. data/spec/support/active_record/migrations/20150101010000_create_users.rb +0 -14
  126. data/spec/support/active_record/migrations/20150101020000_create_agents.rb +0 -14
  127. data/spec/support/active_record/mysql2_setup.rb +0 -39
  128. data/spec/support/active_record/mysql_setup.rb +0 -38
  129. data/spec/support/active_record/postgresql_setup.rb +0 -48
  130. data/spec/support/active_record/schema_setup.rb +0 -17
  131. data/spec/support/active_record/sqlite3_setup.rb +0 -40
  132. data/spec/support/data_mapper/schema_setup.rb +0 -15
  133. data/spec/support/data_mapper/sqlite3_setup.rb +0 -39
@@ -1,65 +0,0 @@
1
- require 'redis'
2
-
3
- class RedisWidget
4
-
5
- def self.redis
6
- threaded ||= Redis.new
7
- end
8
-
9
- def self.redis=(connection)
10
- threaded = connection
11
- end
12
-
13
- def self.threaded
14
- Thread.current[self.class.to_s] ||= {}
15
- end
16
-
17
- def initialize(options = {})
18
- options = options.dup
19
- @name = options[:name]
20
- end
21
-
22
- def connection
23
- self.class.redis
24
- end
25
-
26
- def save
27
- unless connection.get(self.class.to_s + ':id')
28
- @id = 0
29
- connection.set(self.class.to_s + ':id', @id)
30
- end
31
- @id = connection.incr(self.class.to_s + ':id')
32
- connection.set(self.class.to_s + ':%d:name' % @id, @name)
33
- end
34
-
35
- def self.count
36
- self.redis.keys(self.to_s + '*name').size
37
- end
38
-
39
- def self.create!
40
- new(:name => 'some widget').save
41
-
42
- end
43
- end
44
-
45
- class RedisWidgetUsingDatabaseOne < RedisWidget
46
-
47
- def self.redis
48
- threaded[self.class.to_s] ||= Redis.new :url => ENV['REDIS_URL_ONE']
49
- end
50
-
51
- def self.create!
52
- new(:name => 'a widget using database one').save
53
- end
54
- end
55
-
56
- class RedisWidgetUsingDatabaseTwo < RedisWidget
57
-
58
- def self.redis
59
- threaded[self.class.to_s] ||= Redis.new :url => ENV['REDIS_URL_TWO']
60
- end
61
-
62
- def self.create!
63
- new(:name => 'a widget using database two').save
64
- end
65
- end
@@ -1,9 +0,0 @@
1
- require 'sequel'
2
-
3
- db = Sequel.sqlite # memory database
4
-
5
- db.run 'DROP TABLE IF EXISTS "sequel_widgets"'
6
-
7
- db.create_table :sequel_widgets do
8
- String :name
9
- end
@@ -1,30 +0,0 @@
1
- Feature: database cleaning
2
- In order to ease example and feature writing
3
- As a developer
4
- I want to have my database in a clean state
5
-
6
- Scenario Outline: ruby app
7
- Given I am using <ORM>
8
- And the <Strategy> cleaning strategy
9
-
10
- When I run my scenarios that rely on a clean database
11
- Then I should see all green
12
-
13
- Examples:
14
- | ORM | Strategy |
15
- | ActiveRecord | transaction |
16
- | ActiveRecord | truncation |
17
- | ActiveRecord | deletion |
18
- | DataMapper | transaction |
19
- | DataMapper | truncation |
20
- | Sequel | transaction |
21
- | Sequel | truncation |
22
- | Sequel | deletion |
23
- | MongoMapper | truncation |
24
- | Mongoid | truncation |
25
- | CouchPotato | truncation |
26
- | Redis | truncation |
27
- | Ohm | truncation |
28
- | Neo4j | deletion |
29
- | Neo4j | truncation |
30
- | Neo4j | transaction |
@@ -1,23 +0,0 @@
1
- Feature: database cleaning
2
- In order to ease example and feature writing
3
- As a developer
4
- I want to have my database in a clean state with default strategy
5
-
6
- Scenario Outline: ruby app
7
- Given I am using <ORM>
8
- And the default cleaning strategy
9
-
10
- When I run my scenarios that rely on a clean database
11
- Then I should see all green
12
-
13
- Examples:
14
- | ORM |
15
- | ActiveRecord |
16
- | DataMapper |
17
- | Sequel |
18
- | MongoMapper |
19
- | Mongoid |
20
- | CouchPotato |
21
- | Redis |
22
- | Ohm |
23
- | Neo4j |
@@ -1,22 +0,0 @@
1
- Feature: multiple database cleaning
2
- In order to ease example and feature writing
3
- As a developer
4
- I want to have my databases in a clean state
5
-
6
- Scenario Outline: ruby app
7
- Given I am using <ORM>
8
- And the <Strategy> cleaning strategy
9
-
10
- When I run my scenarios that rely on clean databases
11
- Then I should see all green
12
-
13
- Examples:
14
- | ORM | Strategy |
15
- | ActiveRecord | truncation |
16
- | ActiveRecord | deletion |
17
- | DataMapper | truncation |
18
- | Sequel | truncation |
19
- | MongoMapper | truncation |
20
- | DataMapper | transaction |
21
- | ActiveRecord | transaction |
22
- | Sequel | transaction |
@@ -1,67 +0,0 @@
1
- Feature: database cleaning using multiple ORMs
2
- In order to ease example and feature writing
3
- As a developer
4
- I want to have my database in a clean state
5
-
6
- Scenario Outline: ruby app
7
- Given I am using <ORM1> and <ORM2>
8
-
9
- When I run my scenarios that rely on clean databases using multiple orms
10
- Then I should see all green
11
-
12
- Examples:
13
- | ORM1 | ORM2 |
14
- | ActiveRecord | DataMapper |
15
- | ActiveRecord | Sequel |
16
- | ActiveRecord | MongoMapper |
17
- | ActiveRecord | Mongoid |
18
- | ActiveRecord | CouchPotato |
19
- | ActiveRecord | Ohm |
20
- | ActiveRecord | Redis |
21
- | DataMapper | ActiveRecord |
22
- | DataMapper | Sequel |
23
- | DataMapper | MongoMapper |
24
- | DataMapper | Mongoid |
25
- | DataMapper | CouchPotato |
26
- | DataMapper | Ohm |
27
- | DataMapper | Redis |
28
- | Sequel | ActiveRecord |
29
- | Sequel | DataMapper |
30
- | Sequel | MongoMapper |
31
- | Sequel | Mongoid |
32
- | Sequel | CouchPotato |
33
- | Sequel | Ohm |
34
- | Sequel | Redis |
35
- | MongoMapper | ActiveRecord |
36
- | MongoMapper | DataMapper |
37
- | MongoMapper | Sequel |
38
- | MongoMapper | Mongoid |
39
- | MongoMapper | CouchPotato |
40
- | MongoMapper | Ohm |
41
- | MongoMapper | Redis |
42
- | CouchPotato | ActiveRecord |
43
- | CouchPotato | DataMapper |
44
- | CouchPotato | Sequel |
45
- | CouchPotato | MongoMapper |
46
- | CouchPotato | Mongoid |
47
- | CouchPotato | Ohm |
48
- | CouchPotato | Redis |
49
- | Ohm | ActiveRecord |
50
- | Ohm | DataMapper |
51
- | Ohm | Sequel |
52
- | Ohm | MongoMapper |
53
- | Ohm | Mongoid |
54
- | Ohm | CouchPotato |
55
- | Redis | ActiveRecord |
56
- | Redis | DataMapper |
57
- | Redis | Sequel |
58
- | Redis | MongoMapper |
59
- | Redis | Mongoid |
60
- | Redis | CouchPotato |
61
- | Redis | Ohm |
62
- | Neo4j | ActiveRecord |
63
- | Neo4j | DataMapper |
64
- | Neo4j | Sequel |
65
- | Neo4j | Redis |
66
- | Neo4j | Ohm |
67
- | Neo4j | MongoMapper |
@@ -1,33 +0,0 @@
1
- orms_pattern = /(ActiveRecord|DataMapper|Sequel|MongoMapper|Mongoid|CouchPotato|Redis|Ohm|Neo4j)/
2
-
3
- Given /^I am using #{orms_pattern}$/ do |orm|
4
- @feature_runner = FeatureRunner.new
5
- @feature_runner.orm = orm
6
- end
7
-
8
- Given /^I am using #{orms_pattern} and #{orms_pattern}$/ do |orm1,orm2|
9
- @feature_runner = FeatureRunner.new
10
- @feature_runner.orm = orm1
11
- @feature_runner.another_orm = orm2
12
- end
13
-
14
- Given /^the (.+) cleaning strategy$/ do |strategy|
15
- @feature_runner.strategy = strategy
16
- end
17
-
18
- When "I run my scenarios that rely on a clean database" do
19
- @feature_runner.go 'example'
20
- end
21
-
22
- When "I run my scenarios that rely on clean databases" do
23
- @feature_runner.multiple_databases = true
24
- @feature_runner.go 'example_multiple_db'
25
- end
26
-
27
- When "I run my scenarios that rely on clean databases using multiple orms" do
28
- @feature_runner.go 'example_multiple_orm'
29
- end
30
-
31
- Then "I should see all green" do
32
- fail "Feature failed with :#{@feature_runner.output}" unless @feature_runner.exit_status == 0
33
- end
@@ -1,4 +0,0 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
2
- require 'database_cleaner'
3
-
4
- require 'rspec/expectations'
@@ -1,39 +0,0 @@
1
- class FeatureRunner
2
- attr_accessor :orm
3
- attr_accessor :another_orm
4
- attr_accessor :multiple_databases
5
- attr_accessor :strategy
6
- attr_accessor :exit_status
7
- attr_accessor :output
8
-
9
- def strategy
10
- @strategy || 'truncation'
11
- end
12
-
13
- def go(feature)
14
- full_dir ||= File.expand_path(File.dirname(__FILE__) + "/../../examples/")
15
- Dir.chdir(full_dir) do
16
-
17
-
18
- ENV['ORM'] = orm
19
- ENV['STRATEGY'] = strategy
20
-
21
- if another_orm
22
- ENV['ANOTHER_ORM'] = another_orm
23
- else
24
- ENV['ANOTHER_ORM'] = nil
25
- end
26
-
27
- if multiple_databases
28
- ENV['MULTIPLE_DBS'] = "true"
29
- else
30
- ENV['MULTIPLE_DBS'] = nil
31
- end
32
-
33
- self.output = `#{"jruby -S " if defined?(JRUBY_VERSION)}cucumber features/#{feature}.feature`
34
-
35
- self.exit_status = $?.exitstatus
36
- end
37
- end
38
-
39
- end
@@ -1,92 +0,0 @@
1
- require 'database_cleaner/generic/base'
2
- require 'active_record'
3
- require 'erb'
4
-
5
- module DatabaseCleaner
6
- module ActiveRecord
7
-
8
- def self.available_strategies
9
- %w[truncation transaction deletion]
10
- end
11
-
12
- def self.config_file_location=(path)
13
- @config_file_location = path
14
- end
15
-
16
- def self.config_file_location
17
- @config_file_location ||= "#{DatabaseCleaner.app_root}/config/database.yml"
18
- end
19
-
20
- module Base
21
- include ::DatabaseCleaner::Generic::Base
22
-
23
- attr_accessor :connection_hash
24
-
25
- def db=(desired_db)
26
- @db = desired_db
27
- load_config
28
- end
29
-
30
- def db
31
- @db ||= super
32
- end
33
-
34
- def load_config
35
- if self.db != :default && self.db.is_a?(Symbol) && File.file?(ActiveRecord.config_file_location)
36
- connection_details = YAML::load(ERB.new(IO.read(ActiveRecord.config_file_location)).result)
37
- @connection_hash = valid_config(connection_details)[self.db.to_s]
38
- end
39
- end
40
-
41
- def valid_config(connection_file)
42
- if !::ActiveRecord::Base.configurations.nil? && !::ActiveRecord::Base.configurations.empty?
43
- if connection_file != ::ActiveRecord::Base.configurations
44
- return ::ActiveRecord::Base.configurations
45
- end
46
- end
47
- connection_file
48
- end
49
-
50
- def connection_class
51
- @connection_class ||= if db && !db.is_a?(Symbol)
52
- db
53
- elsif connection_hash
54
- lookup_from_connection_pool || establish_connection
55
- else
56
- ::ActiveRecord::Base
57
- end
58
- end
59
-
60
- def self.migration_table_name
61
- if ::ActiveRecord::VERSION::MAJOR < 5
62
- ::ActiveRecord::Migrator.schema_migrations_table_name
63
- else
64
- ::ActiveRecord::SchemaMigration.table_name
65
- end
66
- end
67
-
68
- def self.exclusion_condition(column_name)
69
- result = " #{column_name} <> '#{::DatabaseCleaner::ActiveRecord::Base.migration_table_name}' "
70
- if ::ActiveRecord::VERSION::MAJOR >= 5
71
- result += " AND #{column_name} <> '#{::ActiveRecord::Base.internal_metadata_table_name}' "
72
- end
73
- result
74
- end
75
-
76
- private
77
-
78
- def lookup_from_connection_pool
79
- if ::ActiveRecord::Base.respond_to?(:descendants)
80
- database_name = connection_hash["database"] || connection_hash[:database]
81
- models = ::ActiveRecord::Base.descendants
82
- models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
83
- end
84
- end
85
-
86
- def establish_connection
87
- ::ActiveRecord::Base.establish_connection(connection_hash)
88
- end
89
-
90
- end
91
- end
92
- end
@@ -1,106 +0,0 @@
1
- require 'active_record/base'
2
- require 'active_record/connection_adapters/abstract_adapter'
3
- require "database_cleaner/generic/truncation"
4
- require 'database_cleaner/active_record/base'
5
- require 'database_cleaner/active_record/truncation'
6
-
7
- module DatabaseCleaner
8
- module ConnectionAdapters
9
- module AbstractDeleteAdapter
10
- def delete_table(table_name)
11
- raise NotImplementedError
12
- end
13
- end
14
-
15
- module GenericDeleteAdapter
16
- def delete_table(table_name)
17
- execute("DELETE FROM #{quote_table_name(table_name)};")
18
- end
19
- end
20
-
21
- module OracleDeleteAdapter
22
- def delete_table(table_name)
23
- execute("DELETE FROM #{quote_table_name(table_name)}")
24
- end
25
- end
26
- end
27
- end
28
-
29
- module ActiveRecord
30
- module ConnectionAdapters
31
- AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractDeleteAdapter }
32
-
33
- JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(JdbcAdapter)
34
- AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(AbstractMysqlAdapter)
35
- Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(Mysql2Adapter)
36
- SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLiteAdapter)
37
- SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLite3Adapter)
38
- PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(PostgreSQLAdapter)
39
- IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(IBM_DBAdapter)
40
- SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::GenericDeleteAdapter } if defined?(SQLServerAdapter)
41
- OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleDeleteAdapter } if defined?(OracleEnhancedAdapter)
42
- end
43
- end
44
-
45
- module DatabaseCleaner::ActiveRecord
46
- module SelectiveTruncation
47
- def tables_to_truncate(connection)
48
- if information_schema_exists?(connection)
49
- (@only || tables_with_new_rows(connection)) - @tables_to_exclude
50
- else
51
- super
52
- end
53
- end
54
-
55
- def tables_with_new_rows(connection)
56
- @db_name ||= connection.instance_variable_get('@config')[:database]
57
- stats = table_stats_query(connection, @db_name)
58
- if stats != ''
59
- connection.exec_query(stats).inject([]) {|all, stat| all << stat['table_name'] if stat['exact_row_count'] > 0; all }
60
- else
61
- []
62
- end
63
- end
64
-
65
- def table_stats_query(connection, db_name)
66
- if @cache_tables && !@table_stats_query.nil?
67
- return @table_stats_query
68
- else
69
- @table_stats_query = connection.select_values(<<-SQL).join(' UNION ')
70
- SELECT CONCAT('SELECT \"', table_name, '\" AS table_name, COUNT(*) AS exact_row_count FROM ', table_name)
71
- FROM
72
- INFORMATION_SCHEMA.TABLES
73
- WHERE
74
- table_schema = '#{db_name}'
75
- AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
76
- SQL
77
- end
78
- end
79
-
80
- def information_schema_exists? connection
81
- return false unless connection.is_a? ActiveRecord::ConnectionAdapters::Mysql2Adapter
82
- @information_schema_exists ||=
83
- begin
84
- connection.execute("SELECT 1 FROM information_schema.tables")
85
- true
86
- rescue
87
- false
88
- end
89
- end
90
- end
91
-
92
- class Deletion < Truncation
93
- if defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
94
- include SelectiveTruncation
95
- end
96
-
97
- def clean
98
- connection = connection_class.connection
99
- connection.disable_referential_integrity do
100
- tables_to_truncate(connection).each do |table_name|
101
- connection.delete_table table_name
102
- end
103
- end
104
- end
105
- end
106
- end
@@ -1,59 +0,0 @@
1
- require 'database_cleaner/active_record/base'
2
- require 'database_cleaner/generic/transaction'
3
-
4
- module DatabaseCleaner::ActiveRecord
5
- class Transaction
6
- include ::DatabaseCleaner::ActiveRecord::Base
7
- include ::DatabaseCleaner::Generic::Transaction
8
-
9
- def start
10
- # Hack to make sure that the connection is properly setup for
11
- # the clean code.
12
- connection_class.connection.transaction{ }
13
-
14
- if connection_maintains_transaction_count?
15
- if connection_class.connection.respond_to?(:increment_open_transactions)
16
- connection_class.connection.increment_open_transactions
17
- else
18
- connection_class.__send__(:increment_open_transactions)
19
- end
20
- end
21
- if connection_class.connection.respond_to?(:begin_transaction)
22
- connection_class.connection.begin_transaction :joinable => false
23
- else
24
- connection_class.connection.begin_db_transaction
25
- end
26
- end
27
-
28
-
29
- def clean
30
- connection_class.connection_pool.connections.each do |connection|
31
- next unless connection.open_transactions > 0
32
-
33
- if connection.respond_to?(:rollback_transaction)
34
- connection.rollback_transaction
35
- else
36
- connection.rollback_db_transaction
37
- end
38
-
39
- # The below is for handling after_commit hooks.. see https://github.com/bmabey/database_cleaner/issues/99
40
- if connection.respond_to?(:rollback_transaction_records, true)
41
- connection.send(:rollback_transaction_records, true)
42
- end
43
-
44
- if connection_maintains_transaction_count?
45
- if connection.respond_to?(:decrement_open_transactions)
46
- connection.decrement_open_transactions
47
- else
48
- connection_class.__send__(:decrement_open_transactions)
49
- end
50
- end
51
- end
52
- end
53
-
54
- def connection_maintains_transaction_count?
55
- ActiveRecord::VERSION::MAJOR < 4
56
- end
57
-
58
- end
59
- end