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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.db.version;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import javax.annotation.Nullable;
import org.apache.ibatis.session.SqlSession;
import org.sonar.db.MyBatis;
public class DatabaseVersion {
public static final int LAST_VERSION = 1_308;
/**
* The minimum supported version which can be upgraded. Lower
* versions must be previously upgraded to LTS version.
* Note that the value can't be less than current LTS version.
*/
public static final int MIN_UPGRADE_VERSION = 600;
/**
* List of all the tables.
* This list is hardcoded because we didn't succeed in using java.sql.DatabaseMetaData#getTables() in the same way
* for all the supported databases, particularly due to Oracle results.
*/
public static final Set<String> TABLES = ImmutableSet.of(
"active_dashboards",
"active_rules",
"active_rule_parameters",
"activities",
"authors",
"ce_activity",
"ce_queue",
"ce_task_input",
"dashboards",
"duplications_index",
"events",
"file_sources",
"groups",
"groups_users",
"group_roles",
"issues",
"issue_changes",
"issue_filters",
"issue_filter_favourites",
"loaded_templates",
"manual_measures",
"measure_filters",
"measure_filter_favourites",
"metrics",
"notifications",
"permission_templates",
"perm_templates_users",
"perm_templates_groups",
"perm_tpl_characteristics",
"quality_gates",
"quality_gate_conditions",
"projects",
"project_links",
"project_measures",
"project_qprofiles",
"properties",
"resource_index",
"rules",
"rules_parameters",
"rules_profiles",
"scanner_context",
"schema_migrations",
"snapshots",
"users",
"user_roles",
"user_tokens",
"widgets",
"widget_properties"
);
private MyBatis mybatis;
public DatabaseVersion(MyBatis mybatis) {
this.mybatis = mybatis;
}
@VisibleForTesting
static Status getStatus(@Nullable Integer currentVersion, int lastVersion) {
Status status = Status.FRESH_INSTALL;
if (currentVersion != null) {
if (currentVersion == lastVersion) {
status = Status.UP_TO_DATE;
} else if (currentVersion > lastVersion) {
status = Status.REQUIRES_DOWNGRADE;
} else {
status = Status.REQUIRES_UPGRADE;
}
}
return status;
}
public Status getStatus() {
return getStatus(getVersion(), LAST_VERSION);
}
public Integer getVersion() {
SqlSession session = mybatis.openSession(false);
try {
List<Integer> versions = session.getMapper(SchemaMigrationMapper.class).selectVersions();
if (!versions.isEmpty()) {
Collections.sort(versions);
return versions.get(versions.size() - 1);
}
return null;
} catch (RuntimeException e) {
// The table SCHEMA_MIGRATIONS does not exist.
// Ignore this exception -> it will be created by Ruby on Rails migrations.
return null;
} finally {
MyBatis.closeQuietly(session);
}
}
public enum Status {
UP_TO_DATE, REQUIRES_UPGRADE, REQUIRES_DOWNGRADE, FRESH_INSTALL
}
}
|