diff options
author | Léo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com> | 2023-01-16 16:23:42 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-01-16 20:03:43 +0000 |
commit | 33e2501f9cdb705ed85d3add8b439f87fc7c1476 (patch) | |
tree | 3c42e97f8a6c02992ceb2ec192c4ba0501f5f7f8 | |
parent | ba579343810dbb21d48a68a96cd9e66205df237a (diff) | |
download | sonarqube-33e2501f9cdb705ed85d3add8b439f87fc7c1476.tar.gz sonarqube-33e2501f9cdb705ed85d3add8b439f87fc7c1476.zip |
SONAR-18192 - Update size of user uuid column to 255 in audit table
6 files changed, 128 insertions, 5 deletions
diff --git a/server/sonar-db-dao/src/schema/schema-sq.ddl b/server/sonar-db-dao/src/schema/schema-sq.ddl index 0cc0c1c8c34..4a387471810 100644 --- a/server/sonar-db-dao/src/schema/schema-sq.ddl +++ b/server/sonar-db-dao/src/schema/schema-sq.ddl @@ -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, diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/audit/AuditTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/audit/AuditTesting.java index b7a70528041..c8aa50734a7 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/audit/AuditTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/audit/AuditTesting.java @@ -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"); diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java index c4ea40e2ca9..23bfb468d33 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/DbVersion99.java @@ -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 index 00000000000..6c892a14040 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable.java @@ -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 index 00000000000..9a27d3998ac --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTableTest.java @@ -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 index 00000000000..2331e713ece --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v99/UpdateUserUuidColumnSizeInAuditTable/schema.sql @@ -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); |