Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 7147

Show
Ignore:
Timestamp:
06/28/07 18:23:41 (2 years ago)
Author:
bitsweat
Message:

db:create creates the database for the current environment if it's on localhost. db:create:all creates local databases for all environments. Closes #8783.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/railties/CHANGELOG

    r7139 r7147  
    11*SVN* 
     2 
     3* db:create creates the database for the current environment if it's on localhost. db:create:all creates local databases for all environments.  #8783 [matt] 
    24 
    35* Generators: look for generators in all gems, not just those suffixed with _generator, in the gem's generators or rails_generators directory. Allow use of the rails_generators directory instead of the standard generators directory in plugins also.  #8730 [Dr Nic, topfunky] 
  • trunk/railties/lib/tasks/databases.rake

    r7110 r7147  
    1 namespace :db do   
    2   desc 'Creates the databases defined in config/database.yml (unless they already exist)' 
    3   task :create => :environment do  
    4     ActiveRecord::Base.configurations.each_value do |config| 
     1namespace :db do 
     2  namespace :create do 
     3    desc 'Create all the local databases defined in config/database.yml' 
     4    task :all => :environment do 
     5      ActiveRecord::Base.configurations.each_value do |config| 
     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' 
     12  task :create => :environment do 
     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? 
    519      begin 
    620        ActiveRecord::Base.establish_connection(config) 
     
    1125          @charset   = ENV['CHARSET']   || 'utf8' 
    1226          @collation = ENV['COLLATION'] || 'utf8_general_ci' 
    13  
    1427          begin 
    1528            ActiveRecord::Base.establish_connection(config.merge({'database' => nil})) 
    1629            ActiveRecord::Base.connection.create_database(config['database'], {:charset => @charset, :collation => @collation}) 
    1730            ActiveRecord::Base.establish_connection(config) 
     31            p "MySQL #{config['database']} database succesfully created" 
    1832          rescue 
    1933            $stderr.puts "Couldn't create database for #{config.inspect}" 
    2034          end 
    2135        when 'postgresql' 
    22           `createdb "#{config['database']}" -E utf8`   
     36          `createdb "#{config['database']}" -E utf8` 
    2337        when 'sqlite' 
    2438          `sqlite "#{config['database']}"` 
     
    2640          `sqlite3 "#{config['database']}"` 
    2741        end 
    28       end 
    29     end 
    30     ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[RAILS_ENV || 'development']) 
    31   end 
    32    
     42      else 
     43        p "#{config['database']} already exists" 
     44      end 
     45    else 
     46      p "This task only creates local databases. #{config['database']} is on a remote host." 
     47    end 
     48  end 
     49 
    3350  desc 'Drops the database for the current environment' 
    3451  task :drop => :environment do 
     
    4057      FileUtils.rm_f File.join(RAILS_ROOT, config['database']) 
    4158    when 'postgresql' 
    42       `dropdb "#{config['database']}"`    
    43     end 
    44   end 
    45    
     59      `dropdb "#{config['database']}"` 
     60    end 
     61  end 
     62 
    4663  desc "Migrate the database through scripts in db/migrate. Target specific version with VERSION=x" 
    4764  task :migrate => :environment do 
     
    5269  desc 'Drops, creates and then migrates the database for the current environment. Target specific version with VERSION=x' 
    5370  task :reset => ['db:drop', 'db:create', 'db:migrate'] 
    54    
     71 
    5572  desc "Retrieves the charset for the current environment's database" 
    5673  task :charset => :environment do 
     
    7996  desc "Retrieves the current schema version number" 
    8097  task :version => :environment do 
    81     puts "Current version: " +  
    82       ActiveRecord::Migrator.current_version.to_s 
     98    puts "Current version: #{ActiveRecord::Migrator.current_version}" 
    8399  end 
    84100 
     
    115131      abcs = ActiveRecord::Base.configurations 
    116132      case abcs[RAILS_ENV]["adapter"] 
    117         when "mysql", "oci", "oracle" 
    118           ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) 
    119           File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } 
    120         when "postgresql" 
    121           ENV['PGHOST']     = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] 
    122           ENV['PGPORT']     = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] 
    123           ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] 
    124           search_path = abcs[RAILS_ENV]["schema_search_path"] 
    125           search_path = "--schema=#{search_path}" if search_path 
    126           `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` 
    127           raise "Error dumping database" if $?.exitstatus == 1 
    128         when "sqlite", "sqlite3" 
    129           dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] 
    130           `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` 
    131         when "sqlserver" 
    132           `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` 
    133           `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` 
    134         when "firebird" 
    135           set_firebird_env(abcs[RAILS_ENV]) 
    136           db_string = firebird_db_string(abcs[RAILS_ENV]) 
    137           sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql" 
    138         else 
    139           raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
     133      when "mysql", "oci", "oracle" 
     134        ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) 
     135        File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } 
     136      when "postgresql" 
     137        ENV['PGHOST']     = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] 
     138        ENV['PGPORT']     = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] 
     139        ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] 
     140        search_path = abcs[RAILS_ENV]["schema_search_path"] 
     141        search_path = "--schema=#{search_path}" if search_path 
     142        `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` 
     143        raise "Error dumping database" if $?.exitstatus == 1 
     144      when "sqlite", "sqlite3" 
     145        dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] 
     146        `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` 
     147      when "sqlserver" 
     148        `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` 
     149        `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` 
     150      when "firebird" 
     151        set_firebird_env(abcs[RAILS_ENV]) 
     152        db_string = firebird_db_string(abcs[RAILS_ENV]) 
     153        sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql" 
     154      else 
     155        raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
    140156      end 
    141157 
     
    159175      abcs = ActiveRecord::Base.configurations 
    160176      case abcs["test"]["adapter"] 
    161         when "mysql" 
    162           ActiveRecord::Base.establish_connection(:test) 
    163           ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') 
    164           IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| 
    165             ActiveRecord::Base.connection.execute(table) 
    166           end 
    167         when "postgresql" 
    168           ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
    169           ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
    170           ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
    171           `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` 
    172         when "sqlite", "sqlite3" 
    173           dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
    174           `#{abcs["test"]["adapter"]} #{dbfile} < db/#{RAILS_ENV}_structure.sql` 
    175         when "sqlserver" 
    176           `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 
    177         when "oci", "oracle" 
    178           ActiveRecord::Base.establish_connection(:test) 
    179           IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| 
    180             ActiveRecord::Base.connection.execute(ddl) 
    181           end 
    182         when "firebird" 
    183           set_firebird_env(abcs["test"]) 
    184           db_string = firebird_db_string(abcs["test"]) 
    185           sh "isql -i db/#{RAILS_ENV}_structure.sql #{db_string}" 
    186         else 
    187           raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
     177      when "mysql" 
     178        ActiveRecord::Base.establish_connection(:test) 
     179        ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') 
     180        IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| 
     181          ActiveRecord::Base.connection.execute(table) 
     182        end 
     183      when "postgresql" 
     184        ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
     185        ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
     186        ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
     187        `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` 
     188      when "sqlite", "sqlite3" 
     189        dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
     190        `#{abcs["test"]["adapter"]} #{dbfile} < db/#{RAILS_ENV}_structure.sql` 
     191      when "sqlserver" 
     192        `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 
     193      when "oci", "oracle" 
     194        ActiveRecord::Base.establish_connection(:test) 
     195        IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| 
     196          ActiveRecord::Base.connection.execute(ddl) 
     197        end 
     198      when "firebird" 
     199        set_firebird_env(abcs["test"]) 
     200        db_string = firebird_db_string(abcs["test"]) 
     201        sh "isql -i db/#{RAILS_ENV}_structure.sql #{db_string}" 
     202      else 
     203        raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
    188204      end 
    189205    end 
     
    193209      abcs = ActiveRecord::Base.configurations 
    194210      case abcs["test"]["adapter"] 
    195         when "mysql" 
    196           ActiveRecord::Base.establish_connection(:test) 
    197           ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) 
    198         when "postgresql" 
    199           ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
    200           ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
    201           ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
    202           enc_option = "-E #{abcs["test"]["encoding"]}" if abcs["test"]["encoding"] 
    203  
    204           ActiveRecord::Base.clear_active_connections! 
    205           `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
    206           `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
    207         when "sqlite","sqlite3" 
    208           dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
    209           File.delete(dbfile) if File.exist?(dbfile) 
    210         when "sqlserver" 
    211           dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') 
    212           `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` 
    213           `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 
    214         when "oci", "oracle" 
    215           ActiveRecord::Base.establish_connection(:test) 
    216           ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| 
    217             ActiveRecord::Base.connection.execute(ddl) 
    218           end 
    219         when "firebird" 
    220           ActiveRecord::Base.establish_connection(:test) 
    221           ActiveRecord::Base.connection.recreate_database! 
    222         else 
    223           raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
     211      when "mysql" 
     212        ActiveRecord::Base.establish_connection(:test) 
     213        ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) 
     214      when "postgresql" 
     215        ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
     216        ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
     217        ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
     218        enc_option = "-E #{abcs["test"]["encoding"]}" if abcs["test"]["encoding"] 
     219 
     220        ActiveRecord::Base.clear_active_connections! 
     221        `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
     222        `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
     223      when "sqlite","sqlite3" 
     224        dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
     225        File.delete(dbfile) if File.exist?(dbfile) 
     226      when "sqlserver" 
     227        dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') 
     228        `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` 
     229        `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 
     230      when "oci", "oracle" 
     231        ActiveRecord::Base.establish_connection(:test) 
     232        ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| 
     233          ActiveRecord::Base.connection.execute(ddl) 
     234        end 
     235      when "firebird" 
     236        ActiveRecord::Base.establish_connection(:test) 
     237        ActiveRecord::Base.connection.recreate_database! 
     238      else 
     239        raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
    224240      end 
    225241    end