Last active
December 17, 2015 22:59
-
-
Save covard/5685921 to your computer and use it in GitHub Desktop.
Example of a custom rake file in a ruby app with out rails.
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
require 'active_record' | |
require 'yaml' | |
require 'logger' | |
require 'database_cleaner' | |
require_relative 'models/market' | |
CONFIG = YAML::load(File.open('config/database.yml')) | |
task :default => :migrate | |
namespace :db do | |
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x" | |
task :migrate => :environment do | |
ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil ) | |
end | |
task :drop => :environment do | |
puts "----- Drop DB -----" | |
ActiveRecord::Base.connection.drop_database CONFIG["database"] | |
end | |
task :create do | |
puts "----- Create DB -----" | |
config = YAML::load(File.open('config/database.yml')) | |
config['database'] = 'mysql' | |
ActiveRecord::Base.establish_connection config | |
ActiveRecord::Base.connection.create_database CONFIG["database"] | |
ActiveRecord::Base.establish_connection YAML::load(File.open('config/database.yml')) | |
end | |
task :reset => :environment do | |
puts "----- Reset DB -----" | |
Rake::Task["db:drop"].invoke | |
Rake::Task["db:create"].invoke | |
Rake::Task["db:migrate"].invoke | |
Rake::Task["db:seed"].invoke | |
end | |
desc "Truncate all existing data" | |
task :truncate, [:arg1] => :environment do |t, args| | |
if args[:arg1] =~ /all/i | |
DatabaseCleaner.clean_with :truncation | |
else | |
DatabaseCleaner.clean_with :truncation, { only: [args[:arg1]] } | |
end | |
end | |
task :seed => :environment do | |
markets = Market.all | |
if markets.length == 0 | |
names = %w[Alabama Illinois MAPA Tennessee Texas] | |
names.each do |name| | |
puts "\tSeeding Markets with market_name: #{name}" | |
Market.create market_name: name | |
end | |
puts "Finished seeding markets" | |
puts | |
end | |
end | |
end | |
task :environment do | |
ActiveRecord::Base.establish_connection YAML::load(File.open('config/database.yml')) | |
ActiveRecord::Base.logger = Logger.new(File.open('log/database.log', 'a')) | |
end | |
# Happy Rubying =) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment