From 34a6e5a0ac025f92903da3bf321dcb14a1b160ec Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Wed, 31 May 2017 16:33:46 +0200 Subject: [PATCH] SONAR-9328 make MANUAL_MEASURE.COMPONENT_UUID not nullable --- .../org/sonar/db/version/schema-h2.ddl | 2 +- .../db/migration/version/v65/DbVersion65.java | 6 +- .../DropIndexManualMeasuresComponentUuid.java | 40 +++++++++++ ...anualMeasuresComponentUuidNotNullable.java | 46 +++++++++++++ ...reateIndexManualMeasuresComponentUuid.java | 49 ++++++++++++++ .../version/v65/DbVersion65Test.java | 2 +- ...pIndexManualMeasuresComponentUuidTest.java | 45 +++++++++++++ ...lMeasuresComponentUuidNotNullableTest.java | 66 +++++++++++++++++++ ...eIndexManualMeasuresComponentUuidTest.java | 44 +++++++++++++ .../manual_measures.sql | 12 ++++ .../manual_measures.sql | 11 ++++ .../manual_measures.sql | 11 ++++ 12 files changed, 331 insertions(+), 3 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuid.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullable.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuid.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest/manual_measures.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest/manual_measures.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest/manual_measures.sql diff --git a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl index 2e487af0391..47fb0608e38 100644 --- a/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl +++ b/server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl @@ -323,7 +323,7 @@ CREATE INDEX "PROJECTS_QUALIFIER" ON "PROJECTS" ("QUALIFIER"); CREATE TABLE "MANUAL_MEASURES" ( "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "METRIC_ID" INTEGER NOT NULL, - "COMPONENT_UUID" VARCHAR(50), + "COMPONENT_UUID" VARCHAR(50) NOT NULL, "VALUE" DOUBLE, "TEXT_VALUE" VARCHAR(4000), "USER_LOGIN" VARCHAR(255), diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java index b75a7d1ecee..601b8c10a56 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65.java @@ -37,6 +37,10 @@ public class DbVersion65 implements DbVersion { .add(1707, "Ensure ISSUE.PROJECT_UUID is consistent", EnsureIssueProjectUuidConsistencyOnIssues.class) .add(1708, "Clean orphans from PROJECT_LINKS", CleanOrphanRowsInProjectLinks.class) .add(1709, "Clean orphans from SETTINGS", CleanOrphanRowsInProperties.class) - .add(1710, "Clean orphans from MANUAL_MEASURES", CleanOrphanRowsInManualMeasures.class); + .add(1710, "Clean orphans from MANUAL_MEASURES", CleanOrphanRowsInManualMeasures.class) + .add(1711, "Drop index MANUAL_MEASURES.COMPONENT_UUID", DropIndexManualMeasuresComponentUuid.class) + .add(1712, "Make MANUAL_MEASURES.COMPONENT_UUID not nullable", MakeManualMeasuresComponentUuidNotNullable.class) + .add(1713, "Recreate index MANUAL_MEASURES.COMPONENT_UUID", RecreateIndexManualMeasuresComponentUuid.class) + ; } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuid.java new file mode 100644 index 00000000000..7fda3fdb232 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuid.java @@ -0,0 +1,40 @@ +/* + * 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.v65; + +import java.sql.SQLException; +import org.sonar.db.Database; +import org.sonar.server.platform.db.migration.sql.DropIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +public class DropIndexManualMeasuresComponentUuid extends DdlChange { + + public DropIndexManualMeasuresComponentUuid(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new DropIndexBuilder(getDialect()) + .setTable("manual_measures") + .setName("manual_measures_component_uuid") + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullable.java new file mode 100644 index 00000000000..243bd962f0f --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullable.java @@ -0,0 +1,46 @@ +/* + * 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.v65; + +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 MakeManualMeasuresComponentUuidNotNullable extends DdlChange { + + public MakeManualMeasuresComponentUuidNotNullable(Database db) { + super(db); + } + + @Override + public void execute(DdlChange.Context context) throws SQLException { + context.execute(new AlterColumnsBuilder(getDialect(), "manual_measures") + .updateColumn(newVarcharColumnDefBuilder() + .setColumnName("component_uuid") + .setLimit(VarcharColumnDef.UUID_VARCHAR_SIZE) + .setIsNullable(false) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuid.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuid.java new file mode 100644 index 00000000000..aedfd450299 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuid.java @@ -0,0 +1,49 @@ +/* + * 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.v65; + +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.CreateIndexBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class RecreateIndexManualMeasuresComponentUuid extends DdlChange { + + public RecreateIndexManualMeasuresComponentUuid(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + context.execute(new CreateIndexBuilder(getDialect()) + .setTable("manual_measures") + .setName("manual_measures_component_uuid") + .setUnique(false) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("component_uuid") + .setLimit(VarcharColumnDef.UUID_VARCHAR_SIZE) + .setIsNullable(false) + .build()) + .build()); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java index 1b5327a0b68..eafd9346c20 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DbVersion65Test.java @@ -35,6 +35,6 @@ public class DbVersion65Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 11); + verifyMigrationCount(underTest, 14); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest.java new file mode 100644 index 00000000000..c37a423c9ff --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest.java @@ -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.v65; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +public class DropIndexManualMeasuresComponentUuidTest { + private static final String TABLE_MANUAL_MEASURES = "manual_measures"; + private static final String INDEX_MANUAL_MEASURES_COMPONENT_UUID = "manual_measures_component_uuid"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropIndexManualMeasuresComponentUuidTest.class, "manual_measures.sql"); + + private DropIndexManualMeasuresComponentUuid underTest = new DropIndexManualMeasuresComponentUuid(db.database()); + + @Test + public void execute_drops_index_EVENTS_COMPONENT_UUID() throws SQLException { + db.assertIndex(TABLE_MANUAL_MEASURES, INDEX_MANUAL_MEASURES_COMPONENT_UUID, "component_uuid"); + + underTest.execute(); + + db.assertIndexDoesNotExist(TABLE_MANUAL_MEASURES, INDEX_MANUAL_MEASURES_COMPONENT_UUID); + } + +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest.java new file mode 100644 index 00000000000..c2028c9157a --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest.java @@ -0,0 +1,66 @@ +/* + * 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.v65; + +import java.sql.SQLException; +import java.sql.Types; +import java.util.Random; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; + +public class MakeManualMeasuresComponentUuidNotNullableTest { + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(MakeManualMeasuresComponentUuidNotNullableTest.class, "manual_measures.sql"); + + private final Random random = new Random(); + private MakeManualMeasuresComponentUuidNotNullable underTest = new MakeManualMeasuresComponentUuidNotNullable(db.database()); + + @Test + public void execute_makes_column_component_uuid_not_nullable_on_empty_table() throws SQLException { + underTest.execute(); + + verifyColumn(); + } + + @Test + public void execute_makes_column_component_uuid_not_nullable_on_populated_table() throws SQLException { + insertManualMeasure(); + insertManualMeasure(); + insertManualMeasure(); + + underTest.execute(); + + verifyColumn(); + } + + private void verifyColumn() { + db.assertColumnDefinition("manual_measures", "component_uuid", Types.VARCHAR, 50, false); + } + + private void insertManualMeasure() { + db.executeInsert( + "manual_measures", + "METRIC_ID", random.nextInt(), + "COMPONENT_UUID", randomAlphabetic(5)); + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest.java new file mode 100644 index 00000000000..d19998dccee --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest.java @@ -0,0 +1,44 @@ +/* + * 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.v65; + +import java.sql.SQLException; +import org.junit.Rule; +import org.junit.Test; +import org.sonar.db.CoreDbTester; + +public class RecreateIndexManualMeasuresComponentUuidTest { + private static final String TABLE_MANUAL_MEASURES = "manual_measures"; + private static final String INDEX_MANUAL_MEASURES_COMPONENT_UUID = "manual_measures_component_uuid"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(RecreateIndexManualMeasuresComponentUuidTest.class, "manual_measures.sql"); + + private RecreateIndexManualMeasuresComponentUuid underTest = new RecreateIndexManualMeasuresComponentUuid(db.database()); + + @Test + public void execute_adds_index_EVENTS_COMPONENT_UUID() throws SQLException { + db.assertIndexDoesNotExist(TABLE_MANUAL_MEASURES, INDEX_MANUAL_MEASURES_COMPONENT_UUID); + + underTest.execute(); + + db.assertIndex(TABLE_MANUAL_MEASURES, INDEX_MANUAL_MEASURES_COMPONENT_UUID, "component_uuid"); + } +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest/manual_measures.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest/manual_measures.sql new file mode 100644 index 00000000000..c9ca78e50d1 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIndexManualMeasuresComponentUuidTest/manual_measures.sql @@ -0,0 +1,12 @@ +CREATE TABLE "MANUAL_MEASURES" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "METRIC_ID" INTEGER NOT NULL, + "COMPONENT_UUID" VARCHAR(50), + "VALUE" DOUBLE, + "TEXT_VALUE" VARCHAR(4000), + "USER_LOGIN" VARCHAR(255), + "DESCRIPTION" VARCHAR(4000), + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT +); +CREATE INDEX "MANUAL_MEASURES_COMPONENT_UUID" ON "MANUAL_MEASURES" ("COMPONENT_UUID"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest/manual_measures.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest/manual_measures.sql new file mode 100644 index 00000000000..a719be57847 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/MakeManualMeasuresComponentUuidNotNullableTest/manual_measures.sql @@ -0,0 +1,11 @@ +CREATE TABLE "MANUAL_MEASURES" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "METRIC_ID" INTEGER NOT NULL, + "COMPONENT_UUID" VARCHAR(50), + "VALUE" DOUBLE, + "TEXT_VALUE" VARCHAR(4000), + "USER_LOGIN" VARCHAR(255), + "DESCRIPTION" VARCHAR(4000), + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT +); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest/manual_measures.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest/manual_measures.sql new file mode 100644 index 00000000000..a719be57847 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/RecreateIndexManualMeasuresComponentUuidTest/manual_measures.sql @@ -0,0 +1,11 @@ +CREATE TABLE "MANUAL_MEASURES" ( + "ID" BIGINT NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "METRIC_ID" INTEGER NOT NULL, + "COMPONENT_UUID" VARCHAR(50), + "VALUE" DOUBLE, + "TEXT_VALUE" VARCHAR(4000), + "USER_LOGIN" VARCHAR(255), + "DESCRIPTION" VARCHAR(4000), + "CREATED_AT" BIGINT, + "UPDATED_AT" BIGINT +); -- 2.39.5