| | 2 | |
|---|
| | 3 | desc 'Creates the databases defined in your config/database.yml (unless they already exist)' |
|---|
| | 4 | task :create => :environment do |
|---|
| | 5 | ActiveRecord::Base.configurations.each_value do |config| |
|---|
| | 6 | begin |
|---|
| | 7 | ActiveRecord::Base.establish_connection(config) |
|---|
| | 8 | ActiveRecord::Base.connection |
|---|
| | 9 | rescue |
|---|
| | 10 | case config['adapter'] |
|---|
| | 11 | when 'mysql' |
|---|
| | 12 | @charset = ENV['CHARSET'] || 'utf8' |
|---|
| | 13 | @collation = ENV['COLLATION'] || 'utf8_general_ci' |
|---|
| | 14 | |
|---|
| | 15 | ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) |
|---|
| | 16 | ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) |
|---|
| | 17 | ActiveRecord::Base.establish_connection(config) |
|---|
| | 18 | when 'postgresql' |
|---|
| | 19 | `createdb "#{config['database']}" -E utf8` |
|---|
| | 20 | end |
|---|
| | 21 | end |
|---|
| | 22 | end |
|---|
| | 23 | ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[RAILS_ENV || 'development']) |
|---|
| | 24 | end |
|---|
| | 25 | |
|---|
| | 26 | desc 'Drops the database for your currenet RAILS_ENV as defined in config/database.yml' |
|---|
| | 27 | task :drop => :environment do |
|---|
| | 28 | config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] |
|---|
| | 29 | case config['adapter'] |
|---|
| | 30 | when 'mysql' |
|---|
| | 31 | ActiveRecord::Base.connection.drop_database config['database'] |
|---|
| | 32 | when 'sqlite3' |
|---|
| | 33 | FileUtils.rm_f File.join(RAILS_ROOT, config['database']) |
|---|
| | 34 | when 'postgresql' |
|---|
| | 35 | `dropdb "#{config['database']}"` |
|---|
| | 36 | end |
|---|
| | 37 | end |
|---|
| | 38 | |
|---|
| | 45 | desc 'Drops, creates and then migrates the database for your current RAILS_ENV. Target specific version with VERSION=x' |
|---|
| | 46 | task :reset => ['db:drop', 'db:create', 'db:migrate'] |
|---|
| | 47 | |
|---|
| | 48 | desc "retrieve the charset for your database defined in your current RAILS_ENV" |
|---|
| | 49 | task :charset => :environment do |
|---|
| | 50 | config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] |
|---|
| | 51 | case config['adapter'] |
|---|
| | 52 | when 'mysql' |
|---|
| | 53 | ActiveRecord::Base.establish_connection(config) |
|---|
| | 54 | puts ActiveRecord::Base.connection.charset |
|---|
| | 55 | else |
|---|
| | 56 | puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' |
|---|
| | 57 | end |
|---|
| | 58 | end |
|---|
| | 59 | |
|---|
| | 60 | desc "retrieve the collation for your database" |
|---|
| | 61 | task :collation => :environment do |
|---|
| | 62 | config = ActiveRecord::Base.configurations[RAILS_ENV || 'development'] |
|---|
| | 63 | case config['adapter'] |
|---|
| | 64 | when 'mysql' |
|---|
| | 65 | ActiveRecord::Base.establish_connection(config) |
|---|
| | 66 | puts ActiveRecord::Base.connection.collation |
|---|
| | 67 | else |
|---|
| | 68 | puts 'sorry, your database adapter is not supported yet, feel free to submit a patch' |
|---|
| | 69 | end |
|---|
| | 70 | end |
|---|
| | 71 | |
|---|