hairtrigger 0.2.13 → 0.2.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/hair_trigger/schema_dumper.rb +17 -6
- data/lib/hair_trigger/version.rb +1 -1
- data/spec/schema_dumper_spec.rb +40 -0
- metadata +2 -2
@@ -1,5 +1,6 @@
|
|
1
1
|
module HairTrigger
|
2
2
|
module SchemaDumper
|
3
|
+
|
3
4
|
def trailer_with_triggers(stream)
|
4
5
|
orig_show_warnings = Builder.show_warnings
|
5
6
|
Builder.show_warnings = false # we already show them when running the migration
|
@@ -10,13 +11,15 @@ module HairTrigger
|
|
10
11
|
end
|
11
12
|
|
12
13
|
def triggers(stream)
|
13
|
-
@connection = ActiveRecord::Base.connection
|
14
14
|
@adapter_name = @connection.adapter_name.downcase.to_sym
|
15
15
|
|
16
|
-
|
16
|
+
all_triggers = @connection.triggers
|
17
17
|
db_trigger_warnings = {}
|
18
|
+
migration_trigger_builders = []
|
19
|
+
|
20
|
+
db_triggers = whitelist_triggers(all_triggers)
|
18
21
|
|
19
|
-
migration_triggers = HairTrigger.current_migrations(:in_rake_task => true, :previous_schema => self.class.previous_schema).map do |(
|
22
|
+
migration_triggers = HairTrigger.current_migrations(:in_rake_task => true, :previous_schema => self.class.previous_schema).map do |(_, builder)|
|
20
23
|
definitions = []
|
21
24
|
builder.generate.each do |statement|
|
22
25
|
if statement =~ /\ACREATE(.*TRIGGER| FUNCTION) ([^ \n]+)/
|
@@ -30,12 +33,13 @@ module HairTrigger
|
|
30
33
|
next unless migration[:definitions].all? do |(name, definition, type)|
|
31
34
|
db_triggers[name] && (db_trigger_warnings[name] = true) && db_triggers[name] == normalize_trigger(name, definition, type)
|
32
35
|
end
|
33
|
-
|
36
|
+
|
37
|
+
migration[:definitions].each do |(name, _, _)|
|
34
38
|
db_triggers.delete(name)
|
35
39
|
db_trigger_warnings.delete(name)
|
36
40
|
end
|
37
41
|
|
38
|
-
|
42
|
+
migration_trigger_builders << migration[:builder]
|
39
43
|
end
|
40
44
|
|
41
45
|
db_triggers.to_a.sort_by{ |t| (t.first + 'a').sub(/\(/, '_') }.each do |(name, definition)|
|
@@ -51,10 +55,11 @@ module HairTrigger
|
|
51
55
|
stream.print " execute(#{definition.inspect})\n\n"
|
52
56
|
end
|
53
57
|
end
|
58
|
+
|
59
|
+
migration_trigger_builders.each { |builder| stream.print builder.to_ruby(' ', false) + "\n\n" }
|
54
60
|
end
|
55
61
|
|
56
62
|
def normalize_trigger(name, definition, type)
|
57
|
-
@connection = ActiveRecord::Base.connection
|
58
63
|
@adapter_name = @connection.adapter_name.downcase.to_sym
|
59
64
|
|
60
65
|
return definition unless @adapter_name == :postgresql
|
@@ -79,6 +84,12 @@ module HairTrigger
|
|
79
84
|
definition
|
80
85
|
end
|
81
86
|
|
87
|
+
def whitelist_triggers(triggers)
|
88
|
+
triggers.reject do |name, source|
|
89
|
+
ActiveRecord::SchemaDumper.ignore_tables.any? { |ignored_table_name| source =~ /ON\s+#{@connection.quote_table_name(ignored_table_name)}\s/ }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
82
93
|
def self.included(base)
|
83
94
|
base.class_eval do
|
84
95
|
alias_method_chain :trailer, :triggers
|
data/lib/hair_trigger/version.rb
CHANGED
data/spec/schema_dumper_spec.rb
CHANGED
@@ -44,6 +44,26 @@ describe "schema dumping" do
|
|
44
44
|
schema_rb.should match(/bob_count \+ 1/)
|
45
45
|
schema_rb.should_not match(/bob_count \+ 2/)
|
46
46
|
end
|
47
|
+
|
48
|
+
it 'should take in consideration active record schema dumper ignore_tables option with regexp' do
|
49
|
+
ActiveRecord::SchemaDumper.ignore_tables = [/users/]
|
50
|
+
|
51
|
+
dump_schema.should_not match(/create_trigger/)
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should take in consideration active record schema dumper ignore_tables option with string' do
|
55
|
+
ActiveRecord::SchemaDumper.ignore_tables = ['users']
|
56
|
+
|
57
|
+
dump_schema.should_not match(/create_trigger/)
|
58
|
+
end
|
59
|
+
|
60
|
+
it 'should take in consideration active record schema dumper ignore_tables option with partial string' do
|
61
|
+
ActiveRecord::SchemaDumper.ignore_tables = ['user']
|
62
|
+
|
63
|
+
dump_schema.should match(/create_trigger/)
|
64
|
+
end
|
65
|
+
|
66
|
+
|
47
67
|
end
|
48
68
|
|
49
69
|
context "with schema.rb" do
|
@@ -79,6 +99,26 @@ describe "schema dumping" do
|
|
79
99
|
schema_rb.should match(/bob_count \+ 1/)
|
80
100
|
schema_rb.should_not match(/bob_count \+ 2/)
|
81
101
|
end
|
102
|
+
|
103
|
+
it 'should take in consideration active record schema dumper ignore_tables option with regexp' do
|
104
|
+
ActiveRecord::SchemaDumper.ignore_tables = [/users/]
|
105
|
+
|
106
|
+
dump_schema.should_not match(/create_trigger/)
|
107
|
+
end
|
108
|
+
|
109
|
+
it 'should take in consideration active record schema dumper ignore_tables option with string' do
|
110
|
+
ActiveRecord::SchemaDumper.ignore_tables = ['users']
|
111
|
+
|
112
|
+
dump_schema.should_not match(/create_trigger/)
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'should take in consideration active record schema dumper ignore_tables option with partial string' do
|
116
|
+
ActiveRecord::SchemaDumper.ignore_tables = ['user']
|
117
|
+
|
118
|
+
dump_schema.should match(/create_trigger/)
|
119
|
+
end
|
120
|
+
|
82
121
|
end
|
122
|
+
|
83
123
|
end
|
84
124
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hairtrigger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.14
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|