database_cleaner 1.7.0 → 1.8.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/CONTRIBUTE.markdown +1 -2
  3. data/Gemfile.lock +130 -91
  4. data/History.rdoc +1 -1
  5. data/README.markdown +71 -226
  6. data/Rakefile +12 -8
  7. data/adapters/database_cleaner-active_record/lib/database_cleaner-active_record.rb +1 -0
  8. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record.rb +6 -0
  9. data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/base.rb +13 -4
  10. data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/deletion.rb +19 -19
  11. data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/transaction.rb +0 -0
  12. data/{lib → adapters/database_cleaner-active_record/lib}/database_cleaner/active_record/truncation.rb +22 -19
  13. data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/version.rb +5 -0
  14. data/adapters/database_cleaner-couch_potato/lib/database_cleaner-couch_potato.rb +1 -0
  15. data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato.rb +11 -0
  16. data/{lib → adapters/database_cleaner-couch_potato/lib}/database_cleaner/couch_potato/base.rb +0 -0
  17. data/{lib → adapters/database_cleaner-couch_potato/lib}/database_cleaner/couch_potato/truncation.rb +0 -0
  18. data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/version.rb +5 -0
  19. data/adapters/database_cleaner-data_mapper/lib/database_cleaner-data_mapper.rb +1 -0
  20. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper.rb +4 -0
  21. data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/base.rb +4 -0
  22. data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/transaction.rb +0 -0
  23. data/{lib → adapters/database_cleaner-data_mapper/lib}/database_cleaner/data_mapper/truncation.rb +4 -3
  24. data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/version.rb +5 -0
  25. data/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb +1 -0
  26. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb +10 -0
  27. data/{lib → adapters/database_cleaner-mongo/lib}/database_cleaner/mongo/base.rb +0 -0
  28. data/{lib → adapters/database_cleaner-mongo/lib}/database_cleaner/mongo/truncation.rb +0 -0
  29. data/{lib → adapters/database_cleaner-mongo/lib}/database_cleaner/mongo/truncation_mixin.rb +0 -0
  30. data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb +5 -0
  31. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner-mongo_mapper.rb +1 -0
  32. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper.rb +4 -0
  33. data/{lib → adapters/database_cleaner-mongo_mapper/lib}/database_cleaner/mongo_mapper/base.rb +4 -0
  34. data/{lib → adapters/database_cleaner-mongo_mapper/lib}/database_cleaner/mongo_mapper/truncation.rb +0 -0
  35. data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/version.rb +5 -0
  36. data/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb +1 -0
  37. data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongo2/truncation_mixin.rb +0 -0
  38. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb +10 -0
  39. data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongoid/base.rb +0 -0
  40. data/{lib → adapters/database_cleaner-mongoid/lib}/database_cleaner/mongoid/truncation.rb +0 -0
  41. data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb +5 -0
  42. data/adapters/database_cleaner-moped/lib/database_cleaner-moped.rb +1 -0
  43. data/adapters/database_cleaner-moped/lib/database_cleaner/moped.rb +10 -0
  44. data/{lib → adapters/database_cleaner-moped/lib}/database_cleaner/moped/base.rb +1 -1
  45. data/{lib → adapters/database_cleaner-moped/lib}/database_cleaner/moped/truncation.rb +0 -0
  46. data/{lib → adapters/database_cleaner-moped/lib}/database_cleaner/moped/truncation_base.rb +4 -0
  47. data/adapters/database_cleaner-moped/lib/database_cleaner/moped/version.rb +5 -0
  48. data/adapters/database_cleaner-neo4j/lib/database_cleaner-neo4j.rb +1 -0
  49. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j.rb +6 -0
  50. data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/base.rb +4 -0
  51. data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/deletion.rb +0 -0
  52. data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/transaction.rb +0 -0
  53. data/{lib → adapters/database_cleaner-neo4j/lib}/database_cleaner/neo4j/truncation.rb +0 -0
  54. data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/version.rb +5 -0
  55. data/adapters/database_cleaner-ohm/lib/database_cleaner-ohm.rb +1 -0
  56. data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm.rb +10 -0
  57. data/{lib → adapters/database_cleaner-ohm/lib}/database_cleaner/ohm/truncation.rb +4 -0
  58. data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/version.rb +5 -0
  59. data/adapters/database_cleaner-redis/lib/database_cleaner-redis.rb +1 -0
  60. data/adapters/database_cleaner-redis/lib/database_cleaner/redis.rb +4 -0
  61. data/{lib → adapters/database_cleaner-redis/lib}/database_cleaner/redis/base.rb +4 -0
  62. data/{lib → adapters/database_cleaner-redis/lib}/database_cleaner/redis/truncation.rb +0 -0
  63. data/adapters/database_cleaner-redis/lib/database_cleaner/redis/version.rb +5 -0
  64. data/adapters/database_cleaner-sequel/lib/database_cleaner-sequel.rb +1 -0
  65. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel.rb +6 -0
  66. data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/base.rb +4 -0
  67. data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/deletion.rb +0 -0
  68. data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/transaction.rb +0 -0
  69. data/{lib → adapters/database_cleaner-sequel/lib}/database_cleaner/sequel/truncation.rb +16 -15
  70. data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/version.rb +5 -0
  71. data/lib/database_cleaner.rb +36 -1
  72. data/lib/database_cleaner/base.rb +85 -103
  73. data/lib/database_cleaner/configuration.rb +93 -83
  74. data/lib/database_cleaner/deprecation.rb +21 -0
  75. data/lib/database_cleaner/null_strategy.rb +6 -6
  76. data/lib/database_cleaner/orm_autodetector.rb +31 -0
  77. data/lib/database_cleaner/safeguard.rb +38 -3
  78. data/lib/database_cleaner/spec.rb +2 -0
  79. data/lib/database_cleaner/spec/database_helper.rb +82 -0
  80. data/lib/database_cleaner/spec/shared_examples.rb +15 -0
  81. data/lib/database_cleaner/version.rb +3 -0
  82. metadata +111 -46
  83. data/VERSION.yml +0 -4
  84. data/lib/database_cleaner/mongo2/base.rb +0 -16
data/Rakefile CHANGED
@@ -1,12 +1,12 @@
1
1
  require "rubygems"
2
2
  require "bundler"
3
+ require "bundler/gem_tasks"
3
4
  Bundler.setup
4
5
 
5
6
  require 'rake'
6
7
  require 'rspec/core'
7
8
  require 'rspec/core/rake_task'
8
9
  RSpec::Core::RakeTask.new(:spec) do |spec|
9
-
10
10
  spec.pattern = FileList['spec/**/*_spec.rb']
11
11
  end
12
12
 
@@ -15,15 +15,19 @@ RSpec::Core::RakeTask.new(:rcov) do |spec|
15
15
  spec.rcov = true
16
16
  end
17
17
 
18
- begin
19
- require 'cucumber/rake/task'
20
- Cucumber::Rake::Task.new(:features)
21
- rescue LoadError
22
- puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
23
- end
18
+ require 'cucumber/rake/task'
19
+ Cucumber::Rake::Task.new(:features)
24
20
 
25
- task :default => [:spec, :features]
21
+ desc "Run adapter test suites"
22
+ task :adapters do
23
+ Dir["adapters/*"].each do |adapter_dir|
24
+ Dir.chdir adapter_dir do
25
+ sh "bundle exec rake"
26
+ end
27
+ end
28
+ end
26
29
 
30
+ task :default => [:spec, :features, :adapters]
27
31
 
28
32
  desc "Cleans the project of any tmp file that should not be included in the gemspec."
29
33
  task :clean do
@@ -0,0 +1 @@
1
+ require "database_cleaner/active_record"
@@ -0,0 +1,6 @@
1
+ require 'database_cleaner/active_record/version'
2
+ require 'database_cleaner'
3
+ require 'database_cleaner/active_record/deletion'
4
+ require 'database_cleaner/active_record/transaction'
5
+ require 'database_cleaner/active_record/truncation'
6
+
@@ -1,20 +1,29 @@
1
- require 'database_cleaner/generic/base'
2
1
  require 'active_record'
2
+ require 'database_cleaner/generic/base'
3
3
  require 'erb'
4
+ require 'yaml'
4
5
 
5
6
  module DatabaseCleaner
6
7
  module ActiveRecord
7
-
8
8
  def self.available_strategies
9
9
  %w[truncation transaction deletion]
10
10
  end
11
11
 
12
+ def self.default_strategy
13
+ :transaction
14
+ end
15
+
12
16
  def self.config_file_location=(path)
13
17
  @config_file_location = path
14
18
  end
15
19
 
16
20
  def self.config_file_location
17
- @config_file_location ||= "#{DatabaseCleaner.app_root}/config/database.yml"
21
+ @config_file_location ||= begin
22
+ # Has DC.app_root been set? Check in this intrusive way to avoid triggering deprecation warnings if it hasn't.
23
+ app_root = DatabaseCleaner.send(:configuration).instance_variable_get(:@app_root)
24
+ root = app_root || Dir.pwd
25
+ "#{root}/config/database.yml"
26
+ end
18
27
  end
19
28
 
20
29
  module Base
@@ -79,7 +88,7 @@ module DatabaseCleaner
79
88
  if ::ActiveRecord::Base.respond_to?(:descendants)
80
89
  database_name = connection_hash["database"] || connection_hash[:database]
81
90
  models = ::ActiveRecord::Base.descendants
82
- models.detect { |m| m.connection_pool.spec.config[:database] == database_name }
91
+ models.select(&:connection_pool).detect { |m| m.connection_pool.spec.config[:database] == database_name }
83
92
  end
84
93
  end
85
94
 
@@ -1,7 +1,6 @@
1
- require 'active_record/base'
1
+ require 'active_record'
2
2
  require 'active_record/connection_adapters/abstract_adapter'
3
3
  require "database_cleaner/generic/truncation"
4
- require 'database_cleaner/active_record/base'
5
4
  require 'database_cleaner/active_record/truncation'
6
5
 
7
6
  module DatabaseCleaner
@@ -53,30 +52,31 @@ module DatabaseCleaner::ActiveRecord
53
52
  end
54
53
 
55
54
  def tables_with_new_rows(connection)
56
- @db_name ||= connection.instance_variable_get('@config')[:database]
57
- stats = table_stats_query(connection, @db_name)
55
+ stats = table_stats_query(connection)
58
56
  if stats != ''
59
- connection.exec_query(stats).inject([]) {|all, stat| all << stat['table_name'] if stat['exact_row_count'] > 0; all }
57
+ connection.select_values(stats)
60
58
  else
61
59
  []
62
60
  end
63
61
  end
64
62
 
65
- def table_stats_query(connection, db_name)
66
- if @cache_tables && !@table_stats_query.nil?
67
- return @table_stats_query
68
- else
69
- tables = connection.select_values(<<-SQL)
70
- SELECT table_name
71
- FROM information_schema.tables
72
- WHERE table_schema = '#{db_name}'
73
- AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
74
- SQL
75
- queries = tables.map do |table|
76
- "SELECT #{connection.quote(table)} AS table_name, COUNT(*) AS exact_row_count FROM #{connection.quote_table_name(table)}"
77
- end
78
- @table_stats_query = queries.join(' UNION ')
63
+ def table_stats_query(connection)
64
+ @table_stats_query ||= build_table_stats_query(connection)
65
+ ensure
66
+ @table_stats_query = nil unless @cache_tables
67
+ end
68
+
69
+ def build_table_stats_query(connection)
70
+ tables = connection.select_values(<<-SQL)
71
+ SELECT table_name
72
+ FROM information_schema.tables
73
+ WHERE table_schema = database()
74
+ AND #{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('table_name')};
75
+ SQL
76
+ queries = tables.map do |table|
77
+ "(SELECT #{connection.quote(table)} FROM #{connection.quote_table_name(table)} LIMIT 1)"
79
78
  end
79
+ queries.join(' UNION ALL ')
80
80
  end
81
81
 
82
82
  def information_schema_exists? connection
@@ -1,10 +1,10 @@
1
1
  require 'active_record/base'
2
-
2
+ require 'database_cleaner/active_record/base'
3
3
  require 'active_record/connection_adapters/abstract_adapter'
4
4
 
5
5
  #Load available connection adapters
6
6
  %w(
7
- abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter
7
+ abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter oracle_enhanced_adapter
8
8
  ).each do |known_adapter|
9
9
  begin
10
10
  require "active_record/connection_adapters/#{known_adapter}"
@@ -27,7 +27,11 @@ module DatabaseCleaner
27
27
 
28
28
  def database_cleaner_table_cache
29
29
  # the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
30
- @database_cleaner_tables ||= ::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
30
+ @database_cleaner_tables ||= database_tables
31
+ end
32
+
33
+ def database_tables
34
+ ::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
31
35
  end
32
36
 
33
37
  def truncate_table(table_name)
@@ -63,22 +67,21 @@ module DatabaseCleaner
63
67
  select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)").to_i
64
68
  end
65
69
 
66
- # Returns a boolean indicating if the given table has an auto-inc number higher than 0.
67
- # Note, this is different than an empty table since an table may populated, the index increased,
68
- # but then the table is cleaned. In other words, this function tells us if the given table
69
- # was ever inserted into.
70
- def has_been_used?(table)
71
- if has_rows?(table)
72
- true
73
- else
74
- # Patch for MysqlAdapter with ActiveRecord 3.2.7 later
75
- # select_value("SELECT 1") #=> "1"
76
- select_value(<<-SQL).to_i > 1 # returns nil if not present
77
- SELECT Auto_increment
70
+ def auto_increment_value(table)
71
+ select_value(<<-SQL).to_i
72
+ SELECT auto_increment
78
73
  FROM information_schema.tables
79
- WHERE table_name='#{table}';
80
- SQL
81
- end
74
+ WHERE table_name = '#{table}'
75
+ AND table_schema = database()
76
+ SQL
77
+ end
78
+
79
+ # This method tells us if the given table has been inserted into since its
80
+ # last truncation. Note that the table might have been populated, which
81
+ # increased the auto-increment counter, but then cleaned again such that
82
+ # it appears empty now.
83
+ def has_been_used?(table)
84
+ has_rows?(table) || auto_increment_value(table) > 1
82
85
  end
83
86
 
84
87
  def has_rows?(table)
@@ -244,7 +247,7 @@ module DatabaseCleaner::ActiveRecord
244
247
  private
245
248
 
246
249
  def tables_to_truncate(connection)
247
- tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.tables
250
+ tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.database_tables
248
251
  to_reject = (@tables_to_exclude + connection.database_cleaner_view_cache)
249
252
  (@only || tables_in_db).reject do |table|
250
253
  if ( m = table.match(/([^.]+)$/) )
@@ -0,0 +1,5 @@
1
+ module DatabaseCleaner
2
+ module ActiveRecord
3
+ VERSION = "1.8.0.beta"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "database_cleaner/couch_potato"
@@ -0,0 +1,11 @@
1
+ require "database_cleaner/couch_potato/version"
2
+ require "database_cleaner"
3
+ require "database_cleaner/couch_potato/base"
4
+ require "database_cleaner/couch_potato/truncation"
5
+
6
+ module DatabaseCleaner::CouchPotato
7
+ def self.default_strategy
8
+ :truncation
9
+ end
10
+ end
11
+
@@ -0,0 +1,5 @@
1
+ module DatabaseCleaner
2
+ module CouchPotato
3
+ VERSION = "1.8.0.beta"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "database_cleaner/data_mapper"
@@ -0,0 +1,4 @@
1
+ require "database_cleaner/data_mapper/version"
2
+ require "database_cleaner"
3
+ require "database_cleaner/data_mapper/transaction"
4
+ require "database_cleaner/data_mapper/truncation"
@@ -5,6 +5,10 @@ module DatabaseCleaner
5
5
  %w[truncation transaction]
6
6
  end
7
7
 
8
+ def self.default_strategy
9
+ :transaction
10
+ end
11
+
8
12
  module Base
9
13
  include ::DatabaseCleaner::Generic::Base
10
14
 
@@ -1,5 +1,6 @@
1
1
  require "database_cleaner/generic/truncation"
2
2
  require 'database_cleaner/data_mapper/base'
3
+ require 'data_mapper'
3
4
 
4
5
  module DataMapper
5
6
  module Adapters
@@ -20,7 +21,7 @@ module DataMapper
20
21
 
21
22
  class MysqlAdapter < DataObjectsAdapter
22
23
 
23
- # taken from http://github.com/godfat/dm-mapping/tree/master
24
+ # taken from https://github.com/godfat/dm-mapping/tree/master
24
25
  def storage_names(repository = :default)
25
26
  select 'SHOW TABLES'
26
27
  end
@@ -44,7 +45,7 @@ module DataMapper
44
45
 
45
46
  module SqliteAdapterMethods
46
47
 
47
- # taken from http://github.com/godfat/dm-mapping/tree/master
48
+ # taken from https://github.com/godfat/dm-mapping/tree/master
48
49
  def storage_names(repository = :default)
49
50
  # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
50
51
  sql = <<-SQL
@@ -92,7 +93,7 @@ module DataMapper
92
93
  # maybe codes below gets some postgres/datamapper user going, though.
93
94
  class PostgresAdapter < DataObjectsAdapter
94
95
 
95
- # taken from http://github.com/godfat/dm-mapping/tree/master
96
+ # taken from https://github.com/godfat/dm-mapping/tree/master
96
97
  def storage_names(repository = :default)
97
98
  sql = <<-SQL
98
99
  SELECT table_name FROM "information_schema"."tables"
@@ -0,0 +1,5 @@
1
+ module DatabaseCleaner
2
+ module DataMapper
3
+ VERSION = "1.8.0.beta"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "database_cleaner/mongo"
@@ -0,0 +1,10 @@
1
+ require 'database_cleaner/mongo/version'
2
+ require 'database_cleaner'
3
+ require 'database_cleaner/mongo/truncation'
4
+
5
+ module DatabaseCleaner::Mongo
6
+ def self.default_strategy
7
+ :truncation
8
+ end
9
+ end
10
+
@@ -0,0 +1,5 @@
1
+ module DatabaseCleaner
2
+ module Mongo
3
+ VERSION = "1.8.0.beta"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "database_cleaner/mongo_mapper"
@@ -0,0 +1,4 @@
1
+ require "database_cleaner/mongo_mapper/version"
2
+ require "database_cleaner"
3
+ require "database_cleaner/mongo_mapper/truncation"
4
+
@@ -5,6 +5,10 @@ module DatabaseCleaner
5
5
  %w[truncation]
6
6
  end
7
7
 
8
+ def self.default_strategy
9
+ :truncation
10
+ end
11
+
8
12
  module Base
9
13
  include ::DatabaseCleaner::Generic::Base
10
14
 
@@ -0,0 +1,5 @@
1
+ module DatabaseCleaner
2
+ module MongoMapper
3
+ VERSION = "1.8.0.beta"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "database_cleaner/mongoid"
@@ -0,0 +1,10 @@
1
+ require "database_cleaner/mongoid/version"
2
+ require "database_cleaner"
3
+ require "database_cleaner/mongoid/truncation"
4
+
5
+ module DatabaseCleaner::Mongoid
6
+ def self.default_strategy
7
+ :truncation
8
+ end
9
+ end
10
+
@@ -0,0 +1,5 @@
1
+ module DatabaseCleaner
2
+ module Mongoid
3
+ VERSION = "1.8.0.beta"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "database_cleaner/moped"
@@ -0,0 +1,10 @@
1
+ require "database_cleaner/moped/version"
2
+ require "database_cleaner"
3
+ require "database_cleaner/moped/truncation"
4
+
5
+ module DatabaseCleaner::Moped
6
+ def self.default_strategy
7
+ :truncation
8
+ end
9
+ end
10
+
@@ -32,7 +32,7 @@ module DatabaseCleaner
32
32
  private
33
33
 
34
34
  def session
35
- ::Moped::Session.new([host], database: db)
35
+ @session ||= ::Moped::Session.new([host], database: db)
36
36
  end
37
37
  end
38
38
  end
@@ -13,6 +13,7 @@ module DatabaseCleaner
13
13
  else
14
14
  collections.each { |c| session[c].find.remove_all unless @tables_to_exclude.include?(c) }
15
15
  end
16
+ wait_for_removals_to_finish
16
17
  true
17
18
  end
18
19
 
@@ -35,6 +36,9 @@ module DatabaseCleaner
35
36
  end
36
37
  end
37
38
 
39
+ def wait_for_removals_to_finish
40
+ session.command(getlasterror: 1)
41
+ end
38
42
  end
39
43
  end
40
44
  end