From eec4c24fae862dd7bc9a495fa9cc0e49b56aa1ad Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Mon, 26 Sep 2016 17:06:34 +0200 Subject: [PATCH] SONAR-8099 create default organization when migrating to SQ 6.2 --- .../server/property/InternalProperties.java | 5 ++ .../1401_create_default_organization.rb | 30 +++++++ .../org/sonar/db/version/DatabaseVersion.java | 2 +- .../sonar/db/version/MigrationStepModule.java | 4 +- .../v62/CreateDefaultOrganization.java | 86 +++++++++++++++++++ .../org/sonar/db/version/rows-h2.sql | 1 + .../db/version/MigrationStepModuleTest.java | 2 +- 7 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1401_create_default_organization.rb create mode 100644 sonar-db/src/main/java/org/sonar/db/version/v62/CreateDefaultOrganization.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/property/InternalProperties.java b/server/sonar-server/src/main/java/org/sonar/server/property/InternalProperties.java index f1d5a1be7b4..edacd532887 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/property/InternalProperties.java +++ b/server/sonar-server/src/main/java/org/sonar/server/property/InternalProperties.java @@ -26,6 +26,11 @@ import javax.annotation.Nullable; * Allows to read and write internal properties. */ public interface InternalProperties { + /** + * The UUID of the default organization. + * Can't be null unless SQ is strongly corrupted. + */ + String DEFAULT_ORGANIZATION = "organization.default"; /** * Read the value of the specified property. diff --git a/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1401_create_default_organization.rb b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1401_create_default_organization.rb new file mode 100644 index 00000000000..7cf75b6d7eb --- /dev/null +++ b/server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1401_create_default_organization.rb @@ -0,0 +1,30 @@ + +# +# SonarQube, open source software quality management tool. +# Copyright (C) 2008-2014 SonarSource +# mailto:contact AT sonarsource DOT com +# +# SonarQube 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. +# +# SonarQube 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. +# + +# +# SonarQube 6.2 +# +class CreateDefaultOrganization < ActiveRecord::Migration + + def self.up + execute_java_migration('org.sonar.db.version.v62.CreateDefaultOrganization') + end +end diff --git a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java index 06faef6610b..e071a09f747 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java +++ b/sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java @@ -30,7 +30,7 @@ import org.sonar.db.MyBatis; public class DatabaseVersion { - public static final int LAST_VERSION = 1_400; + public static final int LAST_VERSION = 1_401; /** * The minimum supported version which can be upgraded. Lower diff --git a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java index 23459350e95..6d22fe4f6b2 100644 --- a/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java +++ b/sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java @@ -159,6 +159,7 @@ import org.sonar.db.version.v61.DropIsGlobalFromDashboards; import org.sonar.db.version.v61.PopulateTableProperties2; import org.sonar.db.version.v61.RemoveViewsDefinitionFromProperties; import org.sonar.db.version.v61.ShrinkModuleUuidPathOfProjects; +import org.sonar.db.version.v62.CreateDefaultOrganization; import org.sonar.db.version.v62.CreateTableOrganizations; public class MigrationStepModule extends Module { @@ -340,6 +341,7 @@ public class MigrationStepModule extends Module { CreateTableRuleRepositories.class, // 6.2 - CreateTableOrganizations.class); + CreateTableOrganizations.class, + CreateDefaultOrganization.class); } } diff --git a/sonar-db/src/main/java/org/sonar/db/version/v62/CreateDefaultOrganization.java b/sonar-db/src/main/java/org/sonar/db/version/v62/CreateDefaultOrganization.java new file mode 100644 index 00000000000..3731a6d123d --- /dev/null +++ b/sonar-db/src/main/java/org/sonar/db/version/v62/CreateDefaultOrganization.java @@ -0,0 +1,86 @@ +/* + * 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.v62; + +import java.sql.SQLException; +import org.sonar.api.utils.System2; +import org.sonar.core.util.UuidFactory; +import org.sonar.db.Database; +import org.sonar.db.version.BaseDataChange; +import org.sonar.db.version.Select; + +public class CreateDefaultOrganization extends BaseDataChange { + private static final String KEY_DEFAULT_ORGANIZATION = "default-organization"; + private static final String INTERNAL_PROPERTY_DEFAULT_ORGANIZATION = "organization.default"; + + private final System2 system2; + private final UuidFactory uuidFactory; + + public CreateDefaultOrganization(Database db, System2 system2, UuidFactory uuidFactory) { + super(db); + this.system2 = system2; + this.uuidFactory = uuidFactory; + } + + @Override + public void execute(Context context) throws SQLException { + String uuid = createDefaultOrganization(context); + saveDefaultOrganizationUuid(context, uuid); + } + + private String createDefaultOrganization(Context context) throws SQLException { + Select select = context.prepareSelect("select uuid from organizations where kee=?"); + select.setString(1, KEY_DEFAULT_ORGANIZATION); + String uuid = select.get(row -> row.getNullableString(1)); + if (uuid == null) { + uuid = uuidFactory.create(); + long now = system2.now(); + context.prepareUpsert("insert into organizations" + + " (uuid, kee, name, created_at, updated_at)" + + " values" + + " (?, ?, ?, ?, ?)") + .setString(1, uuid) + .setString(2, KEY_DEFAULT_ORGANIZATION) + .setString(3, "Default Organization") + .setLong(4, now) + .setLong(5, now) + .execute() + .commit(); + } + return uuid; + } + + private void saveDefaultOrganizationUuid(Context context, String uuid) throws SQLException { + Select select = context.prepareSelect("select kee from internal_properties where kee=?"); + select.setString(1, INTERNAL_PROPERTY_DEFAULT_ORGANIZATION); + if (select.get(row -> row.getNullableString(1)) == null) { + context.prepareUpsert("insert into internal_properties" + + " (kee, is_empty, text_value, created_at)" + + " values" + + " (?, ?, ?, ?)") + .setString(1, INTERNAL_PROPERTY_DEFAULT_ORGANIZATION) + .setBoolean(2, false) + .setString(3, uuid) + .setLong(4, system2.now()) + .execute() + .commit(); + } + } +} diff --git a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql index 70c8eebc5ec..fa877cc0fd6 100644 --- a/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql +++ b/sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql @@ -506,6 +506,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1318'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1319'); INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1400'); +INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1401'); INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', '1418215735482', '1418215735482'); ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2; diff --git a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java index 6059b9fa1f9..844afdc41b4 100644 --- a/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java +++ b/sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java @@ -29,6 +29,6 @@ public class MigrationStepModuleTest { public void verify_count_of_added_MigrationStep_types() { ComponentContainer container = new ComponentContainer(); new MigrationStepModule().configure(container); - assertThat(container.size()).isEqualTo(142); + assertThat(container.size()).isEqualTo(143); } } -- 2.39.5