import org.sonar.server.platform.db.migration.version.v82.DbVersion82;
import org.sonar.server.platform.db.migration.version.v83.DbVersion83;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
public class MigrationConfigurationModule extends Module {
@Override
MigrationHistoryMeddler.class,
// Only needed for 8.3
- GetConstraintHelper.class,
+ SqlHelper.class,
DropPrimaryKeySqlGenerator.class);
}
}
public class DropIdFromComponentsTable extends DdlChange {
- static final String ORIGINAL_TABLE_NAME = "projects";
static final String TABLE_NAME = "components";
static final String COLUMN_NAME = "id";
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, COLUMN_NAME));
+ context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, COLUMN_NAME));
context.execute(new DropColumnsBuilder(getDialect(), TABLE_NAME, COLUMN_NAME).build());
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("active_rule_parameters", "active_rule_parameters", "id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("active_rule_parameters", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("ce_activity", "ce_activity","id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("ce_activity", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("ce_queue", "ce_queue","id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("ce_queue", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("duplications_index", "duplications_index", "id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("duplications_index", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("events", "events", "id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("events", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("issues", "issues","id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("issues", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("notifications", "notifications","id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("notifications", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("project_measures", "project_measures", "id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("project_measures", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("snapshots", "snapshots","id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("snapshots", "id"));
}
}
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate("user_tokens", "user_tokens", "id"));
+ context.execute(dropPrimaryKeySqlGenerator.generate("user_tokens", "id"));
}
}
import static java.lang.String.format;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
-import static java.util.Locale.ENGLISH;
-import static org.sonar.server.platform.db.migration.sql.CreateTableBuilder.PRIMARY_KEY_PREFIX;
public class DropPrimaryKeySqlGenerator {
private final Database db;
- private GetConstraintHelper getConstraintHelper;
+ private SqlHelper sqlHelper;
- public DropPrimaryKeySqlGenerator(Database db, GetConstraintHelper getConstraintHelper) {
+ public DropPrimaryKeySqlGenerator(Database db, SqlHelper sqlHelper) {
this.db = db;
- this.getConstraintHelper = getConstraintHelper;
+ this.sqlHelper = sqlHelper;
}
- public List<String> generate(String tableName, String originalTableName, String columnName) throws SQLException {
+ public List<String> generate(String tableName, String columnName) throws SQLException {
Dialect dialect = db.getDialect();
switch (dialect.getId()) {
case PostgreSql.ID:
- return generateForPostgresSql(tableName, originalTableName, columnName, getConstraintHelper.getPostgresSqlConstraint(tableName));
+ return generateForPostgresSql(tableName, columnName, sqlHelper.getPostgresSqlConstraint(tableName));
case MsSql.ID:
- return generateForMsSql(tableName, getConstraintHelper.getMssqlConstraint(tableName));
+ return generateForMsSql(tableName, sqlHelper.getMssqlConstraint(tableName));
case Oracle.ID:
- return generateForOracle(tableName, getConstraintHelper.getOracleConstraint(tableName));
+ return generateForOracle(tableName, sqlHelper.getOracleConstraint(tableName));
case H2.ID:
- return generateForH2(tableName, originalTableName, columnName);
+ return generateForH2(tableName, columnName, sqlHelper.getH2Constraint(tableName));
default:
throw new IllegalStateException(format("Unsupported database '%s'", dialect.getId()));
}
}
- private static List<String> generateForPostgresSql(String tableName, String originalTableName, String column, String constraintName) {
+ private List<String> generateForPostgresSql(String tableName, String column, String constraintName) throws SQLException {
+ String sequence = sqlHelper.getPostgresSqlSequence(tableName, column);
return asList(
format("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT", tableName, column),
- format("DROP SEQUENCE %s_%s_seq", originalTableName, column),
+ format("DROP SEQUENCE %s", sequence),
format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName));
}
return singletonList(format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName));
}
- private static List<String> generateForH2(String tableName, String originalTableName, String column) {
+ private static List<String> generateForH2(String tableName, String column, String constraintName) {
return asList(
- format("ALTER TABLE %s DROP CONSTRAINT %s%s", tableName, PRIMARY_KEY_PREFIX.toUpperCase(ENGLISH), originalTableName),
+ format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName),
format("ALTER TABLE %s ALTER COLUMN %s INTEGER NOT NULL", tableName, column));
}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2020 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.v83.util;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Locale;
-import org.sonar.db.Database;
-
-import static java.lang.String.format;
-
-public class GetConstraintHelper {
-
- private final Database db;
-
- public GetConstraintHelper(Database db) {
- this.db = db;
- }
-
- String getH2Constraint(String tableName) throws SQLException {
- try (Connection connection = db.getDataSource().getConnection();
- PreparedStatement pstmt = connection
- .prepareStatement(format("SELECT constraint_name "
- + "FROM information_schema.constraints "
- + "WHERE table_name = '%s' and constraint_type = 'PRIMARY KEY'", tableName.toUpperCase(Locale.ENGLISH)));
- ResultSet rs = pstmt.executeQuery()) {
- if (rs.next()) {
- return rs.getString(1);
- }
- throw contraintNotFoundException(tableName);
- }
- }
-
- String getPostgresSqlConstraint(String tableName) throws SQLException {
- try (Connection connection = db.getDataSource().getConnection();
- PreparedStatement pstmt = connection
- .prepareStatement(format("SELECT conname " +
- "FROM pg_constraint " +
- "WHERE conrelid = " +
- " (SELECT oid " +
- " FROM pg_class " +
- " WHERE relname LIKE '%s')", tableName));
- ResultSet rs = pstmt.executeQuery()) {
- if (rs.next()) {
- return rs.getString(1);
- }
- throw contraintNotFoundException(tableName);
- }
- }
-
- String getOracleConstraint(String tableName) throws SQLException {
- try (Connection connection = db.getDataSource().getConnection();
- PreparedStatement pstmt = connection
- .prepareStatement(format("SELECT constraint_name " +
- "FROM user_constraints " +
- "WHERE table_name = UPPER('%s') " +
- "AND constraint_type='P'", tableName));
- ResultSet rs = pstmt.executeQuery()) {
- if (rs.next()) {
- return rs.getString(1);
- }
- throw contraintNotFoundException(tableName);
- }
- }
-
- String getMssqlConstraint(String tableName) throws SQLException {
- try (Connection connection = db.getDataSource().getConnection();
- PreparedStatement pstmt = connection
- .prepareStatement(format("SELECT name " +
- "FROM sys.key_constraints " +
- "WHERE type = 'PK' " +
- "AND OBJECT_NAME(parent_object_id) = '%s'", tableName));
- ResultSet rs = pstmt.executeQuery()) {
- if (rs.next()) {
- return rs.getString(1);
- }
- throw contraintNotFoundException(tableName);
- }
- }
-
- private static IllegalStateException contraintNotFoundException(String tableName) {
- return new IllegalStateException(format("Cannot find constraint for table '%s'", tableName));
- }
-
-}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2020 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.v83.util;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Locale;
+import org.sonar.db.Database;
+
+import static java.lang.String.format;
+
+public class SqlHelper {
+
+ private final Database db;
+
+ public SqlHelper(Database db) {
+ this.db = db;
+ }
+
+ String getH2Constraint(String tableName) throws SQLException {
+ try (Connection connection = db.getDataSource().getConnection();
+ PreparedStatement pstmt = connection
+ .prepareStatement(format("SELECT constraint_name "
+ + "FROM information_schema.constraints "
+ + "WHERE table_name = '%s' and constraint_type = 'PRIMARY KEY'", tableName.toUpperCase(Locale.ENGLISH)));
+ ResultSet rs = pstmt.executeQuery()) {
+ if (rs.next()) {
+ return rs.getString(1);
+ }
+ throw contraintNotFoundException(tableName);
+ }
+ }
+
+ String getPostgresSqlConstraint(String tableName) throws SQLException {
+ try (Connection connection = db.getDataSource().getConnection();
+ PreparedStatement pstmt = connection
+ .prepareStatement(format("SELECT conname " +
+ "FROM pg_constraint " +
+ "WHERE conrelid = " +
+ " (SELECT oid " +
+ " FROM pg_class " +
+ " WHERE relname LIKE '%s')", tableName));
+ ResultSet rs = pstmt.executeQuery()) {
+ if (rs.next()) {
+ return rs.getString(1);
+ }
+ throw contraintNotFoundException(tableName);
+ }
+ }
+
+ String getPostgresSqlSequence(String tableName, String columnName) throws SQLException {
+ try (Connection connection = db.getDataSource().getConnection();
+ PreparedStatement pstmt = connection
+ .prepareStatement(format("SELECT pg_get_serial_sequence('%s', '%s')", tableName, columnName));
+ ResultSet rs = pstmt.executeQuery()) {
+ if (rs.next()) {
+ return rs.getString(1);
+ }
+ throw new IllegalStateException(format("Cannot find sequence for table '%s' on column '%s'", tableName, columnName));
+ }
+ }
+
+ String getOracleConstraint(String tableName) throws SQLException {
+ try (Connection connection = db.getDataSource().getConnection();
+ PreparedStatement pstmt = connection
+ .prepareStatement(format("SELECT constraint_name " +
+ "FROM user_constraints " +
+ "WHERE table_name = UPPER('%s') " +
+ "AND constraint_type='P'", tableName));
+ ResultSet rs = pstmt.executeQuery()) {
+ if (rs.next()) {
+ return rs.getString(1);
+ }
+ throw contraintNotFoundException(tableName);
+ }
+ }
+
+ String getMssqlConstraint(String tableName) throws SQLException {
+ try (Connection connection = db.getDataSource().getConnection();
+ PreparedStatement pstmt = connection
+ .prepareStatement(format("SELECT name " +
+ "FROM sys.key_constraints " +
+ "WHERE type = 'PK' " +
+ "AND OBJECT_NAME(parent_object_id) = '%s'", tableName));
+ ResultSet rs = pstmt.executeQuery()) {
+ if (rs.next()) {
+ return rs.getString(1);
+ }
+ throw contraintNotFoundException(tableName);
+ }
+ }
+
+ private static IllegalStateException contraintNotFoundException(String tableName) {
+ return new IllegalStateException(format("Cannot find constraint for table '%s'", tableName));
+ }
+
+}
import org.junit.rules.ExpectedException;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static java.sql.Types.INTEGER;
import static org.sonar.server.platform.db.migration.version.v83.DropIdFromComponentsTable.COLUMN_NAME;
@Rule
public ExpectedException expectedException = ExpectedException.none();
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(dbTester.database(), new GetConstraintHelper(dbTester.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(dbTester.database(), new SqlHelper(dbTester.database()));
private DropIdFromComponentsTable underTest = new DropIdFromComponentsTable(dbTester.database(), dropPrimaryKeySqlGenerator);
@Test
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfActiveRuleParametersTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfActiveRuleParametersTable(db.database(), dropPrimaryKeySqlGenerator);
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfCeActivityTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfCeActivityTable(db.database(), dropPrimaryKeySqlGenerator);
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfCeQueueTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfCeQueueTable(db.database(), dropPrimaryKeySqlGenerator);
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfDuplicationsIndexTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfDuplicationsIndexTable(db.database(), dropPrimaryKeySqlGenerator);
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfEventsTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private DropPrimaryKeyOnIdColumnOfEventsTable underTest = new DropPrimaryKeyOnIdColumnOfEventsTable(db.database(), dropPrimaryKeySqlGenerator);
import org.junit.Test;
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfIssuesTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private DropPrimaryKeyOnIdColumnOfIssuesTable underTest = new DropPrimaryKeyOnIdColumnOfIssuesTable(db.database(), dropPrimaryKeySqlGenerator);
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfNotificationTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfNotificationTable(db.database(), dropPrimaryKeySqlGenerator);
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.step.MigrationStep;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfProjectMeasuresTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private MigrationStep underTest = new DropPrimaryKeyOnIdColumnOfProjectMeasuresTable(db.database(), dropPrimaryKeySqlGenerator);
import org.sonar.db.CoreDbTester;
import org.sonar.server.platform.db.migration.version.v83.snapshots.issues.DropPrimaryKeyOnIdColumnOfSnapshotsTable;
import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySqlGenerator;
-import org.sonar.server.platform.db.migration.version.v83.util.GetConstraintHelper;
+import org.sonar.server.platform.db.migration.version.v83.util.SqlHelper;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
@Rule
public CoreDbTester db = CoreDbTester.createForSchema(DropPrimaryKeyOnIdColumnOfSnapshotsTableTest.class, "schema.sql");
- private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new GetConstraintHelper(db.database()));
+ private DropPrimaryKeySqlGenerator dropPrimaryKeySqlGenerator = new DropPrimaryKeySqlGenerator(db.database(), new SqlHelper(db.database()));
private DropPrimaryKeyOnIdColumnOfSnapshotsTable underTest = new DropPrimaryKeyOnIdColumnOfSnapshotsTable(db.database(), dropPrimaryKeySqlGenerator);
public class DropPrimaryKeySqlGeneratorTest {
private static final String TABLE_NAME = "issues";
- private static final String ORIGINAL_TABLE_NAME = "original_issues";
private static final String PK_COLUMN = "id";
private static final String CONSTRAINT = "pk_id";
private static final org.sonar.db.dialect.H2 H2 = new H2();
private Database db = mock(Database.class);
- private GetConstraintHelper getConstraintHelper = mock(GetConstraintHelper.class);
+ private SqlHelper sqlHelper = mock(SqlHelper.class);
- private DropPrimaryKeySqlGenerator underTest = new DropPrimaryKeySqlGenerator(db, getConstraintHelper);
+ private DropPrimaryKeySqlGenerator underTest = new DropPrimaryKeySqlGenerator(db, sqlHelper);
@Test
public void generate_for_postgres_sql() throws SQLException {
- when(getConstraintHelper.getPostgresSqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
+ when(sqlHelper.getPostgresSqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
+ when(sqlHelper.getPostgresSqlSequence(TABLE_NAME, "id")).thenReturn(TABLE_NAME + "_id_seq");
when(db.getDialect()).thenReturn(POSTGRESQL);
- List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
+ List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN);
assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
- "DROP SEQUENCE original_issues_id_seq",
+ "DROP SEQUENCE issues_id_seq",
"ALTER TABLE issues DROP CONSTRAINT pk_id");
}
@Test
public void generate_for_ms_sql() throws SQLException {
- when(getConstraintHelper.getMssqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
+ when(sqlHelper.getMssqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
when(db.getDialect()).thenReturn(MS_SQL);
- List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
+ List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN);
assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");
}
@Test
public void generate_for_oracle() throws SQLException {
- when(getConstraintHelper.getOracleConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
+ when(sqlHelper.getOracleConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
when(db.getDialect()).thenReturn(ORACLE);
- List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
+ List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN);
assertThat(sqls).containsExactly("DROP TRIGGER issues_IDT",
"DROP SEQUENCE issues_SEQ",
@Test
public void generate_for_h2() throws SQLException {
- when(getConstraintHelper.getH2Constraint(TABLE_NAME)).thenReturn(CONSTRAINT);
+ when(sqlHelper.getH2Constraint(TABLE_NAME)).thenReturn(CONSTRAINT);
when(db.getDialect()).thenReturn(H2);
- List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
+ List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN);
- assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT PK_original_issues",
+ assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id",
"ALTER TABLE issues ALTER COLUMN id INTEGER NOT NULL");
}
}