3 * Copyright (C) 2009-2021 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.sql.SQLException;
23 import java.util.List;
24 import java.util.Optional;
25 import org.junit.Test;
26 import org.sonar.db.Database;
27 import org.sonar.db.dialect.Dialect;
28 import org.sonar.db.dialect.H2;
29 import org.sonar.db.dialect.MsSql;
30 import org.sonar.db.dialect.Oracle;
31 import org.sonar.db.dialect.PostgreSql;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.assertj.core.api.Assertions.assertThatThrownBy;
35 import static org.mockito.Mockito.mock;
36 import static org.mockito.Mockito.when;
38 public class DropPrimaryKeySqlGeneratorTest {
40 private static final String TABLE_NAME = "issues";
41 private static final String PK_COLUMN = "id";
42 private static final String CONSTRAINT = "pk_id";
44 private static final PostgreSql POSTGRESQL = new PostgreSql();
45 private static final MsSql MS_SQL = new MsSql();
46 private static final Oracle ORACLE = new Oracle();
47 private static final org.sonar.db.dialect.H2 H2 = new H2();
49 private final Database db = mock(Database.class);
50 private final DbPrimaryKeyConstraintFinder dbConstraintFinder = mock(DbPrimaryKeyConstraintFinder.class);
52 private final DropPrimaryKeySqlGenerator underTest = new DropPrimaryKeySqlGenerator(db, dbConstraintFinder);
55 public void generate_unknown_dialect() throws SQLException {
56 Dialect mockDialect = mock(Dialect.class);
57 when(mockDialect.getId()).thenReturn("unknown-db-vendor");
58 when(db.getDialect()).thenReturn(mockDialect);
59 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
61 assertThatThrownBy(() -> underTest.generate(TABLE_NAME, PK_COLUMN, true))
62 .isInstanceOf(IllegalStateException.class);
66 public void generate_for_postgres_sql() throws SQLException {
67 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
68 when(dbConstraintFinder.getPostgresSqlSequence(TABLE_NAME, "id")).thenReturn(TABLE_NAME + "_id_seq");
69 when(db.getDialect()).thenReturn(POSTGRESQL);
71 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
73 assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
74 "DROP SEQUENCE issues_id_seq",
75 "ALTER TABLE issues DROP CONSTRAINT pk_id");
79 public void generate_for_postgres_sql_no_seq() throws SQLException {
80 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
81 when(dbConstraintFinder.getPostgresSqlSequence(TABLE_NAME, "id")).thenReturn(null);
82 when(db.getDialect()).thenReturn(POSTGRESQL);
84 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
86 assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
87 "ALTER TABLE issues DROP CONSTRAINT pk_id");
91 public void generate_for_ms_sql() throws SQLException {
92 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
93 when(db.getDialect()).thenReturn(MS_SQL);
95 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
97 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");
101 public void generate_for_oracle_autogenerated_true() throws SQLException {
102 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
103 when(db.getDialect()).thenReturn(ORACLE);
105 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
107 assertThat(sqls).containsExactly("DROP TRIGGER issues_IDT",
108 "DROP SEQUENCE issues_SEQ",
109 "ALTER TABLE issues DROP CONSTRAINT pk_id DROP INDEX");
113 public void generate_for_oracle_autogenerated_false() throws SQLException {
114 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
115 when(db.getDialect()).thenReturn(ORACLE);
117 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, false);
119 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id DROP INDEX");
123 public void generate_for_h2() throws SQLException {
124 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(Optional.of(CONSTRAINT));
125 when(db.getDialect()).thenReturn(H2);
127 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
129 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");