GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of nicksieger/activerecord-jdbc-adapter
Description: ActiveRecord adapter for JDBC and JRuby
Homepage: http://jruby-extras.rubyforge.org/activerecord-jdbc-adapter
Clone URL: git://github.com/abedra/activerecord-jdbc-adapter.git
abedra (author)
Mon May 12 07:25:39 -0700 2008
commit  49fcfff2d30f6ed6339f58484c297118484eaa9b
tree    1721914217dd9151864b075890b389b6f580effb
parent  ebd4c32571883dc13fdd26323bce4ddbdf66e03e
100644 85 lines (77 sloc) 3.131 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
if defined?(namespace) && RUBY_PLATFORM =~ /java/ && ENV["SKIP_AR_JDBC_RAKE_REDEFINES"].nil?
  def redefine_task(*args, &block)
    task_name = Hash === args.first ? args.first.keys[0] : args.first
    existing_task = Rake.application.lookup task_name
    if existing_task
      class << existing_task; public :instance_variable_set; end
      existing_task.instance_variable_set "@prerequisites", FileList[]
      existing_task.instance_variable_set "@actions", []
    end
    task(*args, &block)
  end
 
  namespace :db do
    redefine_task :create => :environment do
      create_database(ActiveRecord::Base.configurations[RAILS_ENV])
    end
 
    def create_database(config)
      begin
        ActiveRecord::Base.establish_connection(config)
        ActiveRecord::Base.connection
      rescue
        begin
          url = config['url']
          if url
            if url =~ /^(.*\/)/
              url = $1
            end
          end
 
          ActiveRecord::Base.establish_connection(config.merge({'database' => nil, 'url' => url}))
          ActiveRecord::Base.connection.create_database(config['database'])
          ActiveRecord::Base.establish_connection(config)
        rescue
          if (config['driver'] || config['adapter']) =~ /postgr/
            `createdb "#{config['database']}" -E utf8`
          else
            warn "couldn't create database #{config['database']}"
          end
        end
      end
    end
    
    redefine_task :drop => :environment do
      begin
        config = ActiveRecord::Base.configurations[environment_name]
        ActiveRecord::Base.establish_connection(config)
        db = ActiveRecord::Base.connection.database_name
        ActiveRecord::Base.connection.recreate_database(db)
      rescue
      end
    end
 
    namespace :structure do
      redefine_task :dump => :environment do
        abcs = ActiveRecord::Base.configurations
        ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
        File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
        if ActiveRecord::Base.connection.supports_migrations?
          File.open("db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
        end
      end
    end
    
    namespace :test do
      redefine_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
        abcs = ActiveRecord::Base.configurations
        ActiveRecord::Base.establish_connection(:test)
        ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if abcs["test"]["adapter"] =~ /mysql/i
        IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
          puts "****** Executing #{ddl} *******"
          ActiveRecord::Base.connection.execute(ddl)
        end
      end
 
      redefine_task :purge => :environment do
        abcs = ActiveRecord::Base.configurations
        ActiveRecord::Base.establish_connection(:test)
        db = ActiveRecord::Base.connection.database_name
        ActiveRecord::Base.connection.recreate_database(db)
      end
    end
  end
end