]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18192 - Update size of user uuid column to 255 in audit table
authorLéo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com>
Mon, 16 Jan 2023 15:23:42 +0000 (16:23 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 16 Jan 2023 20:03:43 +0000 (20:03 +0000)
server/sonar-db-dao/src/schema/schema-sq.ddl
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/audit/AuditTesting.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable/schema.sql [new file with mode: 0644]

index 0cc0c1c8c3436f0a20893f54c63065c371e15e80..4a387471810c165793bbbf53fe174859e0f8919c 100644 (file)
@@ -103,7 +103,7 @@ CREATE INDEX "IDX_APP_PROJ_PROJECT_UUID" ON "APP_PROJECTS"("PROJECT_UUID" NULLS
 
 CREATE TABLE "AUDITS"(
     "UUID" CHARACTER VARYING(40) NOT NULL,
-    "USER_UUID" CHARACTER VARYING(40) NOT NULL,
+    "USER_UUID" CHARACTER VARYING(255) NOT NULL,
     "USER_LOGIN" CHARACTER VARYING(255) NOT NULL,
     "CATEGORY" CHARACTER VARYING(25) NOT NULL,
     "OPERATION" CHARACTER VARYING(50) NOT NULL,
index b7a70528041dbf13f83d989239a941f9fb6a3174..c8aa50734a757e1560753d3bb64ffb6b9c665cf3 100644 (file)
@@ -45,9 +45,9 @@ public class AuditTesting {
 
   public static AuditDto newAuditDto(long createdAt, String operation) {
     AuditDto auditDto = new AuditDto();
-    auditDto.setUuid(randomAlphanumeric(20));
-    auditDto.setUserUuid(randomAlphanumeric(40));
-    auditDto.setUserLogin(randomAlphanumeric(40));
+    auditDto.setUuid(randomAlphanumeric(40));
+    auditDto.setUserUuid(randomAlphanumeric(255));
+    auditDto.setUserLogin(randomAlphanumeric(255));
     auditDto.setNewValue("{ \"someKey\": \"someValue\",  \"anotherKey\": \"\\\"anotherValue\\\" with quotes \\ \n\t\b\f\r\"}");
     auditDto.setOperation(operation);
     auditDto.setCategory("category");
index c4ea40e2ca9619667ba876706b726174d4a4dd2f..23bfb468d33f319bc5e58d00ceec71a3f53c488d 100644 (file)
@@ -27,6 +27,7 @@ public class DbVersion99 implements DbVersion {
   public void addSteps(MigrationStepRegistry registry) {
     registry
       .add(6800, "Add node_name column to ce_activity table", AddNodeNameColumnToCeActivityTable.class)
-      .add(6801, "Delete all analysis cache", DeleteAnalysisCache.class);
+      .add(6801, "Delete all analysis cache", DeleteAnalysisCache.class)
+      .add(6802, "Change user_uuid field size to 255 for audit table", UpdateUserUuidColumnSizeInAuditTable.class);
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable.java
new file mode 100644 (file)
index 0000000..6c892a1
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v99;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
+
+public class UpdateUserUuidColumnSizeInAuditTable extends DdlChange {
+
+  @VisibleForTesting
+  static final String TABLE_NAME = "audits";
+
+  @VisibleForTesting
+  static final String COLUMN_NAME = "user_uuid";
+
+  private static final VarcharColumnDef columnDefinition = newVarcharColumnDefBuilder()
+    .setColumnName(COLUMN_NAME)
+    .setIsNullable(false)
+    .setLimit(255)
+    .build();
+
+  public UpdateUserUuidColumnSizeInAuditTable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AlterColumnsBuilder(getDialect(), TABLE_NAME)
+      .updateColumn(columnDefinition)
+      .build());
+
+
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTableTest.java
new file mode 100644 (file)
index 0000000..9a27d39
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2023 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.v99;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.db.CoreDbTester;
+
+import static org.sonar.server.platform.db.migration.version.v99.UpdateUserUuidColumnSizeInAuditTable.COLUMN_NAME;
+import static org.sonar.server.platform.db.migration.version.v99.UpdateUserUuidColumnSizeInAuditTable.TABLE_NAME;
+
+
+public class UpdateUserUuidColumnSizeInAuditTableTest {
+  @Rule
+  public final CoreDbTester db = CoreDbTester.createForSchema(UpdateUserUuidColumnSizeInAuditTable.class, "schema.sql");
+
+  private final UpdateUserUuidColumnSizeInAuditTable underTest = new UpdateUserUuidColumnSizeInAuditTable(db.database());
+
+  @Test
+  public void migration_should_add_column() throws SQLException {
+    db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 40, false);
+    underTest.execute();
+    db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 255, false);
+  }
+
+  @Test
+  public void migration_should_be_reentrant() throws SQLException {
+    db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 40, false);
+    // re-entrant
+    underTest.execute();
+    underTest.execute();
+    db.assertColumnDefinition(TABLE_NAME, COLUMN_NAME, Types.VARCHAR, 255, false);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable/schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable/schema.sql
new file mode 100644 (file)
index 0000000..2331e71
--- /dev/null
@@ -0,0 +1,12 @@
+CREATE TABLE "AUDITS"(
+                         "UUID" CHARACTER VARYING(40) NOT NULL,
+                         "USER_UUID" CHARACTER VARYING(40) NOT NULL,
+                         "USER_LOGIN" CHARACTER VARYING(255) NOT NULL,
+                         "CATEGORY" CHARACTER VARYING(25) NOT NULL,
+                         "OPERATION" CHARACTER VARYING(50) NOT NULL,
+                         "NEW_VALUE" CHARACTER VARYING(4000),
+                         "CREATED_AT" BIGINT NOT NULL,
+                         "USER_TRIGGERED" BOOLEAN DEFAULT TRUE NOT NULL
+);
+ALTER TABLE "AUDITS" ADD CONSTRAINT "PK_AUDITS" PRIMARY KEY("UUID");
+CREATE INDEX "AUDITS_CREATED_AT" ON "AUDITS"("CREATED_AT" NULLS FIRST);