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

Ticket #11596: edge-0001-Change-all-databases.rake-adapter-when-statements.patch

File edge-0001-Change-all-databases.rake-adapter-when-statements.patch, 7.5 kB (added by nicksieger, 3 months ago)
  • a/railties/lib/tasks/databases.rake

    old new  
    3232      ActiveRecord::Base.connection 
    3333    rescue 
    3434      case config['adapter'] 
    35       when 'mysql' 
     35      when /mysql/ 
    3636        @charset   = ENV['CHARSET']   || 'utf8' 
    3737        @collation = ENV['COLLATION'] || 'utf8_general_ci' 
    3838        begin 
     
    4242        rescue 
    4343          $stderr.puts "Couldn't create database for #{config.inspect}, charset: #{config['charset'] || @charset}, collation: #{config['collation'] || @collation} (if you set the charset manually, make sure you have a matching collation)" 
    4444        end 
    45       when 'postgresql' 
     45      when /postgresql/ 
    4646        @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8' 
    4747        begin 
    4848          ActiveRecord::Base.establish_connection(config.merge('database' => nil)) 
     
    5252          $stderr.puts $!, *($!.backtrace) 
    5353          $stderr.puts "Couldn't create database for #{config.inspect}" 
    5454        end 
    55       when 'sqlite' 
     55      when /sqlite$/ 
    5656        `sqlite "#{config['database']}"` 
    57       when 'sqlite3' 
     57      when /sqlite3$/ 
    5858        `sqlite3 "#{config['database']}"` 
    5959      end 
    6060    else 
     
    232232    task :dump => :environment do 
    233233      abcs = ActiveRecord::Base.configurations 
    234234      case abcs[RAILS_ENV]["adapter"] 
    235       when "mysql", "oci", "oracle" 
     235      when /mysql/, /oci/, /oracle/ 
    236236        ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) 
    237237        File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } 
    238       when "postgresql" 
     238      when /postgresql/ 
    239239        ENV['PGHOST']     = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] 
    240240        ENV['PGPORT']     = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] 
    241241        ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] 
     
    243243        search_path = "--schema=#{search_path}" if search_path 
    244244        `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` 
    245245        raise "Error dumping database" if $?.exitstatus == 1 
    246       when "sqlite", "sqlite3" 
     246      when /sqlite/ 
    247247        dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] 
    248248        `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` 
    249       when "sqlserver" 
     249      when /sqlserver/ 
    250250        `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` 
    251251        `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` 
    252       when "firebird" 
     252      when /firebird/ 
    253253        set_firebird_env(abcs[RAILS_ENV]) 
    254254        db_string = firebird_db_string(abcs[RAILS_ENV]) 
    255255        sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql" 
     
    276276    task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do 
    277277      abcs = ActiveRecord::Base.configurations 
    278278      case abcs["test"]["adapter"] 
    279       when "mysql" 
     279      when /mysql/ 
    280280        ActiveRecord::Base.establish_connection(:test) 
    281281        ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') 
    282282        IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| 
    283283          ActiveRecord::Base.connection.execute(table) 
    284284        end 
    285       when "postgresql" 
     285      when /postgresql/ 
    286286        ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
    287287        ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
    288288        ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
    289289        `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` 
    290       when "sqlite", "sqlite3" 
     290      when /sqlite/ 
    291291        dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
    292292        `#{abcs["test"]["adapter"]} #{dbfile} < db/#{RAILS_ENV}_structure.sql` 
    293       when "sqlserver" 
     293      when /sqlserver/ 
    294294        `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 
    295       when "oci", "oracle" 
     295      when /oci/, /oracle/ 
    296296        ActiveRecord::Base.establish_connection(:test) 
    297297        IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| 
    298298          ActiveRecord::Base.connection.execute(ddl) 
    299299        end 
    300       when "firebird" 
     300      when /firebird/ 
    301301        set_firebird_env(abcs["test"]) 
    302302        db_string = firebird_db_string(abcs["test"]) 
    303303        sh "isql -i db/#{RAILS_ENV}_structure.sql #{db_string}" 
     
    310310    task :purge => :environment do 
    311311      abcs = ActiveRecord::Base.configurations 
    312312      case abcs["test"]["adapter"] 
    313       when "mysql" 
     313      when /mysql/ 
    314314        ActiveRecord::Base.establish_connection(:test) 
    315315        ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) 
    316       when "postgresql" 
     316      when /postgresql/ 
    317317        ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
    318318        ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
    319319        ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
     
    322322        ActiveRecord::Base.clear_active_connections! 
    323323        `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
    324324        `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
    325       when "sqlite","sqlite3" 
     325      when /sqlite/ 
    326326        dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
    327327        File.delete(dbfile) if File.exist?(dbfile) 
    328       when "sqlserver" 
     328      when /sqlserver/ 
    329329        dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') 
    330330        `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` 
    331331        `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 
    332       when "oci", "oracle" 
     332      when /oci/, /oracle/ 
    333333        ActiveRecord::Base.establish_connection(:test) 
    334334        ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| 
    335335          ActiveRecord::Base.connection.execute(ddl) 
    336336        end 
    337       when "firebird" 
     337      when /firebird/ 
    338338        ActiveRecord::Base.establish_connection(:test) 
    339339        ActiveRecord::Base.connection.recreate_database! 
    340340      else 
     
    368368 
    369369def drop_database(config) 
    370370  case config['adapter'] 
    371   when 'mysql' 
     371  when /mysql/ 
    372372    ActiveRecord::Base.connection.drop_database config['database'] 
    373   when /^sqlite/ 
     373  when /sqlite/ 
    374374    FileUtils.rm(File.join(RAILS_ROOT, config['database'])) 
    375   when 'postgresql' 
     375  when /postgresql/ 
    376376    ActiveRecord::Base.establish_connection(config.merge('database' => nil)) 
    377377    ActiveRecord::Base.connection.drop_database config['database'] 
    378378  end