activerecord_unload_all_fixtures 0.1.0 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +9 -1
- data/VERSION +1 -1
- data/lib/activerecord_unload_all_fixtures.rb +76 -32
- metadata +3 -3
data/README.rdoc
CHANGED
@@ -2,7 +2,15 @@
|
|
2
2
|
|
3
3
|
deletes all rows in all tables associated with active record models
|
4
4
|
|
5
|
-
ActiveRecord::
|
5
|
+
ActiveRecord::UnloadAllFixtures::unload_all_fixtures
|
6
|
+
|
7
|
+
if you have some models you don't want to be mercilessly deleted, mark them with a "skip_unload_fixtures"
|
8
|
+
attribute on the model Class
|
9
|
+
|
10
|
+
class Foo < ActiveRecord::Base
|
11
|
+
class << self ; attr_accessor :skip_unload_fixtures ; end
|
12
|
+
Foo::skip_unload_fixtures = true
|
13
|
+
end
|
6
14
|
|
7
15
|
== Install
|
8
16
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
@@ -1,59 +1,103 @@
|
|
1
1
|
require 'set'
|
2
2
|
|
3
|
-
module
|
4
|
-
module
|
3
|
+
module ActiveRecord
|
4
|
+
module UnloadAllFixtures
|
5
5
|
|
6
6
|
class << self
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :ordered_active_record_table_names
|
8
8
|
attr_accessor :table_name_set
|
9
9
|
|
10
10
|
# iterate over all ActiveRecord models associated with db tables, deleting all rows
|
11
11
|
# if we're inside a transaction, use delete, otherwise use truncate
|
12
12
|
def unload_all_fixtures()
|
13
|
-
if
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
13
|
+
if ActiveRecord::UnloadAllFixtures::ordered_active_record_table_names.nil? ||
|
14
|
+
ActiveRecord::UnloadAllFixtures::ordered_active_record_table_names.empty?
|
15
|
+
klasses = ActiveRecord::Base.send(:subclasses).reject{ |klass| klass.skip_unload_fixtures if klass.respond_to?(:skip_unload_fixtures) }
|
16
|
+
ActiveRecord::UnloadAllFixtures::ordered_active_record_table_names = klasses.map do |klass|
|
17
|
+
if defined?(ActiveRecord::WormTable) && klass.ancestors.include?(ActiveRecord::WormTable)
|
18
|
+
[klass.switch_table_name] + klass.table_version_names
|
19
|
+
else
|
20
|
+
klass.table_name
|
21
|
+
end
|
22
|
+
end.flatten.to_set.to_a
|
21
23
|
end
|
22
24
|
|
23
|
-
|
25
|
+
ActiveRecord::UnloadAllFixtures::table_name_set = ActiveRecord::Base::connection.tables.to_set
|
24
26
|
|
25
27
|
# start with the last successful delete ordering, only re-ordering if new foreign key dependencies are found
|
26
|
-
|
28
|
+
ActiveRecord::Base::without_foreign_key_checks do
|
29
|
+
ActiveRecord::UnloadAllFixtures::ordered_active_record_table_names = delete_rows( ActiveRecord::UnloadAllFixtures::ordered_active_record_table_names,
|
30
|
+
ActiveRecord::Base.connection.open_transactions == 0 )
|
31
|
+
end
|
32
|
+
|
27
33
|
true
|
28
34
|
end
|
29
35
|
|
30
|
-
def
|
36
|
+
def delete_rows(table_names, truncate=false, shift_counter=table_names.size)
|
31
37
|
processed = []
|
32
|
-
|
33
|
-
if defined?(ActiveRecord::WormTable) && c.ancestors.include?(ActiveRecord::WormTable)
|
34
|
-
tables = [c.switch_table_name,c.table_version_names]
|
35
|
-
else
|
36
|
-
tables = [c.table_name]
|
37
|
-
end
|
38
|
-
|
38
|
+
table_names.each_with_index{ |table_name,i|
|
39
39
|
begin
|
40
|
-
|
41
|
-
if
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
ActiveRecord::Base.connection.execute("delete from #{table_name}")
|
46
|
-
end
|
40
|
+
if ActiveRecord::UnloadAllFixtures::table_name_set.include?(table_name)
|
41
|
+
if truncate
|
42
|
+
ActiveRecord::Base.connection.execute("truncate table #{table_name}")
|
43
|
+
else
|
44
|
+
ActiveRecord::Base.connection.execute("delete from #{table_name}")
|
47
45
|
end
|
48
46
|
end
|
49
|
-
processed <<
|
50
|
-
rescue
|
51
|
-
|
52
|
-
|
47
|
+
processed << table_name
|
48
|
+
rescue Exception=>e
|
49
|
+
$stderr << e.message << "\n"
|
50
|
+
$stderr << e.backtrace << "\n"
|
51
|
+
remaining = table_names[i..-1]
|
52
|
+
raise "can't remove all tables. tables remaining: #{remaining.join(', ')}" unless shift_counter>0
|
53
|
+
processed += delete_rows( remaining.unshift(remaining.pop), truncate, shift_counter-1 )
|
53
54
|
end
|
54
55
|
}
|
55
56
|
end
|
56
57
|
end
|
58
|
+
|
59
|
+
module MySQL
|
60
|
+
def disable_foreign_key_checks
|
61
|
+
execute "set foreign_key_checks=0"
|
62
|
+
end
|
63
|
+
|
64
|
+
def enable_foreign_key_checks
|
65
|
+
execute "set foreign_key_checks=1"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
module JdbcSpec
|
72
|
+
module MySQL
|
73
|
+
include ActiveRecord::UnloadAllFixtures::MySQL
|
57
74
|
end
|
58
75
|
end
|
59
76
|
|
77
|
+
module ActiveRecord
|
78
|
+
module ConnectionAdapters
|
79
|
+
class AbstractAdapter
|
80
|
+
def disable_foreign_key_checks
|
81
|
+
end
|
82
|
+
|
83
|
+
def enable_foreign_key_checks
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
class MysqlAdapter
|
89
|
+
include ActiveRecord::UnloadAllFixtures::MySQL
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
class Base
|
94
|
+
def self.without_foreign_key_checks
|
95
|
+
begin
|
96
|
+
connection.disable_foreign_key_checks
|
97
|
+
yield
|
98
|
+
ensure
|
99
|
+
connection.enable_foreign_key_checks
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- mccraig mccraig of the clan mccraig
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-05-
|
17
|
+
date: 2010-05-06 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|