database_cleaner 1.99.0 → 2.0.1
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 -47
- metadata +12 -482
- data/CONTRIBUTE.markdown +0 -27
- data/Gemfile.lock +0 -324
- data/History.rdoc +0 -514
- data/LICENSE +0 -20
- data/README.markdown +0 -389
- data/Rakefile +0 -44
- data/TODO +0 -3
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/base.rb +0 -101
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/deletion.rb +0 -108
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/transaction.rb +0 -59
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/truncation.rb +0 -288
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/version.rb +0 -5
- data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record.rb +0 -6
- data/adapters/database_cleaner-active_record/lib/database_cleaner-active_record.rb +0 -1
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/base.rb +0 -7
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/truncation.rb +0 -28
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato/version.rb +0 -5
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner/couch_potato.rb +0 -13
- data/adapters/database_cleaner-couch_potato/lib/database_cleaner-couch_potato.rb +0 -1
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/base.rb +0 -25
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/transaction.rb +0 -28
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/truncation.rb +0 -173
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper/version.rb +0 -5
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner/data_mapper.rb +0 -6
- data/adapters/database_cleaner-data_mapper/lib/database_cleaner-data_mapper.rb +0 -1
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/base.rb +0 -16
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/deletion.rb +0 -8
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation.rb +0 -74
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/truncation_mixin.rb +0 -26
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo/version.rb +0 -5
- data/adapters/database_cleaner-mongo/lib/database_cleaner/mongo.rb +0 -11
- data/adapters/database_cleaner-mongo/lib/database_cleaner-mongo.rb +0 -1
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/base.rb +0 -24
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/truncation.rb +0 -35
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper/version.rb +0 -5
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner/mongo_mapper.rb +0 -5
- data/adapters/database_cleaner-mongo_mapper/lib/database_cleaner-mongo_mapper.rb +0 -1
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/base.rb +0 -20
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/deletion.rb +0 -10
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo1_truncation_mixin.rb +0 -26
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongo2_truncation_mixin.rb +0 -38
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/mongoid_truncation_mixin.rb +0 -65
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/truncation.rb +0 -49
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid/version.rb +0 -5
- data/adapters/database_cleaner-mongoid/lib/database_cleaner/mongoid.rb +0 -11
- data/adapters/database_cleaner-mongoid/lib/database_cleaner-mongoid.rb +0 -1
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/base.rb +0 -39
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/truncation.rb +0 -9
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/truncation_base.rb +0 -44
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped/version.rb +0 -5
- data/adapters/database_cleaner-moped/lib/database_cleaner/moped.rb +0 -12
- data/adapters/database_cleaner-moped/lib/database_cleaner-moped.rb +0 -1
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/base.rb +0 -66
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/deletion.rb +0 -16
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/transaction.rb +0 -35
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/truncation.rb +0 -9
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j/version.rb +0 -5
- data/adapters/database_cleaner-neo4j/lib/database_cleaner/neo4j.rb +0 -7
- data/adapters/database_cleaner-neo4j/lib/database_cleaner-neo4j.rb +0 -1
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/truncation.rb +0 -24
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm/version.rb +0 -5
- data/adapters/database_cleaner-ohm/lib/database_cleaner/ohm.rb +0 -12
- data/adapters/database_cleaner-ohm/lib/database_cleaner-ohm.rb +0 -1
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/base.rb +0 -45
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/deletion.rb +0 -9
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/truncation.rb +0 -26
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis/version.rb +0 -5
- data/adapters/database_cleaner-redis/lib/database_cleaner/redis.rb +0 -5
- data/adapters/database_cleaner-redis/lib/database_cleaner-redis.rb +0 -1
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/base.rb +0 -26
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/deletion.rb +0 -47
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/transaction.rb +0 -40
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/truncation.rb +0 -79
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel/version.rb +0 -5
- data/adapters/database_cleaner-sequel/lib/database_cleaner/sequel.rb +0 -6
- data/adapters/database_cleaner-sequel/lib/database_cleaner-sequel.rb +0 -1
- data/cucumber.yml +0 -1
- data/lib/database_cleaner/base.rb +0 -169
- data/lib/database_cleaner/configuration.rb +0 -136
- data/lib/database_cleaner/cucumber.rb +0 -3
- data/lib/database_cleaner/deprecation.rb +0 -26
- 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/null_strategy.rb +0 -20
- data/lib/database_cleaner/orm_autodetector.rb +0 -41
- data/lib/database_cleaner/safeguard.rb +0 -107
- data/lib/database_cleaner/spec/database_helper.rb +0 -82
- data/lib/database_cleaner/spec/shared_examples.rb +0 -15
- data/lib/database_cleaner/spec.rb +0 -2
- data/lib/database_cleaner/version.rb +0 -3
data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/transaction.rb
DELETED
@@ -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
|
data/adapters/database_cleaner-active_record/lib/database_cleaner/active_record/truncation.rb
DELETED
@@ -1,288 +0,0 @@
|
|
1
|
-
require 'active_record/base'
|
2
|
-
require 'database_cleaner/active_record/base'
|
3
|
-
require 'active_record/connection_adapters/abstract_adapter'
|
4
|
-
require 'database_cleaner/deprecation'
|
5
|
-
|
6
|
-
#Load available connection adapters
|
7
|
-
%w(
|
8
|
-
abstract_mysql_adapter postgresql_adapter sqlite3_adapter mysql_adapter mysql2_adapter oracle_enhanced_adapter
|
9
|
-
).each do |known_adapter|
|
10
|
-
begin
|
11
|
-
require "active_record/connection_adapters/#{known_adapter}"
|
12
|
-
rescue LoadError
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
require "database_cleaner/generic/truncation"
|
17
|
-
require 'database_cleaner/active_record/base'
|
18
|
-
|
19
|
-
module DatabaseCleaner
|
20
|
-
module ConnectionAdapters
|
21
|
-
|
22
|
-
module AbstractAdapter
|
23
|
-
# used to be called views but that can clash with gems like schema_plus
|
24
|
-
# this gem is not meant to be exposing such an extra interface any way
|
25
|
-
def database_cleaner_view_cache
|
26
|
-
@views ||= select_values("select table_name from information_schema.views where table_schema = '#{current_database}'") rescue []
|
27
|
-
end
|
28
|
-
|
29
|
-
def database_cleaner_table_cache
|
30
|
-
# the adapters don't do caching (#130) but we make the assumption that the list stays the same in tests
|
31
|
-
@database_cleaner_tables ||= database_tables
|
32
|
-
end
|
33
|
-
|
34
|
-
def database_tables
|
35
|
-
::ActiveRecord::VERSION::MAJOR >= 5 ? data_sources : tables
|
36
|
-
end
|
37
|
-
|
38
|
-
def truncate_table(table_name)
|
39
|
-
raise NotImplementedError
|
40
|
-
end
|
41
|
-
|
42
|
-
def truncate_tables(tables)
|
43
|
-
tables.each do |table_name|
|
44
|
-
self.truncate_table(table_name)
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
module AbstractMysqlAdapter
|
50
|
-
def truncate_table(table_name)
|
51
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
52
|
-
end
|
53
|
-
|
54
|
-
def truncate_tables(tables)
|
55
|
-
tables.each { |t| truncate_table(t) }
|
56
|
-
end
|
57
|
-
|
58
|
-
def pre_count_truncate_tables(tables, options = {:reset_ids => true})
|
59
|
-
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
60
|
-
truncate_tables(tables.select(&filter))
|
61
|
-
end
|
62
|
-
|
63
|
-
private
|
64
|
-
|
65
|
-
def row_count(table)
|
66
|
-
# Patch for MysqlAdapter with ActiveRecord 3.2.7 later
|
67
|
-
# select_value("SELECT 1") #=> "1"
|
68
|
-
select_value("SELECT EXISTS (SELECT 1 FROM #{quote_table_name(table)} LIMIT 1)").to_i
|
69
|
-
end
|
70
|
-
|
71
|
-
def auto_increment_value(table)
|
72
|
-
select_value(<<-SQL).to_i
|
73
|
-
SELECT auto_increment
|
74
|
-
FROM information_schema.tables
|
75
|
-
WHERE table_name = '#{table}'
|
76
|
-
AND table_schema = database()
|
77
|
-
SQL
|
78
|
-
end
|
79
|
-
|
80
|
-
# This method tells us if the given table has been inserted into since its
|
81
|
-
# last truncation. Note that the table might have been populated, which
|
82
|
-
# increased the auto-increment counter, but then cleaned again such that
|
83
|
-
# it appears empty now.
|
84
|
-
def has_been_used?(table)
|
85
|
-
has_rows?(table) || auto_increment_value(table) > 1
|
86
|
-
end
|
87
|
-
|
88
|
-
def has_rows?(table)
|
89
|
-
row_count(table) > 0
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
module IBM_DBAdapter
|
94
|
-
def truncate_table(table_name)
|
95
|
-
execute("TRUNCATE #{quote_table_name(table_name)} IMMEDIATE")
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
module SQLiteAdapter
|
100
|
-
def delete_table(table_name)
|
101
|
-
execute("DELETE FROM #{quote_table_name(table_name)};")
|
102
|
-
if uses_sequence
|
103
|
-
execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
|
104
|
-
end
|
105
|
-
end
|
106
|
-
alias truncate_table delete_table
|
107
|
-
|
108
|
-
def truncate_tables(tables)
|
109
|
-
tables.each { |t| truncate_table(t) }
|
110
|
-
end
|
111
|
-
|
112
|
-
private
|
113
|
-
|
114
|
-
# Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
|
115
|
-
def uses_sequence
|
116
|
-
select_value("SELECT name FROM sqlite_master WHERE type='table' AND name='sqlite_sequence';")
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
module TruncateOrDelete
|
121
|
-
def truncate_table(table_name)
|
122
|
-
begin
|
123
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)};")
|
124
|
-
rescue ::ActiveRecord::StatementInvalid
|
125
|
-
execute("DELETE FROM #{quote_table_name(table_name)};")
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
|
130
|
-
module OracleAdapter
|
131
|
-
def truncate_table(table_name)
|
132
|
-
execute("TRUNCATE TABLE #{quote_table_name(table_name)}")
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
module PostgreSQLAdapter
|
137
|
-
def db_version
|
138
|
-
@db_version ||= postgresql_version
|
139
|
-
end
|
140
|
-
|
141
|
-
def cascade
|
142
|
-
@cascade ||= db_version >= 80200 ? 'CASCADE' : ''
|
143
|
-
end
|
144
|
-
|
145
|
-
def restart_identity
|
146
|
-
@restart_identity ||= db_version >= 80400 ? 'RESTART IDENTITY' : ''
|
147
|
-
end
|
148
|
-
|
149
|
-
def truncate_table(table_name)
|
150
|
-
truncate_tables([table_name])
|
151
|
-
end
|
152
|
-
|
153
|
-
def truncate_tables(table_names)
|
154
|
-
return if table_names.nil? || table_names.empty?
|
155
|
-
execute("TRUNCATE TABLE #{table_names.map{|name| quote_table_name(name)}.join(', ')} #{restart_identity} #{cascade};")
|
156
|
-
end
|
157
|
-
|
158
|
-
def pre_count_truncate_tables(tables, options = {:reset_ids => true})
|
159
|
-
filter = options[:reset_ids] ? method(:has_been_used?) : method(:has_rows?)
|
160
|
-
truncate_tables(tables.select(&filter))
|
161
|
-
end
|
162
|
-
|
163
|
-
def database_cleaner_table_cache
|
164
|
-
# AR returns a list of tables without schema but then returns a
|
165
|
-
# migrations table with the schema. There are other problems, too,
|
166
|
-
# with using the base list. If a table exists in multiple schemas
|
167
|
-
# within the search path, truncation without the schema name could
|
168
|
-
# result in confusing, if not unexpected results.
|
169
|
-
@database_cleaner_tables ||= tables_with_schema
|
170
|
-
end
|
171
|
-
|
172
|
-
private
|
173
|
-
|
174
|
-
# Returns a boolean indicating if the given table has an auto-inc number higher than 0.
|
175
|
-
# Note, this is different than an empty table since an table may populated, the index increased,
|
176
|
-
# but then the table is cleaned. In other words, this function tells us if the given table
|
177
|
-
# was ever inserted into.
|
178
|
-
def has_been_used?(table)
|
179
|
-
return has_rows?(table) unless has_sequence?(table)
|
180
|
-
|
181
|
-
cur_val = select_value("SELECT currval('#{table}_id_seq');").to_i rescue 0
|
182
|
-
cur_val > 0
|
183
|
-
end
|
184
|
-
|
185
|
-
def has_sequence?(table)
|
186
|
-
select_value("SELECT true FROM pg_class WHERE relname = '#{table}_id_seq';")
|
187
|
-
end
|
188
|
-
|
189
|
-
def has_rows?(table)
|
190
|
-
select_value("SELECT true FROM #{table} LIMIT 1;")
|
191
|
-
end
|
192
|
-
|
193
|
-
def tables_with_schema
|
194
|
-
rows = select_rows <<-_SQL
|
195
|
-
SELECT schemaname || '.' || tablename
|
196
|
-
FROM pg_tables
|
197
|
-
WHERE
|
198
|
-
tablename !~ '_prt_' AND
|
199
|
-
#{::DatabaseCleaner::ActiveRecord::Base.exclusion_condition('tablename')} AND
|
200
|
-
schemaname = ANY (current_schemas(false))
|
201
|
-
_SQL
|
202
|
-
rows.collect { |result| result.first }
|
203
|
-
end
|
204
|
-
end
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
module ActiveRecord
|
209
|
-
module ConnectionAdapters
|
210
|
-
#Apply adapter decoraters where applicable (adapter should be loaded)
|
211
|
-
AbstractAdapter.class_eval { include DatabaseCleaner::ConnectionAdapters::AbstractAdapter }
|
212
|
-
|
213
|
-
if defined?(JdbcAdapter)
|
214
|
-
if defined?(OracleJdbcConnection)
|
215
|
-
JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter }
|
216
|
-
else
|
217
|
-
JdbcAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete }
|
218
|
-
end
|
219
|
-
end
|
220
|
-
AbstractMysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(AbstractMysqlAdapter)
|
221
|
-
Mysql2Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(Mysql2Adapter)
|
222
|
-
MysqlAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::AbstractMysqlAdapter } if defined?(MysqlAdapter)
|
223
|
-
SQLiteAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLiteAdapter)
|
224
|
-
SQLite3Adapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::SQLiteAdapter } if defined?(SQLite3Adapter)
|
225
|
-
PostgreSQLAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::PostgreSQLAdapter } if defined?(PostgreSQLAdapter)
|
226
|
-
IBM_DBAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::IBM_DBAdapter } if defined?(IBM_DBAdapter)
|
227
|
-
SQLServerAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::TruncateOrDelete } if defined?(SQLServerAdapter)
|
228
|
-
OracleEnhancedAdapter.class_eval { include ::DatabaseCleaner::ConnectionAdapters::OracleAdapter } if defined?(OracleEnhancedAdapter)
|
229
|
-
end
|
230
|
-
end
|
231
|
-
|
232
|
-
module DatabaseCleaner::ActiveRecord
|
233
|
-
class Truncation
|
234
|
-
include ::DatabaseCleaner::ActiveRecord::Base
|
235
|
-
include ::DatabaseCleaner::Generic::Truncation
|
236
|
-
|
237
|
-
def initialize(*)
|
238
|
-
super
|
239
|
-
if !@reset_ids.nil?
|
240
|
-
DatabaseCleaner.deprecate "The `:reset_ids` option for the ActiveRecord truncation strategy has no effect, and is deprecated. It will be removed in database_cleaner 2.0 with no replacement."
|
241
|
-
end
|
242
|
-
end
|
243
|
-
|
244
|
-
def clean
|
245
|
-
connection = connection_class.connection
|
246
|
-
connection.disable_referential_integrity do
|
247
|
-
if pre_count? && connection.respond_to?(:pre_count_truncate_tables)
|
248
|
-
connection.pre_count_truncate_tables(tables_to_truncate(connection), {:reset_ids => reset_ids?})
|
249
|
-
else
|
250
|
-
connection.truncate_tables(tables_to_truncate(connection))
|
251
|
-
end
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
private
|
256
|
-
|
257
|
-
def tables_to_truncate(connection)
|
258
|
-
tables_in_db = cache_tables? ? connection.database_cleaner_table_cache : connection.database_tables
|
259
|
-
to_reject = (@tables_to_exclude + connection.database_cleaner_view_cache)
|
260
|
-
(@only || tables_in_db).reject do |table|
|
261
|
-
if ( m = table.match(/([^.]+)$/) )
|
262
|
-
to_reject.include?(m[1])
|
263
|
-
else
|
264
|
-
false
|
265
|
-
end
|
266
|
-
end
|
267
|
-
end
|
268
|
-
|
269
|
-
# overwritten
|
270
|
-
def migration_storage_names
|
271
|
-
result = [::DatabaseCleaner::ActiveRecord::Base.migration_table_name]
|
272
|
-
result << ::ActiveRecord::Base.internal_metadata_table_name if ::ActiveRecord::VERSION::MAJOR >= 5
|
273
|
-
result
|
274
|
-
end
|
275
|
-
|
276
|
-
def cache_tables?
|
277
|
-
!!@cache_tables
|
278
|
-
end
|
279
|
-
|
280
|
-
def pre_count?
|
281
|
-
@pre_count == true
|
282
|
-
end
|
283
|
-
|
284
|
-
def reset_ids?
|
285
|
-
@reset_ids != false
|
286
|
-
end
|
287
|
-
end
|
288
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
require "database_cleaner/active_record"
|
@@ -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,13 +0,0 @@
|
|
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
|
-
DatabaseCleaner.deprecate "Due to lack of maintenance, the Couch Potato adapter for DatabaseCleaner is deprecated, and will be removed in v2.0 with no replacement. Contact us if you are interested in resurrecting this adapter!"
|
7
|
-
|
8
|
-
module DatabaseCleaner::CouchPotato
|
9
|
-
def self.default_strategy
|
10
|
-
:truncation
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
@@ -1 +0,0 @@
|
|
1
|
-
require "database_cleaner/couch_potato"
|
@@ -1,25 +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
|
-
def self.default_strategy
|
9
|
-
:transaction
|
10
|
-
end
|
11
|
-
|
12
|
-
module Base
|
13
|
-
include ::DatabaseCleaner::Generic::Base
|
14
|
-
|
15
|
-
def db=(desired_db)
|
16
|
-
@db = desired_db
|
17
|
-
end
|
18
|
-
|
19
|
-
def db
|
20
|
-
@db ||= :default
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
24
|
-
end
|
25
|
-
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,173 +0,0 @@
|
|
1
|
-
require "database_cleaner/generic/truncation"
|
2
|
-
require 'database_cleaner/data_mapper/base'
|
3
|
-
require 'data_mapper'
|
4
|
-
|
5
|
-
module DataMapper
|
6
|
-
module Adapters
|
7
|
-
|
8
|
-
class DataObjectsAdapter
|
9
|
-
|
10
|
-
def storage_names(repository = :default)
|
11
|
-
raise NotImplementedError
|
12
|
-
end
|
13
|
-
|
14
|
-
def truncate_tables(table_names)
|
15
|
-
table_names.each do |table_name|
|
16
|
-
truncate_table table_name
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
class MysqlAdapter < DataObjectsAdapter
|
23
|
-
|
24
|
-
# taken from https://github.com/godfat/dm-mapping/tree/master
|
25
|
-
def storage_names(repository = :default)
|
26
|
-
select 'SHOW TABLES'
|
27
|
-
end
|
28
|
-
|
29
|
-
def truncate_table(table_name)
|
30
|
-
execute("TRUNCATE TABLE #{quote_name(table_name)};")
|
31
|
-
end
|
32
|
-
|
33
|
-
# copied from activerecord
|
34
|
-
def disable_referential_integrity
|
35
|
-
old = select("SELECT @@FOREIGN_KEY_CHECKS;")
|
36
|
-
begin
|
37
|
-
execute("SET FOREIGN_KEY_CHECKS = 0;")
|
38
|
-
yield
|
39
|
-
ensure
|
40
|
-
execute("SET FOREIGN_KEY_CHECKS = ?", *old)
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
module SqliteAdapterMethods
|
47
|
-
|
48
|
-
# taken from https://github.com/godfat/dm-mapping/tree/master
|
49
|
-
def storage_names(repository = :default)
|
50
|
-
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 177
|
51
|
-
sql = <<-SQL
|
52
|
-
SELECT name
|
53
|
-
FROM sqlite_master
|
54
|
-
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
|
55
|
-
SQL
|
56
|
-
# activerecord-2.1.0/lib/active_record/connection_adapters/sqlite_adapter.rb: 181
|
57
|
-
select(sql)
|
58
|
-
end
|
59
|
-
|
60
|
-
def truncate_table(table_name)
|
61
|
-
execute("DELETE FROM #{quote_name(table_name)};")
|
62
|
-
if uses_sequence?
|
63
|
-
execute("DELETE FROM sqlite_sequence where name = '#{table_name}';")
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
# this is a no-op copied from activerecord
|
68
|
-
# i didn't find out if/how this is possible
|
69
|
-
# activerecord also doesn't do more here
|
70
|
-
def disable_referential_integrity
|
71
|
-
yield
|
72
|
-
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
# Returns a boolean indicating if the SQLite database is using the sqlite_sequence table.
|
77
|
-
def uses_sequence?
|
78
|
-
sql = <<-SQL
|
79
|
-
SELECT name FROM sqlite_master
|
80
|
-
WHERE type='table' AND name='sqlite_sequence'
|
81
|
-
SQL
|
82
|
-
select(sql).first
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
class SqliteAdapter; include SqliteAdapterMethods; end
|
87
|
-
class Sqlite3Adapter; include SqliteAdapterMethods; end
|
88
|
-
|
89
|
-
# FIXME
|
90
|
-
# i don't know if this works
|
91
|
-
# i basically just copied activerecord code to get a rough idea what they do.
|
92
|
-
# i don't have postgres available, so i won't be the one to write this.
|
93
|
-
# maybe codes below gets some postgres/datamapper user going, though.
|
94
|
-
class PostgresAdapter < DataObjectsAdapter
|
95
|
-
|
96
|
-
# taken from https://github.com/godfat/dm-mapping/tree/master
|
97
|
-
def storage_names(repository = :default)
|
98
|
-
sql = <<-SQL
|
99
|
-
SELECT table_name FROM "information_schema"."tables"
|
100
|
-
WHERE table_schema = current_schema() and table_type = 'BASE TABLE'
|
101
|
-
SQL
|
102
|
-
select(sql)
|
103
|
-
end
|
104
|
-
|
105
|
-
def truncate_table(table_name)
|
106
|
-
execute("TRUNCATE TABLE #{quote_name(table_name)} RESTART IDENTITY CASCADE;")
|
107
|
-
end
|
108
|
-
|
109
|
-
# override to use a single statement
|
110
|
-
def truncate_tables(table_names)
|
111
|
-
quoted_names = table_names.collect { |n| quote_name(n) }.join(', ')
|
112
|
-
execute("TRUNCATE TABLE #{quoted_names} RESTART IDENTITY;")
|
113
|
-
end
|
114
|
-
|
115
|
-
# FIXME
|
116
|
-
# copied from activerecord
|
117
|
-
def supports_disable_referential_integrity?
|
118
|
-
version = select("SHOW server_version")[0][0].split('.')
|
119
|
-
(version[0].to_i >= 8 && version[1].to_i >= 1) ? true : false
|
120
|
-
rescue
|
121
|
-
return false
|
122
|
-
end
|
123
|
-
|
124
|
-
# FIXME
|
125
|
-
# copied unchanged from activerecord
|
126
|
-
def disable_referential_integrity(repository = :default)
|
127
|
-
if supports_disable_referential_integrity? then
|
128
|
-
execute(storage_names(repository).collect do |name|
|
129
|
-
"ALTER TABLE #{quote_name(name)} DISABLE TRIGGER ALL"
|
130
|
-
end.join(";"))
|
131
|
-
end
|
132
|
-
yield
|
133
|
-
ensure
|
134
|
-
if supports_disable_referential_integrity? then
|
135
|
-
execute(storage_names(repository).collect do |name|
|
136
|
-
"ALTER TABLE #{quote_name(name)} ENABLE TRIGGER ALL"
|
137
|
-
end.join(";"))
|
138
|
-
end
|
139
|
-
end
|
140
|
-
|
141
|
-
end
|
142
|
-
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
|
147
|
-
module DatabaseCleaner
|
148
|
-
module DataMapper
|
149
|
-
class Truncation
|
150
|
-
include ::DatabaseCleaner::DataMapper::Base
|
151
|
-
include ::DatabaseCleaner::Generic::Truncation
|
152
|
-
|
153
|
-
def clean(repository = self.db)
|
154
|
-
adapter = ::DataMapper.repository(repository).adapter
|
155
|
-
adapter.disable_referential_integrity do
|
156
|
-
adapter.truncate_tables(tables_to_truncate(repository))
|
157
|
-
end
|
158
|
-
end
|
159
|
-
|
160
|
-
private
|
161
|
-
|
162
|
-
def tables_to_truncate(repository = self.db)
|
163
|
-
(@only || ::DataMapper.repository(repository).adapter.storage_names(repository)) - @tables_to_exclude
|
164
|
-
end
|
165
|
-
|
166
|
-
# overwritten
|
167
|
-
def migration_storage_names
|
168
|
-
%w[migration_info]
|
169
|
-
end
|
170
|
-
|
171
|
-
end
|
172
|
-
end
|
173
|
-
end
|
@@ -1,6 +0,0 @@
|
|
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
|
-
DatabaseCleaner.deprecate "Due to lack of maintenance, the DataMapper adapter for DatabaseCleaner is deprecated, and will be removed in v2.0 with no replacement. Contact us if you are interested in resurrecting this adapter!"
|
@@ -1 +0,0 @@
|
|
1
|
-
require "database_cleaner/data_mapper"
|
@@ -1,16 +0,0 @@
|
|
1
|
-
module DatabaseCleaner
|
2
|
-
module Mongo
|
3
|
-
def self.available_strategies
|
4
|
-
%w[truncation deletion]
|
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
|