aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server/src/main/webapp/WEB-INF/lib/database_version.rb
blob: 7ba8d01dcd22941ab405217747d8ad11b1c72a39 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#
# Sonar, entreprise quality control tool.
# Copyright (C) 2008-2012 SonarSource
# mailto:contact AT sonarsource DOT com
#
# Sonar is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Sonar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with Sonar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
#
require 'java'

class DatabaseVersion

  class DeprecatedSchemaInfo < ActiveRecord::Base
    set_table_name 'schema_info'
  end

  def self.current_version
    begin
      result=ActiveRecord::Migrator.current_version
    rescue
      result=0
    end

    if result==0
      begin
        result=DeprecatedSchemaInfo.find(:first).version
      rescue
      end
    end
    result
  end

  def self.target_version
    files = Dir["#{migrations_path}/[0-9]*_*.rb"].sort
    files.last.scan(/([0-9]+)_[_a-z0-9]*.rb/).first[0].to_i
  end

  def self.migrations_path
    File.dirname(__FILE__).to_s + "/../db/migrate/"
  end

  $uptodate = false

  def self.uptodate?
    unless $uptodate
      $uptodate = (current_version>=target_version)
    end
    $uptodate
  end

  def self.upgrade_and_start
    ActiveRecord::Migrator.migrate(migrations_path)
    Java::OrgSonarServerPlatform::Platform.getInstance().start()
    load_plugin_webservices()
  end

  def self.load_plugin_webservices
    ActionController::Routing::Routes.load_sonar_plugins_routes
  end

  def self.automatic_setup
    if current_version<=0
      try_restore_structure_dump()
      upgrade_and_start()
    elsif uptodate?
      load_plugin_webservices()
    else
      puts 'Database must be upgraded. Please browse /setup'
    end
    uptodate?
  end

  def self.connected?
    ActiveRecord::Base.connected?
  end

  def self.try_restore_structure_dump()
    ::Java::OrgSonarServerUi::JRubyFacade.getInstance().createDatabase()
  end

  def self.execute_sql_requests(requests)
    requests.each do |request|
      unless request.blank? || request.start_with?('--')
        request.chomp!
        request.chop! if request.end_with?(';')
        ActiveRecord::Base.connection.execute(request)
      end
    end
  end

  def self.dialect
    ::Java::OrgSonarServerUi::JRubyFacade.getInstance().getDatabase().getDialect().getActiveRecordDialectCode()
  end

  def self.production?
    @@production ||=
      begin
        dialect()!='.h2.'
      end
  end
end