Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- desc 'Creates the databases defined in your config/database.yml (unless they already exist)'
- task 'db:create' => [ 'environment' ] do
- ActiveRecord::Base.configurations.each_value do |config|
- begin
- ActiveRecord::Base.establish_connection(config)
- ActiveRecord::Base.connection
- rescue
- case config["adapter"]
- when "mysql" then
- ActiveRecord:: Base.establish_connection(config.merge({ "database" => nil }))
- ActiveRecord::Base.connection.create_database(config["database"])
- ActiveRecord::Base.establish_connection(config)
- when "postgresql" then
- `createdb \"#{config["database"]}\" -E utf8`
- else
- # do nothing
- end
- end
- end
- ActiveRecord::Base.establish_connection (ActiveRecord::Base.configurations[(RAILS_ENV or "development")])
- end
- desc 'Drops the database for your currenet RAILS_ENV as defined in config/database.yml'
- task 'db:drop' => [ 'environment' ] do
- config = ActiveRecord::Base.configurations[(RAILS_ENV or "development")]
- case config["adapter"]
- when "mysql" then
- begin
- ActiveRecord::Base.establish_connection (config)
- ActiveRecord::Base.connection.current_database
- ActiveRecord::Base.connection.drop_database(config["database"])
- rescue
- # do nothing
- end
- when "sqlite3" then
- FileUtils.rm_f(File.join(RAILS_ROOT, config["database"]))
- when "postgresql" then
- `dropdb \"#{config["database"]}\"`
- else
- # do nothing
- end
- end
- desc 'Drops, creates and then migrates the database for your current RAILS_ENV. Target specific version with VERSION=x'
- task 'db:reset' => [ 'db:drop', 'db:create', 'db:migrate' ] do
- # do nothing
- end
- desc 'Launches the database shell using the values defined in config/database.yml'
- task 'db:shell' => [ 'environment' ] do
- config = ActiveRecord::Base.configurations [(RAILS_ENV or "development")]
- command = ""
- case config["adapter"]
- when "mysql" then
- (command << "mysql ")
- (command << "--host=#{(config["host"] or "localhost")} ")
- (command << "--port=#{(config["port"] or 3306)} ")
- (command << "--user=#{(config["username"] or "root")} ")
- (command << "--password=#{(config["password"] or "")} ")
- (command << config["database"])
- when "postgresql" then
- puts("You should consider switching to MySQL or get off your butt and submit a patch")
- else
- (command << "echo Unsupported database adapter: #{config["adapter"]}")
- end
- system(command)
- end
Add Comment
Please, Sign In to add comment