]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8099 create default organization when migrating to SQ 6.2
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 26 Sep 2016 15:06:34 +0000 (17:06 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 28 Sep 2016 13:26:28 +0000 (15:26 +0200)
server/sonar-server/src/main/java/org/sonar/server/property/InternalProperties.java
server/sonar-web/src/main/webapp/WEB-INF/db/migrate/1401_create_default_organization.rb [new file with mode: 0644]
sonar-db/src/main/java/org/sonar/db/version/DatabaseVersion.java
sonar-db/src/main/java/org/sonar/db/version/MigrationStepModule.java
sonar-db/src/main/java/org/sonar/db/version/v62/CreateDefaultOrganization.java [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/test/java/org/sonar/db/version/MigrationStepModuleTest.java

index f1d5a1be7b413211082fcb823444a41704b3db60..edacd5328873910211fd102a451dad283fe1c09a 100644 (file)
@@ -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 (file)
index 0000000..7cf75b6
--- /dev/null
@@ -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
index 06faef6610b7d881feeb4c39ccb33e2dcf3b9989..e071a09f747743e10c6b9ddfe96f2d2de5f11391 100644 (file)
@@ -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
index 23459350e9511795a3fde7b01402a1c7bd8e3add..6d22fe4f6b277a7b584b220dbd282cb334a90108 100644 (file)
@@ -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 (file)
index 0000000..3731a6d
--- /dev/null
@@ -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();
+    }
+  }
+}
index 70c8eebc5ecb054c23b5892fbc04b3fd48687d78..fa877cc0fd618e7dea70b1a0e6f542ac95b99eeb 100644 (file)
@@ -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;
index 6059b9fa1f9779dca6fae1db66b557cad9dd72da..844afdc41b4f1a3b4cb87177368603e57b9cb9fe 100644 (file)
@@ -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);
   }
 }