Skip to content

Instantly share code, notes, and snippets.

@aaronyo
Forked from wbailey/databases.rake
Last active December 19, 2015 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronyo/5868990 to your computer and use it in GitHub Desktop.
Save aaronyo/5868990 to your computer and use it in GitHub Desktop.
Standalone migrations for postgres with foreign key support.
require 'yaml'
require 'logger'
require 'active_record'
require 'foreigner'
namespace :db do
def create_database config
options = {:charset => 'utf8'}
create_db = lambda do |config|
ActiveRecord::Base.establish_connection config.merge('database' => 'postgres')
ActiveRecord::Base.connection.create_database config['database'], options
ActiveRecord::Base.establish_connection config
end
begin
create_db.call config
rescue Exception => sqlerr
$stderr.puts sqlerr
$stderr.puts "Couldn't create database for #{config.inspect}, charset: utf8, collation: utf8_unicode_ci"
$stderr.puts "(if you set the charset manually, make sure you have a matching collation)" if config['charset']
end
end
def configure_connection(config = @config)
ActiveRecord::Base.establish_connection config
ActiveRecord::Base.logger = Logger.new STDOUT if config['logger']
Foreigner.load
end
task :environment do
DATABASE_ENV = ENV['DATABASE_ENV'] || 'development'
MIGRATIONS_DIR = ENV['MIGRATIONS_DIR'] || 'db/migrate'
end
task :configuration => :environment do
@config = YAML.load_file('db/config.yml')[DATABASE_ENV]
end
task :configure_connection => :configuration do
configure_connection
end
desc 'Create the database from config/database.yml for the current DATABASE_ENV'
task :create => :configure_connection do
create_database @config
end
desc 'Drops the database for the current DATABASE_ENV'
task :drop => :environment do
config = YAML.load_file('db/config.yml')[DATABASE_ENV]
db_to_drop = config['database']
config['database'] = 'postgres'
configure_connection config
ActiveRecord::Base.connection.drop_database db_to_drop
end
desc 'Migrate the database (options: VERSION=x, VERBOSE=false).'
task :migrate => :configure_connection do
ActiveRecord::Migration.verbose = true
ActiveRecord::Migrator.migrate MIGRATIONS_DIR, ENV['VERSION'] ? ENV['VERSION'].to_i : nil
end
desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
task :rollback => :configure_connection do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.rollback MIGRATIONS_DIR, step
end
desc "Retrieves the current schema version number"
task :version => :configure_connection do
puts "Current version: #{ActiveRecord::Migrator.current_version}"
end
namespace :schema do
desc "Retrieves the current schema version number"
task :dump => :configure_connection do
ActiveRecord::SchemaDumper.dump
end
desc "Retrieves the current schema version number"
task :load => :configure_connection do
load("db/schema.rb")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment