]> source.dussan.org Git - sonarqube.git/blob
bd8f86944170bdad5f5175a3a249aae325cddd9d
[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
6       public :instance_variable_set
7       attr_reader :actions
8     end
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", []
13   end
14   redefined_task = task(*args, &block)
15   enhancements.each {|enhancement| redefined_task.actions << enhancement}
16 end
17
18 def rails_env
19   defined?(Rails.env) ? Rails.env : RAILS_ENV
20 end
21
22 namespace :db do
23   redefine_task :create => :rails_env do
24     create_database(ActiveRecord::Base.configurations[rails_env])
25   end
26   task :create => :load_config if Rake.application.lookup(:load_config)
27
28   redefine_task :drop => :environment do
29     config = ActiveRecord::Base.configurations[rails_env]
30     begin
31       db = find_database_name(config)
32       ActiveRecord::Base.connection.drop_database(db)
33     rescue
34       drop_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
35     end
36   end
37   task :drop => :load_config if Rake.application.lookup(:load_config)
38
39   namespace :create do
40     task :all => :rails_env
41   end
42
43   namespace :drop do
44     task :all => :environment
45   end
46
47   class << self
48     alias_method :previous_create_database, :create_database
49     alias_method :previous_drop_database, :drop_database
50   end
51
52   def find_database_name(config)
53     db = config['database']
54     if config['adapter'] =~ /postgresql/i
55       config = config.dup
56       if config['url']
57         url = config['url'].dup
58         db = url[/\/([^\/]*)$/, 1]
59         if db
60           url[/\/([^\/]*)$/, 1] = 'postgres'
61           config['url'] = url
62         end
63       else
64         db = config['database']
65         config['database'] = 'postgres'
66       end
67       ActiveRecord::Base.establish_connection(config)
68     else
69       ActiveRecord::Base.establish_connection(config)
70       db = ActiveRecord::Base.connection.database_name
71     end
72     db
73   end
74
75   def create_database(config)
76     begin
77       ActiveRecord::Base.establish_connection(config)
78       ActiveRecord::Base.connection
79     rescue
80       begin
81         if url = config['url'] and url =~ /^(.*(?<!\/)\/)(?=\w)/
82           url = $1
83         end
84
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)
88       rescue => e
89         raise e unless config['adapter'] =~ /mysql|postgresql|sqlite/
90         previous_create_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
91       end
92     end
93   end
94
95   def drop_database(config)
96     previous_drop_database(config.merge('adapter' => config['adapter'].sub(/^jdbc/, '')))
97   end
98
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 }
106       end
107     end
108   end
109
110   namespace :test do
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|
117         begin
118           ActiveRecord::Base.connection.execute(ddl.chomp(';'))
119         rescue Exception => ex
120           puts ex.message
121         end
122       end
123     end
124
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)
129     end
130   end
131 end