Skip to content
This repository
Browse code

db:create works with remote databases whereas db:create:all only crea…

…tesdatabases on localhost. Closes #9753.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7718 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9264bdc8f618344307f07790a07a60dc04b80434 1 parent f854ecd
Jeremy Kemper jeremy authored

Showing 2 changed files with 53 additions and 31 deletions. Show diff stats Hide diff stats

  1. +3 0  railties/CHANGELOG
  2. +50 31 railties/lib/tasks/databases.rake
3  railties/CHANGELOG
... ... @@ -1,5 +1,8 @@
1 1 *SVN*
2 2
  3 +* db:create works with remote databases whereas db:create:all only creates
  4 +databases on localhost. #9753 [Trevor Wennblom]
  5 +
3 6 * Removed calls to fixtures in generated tests as fixtures :all is now present by default in test_helper.rb [DHH]
4 7
5 8 * Add --prefix option to script/server when using mongrel. [dacat]
81 railties/lib/tasks/databases.rake
@@ -3,46 +3,58 @@ namespace :db do
3 3 desc 'Create all the local databases defined in config/database.yml'
4 4 task :all => :environment do
5 5 ActiveRecord::Base.configurations.each_value do |config|
6   - create_local_database(config)
  6 + # Skip entries that don't have a database key, such as the first entry here:
  7 + #
  8 + # defaults: &defaults
  9 + # adapter: mysql
  10 + # username: root
  11 + # password:
  12 + # host: localhost
  13 + #
  14 + # development:
  15 + # database: blog_development
  16 + # <<: *defaults
  17 + next unless config['database']
  18 + # Only connect to local databases
  19 + if config['host'] == 'localhost' || config['host'].blank?
  20 + create_database(config)
  21 + else
  22 + p "This task only creates local databases. #{config['database']} is on a remote host."
  23 + end
7 24 end
8 25 end
9 26 end
10 27
11   - desc 'Create the local database defined in config/database.yml for the current RAILS_ENV'
  28 + desc 'Create the database defined in config/database.yml for the current RAILS_ENV'
12 29 task :create => :environment do
13   - create_local_database(ActiveRecord::Base.configurations[RAILS_ENV])
  30 + create_database(ActiveRecord::Base.configurations[RAILS_ENV])
14 31 end
15 32
16   - def create_local_database(config)
17   - # Only connect to local databases
18   - if config['host'] == 'localhost' || config['host'].blank?
19   - begin
20   - ActiveRecord::Base.establish_connection(config)
21   - ActiveRecord::Base.connection
22   - rescue
23   - case config['adapter']
24   - when 'mysql'
25   - @charset = ENV['CHARSET'] || 'utf8'
26   - @collation = ENV['COLLATION'] || 'utf8_general_ci'
27   - begin
28   - ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
29   - ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation})
30   - ActiveRecord::Base.establish_connection(config)
31   - rescue
32   - $stderr.puts "Couldn't create database for #{config.inspect}"
33   - end
34   - when 'postgresql'
35   - `createdb "#{config['database']}" -E utf8`
36   - when 'sqlite'
37   - `sqlite "#{config['database']}"`
38   - when 'sqlite3'
39   - `sqlite3 "#{config['database']}"`
  33 + def create_database(config)
  34 + begin
  35 + ActiveRecord::Base.establish_connection(config)
  36 + ActiveRecord::Base.connection
  37 + rescue
  38 + case config['adapter']
  39 + when 'mysql'
  40 + @charset = ENV['CHARSET'] || 'utf8'
  41 + @collation = ENV['COLLATION'] || 'utf8_general_ci'
  42 + begin
  43 + ActiveRecord::Base.establish_connection(config.merge({'database' => nil}))
  44 + ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation})
  45 + ActiveRecord::Base.establish_connection(config)
  46 + rescue
  47 + $stderr.puts "Couldn't create database for #{config.inspect}"
40 48 end
41   - else
42   - p "#{config['database']} already exists"
  49 + when 'postgresql'
  50 + `createdb "#{config['database']}" -E utf8`
  51 + when 'sqlite'
  52 + `sqlite "#{config['database']}"`
  53 + when 'sqlite3'
  54 + `sqlite3 "#{config['database']}"`
43 55 end
44 56 else
45   - p "This task only creates local databases. #{config['database']} is on a remote host."
  57 + p "#{config['database']} already exists"
46 58 end
47 59 end
48 60
@@ -50,7 +62,14 @@ namespace :db do
50 62 desc 'Drops all the local databases defined in config/database.yml'
51 63 task :all => :environment do
52 64 ActiveRecord::Base.configurations.each_value do |config|
53   - drop_database(config)
  65 + # Skip entries that don't have a database key
  66 + next unless config['database']
  67 + # Only connect to local databases
  68 + if config['host'] == 'localhost' || config['host'].blank?
  69 + drop_database(config)
  70 + else
  71 + p "This task only drops local databases. #{config['database']} is on a remote host."
  72 + end
54 73 end
55 74 end
56 75 end

0 comments on commit 9264bdc

Please sign in to comment.
Something went wrong with that request. Please try again.