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 org.junit.Test;
23 import org.sonar.db.dialect.Dialect;
24 import org.sonar.db.dialect.H2;
25 import org.sonar.db.dialect.MsSql;
26 import org.sonar.db.dialect.Oracle;
27 import org.sonar.db.dialect.PostgreSql;
28 import org.sonar.server.platform.db.migration.def.BigIntegerColumnDef;
29 import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.assertj.core.api.Assertions.assertThatThrownBy;
33 import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder;
34 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
36 public class AddColumnsBuilderTest {
38 private static final String TABLE_NAME = "issues";
40 public void add_columns_on_h2() {
41 assertThat(createSampleBuilder(new H2()).build())
42 .isEqualTo("ALTER TABLE issues ADD (date_in_ms BIGINT NULL, name VARCHAR (10) NOT NULL, col_with_default BOOLEAN DEFAULT false NOT NULL, varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL)");
46 public void add_columns_on_oracle() {
47 assertThat(createSampleBuilder(new Oracle()).build())
48 .isEqualTo("ALTER TABLE issues ADD (date_in_ms NUMBER (38) NULL, name VARCHAR2 (10 CHAR) NOT NULL, col_with_default NUMBER(1) DEFAULT 0 NOT NULL, varchar_col_with_default VARCHAR2 (3 CHAR) DEFAULT 'foo' NOT NULL)");
52 public void add_columns_on_postgresql() {
53 assertThat(createSampleBuilder(new PostgreSql()).build())
54 .isEqualTo("ALTER TABLE issues ADD COLUMN date_in_ms BIGINT NULL, ADD COLUMN name VARCHAR (10) NOT NULL, ADD COLUMN col_with_default BOOLEAN DEFAULT false NOT NULL, ADD COLUMN varchar_col_with_default VARCHAR (3) DEFAULT 'foo' NOT NULL");
58 public void add_columns_on_mssql() {
59 assertThat(createSampleBuilder(new MsSql()).build())
60 .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");
64 public void fail_with_ISE_if_no_column() {
65 assertThatThrownBy(() -> new AddColumnsBuilder(new H2(), TABLE_NAME).build())
66 .isInstanceOf(IllegalStateException.class)
67 .hasMessage("No column has been defined");
71 private AddColumnsBuilder createSampleBuilder(Dialect dialect) {
72 return new AddColumnsBuilder(dialect, TABLE_NAME)
73 .addColumn(new BigIntegerColumnDef.Builder().setColumnName("date_in_ms").setIsNullable(true).build())
74 .addColumn(new VarcharColumnDef.Builder().setColumnName("name").setLimit(10).setIsNullable(false).build())
76 // columns with default values
77 .addColumn(newBooleanColumnDefBuilder().setColumnName("col_with_default").setDefaultValue(false).setIsNullable(false).build())
78 .addColumn(newVarcharColumnDefBuilder().setColumnName("varchar_col_with_default").setLimit(3).setDefaultValue("foo").setIsNullable(false).build());