1 def redefine_task(*args, &block)
2 task_name = Hash === args.first ? args.first.keys[0] : args.first
3 existing_task = Rake.application.lookup task_name
6 public :instance_variable_set
9 existing_task.instance_variable_set "@prerequisites", FileList[]
10 existing_task.actions.shift
11 enhancements = existing_task.actions
12 existing_task.instance_variable_set "@actions", []
14 redefined_task = task(*args, &block)
15 enhancements.each {|enhancement| redefined_task.actions << enhancement}
19 defined?(Rails.env) ? Rails.env : RAILS_ENV
23 redefine_task :create => :rails_env do
24 create_database(ActiveRecord::Base.configurations[rails_env])
26 task :create => :load_config if Rake.application.lookup(:load_config)
28 redefine_task :drop => :environment do
29 config = ActiveRecord::Base.configurations[rails_env]
31 db = find_database_name(config)
32 ActiveRecord::Base.connection.drop_database(db)
34 drop_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
37 task :drop => :load_config if Rake.application.lookup(:load_config)
40 task :all => :rails_env
44 task :all => :environment
48 alias_method :previous_create_database, :create_database
49 alias_method :previous_drop_database, :drop_database
52 def find_database_name(config)
53 db = config['database']
54 if config['adapter'] =~ /postgresql/i
57 url = config['url'].dup
58 db = url[/\/([^\/]*)$/, 1]
60 url[/\/([^\/]*)$/, 1] = 'postgres'
64 db = config['database']
65 config['database'] = 'postgres'
67 ActiveRecord::Base.establish_connection(config)
69 ActiveRecord::Base.establish_connection(config)
70 db = ActiveRecord::Base.connection.database_name
75 def create_database(config)
77 ActiveRecord::Base.establish_connection(config)
78 ActiveRecord::Base.connection
81 if url = config['url'] and url =~ /^(.*(?<!\/)\/)(?=\w)/
85 ActiveRecord::Base.establish_connection(config.merge({'database' => nil, 'url' => url}))
86 ActiveRecord::Base.connection.create_database(config['database'])
87 ActiveRecord::Base.establish_connection(config)
89 raise e unless config['adapter'] =~ /mysql|postgresql|sqlite/
90 previous_create_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
95 def drop_database(config)
96 previous_drop_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
99 namespace :structure do
100 redefine_task :dump => :environment do
101 abcs = ActiveRecord::Base.configurations
102 ActiveRecord::Base.establish_connection(abcs[rails_env])
103 File.open("db/#{rails_env}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
104 if ActiveRecord::Base.connection.supports_migrations?
105 File.open("db/#{rails_env}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
111 redefine_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
112 abcs = ActiveRecord::Base.configurations
113 abcs['test']['pg_params'] = '?allowEncodingChanges=true' if abcs['test']['adapter'] =~ /postgresql/i
114 ActiveRecord::Base.establish_connection(abcs["test"])
115 ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if abcs["test"]["adapter"] =~ /mysql/i
116 IO.readlines("db/#{rails_env}_structure.sql").join.split(";\n\n").each do |ddl|
118 ActiveRecord::Base.connection.execute(ddl.chomp(';'))
119 rescue Exception => ex
125 redefine_task :purge => :environment do
126 abcs = ActiveRecord::Base.configurations
127 db = find_database_name(abcs['test'])
128 ActiveRecord::Base.connection.recreate_database(db)