From 71987dfa5a6c563b3d8acf86592517a61ade3bc7 Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Mon, 29 May 2017 15:43:39 +0200 Subject: SONAR-9304 drop use of rules_profiles.is_default --- .../version/v65/CreateTableQProfiles.java | 87 ++++++++++++++++++++++ .../db/migration/version/v65/DbVersion65.java | 4 +- .../v65/DropIsDefaultColumnFromRulesProfiles.java | 73 ++++++++++++++++++ .../version/v65/CreateTableQProfilesTest.java | 67 +++++++++++++++++ .../db/migration/version/v65/DbVersion65Test.java | 2 +- .../DropIsDefaultColumnFromRulesProfilesTest.java | 43 +++++++++++ .../version/v65/CreateTableQProfilesTest/empty.sql | 0 .../initial.sql | 16 ++++ 8 files changed, 290 insertions(+), 2 deletions(-) create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfiles.java create mode 100644 server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfiles.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest.java create mode 100644 server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest.java create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest/empty.sql create mode 100644 server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest/initial.sql (limited to 'server/sonar-db-migration') diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfiles.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfiles.java new file mode 100644 index 00000000000..23d137c1142 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfiles.java @@ -0,0 +1,87 @@ +/* + * 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.sql.CreateTableBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static org.sonar.server.platform.db.migration.def.BigIntegerColumnDef.newBigIntegerColumnDefBuilder; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE; +import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder; + +public class CreateTableQProfiles extends DdlChange { + + private static final String TABLE_NAME = "qprofiles"; + + public CreateTableQProfiles(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + VarcharColumnDef organizationColumn = newVarcharColumnDefBuilder() + .setColumnName("organization_uuid") + .setLimit(UUID_SIZE) + .setIsNullable(false) + .setIgnoreOracleUnit(true) + .build(); + context.execute( + new CreateTableBuilder(getDialect(), TABLE_NAME) + .addPkColumn(newVarcharColumnDefBuilder() + .setColumnName("uuid") + .setLimit(UUID_SIZE) + .setIsNullable(false) + .setIgnoreOracleUnit(true) + .build()) + .addColumn(organizationColumn) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("rules_profile_uuid") + .setLimit(UUID_SIZE) + .setIsNullable(false) + .setIgnoreOracleUnit(true) + .build()) + .addColumn(newVarcharColumnDefBuilder() + .setColumnName("parent_uuid") + .setLimit(UUID_SIZE) + .setIsNullable(true) + .setIgnoreOracleUnit(true) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("created_at") + .setIsNullable(false) + .build()) + .addColumn(newBigIntegerColumnDefBuilder() + .setColumnName("updated_at") + .setIsNullable(false) + .build()) + .build()); + + context.execute( + new CreateIndexBuilder(getDialect()) + .setTable(TABLE_NAME) + .setName("qprofiles_org_uuid") + .addColumn(organizationColumn) + .build()); + } +} 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 4bad1e1ce30..138c1f77f8c 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 @@ -46,6 +46,8 @@ public class DbVersion65 implements DbVersion { .add(1717, "Make rules_profiles.is_built_in not null", MakeRulesProfilesIsBuiltInNotNullable.class) .add(1718, "Delete unused loaded_templates on quality profiles", DeleteLoadedTemplatesOnQProfiles.class) .add(1719, "Create table default_qprofiles", CreateTableDefaultQProfiles.class) - .add(1720, "Populate table default_qprofiles", PopulateTableDefaultQProfiles.class); + .add(1720, "Populate table default_qprofiles", PopulateTableDefaultQProfiles.class) + .add(1721, "Drop rules_profiles.is_default", DropIsDefaultColumnFromRulesProfiles.class) + .add(1722, "Create table qprofiles", CreateTableQProfiles.class); } } diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfiles.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfiles.java new file mode 100644 index 00000000000..d50668a5c24 --- /dev/null +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfiles.java @@ -0,0 +1,73 @@ +/* + * 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.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import org.sonar.db.Database; +import org.sonar.db.dialect.MsSql; +import org.sonar.server.platform.db.migration.sql.DropColumnsBuilder; +import org.sonar.server.platform.db.migration.step.DdlChange; + +import static java.lang.String.format; + +public class DropIsDefaultColumnFromRulesProfiles extends DdlChange { + + private static final String TABLE_NAME = "rules_profiles"; + private static final String COLUMN_NAME = "is_default"; + + public DropIsDefaultColumnFromRulesProfiles(Database db) { + super(db); + } + + @Override + public void execute(Context context) throws SQLException { + if (getDialect().getId().equals(MsSql.ID)) { + // this should be handled automatically by DropColumnsBuilder + dropMssqlConstraints(); + } + + context.execute(new DropColumnsBuilder(getDialect(), TABLE_NAME, COLUMN_NAME).build()); + } + + private void dropMssqlConstraints() throws SQLException { + try (Connection connection = getDatabase().getDataSource().getConnection(); + PreparedStatement pstmt = connection + .prepareStatement(format("SELECT d.name " + + "FROM sys.default_constraints d " + + "INNER JOIN sys.columns AS c ON d.parent_column_id = c.column_id " + + "WHERE OBJECT_NAME(d.parent_object_id)='%s' AND c.name='%s'", TABLE_NAME, COLUMN_NAME)); + ResultSet rs = pstmt.executeQuery()) { + while (rs.next()) { + String constraintName = rs.getString(1); + dropMssqlConstraint(connection, constraintName); + } + } + } + + private void dropMssqlConstraint(Connection connection, String constraintName) throws SQLException { + try (Statement stmt = connection.createStatement()) { + stmt.executeUpdate(format("ALTER TABLE %s DROP CONSTRAINT %s", TABLE_NAME, constraintName)); + } + } +} diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest.java new file mode 100644 index 00000000000..6ad59c3fdc5 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest.java @@ -0,0 +1,67 @@ +/* + * 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 org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.db.CoreDbTester; + +import static org.assertj.core.api.Assertions.assertThat; + + +public class CreateTableQProfilesTest { + + private static final String TABLE = "qprofiles"; + + @Rule + public final CoreDbTester db = CoreDbTester.createForSchema(CreateTableQProfilesTest.class, "empty.sql"); + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + private CreateTableQProfiles underTest = new CreateTableQProfiles(db.database()); + + @Test + public void creates_table_on_empty_db() throws SQLException { + underTest.execute(); + + assertThat(db.countRowsOfTable(TABLE)).isEqualTo(0); + + db.assertColumnDefinition(TABLE, "uuid", Types.VARCHAR, 40, false); + db.assertPrimaryKey(TABLE, "pk_qprofiles", "uuid"); + db.assertColumnDefinition(TABLE, "organization_uuid", Types.VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "rules_profile_uuid", Types.VARCHAR, 40, false); + db.assertColumnDefinition(TABLE, "created_at", Types.BIGINT, null, false); + db.assertColumnDefinition(TABLE, "updated_at", Types.BIGINT, null, false); + db.assertIndex(TABLE, "qprofiles_org_uuid", "organization_uuid"); + } + + @Test + public void migration_is_not_reentrant() throws SQLException { + underTest.execute(); + + expectedException.expect(IllegalStateException.class); + + underTest.execute(); + } + +} 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 aac62ae6530..c5921805052 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, 21); + verifyMigrationCount(underTest, 23); } } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest.java new file mode 100644 index 00000000000..36c427b56f5 --- /dev/null +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest.java @@ -0,0 +1,43 @@ +/* + * 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 DropIsDefaultColumnFromRulesProfilesTest { + + private static final String TABLE_NAME = "rules_profiles"; + + @Rule + public CoreDbTester db = CoreDbTester.createForSchema(DropIsDefaultColumnFromRulesProfilesTest.class, "initial.sql"); + + private DropIsDefaultColumnFromRulesProfiles underTest = new DropIsDefaultColumnFromRulesProfiles(db.database()); + + @Test + public void column_is_dropped() throws SQLException { + underTest.execute(); + + db.assertColumnDoesNotExist(TABLE_NAME, "is_default"); + } + +} diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest/empty.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/CreateTableQProfilesTest/empty.sql new file mode 100644 index 00000000000..e69de29bb2d diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest/initial.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest/initial.sql new file mode 100644 index 00000000000..2890a4409c8 --- /dev/null +++ b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v65/DropIsDefaultColumnFromRulesProfilesTest/initial.sql @@ -0,0 +1,16 @@ +CREATE TABLE "RULES_PROFILES" ( + "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1), + "NAME" VARCHAR(100) NOT NULL, + "LANGUAGE" VARCHAR(20), + "ORGANIZATION_UUID" VARCHAR(40) NOT NULL, + "KEE" VARCHAR(255) NOT NULL, + "PARENT_KEE" VARCHAR(255), + "RULES_UPDATED_AT" VARCHAR(100), + "IS_DEFAULT" BOOLEAN NOT NULL DEFAULT FALSE, + "CREATED_AT" TIMESTAMP, + "UPDATED_AT" TIMESTAMP, + "LAST_USED" BIGINT, + "USER_UPDATED_AT" BIGINT, + "IS_BUILT_IN" BOOLEAN NOT NULL +); +CREATE UNIQUE INDEX "UNIQ_QPROF_KEY" ON "RULES_PROFILES" ("KEE"); -- cgit v1.2.3