Skip to content

Commit 3a7253b

Browse files
authored
Merge pull request #96 from combinaut/add-trigger-filtering-support
Add trigger filtering support to schema dumper
2 parents 1554d13 + 30ddc04 commit 3a7253b

File tree

3 files changed

+143
-38
lines changed

3 files changed

+143
-38
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,21 @@ As long as you don't delete old migrations and schema.rb prior to running
240240
If you have deleted all trigger migrations, you can regenerate a new
241241
baseline for model triggers via `rake db:generate_trigger_migration`.
242242

243+
### Filtering
244+
245+
It is possible to filter which triggers are dumped by setting any of these
246+
configuration values:
247+
248+
```ruby
249+
HairTrigger::SchemaDumper::Configuration.ignore_triggers = 'exact_trigger_name'
250+
HairTrigger::SchemaDumper::Configuration.ignore_tables = [/partial_/, 'exact_table_name']
251+
HairTrigger::SchemaDumper::Configuration.allow_triggers = [/partial_/, 'exact_trigger_name']
252+
HairTrigger::SchemaDumper::Configuration.allow_tables = 'exact_table_name'
253+
```
254+
255+
Each option can accept a single String or Regexp, or a mixed array of both.
256+
257+
243258
## Testing
244259

245260
To stay on top of things, it's strongly recommended that you add a test or

lib/hair_trigger/schema_dumper.rb

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
module HairTrigger
22
module SchemaDumper
3+
module Configuration
4+
mattr_accessor :allow_tables
5+
mattr_accessor :allow_triggers
6+
mattr_accessor :ignore_tables
7+
mattr_accessor :ignore_triggers
8+
end
9+
310
module TrailerWithTriggersSupport
411
def trailer(stream)
512
orig_show_warnings = Builder.show_warnings
@@ -91,9 +98,35 @@ def normalize_trigger(name, definition, type)
9198
end
9299

