Created
March 6, 2015 21:00
-
-
Save rietta/39be7d1d6a1d2e5b7513 to your computer and use it in GitHub Desktop.
Revisions
-
rietta created this gist
Mar 6, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ namespace :db do desc "Update and create SQL views" task :views => :environment do Dir["#{Rails.root}/db/sql_views/*.sql"].each do |file_name| STDERR.puts "Applying the SQL view at #{file_name}" source_file = File.new(file_name, 'r') if source_file and (sql_content = source_file.read) ActiveRecord::Base.transaction do # Each statement ends with a semicolon followed by a newline. sql_lines = sql_content.split(/;[ \t]*$/) if sql_lines.respond_to?(:each) sql_lines.each do |line| ActiveRecord::Base.connection.execute "#{line};" end end end # transaction end end # Dir.each end # task end