Skip to content

Instantly share code, notes, and snippets.

@lstoll
Created January 16, 2009 02:57
Show Gist options
  • Save lstoll/47783 to your computer and use it in GitHub Desktop.
Save lstoll/47783 to your computer and use it in GitHub Desktop.
APP_BASE = File.dirname(File.expand_path(__FILE__))
namespace :db do
task :ar_init do
# Load the database config
require 'active_record'
database_yml = YAML::load(File.open(APP_BASE + "/config/database.yml"))
init_env == "rake" ? current_env = "development".to_sym : current_env = init_env.to_sym # Default to dev env, symbolize
ActiveRecord::Base.establish_connection(database_yml[current_env])
# set a logger for STDOUT
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x. Turn off output with VERBOSE=false."
task :migrate => :ar_init do
ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
ActiveRecord::Migrator.migrate(APP_BASE + "/db/ar_migrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
namespace :schema do
desc "Create a db/ar_schema.rb file that can be portably used against any DB supported by AR"
task :dump => :ar_init do
require 'active_record/schema_dumper'
File.open(ENV['SCHEMA'] || APP_BASE + "/db/ar_schema.rb", "w") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
desc "Load a ar_schema.rb file into the database"
task :load => :ar_init do
file = ENV['SCHEMA'] || APP_BASE + "/db/ar_schema.rb"
load(file)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment