From 34f33d624b7107e59205d2ecbeb4661b6006512b Mon Sep 17 00:00:00 2001 From: Alain Kermis Date: Wed, 1 Mar 2023 16:41:07 +0100 Subject: [PATCH] [NO JIRA] Revert NVARCHAR to VARCHAR conversion for MSSQL --- .../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, 16 insertions(+), 11 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 71ebfc36428..225b856021e 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,6 +23,7 @@ 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; @@ -73,10 +74,14 @@ public class VarcharColumnDef extends AbstractColumnDef { @Override public String generateSqlType(Dialect dialect) { - if (dialect.getId().equals(Oracle.ID)) { - return format("VARCHAR2 (%d%s)", columnSize, ignoreOracleUnit ? "" : " CHAR"); + 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); } - 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 84b465e02d1..04137db1b3e 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("VARCHAR (10)"); + assertThat(def.generateSqlType(new MsSql())).isEqualTo("NVARCHAR (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 49cf34a647d..b19dec98acb 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 VARCHAR (10) NOT NULL, col_with_default BIT DEFAULT 0 NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL"); + .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"); } @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 47a7abb5451..5909fc1717f 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 VARCHAR (10) NULL"); + "ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NULL"); } @Test public void update_not_nullable_column_on_mssql() { assertThat(createNotNullableBuilder(new MsSql()).build()) - .containsOnly("ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NOT NULL"); + .containsOnly("ALTER TABLE issues ALTER COLUMN name NVARCHAR (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 c23c177efda..39bf2f59421 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 VARCHAR (40) NOT NULL"); + verifySql(new MsSql(), selectInto, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid NVARCHAR (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 VARCHAR (40)) AS rule_uuid INTO issues_copy 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 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 8dc5f2fc858..380b6da3186 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 VARCHAR (1) DEFAULT 'P' NOT NULL)"); + verifyDefaultClauseOnVarcharColumn(MS_SQL, "CREATE TABLE table_42 (status NVARCHAR (1) DEFAULT 'P' NOT NULL)"); } @Test -- 2.39.5