]> source.dussan.org Git - sonarqube.git/commitdiff
Remove usage of original table name when dropping primary key
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 20 Apr 2020 09:45:55 +0000 (11:45 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 25 May 2020 20:05:19 +0000 (20:05 +0000)
26 files changed:
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/MigrationConfigurationModule.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/DropIdFromComponentsTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/activeruleparameters/DropPrimaryKeyOnIdColumnOfActiveRuleParametersTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/ceactivity/DropPrimaryKeyOnIdColumnOfCeActivityTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/cequeue/DropPrimaryKeyOnIdColumnOfCeQueueTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/duplicationsindex/DropPrimaryKeyOnIdColumnOfDuplicationsIndexTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/events/DropPrimaryKeyOnIdColumnOfEventsTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/issues/DropPrimaryKeyOnIdColumnOfIssuesTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/notifications/DropPrimaryKeyOnIdColumnOfNotificationTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/projectmeasures/DropPrimaryKeyOnIdColumnOfProjectMeasuresTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/snapshots/issues/DropPrimaryKeyOnIdColumnOfSnapshotsTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/usertokens/DropPrimaryKeyOnIdColumnOfUserTokensTable.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/DropPrimaryKeySqlGenerator.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/GetConstraintHelper.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/SqlHelper.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/DropIdFromComponentsTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/activeruleparameters/DropPrimaryKeyOnIdColumnOfActiveRuleParametersTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/ceactivity/DropPrimaryKeyOnIdColumnOfCeActivityTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/cequeue/DropPrimaryKeyOnIdColumnOfCeQueueTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/duplicationsindex/DropPrimaryKeyOnIdColumnOfDuplicationsIndexTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/events/DropPrimaryKeyOnIdColumnOfEventsTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/issues/DropPrimaryKeyOnIdColumnOfIssuesTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/notifications/DropPrimaryKeyOnIdColumnOfNotificationTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/projectmeasures/DropPrimaryKeyOnIdColumnOfProjectMeasuresTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/snapshots/DropPrimaryKeyOnIdColumnOfSnapshotsTableTest.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v83/util/DropPrimaryKeySqlGeneratorTest.java

index 8e9fe8143110274aee75218189eec8d53284d194..765b0d6c047c7ed6e745476a7c523e064a707511 100644 (file)
@@ -30,7 +30,7 @@ import org.sonar.server.platform.db.migration.version.v81.DbVersion81;
 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
@@ -52,7 +52,7 @@ public class MigrationConfigurationModule extends Module {
       MigrationHistoryMeddler.class,
 
       // Only needed for 8.3
-      GetConstraintHelper.class,
+      SqlHelper.class,
       DropPrimaryKeySqlGenerator.class);
   }
 }
index b5a1142d97f13ca50a72b4f5f39eec8d8a84c572..37509a845aa1bfb3d93f230d97870a4dfc184b25 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.server.platform.db.migration.version.v83.util.DropPrimaryKeySql
 
 public class DropIdFromComponentsTable extends DdlChange {
 
-  static final String ORIGINAL_TABLE_NAME = "projects";
   static final String TABLE_NAME = "components";
   static final String COLUMN_NAME = "id";
 
@@ -39,7 +38,7 @@ public class DropIdFromComponentsTable extends DdlChange {
   }
   @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());
   }
 }
index d8c0a307642b81c42da06eef75dbd43c2dda4632..4dd3c20d465caebc1045f0098da8daf6f2d8f3c9 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfActiveRuleParametersTable extends DdlChan
 
   @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"));
   }
 
 }
index 200219294eef6432a929a9cfe068cc822adf37dc..0cd480a2a04ba2a2b87f3a860ab00a7835032414 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfCeActivityTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("ce_activity", "ce_activity","id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("ce_activity", "id"));
   }
 
 }
index 4e2a02ce6a76b8ba5187c31b7704df0355a45f0c..955daec39363c8461a7dead3ba1568d5a38e2fd1 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfCeQueueTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("ce_queue", "ce_queue","id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("ce_queue", "id"));
   }
 
 }
index 1f6bdfe29114729952ecd54f799c8e32b8baffb4..bb941f85773fcb530429243c29e0718b59906869 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfDuplicationsIndexTable extends DdlChange
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("duplications_index", "duplications_index", "id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("duplications_index", "id"));
   }
 
 }
index 9e4228868fc6beedaf3c08117aff7ebc894f4c0a..ddcb55808abb8fc10302d9dd226b5535de44bdbc 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfEventsTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("events", "events", "id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("events", "id"));
   }
 
 }
index bfef73c5af9dff6deb7b736b500dec056fab3506..017c32b96ada83f8ef08a43dc3b4746418d2d53d 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfIssuesTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("issues", "issues","id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("issues", "id"));
   }
 
 }
index 3ff0231e7c154382004f2ae63714102b0f788c0f..63260acb91d22291721b585c5e1ebfd7a20c0ff0 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfNotificationTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("notifications", "notifications","id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("notifications", "id"));
   }
 
 }
