3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.sql;
22 import java.util.List;
23 import org.junit.Test;
24 import org.sonar.db.dialect.Dialect;
25 import org.sonar.db.dialect.H2;
26 import org.sonar.db.dialect.MsSql;
27 import org.sonar.db.dialect.Oracle;
28 import org.sonar.db.dialect.PostgreSql;
30 import static org.assertj.core.api.Assertions.assertThat;
31 import static org.assertj.core.api.Assertions.assertThatThrownBy;
32 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
34 public class CreateTableAsBuilderTest {
37 public void create_table() {
38 String createTableAs = "CREATE TABLE issues_copy (rule_uuid) AS (SELECT rule_uuid FROM issues)";
39 String selectInto = "SELECT rule_uuid INTO issues_copy FROM issues";
41 verifySql(new H2(), createTableAs, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL");
42 verifySql(new MsSql(), selectInto, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL");
43 verifySql(new Oracle(), createTableAs, "ALTER TABLE issues_copy MODIFY (rule_uuid VARCHAR2 (40 CHAR) NOT NULL)");
44 verifySql(new PostgreSql(), createTableAs, "ALTER TABLE issues_copy ALTER COLUMN rule_uuid TYPE VARCHAR (40), ALTER COLUMN rule_uuid SET NOT NULL");
48 public void create_table_with_cast() {
49 verifySqlWithCast(new H2(), "CREATE TABLE issues_copy (rule_uuid) AS (SELECT CAST (rule_id AS VARCHAR (40)) AS rule_uuid FROM issues)",
50 "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL");
51 verifySqlWithCast(new MsSql(), "SELECT CAST (rule_id AS VARCHAR (40)) AS rule_uuid INTO issues_copy FROM issues",
52 "ALTER TABLE issues_copy ALTER COLUMN rule_uuid VARCHAR (40) NOT NULL");
53 verifySqlWithCast(new Oracle(), "CREATE TABLE issues_copy (rule_uuid) AS (SELECT CAST (rule_id AS VARCHAR2 (40 CHAR)) AS rule_uuid FROM issues)",
54 "ALTER TABLE issues_copy MODIFY (rule_uuid VARCHAR2 (40 CHAR) NOT NULL)");
55 verifySqlWithCast(new PostgreSql(), "CREATE TABLE issues_copy (rule_uuid) AS (SELECT CAST (rule_id AS VARCHAR (40)) AS rule_uuid FROM issues)",
56 "ALTER TABLE issues_copy ALTER COLUMN rule_uuid TYPE VARCHAR (40), ALTER COLUMN rule_uuid SET NOT NULL");
60 public void fail_if_columns_not_set() {
61 assertThatThrownBy(() -> new CreateTableAsBuilder(new H2(), "issues_copy", "issues")
63 .isInstanceOf(IllegalStateException.class);
67 public void fail_if_table_not_set() {
68 assertThatThrownBy(() -> new CreateTableAsBuilder(new H2(), null, "issues")
70 .isInstanceOf(NullPointerException.class);
73 private static void verifySqlWithCast(Dialect dialect, String... expectedSql) {
74 List<String> actual = new CreateTableAsBuilder(dialect, "issues_copy", "issues")
75 .addColumnWithCast(newVarcharColumnDefBuilder().setColumnName("rule_uuid").setIsNullable(false).setLimit(40).build(), "rule_id")
77 assertThat(actual).containsExactly(expectedSql);
80 private static void verifySql(Dialect dialect, String... expectedSql) {
81 List<String> actual = new CreateTableAsBuilder(dialect, "issues_copy", "issues")
82 .addColumn(newVarcharColumnDefBuilder().setColumnName("rule_uuid").setIsNullable(false).setLimit(40).build())
84 assertThat(actual).containsExactly(expectedSql);