database_cleaner 1.7.0 → 2.0.1

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 +17 -417
  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,166 +0,0 @@
1
- require 'database_cleaner/null_strategy'
2
- require 'database_cleaner/safeguard'
3
- module DatabaseCleaner
4
- class Base
5
- include Comparable
6
-
7
- def <=>(other)
8
- (self.orm <=> other.orm) == 0 ? self.db <=> other.db : self.orm <=> other.orm
9
- end
10
-
11
- def initialize(desired_orm = nil,opts = {})
12
- if [:autodetect, nil, "autodetect"].include?(desired_orm)
13
- autodetect
14
- else
15
- self.orm = desired_orm
16
- end
17
- self.db = opts[:connection] || opts[:model] if opts.has_key?(:connection) || opts.has_key?(:model)
18
- set_default_orm_strategy
19
- Safeguard.new.run
20
- end
21
-
22
- def db=(desired_db)
23
- self.strategy_db = desired_db
24
- @db = desired_db
25
- end
26
-
27
- def strategy_db=(desired_db)
28
- if strategy.respond_to? :db=
29
- strategy.db = desired_db
30
- elsif desired_db!= :default
31
- raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
32
- end
33
- end
34
-
35
- def db
36
- @db ||= :default
37
- end
38
-
39
- def create_strategy(*args)
40
- strategy, *strategy_args = args
41
- orm_strategy(strategy).new(*strategy_args)
42
- end
43
-
44
- def clean_with(*args)
45
- strategy = create_strategy(*args)
46
- set_strategy_db strategy, self.db
47
-
48
- strategy.clean
49
- strategy
50
- end
51
-
52
- alias clean_with! clean_with
53
-
54
- def set_strategy_db(strategy, desired_db)
55
- if strategy.respond_to? :db=
56
- strategy.db = desired_db
57
- elsif desired_db != :default
58
- raise ArgumentError, "You must provide a strategy object that supports non default databases when you specify a database"
59
- end
60
- end
61
-
62
- def strategy=(args)
63
- strategy, *strategy_args = args
64
- if strategy.is_a?(Symbol)
65
- @strategy = create_strategy(*args)
66
- elsif strategy_args.empty?
67
- @strategy = strategy
68
- else
69
- raise ArgumentError, "You must provide a strategy object, or a symbol for a known strategy along with initialization params."
70
- end
71
-
72
- set_strategy_db @strategy, self.db
73
-
74
- @strategy
75
- end
76
-
77
- def strategy
78
- @strategy ||= NullStrategy
79
- end
80
-
81
- def orm=(desired_orm)
82
- @orm = desired_orm.to_sym
83
- end
84
-
85
- def orm
86
- @orm || autodetect
87
- end
88
-
89
- def start
90
- strategy.start
91
- end
92
-
93
- def clean
94
- strategy.clean
95
- end
96
-
97
- alias clean! clean
98
-
99
- def cleaning(&block)
100
- strategy.cleaning(&block)
101
- end
102
-
103
- def auto_detected?
104
- !!@autodetected
105
- end
106
-
107
- def autodetect_orm
108
- if defined? ::ActiveRecord
109
- :active_record
110
- elsif defined? ::DataMapper
111
- :data_mapper
112
- elsif defined? ::MongoMapper
113
- :mongo_mapper
114
- elsif defined? ::Mongoid
115
- :mongoid
116
- elsif defined? ::CouchPotato
117
- :couch_potato
118
- elsif defined? ::Sequel
119
- :sequel
120
- elsif defined? ::Moped
121
- :moped
122
- elsif defined? ::Ohm
123
- :ohm
124
- elsif defined? ::Redis
125
- :redis
126
- elsif defined? ::Neo4j
127
- :neo4j
128
- end
129
- end
130
-
131
- private
132
-
133
- def orm_module
134
- ::DatabaseCleaner.orm_module(orm)
135
- end
136
-
137
- def orm_strategy(strategy)
138
- require "database_cleaner/#{orm.to_s}/#{strategy.to_s}"
139
- orm_module.const_get(strategy.to_s.capitalize)
140
- rescue LoadError
141
- if orm_module.respond_to? :available_strategies
142
- raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM! Available strategies: #{orm_module.available_strategies.join(', ')}"
143
- else
144
- raise UnknownStrategySpecified, "The '#{strategy}' strategy does not exist for the #{orm} ORM!"
145
- end
146
- end
147
-
148
- def autodetect
149
- @autodetected = true
150
-
151
- @orm ||= autodetect_orm ||
152
- raise(NoORMDetected, "No known ORM was detected! Is ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, Moped, or CouchPotato, Redis or Ohm loaded?")
153
- end
154
-
155
- def set_default_orm_strategy
156
- case orm
157
- when :active_record, :data_mapper, :sequel
158
- self.strategy = :transaction
159
- when :mongo_mapper, :mongoid, :couch_potato, :moped, :ohm, :redis
160
- self.strategy = :truncation
161
- when :neo4j
162
- self.strategy = :transaction
163
- end
164
- end
165
- end
166
- end
@@ -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