3 * Copyright (C) 2009-2017 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.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.db.dialect.Dialect;
26 import org.sonar.db.dialect.H2;
27 import org.sonar.db.dialect.MsSql;
28 import org.sonar.db.dialect.MySql;
29 import org.sonar.db.dialect.Oracle;
30 import org.sonar.db.dialect.PostgreSql;
31 import org.sonar.server.platform.db.migration.def.BooleanColumnDef;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.sonar.server.platform.db.migration.def.BooleanColumnDef.newBooleanColumnDefBuilder;
35 import static org.sonar.server.platform.db.migration.def.DecimalColumnDef.newDecimalColumnDefBuilder;
36 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.newVarcharColumnDefBuilder;
38 public class AlterColumnsBuilderTest {
40 private static final String TABLE_NAME = "issues";
42 public ExpectedException thrown = ExpectedException.none();
45 public void update_columns_on_h2() {
46 assertThat(createSampleBuilder(new H2()).build())
48 "ALTER TABLE issues ALTER COLUMN value DOUBLE NULL",
49 "ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NULL");
53 public void update_not_nullable_column_on_h2() {
54 assertThat(createNotNullableBuilder(new H2()).build())
55 .containsOnly("ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NOT NULL");
59 public void update_columns_on_mssql() {
60 assertThat(createSampleBuilder(new MsSql()).build())
62 "ALTER TABLE issues ALTER COLUMN value DECIMAL (30,20) NULL",
63 "ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NULL");
67 public void update_not_nullable_column_on_mssql() {
68 assertThat(createNotNullableBuilder(new MsSql()).build())
69 .containsOnly("ALTER TABLE issues ALTER COLUMN name NVARCHAR (10) NOT NULL");
73 public void update_columns_on_postgres() {
74 assertThat(createSampleBuilder(new PostgreSql()).build())
75 .containsOnly("ALTER TABLE issues " +
76 "ALTER COLUMN value TYPE NUMERIC (30,20), ALTER COLUMN value DROP NOT NULL, " +
77 "ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name DROP NOT NULL");
81 public void update_not_nullable_column_on_postgres() {
82 assertThat(createNotNullableBuilder(new PostgreSql()).build())
83 .containsOnly("ALTER TABLE issues ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name SET NOT NULL");
87 public void update_columns_on_mysql() {
88 assertThat(createSampleBuilder(new MySql()).build())
89 .containsOnly("ALTER TABLE issues MODIFY COLUMN value DECIMAL (30,20) NULL, MODIFY COLUMN name VARCHAR (10) NULL");
93 public void update_not_nullable_column_on_mysql() {
94 assertThat(createNotNullableBuilder(new MySql()).build())
95 .containsOnly("ALTER TABLE issues MODIFY COLUMN name VARCHAR (10) NOT NULL");
99 public void update_columns_on_oracle() {
100 assertThat(createSampleBuilder(new Oracle()).build())
102 "ALTER TABLE issues MODIFY (value NUMERIC (30,20) NULL)",
103 "ALTER TABLE issues MODIFY (name VARCHAR (10 CHAR) NULL)");
107 public void update_not_nullable_column_on_oracle() {
108 assertThat(createNotNullableBuilder(new Oracle()).build())
109 .containsOnly("ALTER TABLE issues MODIFY (name VARCHAR (10 CHAR) NOT NULL)");
113 public void fail_with_ISE_if_no_column() {
114 thrown.expect(IllegalStateException.class);
115 thrown.expectMessage("No column has been defined");
117 new AlterColumnsBuilder(new H2(), TABLE_NAME).build();
121 * As we want DEFAULT value to be removed from all tables, it is supported
122 * only on creation of tables and columns, not on alter.
125 public void updateColumn_throws_IAE_if_default_value_is_defined() {
126 BooleanColumnDef column = newBooleanColumnDefBuilder()
127 .setColumnName("enabled")
128 .setIsNullable(false)
129 .setDefaultValue(false)
131 AlterColumnsBuilder alterColumnsBuilder = new AlterColumnsBuilder(new H2(), TABLE_NAME);
133 thrown.expect(IllegalArgumentException.class);
134 thrown.expectMessage("Default value is not supported on alter of column 'enabled'");
136 alterColumnsBuilder.updateColumn(column);
139 private AlterColumnsBuilder createSampleBuilder(Dialect dialect) {
140 return new AlterColumnsBuilder(dialect, TABLE_NAME)
142 newDecimalColumnDefBuilder()
143 .setColumnName("value")
149 newVarcharColumnDefBuilder()
150 .setColumnName("name")
156 private AlterColumnsBuilder createNotNullableBuilder(Dialect dialect) {
157 return new AlterColumnsBuilder(dialect, TABLE_NAME)
159 newVarcharColumnDefBuilder()
160 .setColumnName("name")
162 .setIsNullable(false)