Skip to content

Instantly share code, notes, and snippets.

@alg
Created January 18, 2010 09:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save alg/279918 to your computer and use it in GitHub Desktop.
Save alg/279918 to your computer and use it in GitHub Desktop.
namespace :db do
desc 'Seed database from db/seed.sql file instead of the traditional db/seed.rb'
namespace :seed do
config = Rails::Configuration.new.database_configuration[RAILS_ENV]
seed_sql = File.expand_path(File.dirname(__FILE__) + '/../../db/seed.sql')
if !File.exists?(seed_sql)
puts "Missing RAILS_ROOT/db/seed.sql"
else
case config['adapter']
when 'mysql'
database = config['database']
username = config['username']
password = config['password']
`mysql #{database} -u#{username} -p#{password} < #{seed_sql}`
when 'sqlite3'
database = config['database']
`sqlite3 #{database} < #{seed_sql}`
else
puts "Wrong adapter. Only MySQL and Sqlite3 are supported."
end
end
end
end
@Tuman829
Copy link

Tuman829 commented Jun 4, 2013

Will this work for the current version of rails? (with Rails.env rather then RAILS_ENV)

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