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.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/lib/database_cleaner.rb +1 -12
  3. metadata +24 -418
  4. data/CONTRIBUTE.markdown +0 -28
  5. data/Gemfile.lock +0 -286
  6. data/History.rdoc +0 -453
  7. data/LICENSE +0 -20
  8. data/README.markdown +0 -541
  9. data/Rakefile +0 -40
  10. data/TODO +0 -3
  11. data/VERSION.yml +0 -4
  12. data/cucumber.yml +0 -1
  13. data/lib/database_cleaner/active_record/base.rb +0 -92
  14. data/lib/database_cleaner/active_record/deletion.rb +0 -108
  15. data/lib/database_cleaner/active_record/transaction.rb +0 -59
  16. data/lib/database_cleaner/active_record/truncation.rb +0 -275
  17. data/lib/database_cleaner/base.rb +0 -166
  18. data/lib/database_cleaner/configuration.rb +0 -131
  19. data/lib/database_cleaner/couch_potato/base.rb +0 -7
  20. data/lib/database_cleaner/couch_potato/truncation.rb +0 -28
  21. data/lib/database_cleaner/cucumber.rb +0 -3
  22. data/lib/database_cleaner/data_mapper/base.rb +0 -21
  23. data/lib/database_cleaner/data_mapper/transaction.rb +0 -28
  24. data/lib/database_cleaner/data_mapper/truncation.rb +0 -172
  25. data/lib/database_cleaner/generic/base.rb +0 -29
  26. data/lib/database_cleaner/generic/transaction.rb +0 -11
  27. data/lib/database_cleaner/generic/truncation.rb +0 -40
  28. data/lib/database_cleaner/mongo/base.rb +0 -16
  29. data/lib/database_cleaner/mongo/truncation.rb +0 -62
  30. data/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
  31. data/lib/database_cleaner/mongo2/base.rb +0 -16
  32. data/lib/database_cleaner/mongo2/truncation_mixin.rb +0 -39
  33. data/lib/database_cleaner/mongo_mapper/base.rb +0 -20
  34. data/lib/database_cleaner/mongo_mapper/truncation.rb +0 -19
  35. data/lib/database_cleaner/mongoid/base.rb +0 -20
  36. data/lib/database_cleaner/mongoid/truncation.rb +0 -49
  37. data/lib/database_cleaner/moped/base.rb +0 -39
  38. data/lib/database_cleaner/moped/truncation.rb +0 -9
  39. data/lib/database_cleaner/moped/truncation_base.rb +0 -40
  40. data/lib/database_cleaner/neo4j/base.rb +0 -62
  41. data/lib/database_cleaner/neo4j/deletion.rb +0 -16
  42. data/lib/database_cleaner/neo4j/transaction.rb +0 -35
  43. data/lib/database_cleaner/neo4j/truncation.rb +0 -9
  44. data/lib/database_cleaner/null_strategy.rb +0 -20
  45. data/lib/database_cleaner/ohm/truncation.rb +0 -15
  46. data/lib/database_cleaner/redis/base.rb +0 -37
  47. data/lib/database_cleaner/redis/truncation.rb +0 -26
  48. data/lib/database_cleaner/safeguard.rb +0 -72
  49. data/lib/database_cleaner/sequel/base.rb +0 -22
  50. data/lib/database_cleaner/sequel/deletion.rb +0 -47
  51. data/lib/database_cleaner/sequel/transaction.rb +0 -40
  52. data/lib/database_cleaner/sequel/truncation.rb +0 -78
@@ -1,131 +0,0 @@
1
- require 'database_cleaner/base'
2
-
3
- module DatabaseCleaner
4
-
5
- class NoORMDetected < StandardError; end
6
- class UnknownStrategySpecified < ArgumentError; end
7
-
8
- class << self
9
- def init_cleaners
10
- @cleaners ||= {}
11
- # ghetto ordered hash.. maintains 1.8 compat and old API
12
- @connections ||= []
13
- end
14
-
15
- def [](orm,opts = {})
16
- raise NoORMDetected unless orm
17
- init_cleaners
18
- # TODO: deprecate
19
- # this method conflates creation with lookup. Both a command and a query. Yuck.
20
- if @cleaners.has_key? [orm, opts]
21
- @cleaners[[orm, opts]]
22
- else
23
- add_cleaner(orm, opts)
24
- end
25
- end
26
-
27
- def add_cleaner(orm,opts = {})
28
- init_cleaners
29
- cleaner = DatabaseCleaner::Base.new(orm,opts)
30
- @cleaners[[orm, opts]] = cleaner
31
- @connections << cleaner
32
- cleaner
33
- end
34
-
35
- def app_root=(desired_root)
36
- @app_root = desired_root
37
- end
38
-
39
- def app_root
40
- @app_root ||= Dir.pwd
41
- end
42
-
43
- def connections
44
- # double yuck.. can't wait to deprecate this whole class...
45
- unless defined?(@cleaners) && @cleaners
46
- autodetected = ::DatabaseCleaner::Base.new
47
- add_cleaner(autodetected.orm)
48
- end
49
- @connections
50
- end
51
-
52
- def logger=(log_source)
53
- @logger = log_source
54
- end
55
-
56
- def logger
57
- return @logger if @logger
58
-
59
- @logger = Logger.new(STDOUT)
60
- @logger.level = Logger::ERROR
61
- @logger
62
- end
63
-
64
- def strategy=(stratagem)
65
- connections.each { |connect| connect.strategy = stratagem }
66
- remove_duplicates
67
- end
68
-
69
- def orm=(orm)
70
- connections.each { |connect| connect.orm = orm }
71
- remove_duplicates
72
- end
73
-
74
- def start
75
- connections.each { |connection| connection.start }
76
- end
77
-
78
- def clean
79
- connections.each { |connection| connection.clean }
80
- end
81
-
82
- alias clean! clean
83
-
84
- def cleaning(&inner_block)
85
- connections.inject(inner_block) do |curr_block, connection|
86
- proc { connection.cleaning(&curr_block) }
87
- end.call
88
- end
89
-
90
- def clean_with(*args)
91
- connections.each { |connection| connection.clean_with(*args) }
92
- end
93
-
94
- alias clean_with! clean_with
95
-
96
- def remove_duplicates
97
- temp = []
98
- connections.each do |connect|
99
- temp.push connect unless temp.include? connect
100
- end
101
- @connections = temp
102
- end
103
-
104
- def orm_module(symbol)
105
- case symbol
106
- when :active_record
107
- DatabaseCleaner::ActiveRecord
108
- when :data_mapper
109
- DatabaseCleaner::DataMapper
110
- when :mongo
111
- DatabaseCleaner::Mongo
112
- when :mongoid
113
- DatabaseCleaner::Mongoid
114
- when :mongo_mapper
115
- DatabaseCleaner::MongoMapper
116
- when :moped
117
- DatabaseCleaner::Moped
118
- when :couch_potato
119
- DatabaseCleaner::CouchPotato
120
- when :sequel
121
- DatabaseCleaner::Sequel
122
- when :ohm
123
- DatabaseCleaner::Ohm
124
- when :redis
125
- DatabaseCleaner::Redis
126
- when :neo4j
127
- DatabaseCleaner::Neo4j
128
- end
129
- end
130
- end
131
- end
@@ -1,7 +0,0 @@
1
- module DatabaseCleaner
2
- module CouchPotato
3
- def self.available_strategies
4
- %w[truncation]
5
- end
6
- end
7
- end
@@ -1,28 +0,0 @@
1
- require 'database_cleaner/generic/truncation'
2
-
3
- module DatabaseCleaner
4
- module CouchPotato
5
- class Truncation
6
- include ::DatabaseCleaner::Generic::Truncation
7
-
8
- def initialize(options = {})
9
- if options.has_key?(:only) || options.has_key?(:except)
10
- raise ArgumentError, "The :only and :except options are not available for use with CouchPotato/CouchDB."
11
- elsif !options.empty?
12
- raise ArgumentError, "Unsupported option. You specified #{options.keys.join(',')}."
13
- end
14
- super
15
- end
16
-
17
- def clean
18
- database.recreate!
19
- end
20
-
21
- private
22
-
23
- def database
24
- ::CouchPotato.couchrest_database
25
- end
26
- end
27
- end
28
- end
@@ -1,3 +0,0 @@
1
- Around do |scenario, block|
2
- DatabaseCleaner.cleaning(&block)
3
- end
@@ -1,21 +0,0 @@
1
- require 'database_cleaner/generic/base'
2
- module DatabaseCleaner
3
- module DataMapper
4
- def self.available_strategies
5
- %w[truncation transaction]
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
-
19
- end
20
- end
21
- end
@@ -1,28 +0,0 @@
1
- require 'database_cleaner/data_mapper/base'
2
- require 'database_cleaner/generic/transaction'
3
-
4
- module DatabaseCleaner::DataMapper
5
- class Transaction
6
- include ::DatabaseCleaner::DataMapper::Base
7
- include ::DatabaseCleaner::Generic::Transaction
8
-
9
- def start(repository = self.db)
10
- ::DataMapper.repository(repository) do |r|
11
- transaction = DataMapper::Transaction.new(r)
12
- transaction.begin
13
- r.adapter.push_transaction(transaction)
14
- end
15
- end
16
-
17
- def clean(repository = self.db)
18
- ::DataMapper.repository(repository) do |r|
19
- adapter = r.adapter
20
- while adapter.current_transaction
21
- adapter.current_transaction.rollback
22
- adapter.pop_transaction
23
- end
24
- end
25
- end
26
-
27
- end
28
- end
@@ -1,172 +0,0 @@
1
- require "database_cleaner/generic/truncation"
2
- require 'database_cleaner/data_mapper/base'
3
-
4
- module DataMapper
5
- module Adapters
6
-
7
- class DataObjectsAdapter
8
-
9
- def storage_names(repository = :default)
10
- raise NotImplementedError
11
- end
12
-
13
- def truncate_tables(table_names)
14
- table_names.each do |table_name|
15
- truncate_table table_name
16
- end
17
- end
18
-
19
- end
20
-
21
- class MysqlAdapter < DataObjectsAdapter
22
-
23
- # taken from http://github.com/godfat/dm-mapping/tree/master
24
- def storage_names(repository = :default)
25
- select 'SHOW TABLES'
26
- end
27
-
28
- def truncate_table(table_name)
29
- execute("TRUNCATE TABLE #{quote_name(table_name)};")
30
- end
31
-
32
- # copied from activerecord
33
- def disable_referential_integrity
34
- old = select("SELECT @@FOREIGN_KEY_CHECKS;")
35
- begin
36
- execute("SET FOREIGN_KEY_CHECKS = 0;")
37
- yield
38
- ensure
39
- execute("SET FOREIGN_KEY_CHECKS = ?", *old)
40
- end
41
- end
42
-
43
- end
44
-
45
- module SqliteAdapterMethods
46
-
47
- # taken from http://github.com/godfat/dm-mapping/tree/master
48
- def storage_names(repository = :default)
49
- # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
50
- sql = <<-SQL
51
- SELECT name
52
- FROM sqlite_master
53
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
54
- SQL
55
- # activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 181
56
- select(sql)
57
- end
58
-
59
- def truncate_table(table_name)
60
- execute("DELETE FROM #{quote_name(table_name)};")
61
- if uses_sequence?
62
- execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
63
- end
64
- end
65
-
66
- # this is a no-op copied from activerecord
67
- # i didn't find out if/how this is possible
68
- # activerecord also doesn't do more here
69
- def disable_referential_integrity
70
- yield
71
- end
72
-
73
- private
74
-
75
- # Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
76
- def uses_sequence?
77
- sql = <<-SQL
78
- SELECT name FROM sqlite_master
79
- WHERE type='table' AND name='sqlite_sequence'
80
- SQL
81
- select(sql).first
82
- end
83
- end
84
-
85
- class SqliteAdapter; include SqliteAdapterMethods; end
86
- class Sqlite3Adapter; include SqliteAdapterMethods; end
87
-
88
- # FIXME
89
- # i don't know if this works
90
- # i basically just copied activerecord code to get a rough idea what they do.
91
- # i don't have postgres available, so i won't be the one to write this.
92
- # maybe codes below gets some postgres/datamapper user going, though.
93
- class PostgresAdapter < DataObjectsAdapter
94
-
95
- # taken from http://github.com/godfat/dm-mapping/tree/master
96
- def storage_names(repository = :default)
97
- sql = <<-SQL
98
- SELECT table_name FROM "information_schema"."tables"
99
- WHERE table_schema = current_schema() and table_type = 'BASE TABLE'
100
- SQL
101
- select(sql)
102
- end
103
-
104
- def truncate_table(table_name)
105
- execute("TRUNCATE TABLE #{quote_name(table_name)} RESTART IDENTITY CASCADE;")
106
- end
107
-
108
- # override to use a single statement
109
- def truncate_tables(table_names)
110
- quoted_names = table_names.collect { |n| quote_name(n) }.join(', ')
111
- execute("TRUNCATE TABLE #{quoted_names} RESTART IDENTITY;")
112
- end
113
-
114
- # FIXME
115
- # copied from activerecord
116
- def supports_disable_referential_integrity?
117
- version = select("SHOW server_version")[0][0].split('.')
118
- (version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false
119
- rescue
120
- return false
121
- end
122
-
123
- # FIXME
124
- # copied unchanged from activerecord
125
- def disable_referential_integrity(repository = :default)
126
- if supports_disable_referential_integrity? then
127
- execute(storage_names(repository).collect do |name|
128
- "ALTER TABLE #{quote_name(name)} DISABLE TRIGGER ALL"
129
- end.join(";"))
130
- end
131
- yield
132
- ensure
133
- if supports_disable_referential_integrity? then
134
- execute(storage_names(repository).collect do |name|
135
- "ALTER TABLE #{quote_name(name)} ENABLE TRIGGER ALL"
136
- end.join(";"))
137
- end
138
- end
139
-
140
- end
141
-
142
- end
143
- end
144
-
145
-
146
- module DatabaseCleaner
147
- module DataMapper
148
- class Truncation
149
- include ::DatabaseCleaner::DataMapper::Base
150
- include ::DatabaseCleaner::Generic::Truncation
151
-
152
- def clean(repository = self.db)
153
- adapter = ::DataMapper.repository(repository).adapter
154
- adapter.disable_referential_integrity do
155
- adapter.truncate_tables(tables_to_truncate(repository))
156
- end
157
- end
158
-
159
- private
160
-
161
- def tables_to_truncate(repository = self.db)
162
- (@only || ::DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
163
- end
164
-
165
- # overwritten
166
- def migration_storage_names
167
- %w[migration_info]
168
- end
169
-
170
- end
171
- end
172
- end
@@ -1,29 +0,0 @@
1
- module ::DatabaseCleaner
2
- module Generic
3
- module Base
4
-
5
- def self.included(base)
6
- base.extend(ClassMethods)
7
- end
8
-
9
- def db
10
- :default
11
- end
12
-
13
- def cleaning(&block)
14
- begin
15
- start
16
- yield
17
- ensure
18
- clean
19
- end
20
- end
21
-
22
- module ClassMethods
23
- def available_strategies
24
- %W[]
25
- end
26
- end
27
- end
28
- end
29
- end
@@ -1,11 +0,0 @@
1
- module DatabaseCleaner
2
- module Generic
3
- module Transaction
4
- def initialize(opts = {})
5
- if !opts.empty?
6
- raise ArgumentError, "Options are not available for transaction strategies."
7
- end
8
- end
9
- end
10
- end
11
- end
@@ -1,40 +0,0 @@
1
- module DatabaseCleaner
2
- module Generic
3
- module Truncation
4
- def initialize(opts={})
5
- if !opts.empty? && !(opts.keys - [:only, :except, :pre_count, :reset_ids, :cache_tables]).empty?
6
- raise ArgumentError, "The only valid options are :only, :except, :pre_count, :reset_ids or :cache_tables. You specified #{opts.keys.join(',')}."
7
- end
8
- if opts.has_key?(:only) && opts.has_key?(:except)
9
- raise ArgumentError, "You may only specify either :only or :except. Doing both doesn't really make sense does it?"
10
- end
11
-
12
- @only = opts[:only]
13
- @tables_to_exclude = Array( (opts[:except] || []).dup ).flatten
14
- @tables_to_exclude += migration_storage_names
15
- @pre_count = opts[:pre_count]
16
- @reset_ids = opts[:reset_ids]
17
- @cache_tables = opts.has_key?(:cache_tables) ? !!opts[:cache_tables] : true
18
- end
19
-
20
- def start
21
- #included for compatability reasons, do nothing if you don't need to
22
- end
23
-
24
- def clean
25
- raise NotImplementedError
26
- end
27
-
28
- private
29
- def tables_to_truncate
30
- raise NotImplementedError
31
- end
32
-
33
- # overwrite in subclasses
34
- # default implementation given because migration storage need not be present
35
- def migration_storage_names
36
- %w[]
37
- end
38
- end
39
- end
40
- end
@@ -1,16 +0,0 @@
1
- module DatabaseCleaner
2
- module Mongo
3
- def self.available_strategies
4
- %w[truncation]
5
- end
6
- module Base
7
- def db=(desired_db)
8
- @db = desired_db
9
- end
10
-
11
- def db
12
- @db || raise("You have not specified a database. (see Mongo::Database)")
13
- end
14
- end
15
- end
16
- end
@@ -1,62 +0,0 @@
1
- require 'database_cleaner/mongo/base'
2
- require 'database_cleaner/generic/truncation'
3
- require 'database_cleaner/mongo/truncation_mixin'
4
- module DatabaseCleaner
5
- module Mongo
6
- class Truncation
7
- include ::DatabaseCleaner::Generic::Truncation
8
- include TruncationMixin
9
- include Base
10
- private
11
-
12
- def database
13
- db
14
- end
15
-
16
- def collections_cache
17
- @@collections_cache ||= {}
18
- end
19
-
20
- def mongoid_collection_names
21
- @@mongoid_collection_names ||= Hash.new{|h,k| h[k]=[]}.tap do |names|
22
- ObjectSpace.each_object(Class) do |klass|
23
- (names[klass.db.name] << klass.collection_name) if valid_collection_name?(klass)
24
- end
25
- end
26
- end
27
-
28
- def not_caching(db_name, list)
29
- @@not_caching ||= {}
30
-
31
- unless @@not_caching.has_key?(db_name)
32
- @@not_caching[db_name] = true
33
-
34
- puts "Not caching collection names for db #{db_name}. Missing these from models: #{list}"
35
- end
36
- end
37
-
38
- def collections
39
- return collections_cache[database.name] if collections_cache.has_key?(database.name)
40
- db_collections = database.collections.select { |c| c.name !~ /^system\./ }
41
-
42
- missing_collections = mongoid_collection_names[database.name] - db_collections.map(&:name)
43
-
44
- if missing_collections.empty?
45
- collections_cache[database.name] = db_collections
46
- else
47
- not_caching(database.name, missing_collections)
48
- end
49
-
50
- db_collections
51
- end
52
-
53
- private
54
-
55
- def valid_collection_name?(klass)
56
- klass.ancestors.map(&:to_s).include?('Mongoid::Document') &&
57
- !klass.embedded &&
58
- !klass.collection_name.empty?
59
- end
60
- end
61
- end
62
- end
@@ -1,26 +0,0 @@
1
- module DatabaseCleaner
2
- module Mongo
3
- module TruncationMixin
4
-
5
- def clean
6
- if @only
7
- collections.each { |c| c.send(truncate_method_name) if @only.include?(c.name) }
8
- else
9
- collections.each { |c| c.send(truncate_method_name) unless @tables_to_exclude.include?(c.name) }
10
- end
11
- true
12
- end
13
-
14
- private
15
-
16
- def collections
17
- database.collections.select { |c| c.name !~ /^system\./ }
18
- end
19
-
20
- def truncate_method_name
21
- # This constant only exists in the 2.x series.
22
- defined?(::Mongo::VERSION) ? :delete_many : :remove
23
- end
24
- end
25
- end
26
- end
@@ -1,16 +0,0 @@
1
- module DatabaseCleaner
2
- module Mongo2
3
- def self.available_strategies
4
- %w[truncation]
5
- end
6
- module Base
7
- def db=(desired_db)
8
- @db = desired_db
9
- end
10
-
11
- def db
12
- @db || raise("You have not specified a database. (see Mongo2::Database)")
13
- end
14
- end
15
- end
16
- end
@@ -1,39 +0,0 @@
1
- module DatabaseCleaner
2
- module Mongo2
3
- module TruncationMixin
4
-
5
- def clean
6
- if @only
7
- collections.each { |c| database[c].find.delete_many if @only.include?(c) }
8
- else
9
- collections.each { |c| database[c].find.delete_many unless @tables_to_exclude.include?(c) }
10
- end
11
- true
12
- end
13
-
14
- private
15
-
16
- def database
17
- if @db.nil? || @db == :default
18
- ::Mongoid::Clients.default
19
- else
20
- ::Mongoid::Clients.with_name(@db)
21
- end
22
- end
23
-
24
- def collections
25
- if db != :default
26
- database.use(db)
27
- end
28
-
29
- database.collections.collect { |c| c.namespace.split('.',2)[1] }
30
-
31
- # database['system.namespaces'].find(:name => { '$not' => /\.system\.|\$/ }).to_a.map do |collection|
32
- # _, name = collection['name'].split('.', 2)
33
- # name
34
- # end
35
- end
36
-
37
- end
38
- end
39
- end
@@ -1,20 +0,0 @@
1
- require 'database_cleaner/generic/base'
2
- module DatabaseCleaner
3
- module MongoMapper
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,19 +0,0 @@
1
- require 'database_cleaner/mongo_mapper/base'
2
- require 'database_cleaner/generic/truncation'
3
- require 'database_cleaner/mongo/truncation_mixin'
4
-
5
- module DatabaseCleaner
6
- module MongoMapper
7
- class Truncation
8
- include ::DatabaseCleaner::MongoMapper::Base
9
- include ::DatabaseCleaner::Generic::Truncation
10
- include ::DatabaseCleaner::Mongo::TruncationMixin
11
-
12
- private
13
-
14
- def database
15
- ::MongoMapper.database
16
- end
17
- end
18
- end
19
- end