forked from jenseng/hair_trigger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
safer migration loading, some speedups
we no longer run each migration in noop mode to get at the triggers (using an AR::Migration monkey-patch)... this was uber-dangerous since we only intercepted things that fell through to the connection (e.g. create_table or execute), and not other stuff (e.g. Foo.update_all or MyWebService.call). we now use ruby_parser to extract triggers (or in the case of schema.rb, just a simple regex) added some little regex checks to model and migration loading so that we don't bother loading or parsing classes that have no triggers
- Loading branch information
Showing
9 changed files
with
121 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,16 @@ | ||
source "http://rubygems.org" | ||
|
||
gem "activerecord", ">=2.3.0" | ||
gem 'ruby_parser', '2.0.6' | ||
gem 'ruby2ruby', '1.2.5' | ||
group :development do | ||
gem "rspec", "~> 2.3.0" | ||
gem "bundler", "~> 1.0.0" | ||
gem "jeweler", "~> 1.5.2" | ||
gem "jeweler", "~> 1.6.1" | ||
gem "rcov", ">= 0" | ||
gem 'mysql', '>= 2.8.1' | ||
gem 'mysql2', '>= 0.2.7' | ||
gem 'mysql2', '>= 0.2.7', '< 0.3' | ||
gem 'pg', '>= 0.10.1' | ||
gem 'sqlite3-ruby', '>= 1.3.2' | ||
gem 'ruby-debug', '0.10.4' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.10 | ||
0.1.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require 'ruby_parser' | ||
require 'ruby2ruby' | ||
|
||
module HairTrigger | ||
module MigrationReader | ||
class << self | ||
def get_triggers(source, options) | ||
triggers = [] | ||
if source.is_a?(String) | ||
# schema.rb contents... because it's auto-generated and we know | ||
# exactly what it will look like, we can safely use a regex | ||
source.scan(/^ create_trigger\(.*?\n end\n\n/m).each do |match| | ||
trigger = instance_eval("generate_" + match.strip) | ||
triggers << trigger if options[:include_manual_triggers] || trigger.options[:generated] | ||
end | ||
else | ||
contents = File.read(source.filename) | ||
return [] unless contents =~ /(create|drop)_trigger/ | ||
sexps = RubyParser.new.parse(contents) | ||
# find the migration class | ||
sexps = [sexps] unless sexps[0] == :block | ||
sexps = sexps.detect{ |s| s.is_a?(Sexp) && s[0] == :class && s[1] == source.name.to_sym }.last | ||
# find the block of the up method | ||
sexps = sexps.last if sexps.last.is_a?(Sexp) && sexps.last[0] == :block | ||
sexps = sexps.detect{ |s| s.is_a?(Sexp) && s[0] == :defs && s[1] && s[1][0] == :self && s[2] == :up }.last.last | ||
sexps.each do |sexp| | ||
next unless (method = extract_method_call(sexp)) && [:create_trigger, :drop_trigger].include?(method) | ||
trigger = instance_eval("generate_" + generator.process(sexp)) | ||
triggers << trigger if options[:include_manual_triggers] || trigger.options[:generated] | ||
end | ||
end | ||
triggers | ||
rescue | ||
$stderr.puts "Error reading triggers in #{source.filename rescue "schema.rb"}: #{$!}" | ||
end | ||
|
||
private | ||
def extract_method_call(exp) | ||
return nil unless exp.is_a?(Array) | ||
if exp[0] == :iter | ||
exp = exp[1] while exp[1].is_a?(Array) && exp[1][0] == :call | ||
end | ||
if exp[0] == :call | ||
exp[2] | ||
end | ||
end | ||
|
||
def generate_create_trigger(*arguments) | ||
arguments.unshift({}) if arguments.empty? | ||
arguments.unshift(nil) if arguments.first.is_a?(Hash) | ||
arguments[1][:compatibility] ||= HairTrigger::Builder.base_compatibility | ||
::HairTrigger::Builder.new(*arguments) | ||
end | ||
|
||
def generate_drop_trigger(*arguments) | ||
::HairTrigger::Builder.new(arguments[0], {:table => arguments[1], :drop => true}) | ||
end | ||
|
||
def generator | ||
@generator ||= Ruby2Ruby.new | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters