From d73885870fb3a4f6599bcade41b28430927f7539 Mon Sep 17 00:00:00 2001 From: Alain Kermis Date: Tue, 28 Feb 2023 10:40:14 +0100 Subject: [PATCH] SONAR-17525 MSSQL UUID column should use VARCHAR instead NVARCHAR --- .../platform/db/migration/def/VarcharColumnDef.java | 11 +++-------- .../db/migration/def/VarcharColumnDefTest.java | 2 +- .../db/migration/sql/AddColumnsBuilderTest.java | 2 +- .../db/migration/sql/AlterColumnsBuilderTest.java | 4 ++-- .../db/migration/sql/CreateTableAsBuilderTest.java | 6 +++--- .../db/migration/sql/CreateTableBuilderTest.java | 2 +- 6 files changed, 11 insertions(+), 16 deletions(-) diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/VarcharColumnDef.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/VarcharColumnDef.java index 225b856021e..71ebfc36428 100644 --- a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/VarcharColumnDef.java +++ b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/def/VarcharColumnDef.java @@ -23,7 +23,6 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.sonar.db.dialect.Dialect; -import org.sonar.db.dialect.MsSql; import org.sonar.db.dialect.Oracle; import static java.lang.String.format; @@ -74,14 +73,10 @@ public class VarcharColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - switch (dialect.getId()) { - case MsSql.ID: - return format("NVARCHAR (%d)", columnSize); - case Oracle.ID: - return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR"); - default: - return format("VARCHAR (%d)", columnSize); + if (dialect.getId().equals(Oracle.ID)) { + return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR"); } + return format("VARCHAR (%d)", columnSize); } public static class Builder { diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/def/VarcharColumnDefTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/def/VarcharColumnDefTest.java index 04137db1b3e..84b465e02d1 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/def/VarcharColumnDefTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/def/VarcharColumnDefTest.java @@ -67,7 +67,7 @@ public class VarcharColumnDefTest { assertThat(def.generateSqlType(new H2())).isEqualTo("VARCHAR (10)"); assertThat(def.generateSqlType(new PostgreSql())).isEqualTo("VARCHAR (10)"); - assertThat(def.generateSqlType(new MsSql())).isEqualTo("NVARCHAR (10)"); + assertThat(def.generateSqlType(new MsSql())).isEqualTo("VARCHAR (10)"); assertThat(def.generateSqlType(new Oracle())).isEqualTo("VARCHAR2 (10 CHAR)"); } diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AddColumnsBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AddColumnsBuilderTest.java index b19dec98acb..49cf34a647d 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AddColumnsBuilderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AddColumnsBuilderTest.java @@ -57,7 +57,7 @@ public class AddColumnsBuilderTest { @Test public void add_columns_on_mssql() { assertThat(createSampleBuilder(new MsSql()).build()) - .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name NVARCHAR (10) NOT NULL, col_with_default BIT DEFAULT 0 NOT NULL, varchar_col_with_default NVARCHAR (3) DEFAULT 'foo' NOT NULL"); + .isEqualTo("ALTER TABLE issues ADD date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default BIT DEFAULT 0 NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL"); } @Test diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AlterColumnsBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AlterColumnsBuilderTest.java index 5909fc1717f..47a7abb5451 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AlterColumnsBuilderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/AlterColumnsBuilderTest.java @@ -56,13 +56,13 @@ public class AlterColumnsBuilderTest { assertThat(createSampleBuilder(new MsSql()).build()) .containsOnly( "ALTER TABLE issues ALTER COLUMN value DECIMAL (30,20) NULL", - "ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NULL"); + "ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NULL"); } @Test public void update_not_nullable_column_on_mssql() { assertThat(createNotNullableBuilder(new MsSql()).build()) - .containsOnly("ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NOT NULL"); + .containsOnly("ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NOT NULL"); } @Test diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableAsBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableAsBuilderTest.java index 39bf2f59421..c23c177efda 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableAsBuilderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableAsBuilderTest.java @@ -39,7 +39,7 @@ public class CreateTableAsBuilderTest { String selectInto = "SELECT rule_uuid INTO issues_copy FROM issues"; verifySql(new H2(), createTableAs, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL"); - verifySql(new MsSql(), selectInto, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid NVARCHAR (40) NOT NULL"); + verifySql(new MsSql(), selectInto, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL"); verifySql(new Oracle(), createTableAs, "ALTER TABLE issues_copy MODIFY (rule_uuid VARCHAR2 (40 CHAR) NOT NULL)"); verifySql(new PostgreSql(), createTableAs, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid TYPE VARCHAR (40), ALTER COLUMN rule_uuid SET NOT NULL"); } @@ -48,8 +48,8 @@ public class CreateTableAsBuilderTest { public void create_table_with_cast() { verifySqlWithCast(new H2(), "CREATE TABLE issues_copy (rule_uuid) AS (SELECT CAST (rule_id AS VARCHAR (40)) AS rule_uuid FROM issues)", "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL"); - verifySqlWithCast(new MsSql(), "SELECT CAST (rule_id AS NVARCHAR (40)) AS rule_uuid INTO issues_copy FROM issues", - "ALTER TABLE issues_copy ALTER COLUMN rule_uuid NVARCHAR (40) NOT NULL"); + verifySqlWithCast(new MsSql(), "SELECT CAST (rule_id AS VARCHAR (40)) AS rule_uuid INTO issues_copy FROM issues", + "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL"); verifySqlWithCast(new Oracle(), "CREATE TABLE issues_copy (rule_uuid) AS (SELECT CAST (rule_id AS VARCHAR2 (40 CHAR)) AS rule_uuid FROM issues)", "ALTER TABLE issues_copy MODIFY (rule_uuid VARCHAR2 (40 CHAR) NOT NULL)"); verifySqlWithCast(new PostgreSql(), "CREATE TABLE issues_copy (rule_uuid) AS (SELECT CAST (rule_id AS VARCHAR (40)) AS rule_uuid FROM issues)", diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableBuilderTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableBuilderTest.java index 380b6da3186..8dc5f2fc858 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableBuilderTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/sql/CreateTableBuilderTest.java @@ -474,7 +474,7 @@ public class CreateTableBuilderTest { @Test public void build_adds_DEFAULT_clause_on_varchar_column_on_MSSQL() { - verifyDefaultClauseOnVarcharColumn(MS_SQL, "CREATE TABLE table_42 (status NVARCHAR (1) DEFAULT 'P' NOT NULL)"); + verifyDefaultClauseOnVarcharColumn(MS_SQL, "CREATE TABLE table_42 (status VARCHAR (1) DEFAULT 'P' NOT NULL)"); } @Test -- 2.39.5