Skip to content

Instantly share code, notes, and snippets.

@kjlape
Last active October 22, 2015 19:10
Show Gist options
  • Select an option

  • Save kjlape/0698e6cd2e647f328c2a to your computer and use it in GitHub Desktop.

Select an option

Save kjlape/0698e6cd2e647f328c2a to your computer and use it in GitHub Desktop.
Rails rake task to run arbitrary sql scripts in repo.
namespace :dbrun do
namespace :functions do
app_root = Rails.application.config.root
sql_functions_path = File.join(app_root, "db", "functions")
sql_functions_glob_path = File.join(sql_functions_path, "*.sql")
ActiveRecord::Base.configurations = YAML.load(File.read(File.join(app_root, 'config', 'database.yml')))
ActiveRecord::Base.establish_connection (ENV['ENV'] || 'development').to_sym
Dir.glob(sql_functions_glob_path) do |file|
desc "Run #{file}."
task File.basename(file, ".*").to_sym do
path = File.join(sql_functions_path, File.basename(file))
ActiveRecord::Base.connection.execute(File.read(path))
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment