Skip to content

Commit

Permalink
db:create works with remote databases whereas db:create:all only crea…
Browse files Browse the repository at this point in the history
…tesdatabases on localhost. Closes #9753.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7718 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Oct 2, 2007
1 parent f854ecd commit 9264bdc
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 31 deletions.
3 changes: 3 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,8 @@
*SVN* *SVN*


* db:create works with remote databases whereas db:create:all only creates
databases on localhost. #9753 [Trevor Wennblom]

* Removed calls to fixtures in generated tests as fixtures :all is now present by default in test_helper.rb [DHH] * Removed calls to fixtures in generated tests as fixtures :all is now present by default in test_helper.rb [DHH]


* Add --prefix option to script/server when using mongrel. [dacat] * Add --prefix option to script/server when using mongrel. [dacat]
Expand Down
81 changes: 50 additions & 31 deletions railties/lib/tasks/databases.rake
Expand Up @@ -3,54 +3,73 @@ namespace :db do
desc 'Create all the local databases defined in config/database.yml' desc 'Create all the local databases defined in config/database.yml'
task :all => :environment do task :all => :environment do
ActiveRecord::Base.configurations.each_value do |config| ActiveRecord::Base.configurations.each_value do |config|
create_local_database(config) # Skip entries that don't have a database key, such as the first entry here:
#
# defaults: &defaults
# adapter: mysql
# username: root
# password:
# host: localhost
#
# development:
# database: blog_development
# <<: *defaults
next unless config['database']
# Only connect to local databases
if config['host'] == 'localhost' || config['host'].blank?
create_database(config)
else
p "This task only creates local databases. #{config['database']} is on a remote host."
end
end end
end end
end end


desc 'Create the local database defined in config/database.yml for the current RAILS_ENV' desc 'Create the database defined in config/database.yml for the current RAILS_ENV'
task :create => :environment do task :create => :environment do
create_local_database(ActiveRecord::Base.configurations[RAILS_ENV]) create_database(ActiveRecord::Base.configurations[RAILS_ENV])
end end


def create_local_database(config) def create_database(config)
# Only connect to local databases begin
if config['host'] == 'localhost' || config['host'].blank? ActiveRecord::Base.establish_connection(config)
begin ActiveRecord::Base.connection
ActiveRecord::Base.establish_connection(config) rescue
ActiveRecord::Base.connection case config['adapter']
rescue when 'mysql'
case config['adapter'] @charset = ENV['CHARSET'] || 'utf8'
when 'mysql' @collation = ENV['COLLATION'] || 'utf8_general_ci'
@charset = ENV['CHARSET'] || 'utf8' begin
@collation = ENV['COLLATION'] || 'utf8_general_ci' ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
begin ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation})
ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) rescue
ActiveRecord::Base.establish_connection(config) $stderr.puts "Couldn't create database for #{config.inspect}"
rescue
$stderr.puts "Couldn't create database for #{config.inspect}"
end
when 'postgresql'
`createdb "#{config['database']}" -E utf8`
when 'sqlite'
`sqlite "#{config['database']}"`
when 'sqlite3'
`sqlite3 "#{config['database']}"`
end end
else when 'postgresql'
p "#{config['database']} already exists" `createdb "#{config['database']}" -E utf8`
when 'sqlite'
`sqlite "#{config['database']}"`
when 'sqlite3'
`sqlite3 "#{config['database']}"`
end end
else else
p "This task only creates local databases. #{config['database']} is on a remote host." p "#{config['database']} already exists"
end end
end end


namespace :drop do namespace :drop do
desc 'Drops all the local databases defined in config/database.yml' desc 'Drops all the local databases defined in config/database.yml'
task :all => :environment do task :all => :environment do
ActiveRecord::Base.configurations.each_value do |config| ActiveRecord::Base.configurations.each_value do |config|
drop_database(config) # Skip entries that don't have a database key
next unless config['database']
# Only connect to local databases
if config['host'] == 'localhost' || config['host'].blank?
drop_database(config)
else
p "This task only drops local databases. #{config['database']} is on a remote host."
end
end end
end end
end end
Expand Down

0 comments on commit 9264bdc

Please sign in to comment.