]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8752 add column ORGANIZATIONS.USER_ID
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 10 Feb 2017 14:41:57 +0000 (15:41 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 10 Feb 2017 17:29:38 +0000 (18:29 +0100)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizations.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizationsTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/DbVersion63Test.java
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizationsTest/previous-organizations.sql [new file with mode: 0644]
sonar-db/src/main/resources/org/sonar/db/version/rows-h2.sql
sonar-db/src/main/resources/org/sonar/db/version/schema-h2.ddl

diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizations.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizations.java
new file mode 100644 (file)
index 0000000..7d42842
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.server.platform.db.migration.version.v63;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.IntegerColumnDef.newIntegerColumnDefBuilder;
+
+public class AddUserIdToOrganizations extends DdlChange {
+  public AddUserIdToOrganizations(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(
+      new AddColumnsBuilder(getDialect(), "organizations")
+        .addColumn(
+          newIntegerColumnDefBuilder()
+            .setColumnName("user_id")
+            .setIsNullable(true)
+            .build())
+        .build());
+  }
+}
index 578aa4f482e53b063018ff76fe3cf062203deb67..a233e2f7e63de2fd5d9c1e79e348e4c41cd63719 100644 (file)
@@ -47,6 +47,7 @@ public class DbVersion63 implements DbVersion {
       .add(1512, "Make ORGANIZATIONS.GUARDED not nullable", MakeColumnGuardedOfOrganizationsNotNullable.class)
       .add(1513, "Make default organization guarded", MakeDefaultOrganizationGuarded.class)
       .add(1514, "Delete some entries in PROPERTIES", DeleteUselessProperties.class)
+      .add(1516, "Add ORGANIZATIONS.USER_ID", AddUserIdToOrganizations.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizationsTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizationsTest.java
new file mode 100644 (file)
index 0000000..af9c778
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info 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.server.platform.db.migration.version.v63;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbTester;
+
+public class AddUserIdToOrganizationsTest {
+
+  @Rule
+  public final DbTester dbTester = DbTester.createForSchema(System2.INSTANCE, AddUserIdToOrganizationsTest.class, "previous-organizations.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private AddUserIdToOrganizations underTest = new AddUserIdToOrganizations(dbTester.database());
+
+  @Test
+  public void add_nullable_integer_column_user_id_to_table_organizations() throws SQLException {
+    underTest.execute();
+
+    dbTester.assertColumnDefinition("organizations", "user_id", Types.INTEGER, null, true);
+  }
+
+  @Test
+  public void migration_is_not_reentrant() throws SQLException {
+    underTest.execute();
+
+    expectedException.expect(IllegalStateException.class);
+
+    underTest.execute();
+  }
+
+}
+
index 5e9fbdf4074817f59d7d1578483eb6cc458925fc..d66dde23fd2b45bd524348b276e8db322a6226d9 100644 (file)
@@ -41,7 +41,7 @@ public class DbVersion63Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 15);
+    verifyMigrationCount(underTest, 16);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizationsTest/previous-organizations.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v63/AddUserIdToOrganizationsTest/previous-organizations.sql
new file mode 100644 (file)
index 0000000..d581374
--- /dev/null
@@ -0,0 +1,15 @@
+CREATE TABLE "ORGANIZATIONS" (
+  "UUID" VARCHAR(40) NOT NULL PRIMARY KEY,
+  "KEE" VARCHAR(32) NOT NULL,
+  "NAME" VARCHAR(64) NOT NULL,
+  "DESCRIPTION" VARCHAR(256),
+  "URL" VARCHAR(256),
+  "AVATAR_URL" VARCHAR(256),
+  "GUARDED" BOOLEAN NOT NULL,
+  "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40),
+  "DEFAULT_PERM_TEMPLATE_VIEW" VARCHAR(40),
+  "CREATED_AT" BIGINT NOT NULL,
+  "UPDATED_AT" BIGINT NOT NULL
+);
+CREATE UNIQUE INDEX "PK_ORGANIZATIONS" ON "ORGANIZATIONS" ("UUID");
+CREATE UNIQUE INDEX "ORGANIZATION_KEY" ON "ORGANIZATIONS" ("KEE");
index 5051b8112d8645ab2036fcd853259984ae463377..a6b2168a67be2b8d01b7aed02fd3482218a0fd0e 100644 (file)
@@ -528,6 +528,7 @@ INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1511');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1512');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1513');
 INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1514');
+INSERT INTO SCHEMA_MIGRATIONS(VERSION) VALUES ('1516');
 
 INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, IS_ROOT, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, 'a373a0e667abb2604c1fd571eb4ad47fe8cc0878', '48bc4b0d93179b5103fd3885ea9119498e9d161b', true, '1418215735482', '1418215735482');
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
index 742cdc0d75c27f1c839794f705774ed218c27a63..4c459333be09a3e3aaa1e5a19292e6810cc3037f 100644 (file)
@@ -6,6 +6,7 @@ CREATE TABLE "ORGANIZATIONS" (
   "URL" VARCHAR(256),
   "AVATAR_URL" VARCHAR(256),
   "GUARDED" BOOLEAN NOT NULL,
+  "USER_ID" INTEGER,
   "DEFAULT_PERM_TEMPLATE_PROJECT" VARCHAR(40),
   "DEFAULT_PERM_TEMPLATE_VIEW" VARCHAR(40),
   "CREATED_AT" BIGINT NOT NULL,