93100
def whitelist_triggers(triggers)
94-
triggers.reject do |name, source|
101+
triggers = triggers.reject do |name, source|
95102
ActiveRecord::SchemaDumper.ignore_tables.any? { |ignored_table_name| source =~ /ON\s+#{@connection.quote_table_name(ignored_table_name)}\s/ }
96103
end
104+
105+
if Configuration.allow_tables.present?
106+
triggers = triggers.select do |name, source|
107+
Array(Configuration.allow_tables).any? { |allowed_table_name| source =~ /ON\s+#{@connection.quote_table_name(allowed_table_name)}\s/ }
108+
end
109+
end
110+
111+
if Configuration.allow_triggers.present?
112+
triggers = triggers.select do |name, source|
113+
Array(Configuration.allow_triggers).any? { |allowed_trigger_name| allowed_trigger_name === name } # Triple equals to allow regexps or strings as allowed_trigger_name
114+
end
115+
end
116+
117+
if Configuration.ignore_tables.present?
118+
triggers = triggers.reject do |name, source|
119+
Array(Configuration.ignore_tables).any? { |allowed_table_name| source =~ /ON\s+#{@connection.quote_table_name(allowed_table_name)}\s/ }
120+
end
121+
end
122+
123+
if Configuration.ignore_triggers.present?
124+
triggers = triggers.reject do |name, source|
125+
Array(Configuration.ignore_triggers).any? { |allowed_trigger_name| allowed_trigger_name === name } # Triple equals to allow regexps or strings as allowed_trigger_name
126+
end
127+
end
128+
129+
triggers
97130
end
98131

99132
def self.included(base)

spec/schema_dumper_spec.rb

Lines changed: 94 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,98 @@
1515
db_triggers.grep(/bob_count \+ 1/).size.should eql(1)
1616
end
1717

18+
shared_examples_for 'filterable' do
19+
it 'should take in consideration active record schema dumper ignore_tables option with regexp' do
20+
ActiveRecord::SchemaDumper.ignore_tables = [/users/]
21+
22+
dump_schema.should_not match(/create_trigger/)
23+
end
24+
25+
it 'should take in consideration active record schema dumper ignore_tables option with string' do
26+
ActiveRecord::SchemaDumper.ignore_tables = ['users']
27+
28+
dump_schema.should_not match(/create_trigger/)
29+
end
30+
31+
it 'should take in consideration active record schema dumper ignore_tables option with partial string' do
32+
ActiveRecord::SchemaDumper.ignore_tables = ['user']
33+
34+
dump_schema.should match(/create_trigger/)
35+
end
36+
37+
it 'should ignore configured ignore_tables option with regexp' do
38+
HairTrigger::SchemaDumper::Configuration.stub(:ignore_tables).and_return([/users/])
39+
40+
dump_schema.should_not match(/create_trigger/)
41+
end
42+
43+
it 'should ignore configured ignore_tables option with string' do
44+
HairTrigger::SchemaDumper::Configuration.stub(:ignore_tables).and_return(['users'])
45+
46+
dump_schema.should_not match(/create_trigger/)
47+
end
48+
49+
it 'should not ignore configured ignore_tables option with partial string' do
50+
HairTrigger::SchemaDumper::Configuration.stub(:ignore_tables).and_return(['user'])
51+
52+
dump_schema.should match(/create_trigger/)
53+
end
54+
55+
it 'should ignore configured ignore_triggers option with regexp' do
56+
HairTrigger::SchemaDumper::Configuration.stub(:ignore_triggers).and_return([/bob/])
57+
58+
dump_schema.should_not match(/trigger.+bob/i)
59+
end
60+
61+
it 'should ignore configured ignore_triggers option with string' do
62+
HairTrigger::SchemaDumper::Configuration.stub(:ignore_triggers).and_return(['users_after_insert_row_when_new_name_bob__tr'])
63+
64+
dump_schema.should_not match(/trigger.+bob/i)
65+
end
66+
67+
it 'should not ignore configured ignore_triggers option with partial string' do
68+
HairTrigger::SchemaDumper::Configuration.stub(:ignore_triggers).and_return(['users_after_insert_row_when_new_name_bob'])
69+
70+
dump_schema.should match(/trigger.+bob/i)
71+
end
72+
73+
it 'should allow configured allow_tables option with regexp' do
74+
HairTrigger::SchemaDumper::Configuration.stub(:allow_tables).and_return([/users/])
75+
76+
dump_schema.should match(/create_trigger/)
77+
end
78+
79+
it 'should allow configured allow_tables option with string' do
80+
HairTrigger::SchemaDumper::Configuration.stub(:allow_tables).and_return(['users'])
81+
82+
dump_schema.should match(/create_trigger/)
83+
end
84+
85+
it 'should not allow configured allow_tables option with partial string' do
86+
HairTrigger::SchemaDumper::Configuration.stub(:allow_tables).and_return(['user'])
87+
88+
dump_schema.should_not match(/create_trigger/)
89+
end
90+
91+
it 'should allow configured allow_triggers option with regexp' do
92+
HairTrigger::SchemaDumper::Configuration.stub(:allow_triggers).and_return([/bob/])
93+
94+
dump_schema.should match(/trigger.+bob/i)
95+
end
96+
97+
it 'should allow configured allow_triggers option with string' do
98+
HairTrigger::SchemaDumper::Configuration.stub(:allow_triggers).and_return(['users_after_insert_row_when_new_name_bob__tr'])
99+
100+
dump_schema.should match(/trigger.+bob/i)
101+
end
102+
103+
it 'should not allow configured allow_triggers option with partial string' do
104+
HairTrigger::SchemaDumper::Configuration.stub(:allow_triggers).and_return(['users_after_insert_row_when_new_name_bob'])
105+
106+
dump_schema.should_not match(/trigger.+bob/i)
107+
end
108+
end
109+
18110
context "without schema.rb" do
19111
it "should work" do
20112
schema_rb = dump_schema
@@ -45,25 +137,7 @@
45137
schema_rb.should_not match(/bob_count \+ 2/)
46138
end
47139

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-
140+
it_should_behave_like 'filterable'
67141
end
68142

69143
context "with schema.rb" do
@@ -100,24 +174,7 @@
100174
schema_rb.should_not match(/bob_count \+ 2/)
101175
end
102176

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-
177+
it_should_behave_like 'filterable'
121178
end
122179

123180
end

0 commit comments

Comments
 (0)