index a64d19396292251ed4ce31e83b095e0d1760d30d..4135690e0a284accd2b4143ca87b99f181ec2ea8 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfProjectMeasuresTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("project_measures", "project_measures", "id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("project_measures",  "id"));
   }
 
 }
index a309537d5e881095ea0c54f9cbefee8e0186db79..a4384573eedab9e3bc8315d14aece583b52b0671 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfSnapshotsTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("snapshots", "snapshots","id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("snapshots", "id"));
   }
 
 }
index bc6c354d63ac264ccc30b2859e8248d1816ad377..df1b2443872f7b3b52129bf825f334299228e930 100644 (file)
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfUserTokensTable extends DdlChange {
 
   @Override
   public void execute(Context context) throws SQLException {
-    context.execute(dropPrimaryKeySqlGenerator.generate("user_tokens", "user_tokens", "id"));
+    context.execute(dropPrimaryKeySqlGenerator.generate("user_tokens", "id"));
   }
 
 }
index 35c2f51d2e5f698d0891b60d3e270648c4ee6bfd..55649d09b1f3a4e425189a2da7928274ad33a746 100644 (file)
@@ -32,39 +32,38 @@ import org.sonar.db.dialect.PostgreSql;
 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));
   }
 
@@ -79,9 +78,9 @@ public class DropPrimaryKeySqlGenerator {
     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));
   }
 
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/GetConstraintHelper.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/GetConstraintHelper.java
deleted file mode 100644 (file)
index b223e74..0000000
+++ /dev/null
@@ -1,104 +0,0 @@
-/*
- * 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));
-  }
-
-}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/SqlHelper.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v83/util/SqlHelper.java
new file mode 100644 (file)
index 0000000..88b75a7
--- /dev/null
@@ -0,0 +1,116 @@
+/*
+ * 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));
+  }
+
+}
index 96b729ae69fa5c97b976347d292fd74adfbb90ab..fa6119e41a50ef4b38561ae72d1276fbc07fbc47 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.Test;
 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;
@@ -37,7 +37,7 @@ public class DropIdFromComponentsTableTest {
   @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
index 7f0e3e2451507da671741add6ead2d0dd4943eae..fa8b978aad1c71e204cf02f0f0067309076a996f 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfActiveRuleParametersTableTest {
   @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);
 
index d268cca811c0e1560fa38a618cff8874cbae6c71..dee767ffbe118a2d9c894ddeef96d6ac67919a10 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfCeActivityTableTest {
   @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);
 
index d48c130cb9d1a4d5c50b30313e3b8f54adb51bac..525f83c6612d4bfb38f6176bbbd892cec138088a 100644 (file)
@@ -26,7 +26,7 @@ import org.junit.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;
 
@@ -36,7 +36,7 @@ public class DropPrimaryKeyOnIdColumnOfCeQueueTableTest {
   @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);
 
index 00869d195cf20acb5560276d58e53270ee4cee72..8bc11e1d181ec631f537dfa54caf2782f4f5b8ff 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfDuplicationsIndexTableTest {
   @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);
 
index d9700105c0c398df88f9396c0f96f8dfbeb9bb5c..2ad8922b8a67ac6d22bfe2ad4983947d4383c84c 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.Rule;
 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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfEventsTableTest {
   @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);
 
index 7acfaaef1ac3791d1ef3e15e981abc05ab08a7cb..ec52bab62d5a53dd0b3527ace78d654fe63ee789 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.Rule;
 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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfIssuesTableTest {
   @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);
 
index 9c80ca5bd747c6bea9a15a7810cbce91930214ed..ffef46d7d8ef41276aa7b74793075ece421782dd 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfNotificationTableTest {
   @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);
 
index 9beaac73199dc765d638f137b4785545e9c54c7d..f137f8a7dedb43ceff9039351533026265bbd7ef 100644 (file)
@@ -25,7 +25,7 @@ import org.junit.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;
 
@@ -35,7 +35,7 @@ public class DropPrimaryKeyOnIdColumnOfProjectMeasuresTableTest {
   @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);
 
index beebc70faeaf089e0bfcb108b46f8c85351746b9..de7014fef39cf25a41dfbb17ed37a88ae42ffe95 100644 (file)
@@ -26,7 +26,7 @@ import org.junit.Test;
 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;
 
@@ -36,7 +36,7 @@ public class DropPrimaryKeyOnIdColumnOfSnapshotsTableTest {
   @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);
 
index 4e5b3e7abf60efc40cb85c33b95ba8cbefb7ca90..d293ab8ba4626c95a3434063637c806bd45366a4 100644 (file)
@@ -35,7 +35,6 @@ import static org.mockito.Mockito.when;
 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";
 
@@ -45,38 +44,39 @@ public class DropPrimaryKeySqlGeneratorTest {
   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",
@@ -85,12 +85,12 @@ public class DropPrimaryKeySqlGeneratorTest {
 
   @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");
   }
 }