]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-14792 Move MsSQL helper out specific DB migration version
authorJacek <jacek.poreda@sonarsource.com>
Mon, 14 Jun 2021 09:33:05 +0000 (11:33 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 16 Jun 2021 20:03:04 +0000 (20:03 +0000)
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropMsSQLDefaultConstraintsBuilder.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropUnusedPeriodsInSnapshots.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/DropUnusedVariationsInProjectMeasures.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/util/DropMsSQLDefaultConstraintsBuilder.java [deleted file]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/util/package-info.java [deleted file]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/DropMsSQLDefaultConstraintsBuilderTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/util/DropMsSQLDefaultConstraintsBuilderTest.java [deleted file]

diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropMsSQLDefaultConstraintsBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/sql/DropMsSQLDefaultConstraintsBuilder.java
new file mode 100644 (file)
index 0000000..7b6d3e6
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sql;
+
+import com.google.common.base.Preconditions;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.stream.Collectors;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MsSql;
+
+import static java.lang.String.format;
+
+public class DropMsSQLDefaultConstraintsBuilder {
+  private final Database db;
+  private String tableName;
+  private String[] columns;
+
+  public DropMsSQLDefaultConstraintsBuilder(Database db) {
+    this.db = db;
+  }
+
+  public DropMsSQLDefaultConstraintsBuilder setTable(String s) {
+    this.tableName = s;
+    return this;
+  }
+
+  public DropMsSQLDefaultConstraintsBuilder setColumns(String... columns) {
+    this.columns = columns;
+    return this;
+  }
+
+  public List<String> build() throws SQLException {
+    Preconditions.checkArgument(columns.length > 0, "At least one column expected.");
+    Preconditions.checkArgument(MsSql.ID.equals(db.getDialect().getId()), "Expected MsSql dialect was: " + db.getDialect().getId());
+    return getMsSqlDropDefaultConstraintQueries();
+  }
+
+  private List<String> getMsSqlDropDefaultConstraintQueries() throws SQLException {
+    List<String> dropQueries = new LinkedList<>();
+    if (MsSql.ID.equals(db.getDialect().getId())) {
+      List<String> defaultConstraints = getMssqlDefaultConstraints();
+      for (String defaultConstraintName : defaultConstraints) {
+        dropQueries.add("ALTER TABLE " + tableName + " DROP CONSTRAINT " + defaultConstraintName);
+      }
+    }
+    return dropQueries;
+  }
+
+  private List<String> getMssqlDefaultConstraints() throws SQLException {
+    List<String> defaultConstrainNames = new LinkedList<>();
+    String commaSeparatedListOfColumns = Arrays.stream(columns).map(s -> "'" + s + "'")
+      .collect(Collectors.joining(","));
+    try (Connection connection = db.getDataSource().getConnection();
+      PreparedStatement pstmt = connection
+        .prepareStatement(format("SELECT d.name FROM sys.tables t "
+          + "JOIN sys.default_constraints d ON d.parent_object_id = t.object_id "
+          + "JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id = d.parent_column_id  "
+          + "WHERE  t.name = '%s' AND c.name in (%s)", tableName, commaSeparatedListOfColumns));
+      ResultSet rs = pstmt.executeQuery()) {
+      while (rs.next()) {
+        defaultConstrainNames.add(rs.getString(1));
+      }
+    }
+    return defaultConstrainNames;
+  }
+
+}
index 92766d6a684eaa788a4176703d9543260edae25c..1f6d780374c821d101dff3a2d25cefe627e0b16f 100644 (file)
@@ -24,7 +24,7 @@ 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 org.sonar.server.platform.db.migration.version.v85.util.DropMsSQLDefaultConstraintsBuilder;
+import org.sonar.server.platform.db.migration.sql.DropMsSQLDefaultConstraintsBuilder;
 
 public class DropUnusedPeriodsInSnapshots extends DdlChange {
   private static final String TABLE_NAME = "snapshots";
index 03074f729e66a47be8ec1b8caa0f47a5ff7faf50..1c42f6f47f18d8dcb585424c04dd085fba2bdb31 100644 (file)
@@ -24,7 +24,7 @@ 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 org.sonar.server.platform.db.migration.version.v85.util.DropMsSQLDefaultConstraintsBuilder;
+import org.sonar.server.platform.db.migration.sql.DropMsSQLDefaultConstraintsBuilder;
 
 public class DropUnusedVariationsInProjectMeasures extends DdlChange {
   private static final String TABLE_NAME = "project_measures";
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/util/DropMsSQLDefaultConstraintsBuilder.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/util/DropMsSQLDefaultConstraintsBuilder.java
deleted file mode 100644 (file)
index f24409a..0000000
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2021 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.v85.util;
-
-import com.google.common.base.Preconditions;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.util.Arrays;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.stream.Collectors;
-import org.sonar.db.Database;
-import org.sonar.db.dialect.MsSql;
-
-import static java.lang.String.format;
-
-public class DropMsSQLDefaultConstraintsBuilder {
-  private final Database db;
-  private String tableName;
-  private String[] columns;
-
-  public DropMsSQLDefaultConstraintsBuilder(Database db) {
-    this.db = db;
-  }
-
-  public DropMsSQLDefaultConstraintsBuilder setTable(String s) {
-    this.tableName = s;
-    return this;
-  }
-
-  public DropMsSQLDefaultConstraintsBuilder setColumns(String... columns) {
-    this.columns = columns;
-    return this;
-  }
-
-  public List<String> build() throws SQLException {
-    Preconditions.checkArgument(columns.length > 0, "At least one column expected.");
-    Preconditions.checkArgument(MsSql.ID.equals(db.getDialect().getId()), "Expected MsSql dialect was: " + db.getDialect().getId());
-    return getMsSqlDropDefaultConstraintQueries();
-  }
-
-  private List<String> getMsSqlDropDefaultConstraintQueries() throws SQLException {
-    List<String> dropQueries = new LinkedList<>();
-    if (MsSql.ID.equals(db.getDialect().getId())) {
-      List<String> defaultConstraints = getMssqlDefaultConstraints();
-      for (String defaultConstraintName : defaultConstraints) {
-        dropQueries.add("ALTER TABLE " + tableName + " DROP CONSTRAINT " + defaultConstraintName);
-      }
-    }
-    return dropQueries;
-  }
-
-  private List<String> getMssqlDefaultConstraints() throws SQLException {
-    List<String> defaultConstrainNames = new LinkedList<>();
-    String commaSeparatedListOfColumns = Arrays.stream(columns).map(s -> "'" + s + "'")
-      .collect(Collectors.joining(","));
-    try (Connection connection = db.getDataSource().getConnection();
-      PreparedStatement pstmt = connection
-        .prepareStatement(format("SELECT d.name FROM sys.tables t "
-          + "JOIN sys.default_constraints d ON d.parent_object_id = t.object_id "
-          + "JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id = d.parent_column_id  "
-          + "WHERE  t.name = '%s' AND c.name in (%s)", tableName, commaSeparatedListOfColumns));
-      ResultSet rs = pstmt.executeQuery()) {
-      while (rs.next()) {
-        defaultConstrainNames.add(rs.getString(1));
-      }
-    }
-    return defaultConstrainNames;
-  }
-
-}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/util/package-info.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v85/util/package-info.java
deleted file mode 100644 (file)
index 47e5dba..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2021 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.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.server.platform.db.migration.version.v85.util;
-
-import javax.annotation.ParametersAreNonnullByDefault;
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/DropMsSQLDefaultConstraintsBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/DropMsSQLDefaultConstraintsBuilderTest.java
new file mode 100644 (file)
index 0000000..9aa6d65
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2021 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.sql;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import javax.sql.DataSource;
+import org.junit.Test;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.H2;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.db.dialect.PostgreSql;
+import org.sonar.server.platform.db.migration.sql.DropMsSQLDefaultConstraintsBuilder;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public class DropMsSQLDefaultConstraintsBuilderTest {
+
+  private final Database db = mock(Database.class);
+
+  @Test(expected = IllegalArgumentException.class)
+  public void fail_if_oracle() throws Exception {
+    when(db.getDialect()).thenReturn(new Oracle());
+    new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void fail_if_h2() throws Exception {
+    when(db.getDialect()).thenReturn(new H2());
+    new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build();
+  }
+
+  @Test(expected = IllegalArgumentException.class)
+  public void fail_if_postgres() throws Exception {
+    when(db.getDialect()).thenReturn(new PostgreSql());
+    new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build();
+  }
+
+  @Test
+  public void generate_queries_for_mssql() throws Exception {
+    when(db.getDialect()).thenReturn(new MsSql());
+    DataSource dataSource = mock(DataSource.class);
+    when(db.getDataSource()).thenReturn(dataSource);
+    Connection connection = mock(Connection.class);
+    when(dataSource.getConnection()).thenReturn(connection);
+    PreparedStatement statement = mock(PreparedStatement.class);
+    when(connection.prepareStatement(anyString())).thenReturn(statement);
+    ResultSet rsMock = mock(ResultSet.class);
+    when(statement.executeQuery()).thenReturn(rsMock);
+    when(rsMock.next()).thenReturn(true, true, false);
+    when(rsMock.getString(1)).thenReturn("DF__A1", "DF__A2");
+
+    assertThat(new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build())
+      .containsExactly("ALTER TABLE snapshots DROP CONSTRAINT DF__A1", "ALTER TABLE snapshots DROP CONSTRAINT DF__A2");
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/util/DropMsSQLDefaultConstraintsBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v85/util/DropMsSQLDefaultConstraintsBuilderTest.java
deleted file mode 100644 (file)
index 0f15f94..0000000
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2021 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.v85.util;
-
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.ResultSet;
-import javax.sql.DataSource;
-import org.junit.Test;
-import org.sonar.db.Database;
-import org.sonar.db.dialect.H2;
-import org.sonar.db.dialect.MsSql;
-import org.sonar.db.dialect.Oracle;
-import org.sonar.db.dialect.PostgreSql;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DropMsSQLDefaultConstraintsBuilderTest {
-
-  private final Database db = mock(Database.class);
-
-  @Test(expected = IllegalArgumentException.class)
-  public void fail_if_oracle() throws Exception {
-    when(db.getDialect()).thenReturn(new Oracle());
-    new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build();
-  }
-
-  @Test(expected = IllegalArgumentException.class)
-  public void fail_if_h2() throws Exception {
-    when(db.getDialect()).thenReturn(new H2());
-    new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build();
-  }
-
-  @Test(expected = IllegalArgumentException.class)
-  public void fail_if_postgres() throws Exception {
-    when(db.getDialect()).thenReturn(new PostgreSql());
-    new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build();
-  }
-
-  @Test
-  public void generate_queries_for_mssql() throws Exception {
-    when(db.getDialect()).thenReturn(new MsSql());
-    DataSource dataSource = mock(DataSource.class);
-    when(db.getDataSource()).thenReturn(dataSource);
-    Connection connection = mock(Connection.class);
-    when(dataSource.getConnection()).thenReturn(connection);
-    PreparedStatement statement = mock(PreparedStatement.class);
-    when(connection.prepareStatement(anyString())).thenReturn(statement);
-    ResultSet rsMock = mock(ResultSet.class);
-    when(statement.executeQuery()).thenReturn(rsMock);
-    when(rsMock.next()).thenReturn(true, true, false);
-    when(rsMock.getString(1)).thenReturn("DF__A1", "DF__A2");
-
-    assertThat(new DropMsSQLDefaultConstraintsBuilder(db).setTable("snapshots").setColumns("variation_value_2", "variation_value_3").build())
-      .containsExactly("ALTER TABLE snapshots DROP CONSTRAINT DF__A1", "ALTER TABLE snapshots DROP CONSTRAINT DF__A2");
-  }
-}