Changeset 1061

Show
Ignore:
Timestamp:
12/02/07 15:41:14 (10 months ago)
Author:
e.@brainspl.at
Message:

applying patch for the rakefile in merb_activerecord plugin. closes#316

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/merb_activerecord/lib/merb/orms/active_record/tasks/databases.rb

    r709 r1061  
    116116 
    117117  namespace :schema do 
    118     desc "Create a schema/schema.rb file that can be portably used against any DB supported by AR" 
    119     task :dump => :environment do 
     118    desc 'Create a schema/schema.rb file that can be portably used against any DB supported by AR' 
     119    task :dump do 
    120120      require 'active_record/schema_dumper' 
    121121      File.open(ENV['SCHEMA'] || "schema/schema.rb", "w") do |file| 
     
    123123      end 
    124124    end 
    125  
     125     
    126126    desc "Load a schema.rb file into the database" 
    127     task :load => :environment do 
     127    task :load do 
    128128      file = ENV['SCHEMA'] || "schema/schema.rb" 
    129129      load(file) 
     
    131131  end 
    132132 
    133   namespace :structure do 
     133 namespace :structure do 
    134134    desc "Dump the database structure to a SQL file" 
    135     task :dump => :environment do 
    136       abcs = ActiveRecord::Base.configurations 
    137       case abcs[MERB_ENV][:adapter] 
    138       when "mysql", "oci", "oracle" 
    139         ActiveRecord::Base.establish_connection(abcs[MERB_ENV]) 
    140         File.open("schema/#{MERB_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } 
    141       when "postgresql" 
    142         ENV['PGHOST']     = abcs[MERB_ENV]["host"] if abcs[MERB_ENV]["host"
    143         ENV['PGPORT']     = abcs[MERB_ENV]["port"].to_s if abcs[MERB_ENV]["port"
    144         ENV['PGPASSWORD'] = abcs[MERB_ENV]["password"].to_s if abcs[MERB_ENV]["password"
    145         search_path = abcs[MERB_ENV]["schema_search_path"
    146         search_path = "--schema=#{search_path}" if search_path 
    147         `pg_dump -i -U "#{abcs[MERB_ENV]["username"]}" -s -x -O -f schema/#{MERB_ENV}_structure.sql #{search_path} #{abcs[MERB_ENV]["database"]}` 
    148         raise "Error dumping database" if $?.exitstatus == 1 
    149       when "sqlite", "sqlite3" 
    150         dbfile = abcs[MERB_ENV]["database"] || abcs[MERB_ENV]["dbfile"
    151         `#{abcs[MERB_ENV]["adapter"]} #{dbfile} .schema > schema/#{MERB_ENV}_structure.sql` 
    152       when "sqlserver" 
    153         `scptxfr /s #{abcs[MERB_ENV]["host"]} /d #{abcs[MERB_ENV]["database"]} /I /f db\\#{MERB_ENV}_structure.sql /q /A /r` 
    154         `scptxfr /s #{abcs[MERB_ENV]["host"]} /d #{abcs[MERB_ENV]["database"]} /I /F db\ /q /A /r` 
    155       when "firebird" 
    156         set_firebird_env(abcs[MERB_ENV]) 
    157         db_string = firebird_db_string(abcs[MERB_ENV]) 
    158         sh "isql -a #{db_string} > schema/#{MERB_ENV}_structure.sql" 
    159       else 
    160         raise "Task not supported by '#{abcs[MERB_ENV][:adapter]}'" 
     135    task :dump do 
     136      config = ActiveRecord::Base.configurations[MERB_ENV.to_sym] 
     137      case config[:adapter] 
     138        when "mysql", "oci", "oracle" 
     139          ActiveRecord::Base.establish_connection(config[MERB_ENV]) 
     140          File.open("schema/#{MERB_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } 
     141        when "postgresql" 
     142          ENV['PGHOST']     = config[:host] if config[:host
     143          ENV['PGPORT']     = config[:port].to_s if config[:port
     144          ENV['PGPASSWORD'] = config[:password].to_s if config[:password
     145          search_path = config[:schema_search_path
     146          search_path = "--schema=#{search_path}" if search_path 
     147          `pg_dump -i -U "#{config[:username]}" -s -x -O -f schema/#{MERB_ENV}_structure.sql #{search_path} #{config[:database]}` 
     148          raise "Error dumping database" if $?.exitstatus == 1 
     149        when "sqlite", "sqlite3" 
     150          dbfile = config[:database] || config[:dbfile
     151          `#{config[:adapter]} #{dbfile} .schema > schema/#{MERB_ENV}_structure.sql` 
     152        when "sqlserver" 
     153          `scptxfr /s #{config[:host]} /d #{config[:database]} /I /f schema\\#{MERB_ENV}_structure.sql /q /A /r` 
     154          `scptxfr /s #{config[:host]} /d #{config[:database]} /I /F schema\ /q /A /r` 
     155        when "firebird" 
     156          set_firebird_env(config[MERB_ENV]) 
     157          db_string = firebird_db_string(config[MERB_ENV]) 
     158          sh "isql -a #{db_string} > schema/#{MERB_ENV}_structure.sql" 
     159        else 
     160          raise "Task not supported by '#{config[:adapter]}'" 
    161161      end 
    162162 
     
    170170    desc "Recreate the test database from the current environment's database schema" 
    171171    task :clone => %w(db:schema:dump db:test:purge) do 
    172       ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) 
     172      ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[:test]) 
    173173      ActiveRecord::Schema.verbose = false 
    174174      Rake::Task["db:schema:load"].invoke 
    175175    end 
    176176 
    177  
    178177    desc "Recreate the test databases from the development structure" 
    179178    task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do 
    180       abcs = ActiveRecord::Base.configurations 
    181       case abcs["test"]["adapter"
    182       when "mysql" 
    183         ActiveRecord::Base.establish_connection(:test) 
    184         ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') 
    185         IO.readlines("schema/#{MERB_ENV}_structure.sql").join.split("\n\n").each do |table| 
    186           ActiveRecord::Base.connection.execute(table) 
    187         end 
    188       when "postgresql" 
    189         ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"
    190         ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"
    191         ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"
    192         `psql -U "#{abcs["test"]["username"]}" -f schema/#{MERB_ENV}_structure.sql #{abcs["test"]["database"]}` 
    193       when "sqlite", "sqlite3" 
    194         dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"
    195         `#{abcs["test"]["adapter"]} #{dbfile} < schema/#{MERB_ENV}_structure.sql` 
    196       when "sqlserver" 
    197         `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{MERB_ENV}_structure.sql` 
    198       when "oci", "oracle" 
    199         ActiveRecord::Base.establish_connection(:test) 
    200         IO.readlines("schema/#{MERB_ENV}_structure.sql").join.split(";\n\n").each do |ddl| 
    201           ActiveRecord::Base.connection.execute(ddl) 
    202         end 
    203       when "firebird" 
    204         set_firebird_env(abcs["test"]
    205         db_string = firebird_db_string(abcs["test"]
    206         sh "isql -i schema/#{MERB_ENV}_structure.sql #{db_string}" 
    207       else 
    208         raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
    209       end 
    210     end 
    211  
     179      config = ActiveRecord::Base.configurations[:test] 
     180      case config[:adapter
     181        when "mysql" 
     182          ActiveRecord::Base.establish_connection(:test) 
     183          ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') 
     184          IO.readlines("schema/#{MERB_ENV}_structure.sql").join.split("\n\n").each do |table| 
     185            ActiveRecord::Base.connection.execute(table) 
     186          end 
     187        when "postgresql" 
     188          ENV['PGHOST']     = config[:host] if config[:host
     189          ENV['PGPORT']     = config[:port].to_s if config[:port
     190          ENV['PGPASSWORD'] = config[:password].to_s if config[:password
     191          `psql -U "#{config[:username]}" -f schema/#{MERB_ENV}_structure.sql #{config[:database]}` 
     192        when "sqlite", "sqlite3" 
     193          dbfile = config[:database] ||config[:dbfile
     194          `#{config[:adapter]} #{dbfile} < schema/#{MERB_ENV}_structure.sql` 
     195        when "sqlserver" 
     196          `osql -E -S #{config[:host]} -d #{config[:database]} -i schema\\#{MERB_ENV}_structure.sql` 
     197        when "oci", "oracle" 
     198          ActiveRecord::Base.establish_connection(:test) 
     199          IO.readlines("schema/#{MERB_ENV}_structure.sql").join.split(";\n\n").each do |ddl| 
     200            ActiveRecord::Base.connection.execute(ddl) 
     201          end 
     202        when "firebird" 
     203          set_firebird_env(config
     204          db_string = firebird_db_string(config
     205          sh "isql -i schema/#{MERB_ENV}_structure.sql #{db_string}" 
     206        else 
     207          raise "Task not supported by '#{config[:adapter]}'" 
     208      end 
     209    end 
     210     
    212211    desc "Empty the test database" 
    213     task :purge => :environment do 
    214       abcs = ActiveRecord::Base.configurations 
    215       case abcs["test"]["adapter"] 
    216       when "mysql" 
    217         ActiveRecord::Base.establish_connection(:test) 
    218         ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) 
    219       when "postgresql" 
    220         ENV['PGHOST']     = abcs["test"]["host"] if abcs["test"]["host"] 
    221         ENV['PGPORT']     = abcs["test"]["port"].to_s if abcs["test"]["port"] 
    222         ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] 
    223         enc_option = "-E #{abcs["test"]["encoding"]}" if abcs["test"]["encoding"] 
    224  
    225         ActiveRecord::Base.clear_active_connections! 
    226         `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
    227         `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` 
    228       when "sqlite","sqlite3" 
    229         dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] 
    230         File.delete(dbfile) if File.exist?(dbfile) 
    231       when "sqlserver" 
    232         dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') 
    233         `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` 
    234         `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{MERB_ENV}_structure.sql` 
    235       when "oci", "oracle" 
    236         ActiveRecord::Base.establish_connection(:test) 
    237         ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| 
    238           ActiveRecord::Base.connection.execute(ddl) 
    239         end 
    240       when "firebird" 
    241         ActiveRecord::Base.establish_connection(:test) 
    242         ActiveRecord::Base.connection.recreate_database! 
    243       else 
    244         raise "Task not supported by '#{abcs["test"]["adapter"]}'" 
    245       end 
    246     end 
    247  
    248     desc 'Prepare the test database and load the schema' 
    249     task :prepare => :environment do 
     212    task :purge do 
     213      config = ActiveRecord::Base.configurations[:test] 
     214      case config[:adapter] 
     215        when "mysql" 
     216          ActiveRecord::Base.establish_connection(:test) 
     217          ActiveRecord::Base.connection.recreate_database(config[:database]) 
     218        when "postgresql" 
     219          ENV['PGHOST']     = config[:host] if config[:host] 
     220          ENV['PGPORT']     = configs[:port].to_s if config[:port] 
     221          ENV['PGPASSWORD'] = configs[:password].to_s if config[:password] 
     222          enc_option = "-E #{config[:encoding]}" if config[:encoding] 
     223          ActiveRecord::Base.clear_active_connections! 
     224          `dropdb -U "#{config[:username]}" #{config[:database]}` 
     225          `createdb #{enc_option} -U "#{config[:username]}" #{config[:database]}` 
     226        when "sqlite","sqlite3" 
     227          dbfile = config[:database] || config[:dbfile] 
     228          File.delete(dbfile) if File.exist?(dbfile) 
     229        when "sqlserver" 
     230        config  ActiveRecord::Base.establish_connection(:test) 
     231          ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| 
     232            ActiveRecord::Base.connection.execute(ddl) 
     233          end 
     234        when "firebird" 
     235          ActiveRecord::Base.establish_connection(:test) 
     236          ActiveRecord::Base.connection.recreate_database! 
     237        else 
     238          raise "Task not supported by '#{config[:adapter]}'" 
     239      end 
     240    end 
     241 
     242    desc "Prepare the test database and load the schema" 
     243    task :prepare => ["db:test:clone_structure", "db:test:clone"] do 
    250244      if defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank? 
    251245        Rake::Task[{ :sql  => "db:test:clone_structure", :ruby => "db:test:clone" }[ActiveRecord::Base.schema_format]].invoke