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 32 32 ActiveRecord::Base.connection 33 33 rescue 34 34 case config['adapter'] 35 when 'mysql'35 when /mysql/ 36 36 @charset = ENV['CHARSET'] || 'utf8' 37 37 @collation = ENV['COLLATION'] || 'utf8_general_ci' 38 38 begin … … 42 42 rescue 43 43 $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)" 44 44 end 45 when 'postgresql'45 when /postgresql/ 46 46 @encoding = config[:encoding] || ENV['CHARSET'] || 'utf8' 47 47 begin 48 48 ActiveRecord::Base.establish_connection(config.merge('database' => nil)) … … 52 52 $stderr.puts $!, *($!.backtrace) 53 53 $stderr.puts "Couldn't create database for #{config.inspect}" 54 54 end 55 when 'sqlite'55 when /sqlite$/ 56 56 `sqlite "#{config['database']}"` 57 when 'sqlite3'57 when /sqlite3$/ 58 58 `sqlite3 "#{config['database']}"` 59 59 end 60 60 else … … 232 232 task :dump => :environment do 233 233 abcs = ActiveRecord::Base.configurations 234 234 case abcs[RAILS_ENV]["adapter"] 235 when "mysql", "oci", "oracle"235 when /mysql/, /oci/, /oracle/ 236 236 ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) 237 237 File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } 238 when "postgresql"238 when /postgresql/ 239 239 ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] 240 240 ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] 241 241 ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] … … 243 243 search_path = "--schema=#{search_path}" if search_path 244 244 `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` 245 245 raise "Error dumping database" if $?.exitstatus == 1 246 when "sqlite", "sqlite3"246 when /sqlite/ 247 247 dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] 248 248 `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` 249 when "sqlserver"249 when /sqlserver/ 250 250 `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` 251 251 `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` 252 when "firebird"252 when /firebird/ 253 253 set_firebird_env(abcs[RAILS_ENV]) 254 254 db_string = firebird_db_string(abcs[RAILS_ENV]) 255 255 sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql" … … 276 276 task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do 277 277 abcs = ActiveRecord::Base.configurations 278 278 case abcs["test"]["adapter"] 279 when "mysql"279 when /mysql/ 280 280 ActiveRecord::Base.establish_connection(:test) 281 281 ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') 282 282 IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| 283 283 ActiveRecord::Base.connection.execute(table) 284 284 end 285 when "postgresql"285 when /postgresql/ 286 286 ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] 287 287 ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] 288 288 ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 289 289 `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` 290 when "sqlite", "sqlite3"290 when /sqlite/ 291 291 dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 292 292 `#{abcs["test"]["adapter"]} #{dbfile} < db/#{RAILS_ENV}_structure.sql` 293 when "sqlserver"293 when /sqlserver/ 294 294 `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 295 when "oci", "oracle"295 when /oci/, /oracle/ 296 296 ActiveRecord::Base.establish_connection(:test) 297 297 IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| 298 298 ActiveRecord::Base.connection.execute(ddl) 299 299 end 300 when "firebird"300 when /firebird/ 301 301 set_firebird_env(abcs["test"]) 302 302 db_string = firebird_db_string(abcs["test"]) 303 303 sh "isql -i db/#{RAILS_ENV}_structure.sql #{db_string}" … … 310 310 task :purge => :environment do 311 311 abcs = ActiveRecord::Base.configurations 312 312 case abcs["test"]["adapter"] 313 when "mysql"313 when /mysql/ 314 314 ActiveRecord::Base.establish_connection(:test) 315 315 ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) 316 when "postgresql"316 when /postgresql/ 317 317 ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] 318 318 ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] 319 319 ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] … … 322 322 ActiveRecord::Base.clear_active_connections! 323 323 `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 324 324 `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 325 when "sqlite","sqlite3"325 when /sqlite/ 326 326 dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 327 327 File.delete(dbfile) if File.exist?(dbfile) 328 when "sqlserver"328 when /sqlserver/ 329 329 dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') 330 330 `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` 331 331 `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` 332 when "oci", "oracle"332 when /oci/, /oracle/ 333 333 ActiveRecord::Base.establish_connection(:test) 334 334 ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| 335 335 ActiveRecord::Base.connection.execute(ddl) 336 336 end 337 when "firebird"337 when /firebird/ 338 338 ActiveRecord::Base.establish_connection(:test) 339 339 ActiveRecord::Base.connection.recreate_database! 340 340 else … … 368 368 369 369 def drop_database(config) 370 370 case config['adapter'] 371 when 'mysql'371 when /mysql/ 372 372 ActiveRecord::Base.connection.drop_database config['database'] 373 when / ^sqlite/373 when /sqlite/ 374 374 FileUtils.rm(File.join(RAILS_ROOT, config['database'])) 375 when 'postgresql'375 when /postgresql/ 376 376 ActiveRecord::Base.establish_connection(config.merge('database' => nil)) 377 377 ActiveRecord::Base.connection.drop_database config['database'] 378 378 end