aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/test/java/org/sonar/db/version/AlterColumnsBuilderTest.java
blob: d8369c3913b7a0c52eb57f1f5e4b04d0032135bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * SonarQube
 * Copyright (C) 2009-2016 SonarSource SA
 * mailto:contact AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.db.version;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.db.dialect.Dialect;
import org.sonar.db.dialect.H2;
import org.sonar.db.dialect.MsSql;
import org.sonar.db.dialect.MySql;
import org.sonar.db.dialect.Oracle;
import org.sonar.db.dialect.PostgreSql;

import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.version.BooleanColumnDef.newBooleanColumnDefBuilder;
import static org.sonar.db.version.DecimalColumnDef.newDecimalColumnDefBuilder;
import static org.sonar.db.version.VarcharColumnDef.newVarcharColumnDefBuilder;

public class AlterColumnsBuilderTest {

  private static final String TABLE_NAME = "issues";
  @Rule
  public ExpectedException thrown = ExpectedException.none();

  @Test
  public void update_columns_on_h2() {
    assertThat(createSampleBuilder(new H2()).build())
      .containsOnly(
        "ALTER TABLE issues ALTER COLUMN value DOUBLE NULL",
        "ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NULL");
  }

  @Test
  public void update_not_nullable_column_on_h2() {
    assertThat(createNotNullableBuilder(new H2()).build())
      .containsOnly("ALTER TABLE issues ALTER COLUMN name VARCHAR (10) NOT NULL");
  }

  @Test
  public void update_columns_on_mssql() {
    assertThat(createSampleBuilder(new MsSql()).build())
      .containsOnly(
        "ALTER TABLE issues ALTER COLUMN value DECIMAL (30,20) 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 NVARCHAR (10) NOT NULL");
  }

  @Test
  public void update_columns_on_postgres() {
    assertThat(createSampleBuilder(new PostgreSql()).build())
      .containsOnly("ALTER TABLE issues " +
        "ALTER COLUMN value TYPE NUMERIC (30,20), ALTER COLUMN value DROP NOT NULL, " +
        "ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name DROP NOT NULL");
  }

  @Test
  public void update_not_nullable_column_on_postgres() {
    assertThat(createNotNullableBuilder(new PostgreSql()).build())
      .containsOnly("ALTER TABLE issues ALTER COLUMN name TYPE VARCHAR (10), ALTER COLUMN name SET NOT NULL");
  }

  @Test
  public void update_columns_on_mysql() {
    assertThat(createSampleBuilder(new MySql()).build())
      .containsOnly("ALTER TABLE issues MODIFY COLUMN value DECIMAL (30,20) NULL, MODIFY COLUMN name VARCHAR (10) NULL");
  }

  @Test
  public void update_not_nullable_column_on_mysql() {
    assertThat(createNotNullableBuilder(new MySql()).build())
      .containsOnly("ALTER TABLE issues MODIFY COLUMN name VARCHAR (10) NOT NULL");
  }

  @Test
  public void update_columns_on_oracle() {
    assertThat(createSampleBuilder(new Oracle()).build())
      .containsOnly(
        "ALTER TABLE issues MODIFY (value NUMERIC (30,20) NULL)",
        "ALTER TABLE issues MODIFY (name VARCHAR (10) NULL)");
  }

  @Test
  public void update_not_nullable_column_on_oracle() {
    assertThat(createNotNullableBuilder(new Oracle()).build())
      .containsOnly("ALTER TABLE issues MODIFY (name VARCHAR (10) NOT NULL)");
  }

  @Test
  public void fail_with_ISE_if_no_column() {
    thrown.expect(IllegalStateException.class);
    thrown.expectMessage("No column has been defined");

    new AlterColumnsBuilder(new H2(), TABLE_NAME).build();
  }

  /**
   * As we want DEFAULT value to be removed from all tables, it is supported
   * only on creation of tables and columns, not on alter.
   */
  @Test
  public void updateColumn_throws_IAE_if_default_value_is_defined() {
    BooleanColumnDef column = newBooleanColumnDefBuilder()
      .setColumnName("enabled")
      .setIsNullable(false)
      .setDefaultValue(false)
      .build();
    AlterColumnsBuilder alterColumnsBuilder = new AlterColumnsBuilder(new H2(), TABLE_NAME);

    thrown.expect(IllegalArgumentException.class);
    thrown.expectMessage("Default value is not supported on alter of column 'enabled'");

    alterColumnsBuilder.updateColumn(column);
  }

  private AlterColumnsBuilder createSampleBuilder(Dialect dialect) {
    return new AlterColumnsBuilder(dialect, TABLE_NAME)
      .updateColumn(
        newDecimalColumnDefBuilder()
          .setColumnName("value")
          .setPrecision(30)
          .setScale(20)
          .setIsNullable(true)
          .build())
      .updateColumn(
        newVarcharColumnDefBuilder()
          .setColumnName("name")
          .setLimit(10)
          .setIsNullable(true)
          .build());
  }

  private AlterColumnsBuilder createNotNullableBuilder(Dialect dialect) {
    return new AlterColumnsBuilder(dialect, TABLE_NAME)
      .updateColumn(
        newVarcharColumnDefBuilder()
          .setColumnName("name")
          .setLimit(10)
          .setIsNullable(false)
          .build());
  }

}