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.BooleanColumnDef;
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.BooleanColumnDef.newBooleanColumnDefBuilder;
33 import static org.sonar.server.platform.db.migration.def.DecimalColumnDef.newDecimalColumnDefBuilder;
34 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
36 public class AlterColumnsBuilderTest {
38 private static final String TABLE_NAME = "issues";
41 public void update_columns_on_h2() {
42 assertThat(createSampleBuilder(new H2()).build())
44 "ALTER TABLE issues ALTER COLUMN value DOUBLE NULL",
45 "ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NULL");
49 public void update_not_nullable_column_on_h2() {
50 assertThat(createNotNullableBuilder(new H2()).build())
51 .containsOnly("ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NOT NULL");
55 public void update_columns_on_mssql() {
56 assertThat(createSampleBuilder(new MsSql()).build())
58 "ALTER TABLE issues ALTER COLUMN value DECIMAL (30,20) NULL",
59 "ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NULL");
63 public void update_not_nullable_column_on_mssql() {
64 assertThat(createNotNullableBuilder(new MsSql()).build())
65 .containsOnly("ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NOT NULL");
69 public void update_columns_on_postgres() {
70 assertThat(createSampleBuilder(new PostgreSql()).build())
71 .containsOnly("ALTER TABLE issues " +
72 "ALTER COLUMN value TYPE NUMERIC (30,20), ALTER COLUMN value DROP NOT NULL, " +
73 "ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name DROP NOT NULL");
77 public void update_not_nullable_column_on_postgres() {
78 assertThat(createNotNullableBuilder(new PostgreSql()).build())
79 .containsOnly("ALTER TABLE issues ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name SET NOT NULL");
83 public void update_columns_on_oracle() {
84 assertThat(createSampleBuilder(new Oracle()).build())
86 "ALTER TABLE issues MODIFY (value NUMERIC (30,20) NULL)",
87 "ALTER TABLE issues MODIFY (name VARCHAR2 (10 CHAR) NULL)");
91 public void update_not_nullable_column_on_oracle() {
92 assertThat(createNotNullableBuilder(new Oracle()).build())
93 .containsOnly("ALTER TABLE issues MODIFY (name VARCHAR2 (10 CHAR) NOT NULL)");
97 public void fail_with_ISE_if_no_column() {
98 assertThatThrownBy(() -> new AlterColumnsBuilder(new H2(), TABLE_NAME).build())
99 .isInstanceOf(IllegalStateException.class)
100 .hasMessage("No column has been defined");
104 * As we want DEFAULT value to be removed from all tables, it is supported
105 * only on creation of tables and columns, not on alter.
108 public void updateColumn_throws_IAE_if_default_value_is_defined() {
109 BooleanColumnDef column = newBooleanColumnDefBuilder()
110 .setColumnName("enabled")
111 .setIsNullable(false)
112 .setDefaultValue(false)
114 AlterColumnsBuilder alterColumnsBuilder = new AlterColumnsBuilder(new H2(), TABLE_NAME);
116 assertThatThrownBy(() -> alterColumnsBuilder.updateColumn(column))
117 .isInstanceOf(IllegalArgumentException.class)
118 .hasMessage("Default value is not supported on alter of column 'enabled'");
121 private AlterColumnsBuilder createSampleBuilder(Dialect dialect) {
122 return new AlterColumnsBuilder(dialect, TABLE_NAME)
124 newDecimalColumnDefBuilder()
125 .setColumnName("value")
131 newVarcharColumnDefBuilder()
132 .setColumnName("name")
138 private AlterColumnsBuilder createNotNullableBuilder(Dialect dialect) {
139 return new AlterColumnsBuilder(dialect, TABLE_NAME)
141 newVarcharColumnDefBuilder()
142 .setColumnName("name")
144 .setIsNullable(false)