3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v79;
22 import java.sql.SQLException;
23 import org.sonar.api.SonarRuntime;
24 import org.sonar.api.utils.System2;
25 import org.sonar.api.utils.log.Logger;
26 import org.sonar.api.utils.log.Loggers;
27 import org.sonar.db.Database;
28 import org.sonar.server.platform.db.migration.step.DataChange;
30 public class PopulateInstallDateAndVersion extends DataChange {
32 private static final Logger LOG = Loggers.get(PopulateInstallDateAndVersion.class);
33 private static final String INSTALLATION_DATE = "installation.date";
34 private static final String INSTALLATION_VERSION = "installation.version";
35 private final SonarRuntime sonarRuntime;
36 private final System2 system2;
38 public PopulateInstallDateAndVersion(Database db, SonarRuntime sonarRuntime, System2 system2) {
40 this.sonarRuntime = sonarRuntime;
41 this.system2 = system2;
45 protected void execute(Context context) throws SQLException {
46 removeProperties(context);
47 Long createdAt = context.prepareSelect("select min(created_at) from users where created_at is not null")
48 .get(row -> row.getLong(1));
49 if (createdAt != null && createdAt != 0) {
50 populateInstallationDate(context, createdAt);
51 populateInstallationVersion(context, createdAt);
55 private void populateInstallationDate(Context context, Long createdAt) throws SQLException {
56 insertInternalProperty(context, INSTALLATION_DATE, String.valueOf(createdAt));
59 private void populateInstallationVersion(Context context, Long createdAt) throws SQLException {
60 if (Math.abs(system2.now() - createdAt) / 60 / 60 / 1000 <= 24) {
61 String apiVersion = sonarRuntime.getApiVersion().toString();
62 insertInternalProperty(context, INSTALLATION_VERSION, apiVersion);
64 // if the difference between now and smallest account creation date is more than a day, we consider that this is a
65 // start with an existing SQ, and not a fresh start. in this case, we do not populate the internalProperty,
66 // as there is no way to know the original SQ installation version.
67 LOG.warn("skipping " + INSTALLATION_VERSION + " because we cannot determine what is the installation version.");
71 private void insertInternalProperty(Context context, String key, String value) throws SQLException {
72 context.prepareUpsert("insert into internal_properties (kee, is_empty, text_value, clob_value, created_at) VALUES (?, ?, ?, ?, ?)")
77 .setLong(5, system2.now())
78 .execute().commit().close();
81 private static void removeProperties(Context context) throws SQLException {
82 context.prepareUpsert("delete from internal_properties where kee = ? or kee = ?")
83 .setString(1, INSTALLATION_DATE)
84 .setString(2, INSTALLATION_VERSION)
85 .execute().commit().close();