]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15780 Migrate 'image' column type to 'varbinary(max)' in MSSQL
authorJacek <jacek.poreda@sonarsource.com>
Fri, 3 Dec 2021 15:20:03 +0000 (16:20 +0100)
committersonartech <sonartech@sonarsource.com>
Mon, 6 Dec 2021 20:03:53 +0000 (20:03 +0000)
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/v93/DbVersion93.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/FixUsageOfDeprecatedColumnsMsSQL.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/FixUsageOfDeprecatedColumnsMsSQLTest.java [new file with mode: 0644]
server/sonar-docs/package.json

index beb3929e55f78523caf6f072f7dc75bd8be62974..c754396cf1da0781aaa3f963313579c70d125a2b 100644 (file)
@@ -30,6 +30,7 @@ import org.sonar.server.platform.db.migration.version.v00.DbVersion00;
 import org.sonar.server.platform.db.migration.version.v90.DbVersion90;
 import org.sonar.server.platform.db.migration.version.v91.DbVersion91;
 import org.sonar.server.platform.db.migration.version.v92.DbVersion92;
+import org.sonar.server.platform.db.migration.version.v93.DbVersion93;
 
 public class MigrationConfigurationModule extends Module {
   @Override
@@ -40,6 +41,7 @@ public class MigrationConfigurationModule extends Module {
       DbVersion90.class,
       DbVersion91.class,
       DbVersion92.class,
+      DbVersion93.class,
 
       // migration steps
       MigrationStepRegistryImpl.class,
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93.java
new file mode 100644 (file)
index 0000000..b3ca24d
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * 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.v93;
+
+import org.sonar.server.platform.db.migration.step.MigrationStepRegistry;
+import org.sonar.server.platform.db.migration.version.DbVersion;
+
+public class DbVersion93 implements DbVersion {
+  @Override
+  public void addSteps(MigrationStepRegistry registry) {
+    registry
+      .add(6201, "Fix usage of deprecated column in MSSQL", FixUsageOfDeprecatedColumnsMsSQL.class);
+  }
+}
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/FixUsageOfDeprecatedColumnsMsSQL.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v93/FixUsageOfDeprecatedColumnsMsSQL.java
new file mode 100644 (file)
index 0000000..2e83986
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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.v93;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.BlobColumnDef.newBlobColumnDefBuilder;
+
+public class FixUsageOfDeprecatedColumnsMsSQL extends DdlChange {
+
+  public FixUsageOfDeprecatedColumnsMsSQL(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    if (!MsSql.ID.equals(getDatabase().getDialect().getId())) {
+      return;
+    }
+
+    try (Connection c = getDatabase().getDataSource().getConnection();
+      var ps = c.prepareStatement(
+        "SELECT table_name, column_name FROM [INFORMATION_SCHEMA].[COLUMNS] WHERE data_type = ?")) {
+      ps.setString(1, "image");
+      ResultSet rs = ps.executeQuery();
+      while (rs.next()) {
+        var tableName = rs.getString(1);
+        var columnName = rs.getString(2);
+        context.execute(
+          new AlterColumnsBuilder(getDatabase().getDialect(), tableName)
+            .updateColumn(newBlobColumnDefBuilder().setColumnName(columnName).build())
+            .build());
+      }
+    }
+
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/DbVersion93Test.java
new file mode 100644 (file)
index 0000000..cb8cac2
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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.v93;
+
+import org.junit.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMigrationNotEmpty;
+import static org.sonar.server.platform.db.migration.version.DbVersionTestUtils.verifyMinimumMigrationNumber;
+
+public class DbVersion93Test {
+
+  private final DbVersion93 underTest = new DbVersion93();
+
+  @Test
+  public void verify_no_support_component() {
+    assertThat(underTest.getSupportComponents()).isEmpty();
+  }
+
+  @Test
+  public void migrationNumber_starts_at_6101() {
+    verifyMinimumMigrationNumber(underTest, 6201);
+  }
+
+  @Test
+  public void verify_migration_count() {
+    verifyMigrationNotEmpty(underTest);
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/FixUsageOfDeprecatedColumnsMsSQLTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v93/FixUsageOfDeprecatedColumnsMsSQLTest.java
new file mode 100644 (file)
index 0000000..284a14d
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * 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.v93;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import javax.sql.DataSource;
+import org.junit.Test;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.MsSql;
+import org.sonar.db.dialect.PostgreSql;
+import org.sonar.server.platform.db.migration.step.DdlChange.Context;
+
+import static java.util.List.of;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class FixUsageOfDeprecatedColumnsMsSQLTest {
+
+  @Test
+  public void skip_if_not_mssql() {
+    var databaseMock = mock(Database.class);
+    when(databaseMock.getDialect()).thenReturn(new PostgreSql());
+    var underTest = new FixUsageOfDeprecatedColumnsMsSQL(databaseMock);
+    var contextMock = mock(Context.class);
+
+    assertThatCode(() -> underTest.execute(contextMock))
+      .doesNotThrowAnyException();
+    verify(databaseMock, times(0)).getDataSource();
+  }
+
+  @Test
+  public void execute_alter_table_if_columns_with_deprecated_type_exist() throws SQLException {
+    var databaseMock = mock(Database.class);
+    var dataSourceMock = mock(DataSource.class);
+    var connectionMock = mock(Connection.class);
+    var prepareStatementMock = mock(PreparedStatement.class);
+    var resultSetMock = mock(ResultSet.class);
+
+    when(databaseMock.getDialect()).thenReturn(new MsSql());
+    when(databaseMock.getDataSource()).thenReturn(dataSourceMock);
+    when(dataSourceMock.getConnection()).thenReturn(connectionMock);
+    when(connectionMock.prepareStatement(anyString())).thenReturn(prepareStatementMock);
+    when(prepareStatementMock.executeQuery()).thenReturn(resultSetMock);
+
+    when(resultSetMock.next()).thenReturn(true, true, true, true, false);
+    when(resultSetMock.getString(1)).thenReturn("file_sources", "issues", "notifications", "project_measures");
+    when(resultSetMock.getString(2)).thenReturn("binary_data", "locations", "data", "measure_data");
+
+    var underTest = new FixUsageOfDeprecatedColumnsMsSQL(databaseMock);
+    var contextMock = mock(Context.class);
+
+    assertThatCode(() -> underTest.execute(contextMock))
+      .doesNotThrowAnyException();
+    verify(contextMock, times(1)).execute(of("ALTER TABLE file_sources ALTER COLUMN binary_data VARBINARY(MAX) NULL"));
+    verify(contextMock, times(1)).execute(of("ALTER TABLE issues ALTER COLUMN locations VARBINARY(MAX) NULL"));
+    verify(contextMock, times(1)).execute(of("ALTER TABLE notifications ALTER COLUMN data VARBINARY(MAX) NULL"));
+    verify(contextMock, times(1)).execute(of("ALTER TABLE project_measures ALTER COLUMN measure_data VARBINARY(MAX) NULL"));
+  }
+}
index e8a80e609d1ac2d610717deb7d4069909960ec1c..7dde29b11313e9fa66134089d7cc8cce2b45ee90 100644 (file)
     }
   },
   "packageManager": "yarn@3.0.2"
-}
\ No newline at end of file
+}