From: Simon Brandhof Date: Tue, 17 Oct 2017 07:55:00 +0000 (+0200) Subject: SONAR-10000 optimize DB migrations on resource_index X-Git-Tag: 6.7-RC1~170 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=259c985d4569cdd86900f74db1fc008f1f9a57f0;p=sonarqube.git SONAR-10000 optimize DB migrations on resource_index --- diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndex.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndex.java deleted file mode 100644 index 80bb7c2906e..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndex.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.v60; - -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.VarcharColumnDef.UUID_VARCHAR_SIZE; -import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class AddUuidColumnsToResourceIndex extends DdlChange { - - private static final String TABLE_RESOURCE_INDEX = "resource_index"; - - public AddUuidColumnsToResourceIndex(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - context.execute(new AddColumnsBuilder(getDialect(), TABLE_RESOURCE_INDEX) - .addColumn(newVarcharColumnDefBuilder().setColumnName("component_uuid").setLimit(UUID_VARCHAR_SIZE).setIgnoreOracleUnit(true).build()) - .addColumn(newVarcharColumnDefBuilder().setColumnName("root_component_uuid").setLimit(UUID_VARCHAR_SIZE).setIgnoreOracleUnit(true).build()) - .build()); - } - -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndex.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndex.java deleted file mode 100644 index fb0012a5cca..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndex.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import org.sonar.db.Database; -import org.sonar.server.platform.db.migration.step.MassUpdate; -import org.sonar.server.platform.db.migration.step.DataChange; - -public class CleanOrphanRowsInResourceIndex extends DataChange { - - public CleanOrphanRowsInResourceIndex(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - MassUpdate massUpdate = context.prepareMassUpdate(); - massUpdate.select("SELECT ri.id, ri.resource_id, ri.root_project_id from resource_index ri where ri.component_uuid is null or ri.root_component_uuid is null"); - massUpdate.update("DELETE from resource_index WHERE id=?"); - massUpdate.rowPluralName("resource index entries"); - massUpdate.execute((row, update) -> { - update.setLong(1, row.getLong(1)); - return true; - }); - } - -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60.java index adad21c69c2..8154e7d2713 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60.java @@ -36,12 +36,7 @@ public class DbVersion60 implements DbVersion { public void addSteps(MigrationStepRegistry registry) { registry .add(1200, "Create table PERM_TPL_CHARACTERISTICS", CreatePermTemplatesCharacteristics.class) - .add(1201, "Add columns RESOURCE_INDEX.*_UUID", AddUuidColumnsToResourceIndex.class) - .add(1202, "Populate columns RESOURCE_INDEX.*_UUID", PopulateUuidColumnsOfResourceIndex.class) - .add(1203, "Clean orphan rows in RESOURCE_INDEX", CleanOrphanRowsInResourceIndex.class) - .add(1204, "Make columns RESOURCE_INDEX.*_UUID not nullable", MakeUuidColumnsNotNullOnResourceIndex.class) - .add(1205, "Make column RESOURCE_INDEX.resource_index_rid", DropResourceIndexRidFromResourceIndex.class) - .add(1206, "Drop columns RESOURCE_INDEX.*_ID", DropIdColumnsFromResourceIndex.class) + .add(1205, "Drop index resource_index_rid from RESOURCE_INDEX", DropResourceIndexRidFromResourceIndex.class) .add(1207, "Drop unused columns on PROJECT_MEASURES", DropUnusedMeasuresColumns.class) .add(1208, "Add columns SNAPSHOTS.*COMPONENT_UUID", AddComponentUuidColumnsToSnapshots.class) .add(1209, "Populate column SNAPSHOTS.*COMPONENT_UUID", PopulateComponentUuidColumnsOfSnapshots.class) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndex.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndex.java deleted file mode 100644 index 11fa00a89d5..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndex.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import org.sonar.db.Database; -import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder; -import org.sonar.server.platform.db.migration.step.DdlChange; - -public class DropIdColumnsFromResourceIndex extends DdlChange { - - private static final String TABLE_RESOURCE_INDEX = "resource_index"; - - public DropIdColumnsFromResourceIndex(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - context.execute( - new DropColumnsBuilder( - getDialect(), TABLE_RESOURCE_INDEX, - "resource_id", "root_project_id") - .build()); - } - -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndex.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndex.java deleted file mode 100644 index faf7bb2b28d..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndex.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import org.sonar.db.Database; -import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder; -import org.sonar.server.platform.db.migration.sql.CreateIndexBuilder; -import org.sonar.server.platform.db.migration.def.VarcharColumnDef; -import org.sonar.server.platform.db.migration.step.DdlChange; - -import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_VARCHAR_SIZE; -import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; - -public class MakeUuidColumnsNotNullOnResourceIndex extends DdlChange { - - private static final String TABLE_RESOURCE_INDEX = "resource_index"; - - public MakeUuidColumnsNotNullOnResourceIndex(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - VarcharColumnDef componentUuid = newUuidColumn("component_uuid"); - context.execute(new AlterColumnsBuilder(getDialect(), TABLE_RESOURCE_INDEX) - .updateColumn(componentUuid) - .updateColumn(newUuidColumn("root_component_uuid")) - .build()); - - context.execute(new CreateIndexBuilder(getDialect()) - .setTable(TABLE_RESOURCE_INDEX) - .setName("resource_index_component") - .addColumn(componentUuid) - .build()); - } - - private static VarcharColumnDef newUuidColumn(String columnName) { - return newVarcharColumnDefBuilder() - .setColumnName(columnName) - .setLimit(UUID_VARCHAR_SIZE) - .setIsNullable(false) - .setIgnoreOracleUnit(true) - .build(); - } - -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndex.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndex.java deleted file mode 100644 index 4bf764b7cb5..00000000000 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndex.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import java.util.HashMap; -import java.util.Map; -import org.sonar.db.Database; -import org.sonar.server.platform.db.migration.step.MassUpdate; -import org.sonar.server.platform.db.migration.step.Select; -import org.sonar.server.platform.db.migration.step.SqlStatement; -import org.sonar.server.platform.db.migration.step.DataChange; - -public class PopulateUuidColumnsOfResourceIndex extends DataChange { - - public PopulateUuidColumnsOfResourceIndex(Database db) { - super(db); - } - - @Override - public void execute(Context context) throws SQLException { - Map componentUuidById = buildComponentUuidMap(context); - if (componentUuidById.isEmpty()) { - return; - } - - populateUuidColumns(context, componentUuidById); - } - - private static Map buildComponentUuidMap(Context context) throws SQLException { - Map componentUuidById = new HashMap<>(); - context.prepareSelect("select distinct p.id, p.uuid from projects p" + - " join resource_index ri1 on ri1.resource_id = p.id and ri1.component_uuid is null") - .scroll(row -> componentUuidById.put(row.getLong(1), row.getString(2))); - context.prepareSelect("select distinct p.id, p.uuid from projects p" + - " join resource_index ri2 on ri2.root_project_id = p.id and ri2.root_component_uuid is null") - .scroll(row -> componentUuidById.put(row.getLong(1), row.getString(2))); - return componentUuidById; - } - - private void populateUuidColumns(Context context, Map componentUuidById) throws SQLException { - MassUpdate massUpdate = context.prepareMassUpdate(); - massUpdate.select("SELECT ri.id, ri.resource_id, ri.root_project_id from resource_index ri where ri.component_uuid is null or ri.root_component_uuid is null"); - massUpdate.update("UPDATE resource_index SET component_uuid=?, root_component_uuid=? WHERE id=?"); - massUpdate.rowPluralName("resource index entries"); - massUpdate.execute((row, update) -> this.handle(componentUuidById, row, update)); - } - - public boolean handle(Map componentUuidById, Select.Row row, SqlStatement update) throws SQLException { - long id = row.getLong(1); - long componentId = row.getLong(2); - long rootProjectId = row.getLong(3); - - String componentUuid = componentUuidById.get(componentId); - String rootComponentUuid = componentUuidById.get(rootProjectId); - - if (componentUuid == null && rootComponentUuid == null) { - return false; - } - - update.setString(1, componentUuid); - update.setString(2, rootComponentUuid); - update.setLong(3, id); - - return true; - } - -} diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DropTableResourceIndex.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DropTableResourceIndex.java index 9e5943e71f7..12a5ea5bc8c 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DropTableResourceIndex.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v63/DropTableResourceIndex.java @@ -40,10 +40,19 @@ public class DropTableResourceIndex extends DdlChange { .setName("resource_index_key") .build()); - context.execute(new DropIndexBuilder(getDialect()) - .setTable(TABLE_RESOURCE_INDEX) - .setName("resource_index_component") - .build()); + try { + context.execute(new DropIndexBuilder(getDialect()) + .setTable(TABLE_RESOURCE_INDEX) + .setName("resource_index_component") + .build()); + } catch (Exception e) { + // migrating from 5.6. The migration 1204 MakeUuidColumnsNotNullOnResourceIndex, + // introduced in 6.0, has been dropped in 6.7 for performance reasons. There was no need to + // alter the table resource_index while it's dropped later in 6.3. + // As a consequence this index may not exist when upgrading from 6.1+. + // Note that the "delete index if exists" is still not supported by MySQL, Oracle and MSSQL < 2016, + // that's why an exception is raised if the index does not exist. + } context.execute(new DropTableBuilder(getDialect(), TABLE_RESOURCE_INDEX).build()); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndexTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndexTest.java deleted file mode 100644 index f295c2c087d..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndexTest.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import java.sql.Types; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.db.CoreDbTester; - -import static java.lang.String.valueOf; - -public class AddUuidColumnsToResourceIndexTest { - - @Rule - public CoreDbTester db = CoreDbTester.createForSchema(AddUuidColumnsToResourceIndexTest.class, "old_resourceindex.sql"); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private AddUuidColumnsToResourceIndex underTest = new AddUuidColumnsToResourceIndex(db.database()); - - @Test - public void migration_adds_columns_to_empty_table() throws SQLException { - underTest.execute(); - - verifyAddedColumns(); - } - - @Test - public void migration_adds_columns_to_populated_table() throws SQLException { - for (int i = 0; i < 9; i++) { - db.executeInsert( - "resource_index", - "KEE", "key_" + i, - "POSITION", valueOf(i), - "NAME_SIZE", valueOf(i + 1), - "RESOURCE_ID", valueOf(i + 10), - "ROOT_PROJECT_ID", valueOf(i + 20), - "QUALIFIER", (i % 2 == 0 ? "FILE" : "PROJECT")); - } - - underTest.execute(); - - verifyAddedColumns(); - } - - private void verifyAddedColumns() { - db.assertColumnDefinition("resource_index", "component_uuid", Types.VARCHAR, 50, true); - db.assertColumnDefinition("resource_index", "root_component_uuid", Types.VARCHAR, 50, true); - } - - @Test - public void migration_is_not_reentrant() throws SQLException { - underTest.execute(); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Fail to execute "); - underTest.execute(); - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndexTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndexTest.java deleted file mode 100644 index deb0c92097d..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndexTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import java.util.List; -import java.util.stream.Collectors; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.db.CoreDbTester; - -import static java.lang.String.valueOf; -import static org.assertj.core.api.Assertions.assertThat; - -public class CleanOrphanRowsInResourceIndexTest { - - @Rule - public CoreDbTester db = CoreDbTester.createForSchema(CleanOrphanRowsInResourceIndexTest.class, - "in_progress_resourceindex.sql"); - - private CleanOrphanRowsInResourceIndex underTest = new CleanOrphanRowsInResourceIndex(db.database()); - - @Test - public void migration_has_no_effect_on_empty_table() throws SQLException { - underTest.execute(); - - assertThat(db.countRowsOfTable("resource_index")).isEqualTo(0); - } - - @Test - public void migration_deletes_any_row_with_a_null_uuid() throws SQLException { - insertResourceIndex(1, true, true); - insertResourceIndex(2, false, false); - insertResourceIndex(3, true, false); - insertResourceIndex(4, false, true); - insertResourceIndex(5, true, true); - - underTest.execute(); - - assertThat(idsOfRowsInResourceIndex()).containsOnly(1l, 5l); - } - - @Test - public void migration_is_reentrant() throws SQLException { - insertResourceIndex(1, true, true); - insertResourceIndex(2, false, false); - - underTest.execute(); - - assertThat(idsOfRowsInResourceIndex()).containsOnly(1l); - - underTest.execute(); - - assertThat(idsOfRowsInResourceIndex()).containsOnly(1l); - } - - private List idsOfRowsInResourceIndex() { - return db.select("select ID from resource_index").stream().map(map -> (Long) map.get("ID")).collect(Collectors.toList()); - } - - private void insertResourceIndex(long id, boolean hasComponentUiid, boolean hasRootComponentUuid) { - db.executeInsert( - "resource_index", - "ID", valueOf(id), - "KEE", "key_" + id, - "POSITION", valueOf(id + 100), - "NAME_SIZE", valueOf(id + 1000), - "RESOURCE_ID", valueOf(id + 300), - "ROOT_PROJECT_ID", valueOf(id + 4000), - "QUALIFIER", "PROJECT"); - - if (hasComponentUiid) { - db.executeUpdateSql("update resource_index set COMPONENT_UUID=? where id=?", "uuid_" + id, valueOf(id)); - } - if (hasRootComponentUuid) { - db.executeUpdateSql("update resource_index set ROOT_COMPONENT_UUID=? where id=?", "root_uuid_" + id, valueOf(id)); - } - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60Test.java index 52616348249..be8381cc3af 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60Test.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DbVersion60Test.java @@ -42,7 +42,7 @@ public class DbVersion60Test { @Test public void verify_migration_count() { - verifyMigrationCount(underTest, 76); + verifyMigrationCount(underTest, 71); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndexTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndexTest.java deleted file mode 100644 index cb6e9c0ecd3..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndexTest.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import org.junit.Test; -import org.sonar.db.Database; -import org.sonar.db.dialect.PostgreSql; -import org.sonar.server.platform.db.migration.step.DdlChange; - -import static java.util.Collections.singletonList; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -public class DropIdColumnsFromResourceIndexTest { - - private Database database = mock(Database.class); - - private DropIdColumnsFromResourceIndex underTest = new DropIdColumnsFromResourceIndex(database); - - @Test - public void verify_generated_sql_on_postgresql() throws SQLException { - when(database.getDialect()).thenReturn(new PostgreSql()); - - DdlChange.Context context = mock(DdlChange.Context.class); - underTest.execute(context); - - verify(context).execute( - singletonList("ALTER TABLE resource_index DROP COLUMN resource_id, DROP COLUMN root_project_id")); - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest.java deleted file mode 100644 index d6e8d719410..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest.java +++ /dev/null @@ -1,97 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import java.sql.Types; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; -import org.sonar.db.CoreDbTester; - -import static java.lang.String.valueOf; - -public class MakeUuidColumnsNotNullOnResourceIndexTest { - - @Rule - public CoreDbTester db = CoreDbTester.createForSchema(MakeUuidColumnsNotNullOnResourceIndexTest.class, - "in_progress_resourceindex.sql"); - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - private MakeUuidColumnsNotNullOnResourceIndex underTest = new MakeUuidColumnsNotNullOnResourceIndex(db.database()); - - @Test - public void migration_sets_uuid_columns_not_nullable_on_empty_table() throws SQLException { - underTest.execute(); - - verifyColumnDefinitions(); - verifyIndex(); - } - - @Test - public void migration_sets_uuid_columns_not_nullable_on_populated_table() throws SQLException { - insertResourceIndex(1, true, true); - insertResourceIndex(2, true, true); - - underTest.execute(); - - verifyColumnDefinitions(); - verifyIndex(); - } - - @Test - public void migration_fails_if_some_uuid_columns_are_null() throws SQLException { - insertResourceIndex(1, false, true); - - expectedException.expect(IllegalStateException.class); - expectedException.expectMessage("Fail to execute"); - - underTest.execute(); - } - - private void verifyColumnDefinitions() { - db.assertColumnDefinition("resource_index", "component_uuid", Types.VARCHAR, 50, false); - db.assertColumnDefinition("resource_index", "root_component_uuid", Types.VARCHAR, 50, false); - } - - private void verifyIndex() { - db.assertIndex("resource_index", "resource_index_component", "component_uuid"); - } - - private void insertResourceIndex(long id, boolean hasComponentUiid, boolean hasRootComponentUuid) { - db.executeInsert( - "resource_index", - "ID", valueOf(id), - "KEE", "key_" + id, - "POSITION", valueOf(id + 100), - "NAME_SIZE", valueOf(id + 1000), - "RESOURCE_ID", valueOf(id + 300), - "ROOT_PROJECT_ID", valueOf(id + 4000), - "QUALIFIER", "PROJECT"); - - if (hasComponentUiid) { - db.executeUpdateSql("update resource_index set COMPONENT_UUID=? where id=?", "uuid_" + id, valueOf(id)); - } - if (hasRootComponentUuid) { - db.executeUpdateSql("update resource_index set ROOT_COMPONENT_UUID=? where id=?", "root_uuid_" + id, valueOf(id)); - } - } -} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndexTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndexTest.java deleted file mode 100644 index 2605e582c45..00000000000 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndexTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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.v60; - -import java.sql.SQLException; -import java.util.List; -import java.util.Map; -import javax.annotation.Nullable; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.db.CoreDbTester; - -import static java.lang.String.valueOf; -import static org.assertj.core.api.Assertions.assertThat; - -public class PopulateUuidColumnsOfResourceIndexTest { - - @Rule - public CoreDbTester db = CoreDbTester.createForSchema(PopulateUuidColumnsOfResourceIndexTest.class, - "in_progress_resourceindex_with_projects.sql"); - - private PopulateUuidColumnsOfResourceIndex underTest = new PopulateUuidColumnsOfResourceIndex(db.database()); - - @Test - public void migration_has_no_effect_on_empty_tables() throws SQLException { - underTest.execute(); - - assertThat(db.countRowsOfTable("resource_index")).isEqualTo(0); - assertThat(db.countRowsOfTable("projects")).isEqualTo(0); - } - - @Test - public void migration_updates_uuid_columns_with_values_from_table_projects_when_they_exist() throws SQLException { - String uuid1 = insertComponent(40); - String uuid2 = insertComponent(50); - String uuid3 = insertComponent(60); - String uuid4 = insertComponent(70); - - insertResourceIndex(1, 40, 50); - insertResourceIndex(2, 60, 70); - insertResourceIndex(3, 90, 70); // 90 does not exist - insertResourceIndex(4, 40, 100); // 100 does not exist - insertResourceIndex(5, 110, 100); // 110 and 100 do not exist - - underTest.execute(); - - verifyResourceIndex(1, 40, uuid1, 50, uuid2); - verifyResourceIndex(2, 60, uuid3, 70, uuid4); - verifyResourceIndex(3, 90, null, 70, uuid4); - verifyResourceIndex(4, 40, uuid1, 100, null); - verifyResourceIndex(5, 110, null, 100, null); - } - - @Test - public void migration_is_reentrant() throws SQLException { - String uuid1 = insertComponent(40); - String uuid2 = insertComponent(50); - insertResourceIndex(1, 40, 50); - - underTest.execute(); - verifyResourceIndex(1, 40, uuid1, 50, uuid2); - - underTest.execute(); - verifyResourceIndex(1, 40, uuid1, 50, uuid2); - - } - - private void verifyResourceIndex(long id, long resourceId, @Nullable String componentUuid, long rootProjectId, @Nullable String rootComponentUuid) { - List> rows = db.select("select RESOURCE_ID, COMPONENT_UUID, ROOT_PROJECT_ID, ROOT_COMPONENT_UUID from resource_index where ID=" + id); - assertThat(rows).hasSize(1); - Map row = rows.get(0); - assertThat(row.get("RESOURCE_ID")).isEqualTo(resourceId); - assertThat(row.get("COMPONENT_UUID")).isEqualTo(componentUuid); - assertThat(row.get("ROOT_PROJECT_ID")).isEqualTo(rootProjectId); - assertThat(row.get("ROOT_COMPONENT_UUID")).isEqualTo(rootComponentUuid); - } - - private String insertComponent(long id) { - String uuid = "uuid_" + id; - db.executeInsert( - "projects", - "ID", valueOf(id), - "UUID", uuid); - return uuid; - } - - private void insertResourceIndex(long id, long resourceId, long rootProjectId) { - db.executeInsert( - "resource_index", - "ID", valueOf(id), - "KEE", "key_" + id, - "POSITION", valueOf(id + 100), - "NAME_SIZE", valueOf(id + 1000), - "RESOURCE_ID", valueOf(resourceId), - "ROOT_PROJECT_ID", valueOf(rootProjectId), - "QUALIFIER", "PROJECT"); - } -} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndexTest/old_resourceindex.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndexTest/old_resourceindex.sql deleted file mode 100644 index 6037299d5ac..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/AddUuidColumnsToResourceIndexTest/old_resourceindex.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE "RESOURCE_INDEX" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400) NOT NULL, - "POSITION" INTEGER NOT NULL, - "NAME_SIZE" INTEGER NOT NULL, - "RESOURCE_ID" INTEGER NOT NULL, - "ROOT_PROJECT_ID" INTEGER NOT NULL, - "QUALIFIER" VARCHAR(10) NOT NULL -); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndexTest/in_progress_resourceindex.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndexTest/in_progress_resourceindex.sql deleted file mode 100644 index aab2040b1ed..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanOrphanRowsInResourceIndexTest/in_progress_resourceindex.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE "RESOURCE_INDEX" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400) NOT NULL, - "POSITION" INTEGER NOT NULL, - "NAME_SIZE" INTEGER NOT NULL, - "RESOURCE_ID" INTEGER NOT NULL, - "ROOT_PROJECT_ID" INTEGER NOT NULL, - "QUALIFIER" VARCHAR(10) NOT NULL, - "COMPONENT_UUID" VARCHAR(50), - "ROOT_COMPONENT_UUID" VARCHAR(50) -); - diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanUsurperRootComponentsTest/complete_schema.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanUsurperRootComponentsTest/complete_schema.sql index 299da292538..3fabd832864 100644 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanUsurperRootComponentsTest/complete_schema.sql +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/CleanUsurperRootComponentsTest/complete_schema.sql @@ -336,16 +336,6 @@ CREATE TABLE "LOADED_TEMPLATES" ( "TEMPLATE_TYPE" VARCHAR(15) ); -CREATE TABLE "RESOURCE_INDEX" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400) NOT NULL, - "POSITION" INTEGER NOT NULL, - "NAME_SIZE" INTEGER NOT NULL, - "COMPONENT_UUID" VARCHAR(50) NOT NULL, - "ROOT_COMPONENT_UUID" VARCHAR(50) NOT NULL, - "QUALIFIER" VARCHAR(10) NOT NULL -); - CREATE TABLE "AUTHORS" ( "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), "PERSON_ID" INTEGER, @@ -603,10 +593,6 @@ CREATE INDEX "PROJECTS_MODULE_UUID" ON "PROJECTS" ("MODULE_UUID"); CREATE INDEX "PROJECTS_QUALIFIER" ON "PROJECTS" ("QUALIFIER"); -CREATE INDEX "RESOURCE_INDEX_KEY" ON "RESOURCE_INDEX" ("KEE"); - -CREATE INDEX "RESOURCE_INDEX_COMPONENT" ON "RESOURCE_INDEX" ("COMPONENT_UUID"); - CREATE UNIQUE INDEX "UNIQ_AUTHOR_LOGINS" ON "AUTHORS" ("LOGIN"); CREATE INDEX "MEASURE_FILTERS_NAME" ON "MEASURE_FILTERS" ("NAME"); diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndexTest/in_progress_resourceindex.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndexTest/in_progress_resourceindex.sql deleted file mode 100644 index aab2040b1ed..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/DropIdColumnsFromResourceIndexTest/in_progress_resourceindex.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE "RESOURCE_INDEX" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400) NOT NULL, - "POSITION" INTEGER NOT NULL, - "NAME_SIZE" INTEGER NOT NULL, - "RESOURCE_ID" INTEGER NOT NULL, - "ROOT_PROJECT_ID" INTEGER NOT NULL, - "QUALIFIER" VARCHAR(10) NOT NULL, - "COMPONENT_UUID" VARCHAR(50), - "ROOT_COMPONENT_UUID" VARCHAR(50) -); - diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest/in_progress_resourceindex.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest/in_progress_resourceindex.sql deleted file mode 100644 index aab2040b1ed..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/MakeUuidColumnsNotNullOnResourceIndexTest/in_progress_resourceindex.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE "RESOURCE_INDEX" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400) NOT NULL, - "POSITION" INTEGER NOT NULL, - "NAME_SIZE" INTEGER NOT NULL, - "RESOURCE_ID" INTEGER NOT NULL, - "ROOT_PROJECT_ID" INTEGER NOT NULL, - "QUALIFIER" VARCHAR(10) NOT NULL, - "COMPONENT_UUID" VARCHAR(50), - "ROOT_COMPONENT_UUID" VARCHAR(50) -); - diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndexTest/in_progress_resourceindex_with_projects.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndexTest/in_progress_resourceindex_with_projects.sql deleted file mode 100644 index 2551ca942d0..00000000000 --- a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v60/PopulateUuidColumnsOfResourceIndexTest/in_progress_resourceindex_with_projects.sql +++ /dev/null @@ -1,34 +0,0 @@ -CREATE TABLE "RESOURCE_INDEX" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400) NOT NULL, - "POSITION" INTEGER NOT NULL, - "NAME_SIZE" INTEGER NOT NULL, - "RESOURCE_ID" INTEGER NOT NULL, - "ROOT_PROJECT_ID" INTEGER NOT NULL, - "QUALIFIER" VARCHAR(10) NOT NULL, - "COMPONENT_UUID" VARCHAR(50), - "ROOT_COMPONENT_UUID" VARCHAR(50) -); - -CREATE TABLE "PROJECTS" ( - "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), - "KEE" VARCHAR(400), - "ROOT_ID" INTEGER, - "UUID" VARCHAR(50), - "PROJECT_UUID" VARCHAR(50), - "MODULE_UUID" VARCHAR(50), - "MODULE_UUID_PATH" VARCHAR(4000), - "NAME" VARCHAR(2000), - "DESCRIPTION" VARCHAR(2000), - "ENABLED" BOOLEAN NOT NULL DEFAULT TRUE, - "SCOPE" VARCHAR(3), - "QUALIFIER" VARCHAR(10), - "DEPRECATED_KEE" VARCHAR(400), - "PATH" VARCHAR(2000), - "LANGUAGE" VARCHAR(20), - "COPY_RESOURCE_ID" INTEGER, - "LONG_NAME" VARCHAR(2000), - "PERSON_ID" INTEGER, - "CREATED_AT" TIMESTAMP, - "AUTHORIZATION_UPDATED_AT" BIGINT -);