]> source.dussan.org Git - sonarqube.git/blob
94d2f76105b309c33b2165a6f4f6e313da72d2a9
[sonarqube.git] /
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
4   if existing_task
5     class << existing_task; public :instance_variable_set; end
6     existing_task.instance_variable_set "@prerequisites", FileList[]
7     existing_task.instance_variable_set "@actions", []
8   end
9   task(*args, &block)
10 end
11
12 namespace :db do
13   if Rake::Task["db:create"]
14     redefine_task :create => :environment do
15       create_database(ActiveRecord::Base.configurations[RAILS_ENV])
16     end
17
18     class << self; alias_method :previous_create_database, :create_database; end
19     def create_database(config)
20       begin
21         ActiveRecord::Base.establish_connection(config)
22         ActiveRecord::Base.connection
23       rescue
24         begin
25           url = config['url']
26           if url
27             if url =~ /^(.*\/)/
28               url = $1
29             end
30           end
31
32           ActiveRecord::Base.establish_connection(config.merge({'database' => nil, 'url' => url}))
33           ActiveRecord::Base.connection.create_database(config['database'])
34           ActiveRecord::Base.establish_connection(config)
35         rescue
36           previous_create_database(config)
37         end
38       end
39     end
40
41     redefine_task :drop => :environment do
42       config = ActiveRecord::Base.configurations[RAILS_ENV]
43       begin
44         ActiveRecord::Base.establish_connection(config)
45         db = ActiveRecord::Base.connection.database_name
46         ActiveRecord::Base.connection.drop_database(db)
47       rescue
48         drop_database(config)
49       end
50     end
51   end
52
53   namespace :structure do
54     redefine_task :dump => :environment do
55       abcs = ActiveRecord::Base.configurations
56       ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
57       File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
58       if ActiveRecord::Base.connection.supports_migrations?
59         File.open("db/#{RAILS_ENV}_structure.sql", "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
60       end
61     end
62   end
63
64   namespace :test do
65     redefine_task :clone_structure => [ "db:structure:dump", "db:test:purge" ] do
66       abcs = ActiveRecord::Base.configurations
67       abcs['test']['pg_params'] = '?allowEncodingChanges=true' if abcs['test']['adapter'] =~ /postgresql/i
68       ActiveRecord::Base.establish_connection(abcs["test"])
69       ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') if abcs["test"]["adapter"] =~ /mysql/i
70       IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl|
71         ActiveRecord::Base.connection.execute(ddl.chomp(';'))
72       end
73     end
74
75     redefine_task :purge => :environment do
76       abcs = ActiveRecord::Base.configurations
77       config = abcs['test'].dup
78       if config['adapter'] =~ /postgresql/i
79         if config['url']
80           db = config['url'][/\/([^\/]*)$/, 1]
81           config['url'][/\/([^\/]*)$/, 1] if db_name
82         else
83           db = config['database']
84           config['database'] = 'postgres'
85         end
86         ActiveRecord::Base.establish_connection(config)
87       else
88         ActiveRecord::Base.establish_connection(config)
89         db = ActiveRecord::Base.connection.database_name
90       end
91       ActiveRecord::Base.connection.recreate_database(db)
92     end
93   end
94 end