| 6 | | create_local_database(config) |
|---|
| 7 | | end |
|---|
| 8 | | end |
|---|
| 9 | | end |
|---|
| 10 | | |
|---|
| 11 | | desc 'Create the local database defined in config/database.yml for the current RAILS_ENV' |
|---|
| | 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 |
|---|
| | 24 | end |
|---|
| | 25 | end |
|---|
| | 26 | end |
|---|
| | 27 | |
|---|
| | 28 | desc 'Create the database defined in config/database.yml for the current RAILS_ENV' |
|---|
| 13 | | create_local_database(ActiveRecord::Base.configurations[RAILS_ENV]) |
|---|
| 14 | | end |
|---|
| 15 | | |
|---|
| 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']}"` |
|---|
| 40 | | end |
|---|
| 41 | | else |
|---|
| 42 | | p "#{config['database']} already exists" |
|---|
| | 30 | create_database(ActiveRecord::Base.configurations[RAILS_ENV]) |
|---|
| | 31 | end |
|---|
| | 32 | |
|---|
| | 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}" |
|---|
| | 48 | end |
|---|
| | 49 | when 'postgresql' |
|---|
| | 50 | `createdb "#{config['database']}" -E utf8` |
|---|
| | 51 | when 'sqlite' |
|---|
| | 52 | `sqlite "#{config['database']}"` |
|---|
| | 53 | when 'sqlite3' |
|---|
| | 54 | `sqlite3 "#{config['database']}"` |
|---|