Skip to content

Instantly share code, notes, and snippets.

@wbailey
Created February 11, 2011 08:58
Show Gist options
  • Select an option

  • Save wbailey/822095 to your computer and use it in GitHub Desktop.

Select an option

Save wbailey/822095 to your computer and use it in GitHub Desktop.
ActiveRecord migrations outside of Rails
require 'yaml'
require 'logger'
require 'active_record'
namespace :db do
def create_database config
options = {:charset => 'utf8', :collation => 'utf8_unicode_ci'}
create_db = lambda do |config|
ActiveRecord::Base.establish_connection config.merge('database' => nil)
ActiveRecord::Base.connection.create_database config['database'], options
ActiveRecord::Base.establish_connection config
end
begin
create_db.call config
rescue Mysql::Error => sqlerr
if sqlerr.errno == 1405
print "#{sqlerr.error}. \nPlease provide the root password for your mysql installation\n>"
root_password = $stdin.gets.strip
grant_statement = <<-SQL
GRANT ALL PRIVILEGES ON #{config['database']}.*
TO '#{config['username']}'@'localhost'
IDENTIFIED BY '#{config['password']}' WITH GRANT OPTION;
SQL
create_db.call config.merge('database' => nil, 'username' => 'root', 'password' => root_password)
else
$stderr.puts sqlerr.error
$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
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('config/databases.yml')[DATABASE_ENV]
end
task :configure_connection => :configuration do
ActiveRecord::Base.establish_connection @config
ActiveRecord::Base.logger = Logger.new STDOUT if @config['logger']
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 => :configure_connection do
ActiveRecord::Base.connection.drop_database @config['database']
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
end
@aaronyo
Copy link
Copy Markdown

aaronyo commented Jun 26, 2013

This was super helpful to me. Thanks! Was easy to get foreigner to play with this, which is what I was after and why I had to stop using the standalone_migrations gem.

@patriciomacadden
Copy link
Copy Markdown

Thanks for this gist! But let me correct you something: in https://gist.github.com/wbailey/822095#file-databases-rake-L43 should be database.yml instead of databases.yml

@metrue
Copy link
Copy Markdown

metrue commented Aug 9, 2014

after setting up the configure file, type rake migrate. it complained "Don't know how to build task 'migrate'"
that is weird.

@jwliechty
Copy link
Copy Markdown

There is a better solution than defining the tasks yourself....
https://gist.github.com/drogus/6087979

@euclid1990
Copy link
Copy Markdown

I have made minimal example/setup using Rails migration and active record outside Rails
https://github.com/euclid1990/rails-migration
(Support Rails >= 5.2)
🔢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment