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 org.junit.Test;
25 import org.sonar.db.Database;
26 import org.sonar.db.dialect.Dialect;
27 import org.sonar.db.dialect.H2;
28 import org.sonar.db.dialect.MsSql;
29 import org.sonar.db.dialect.Oracle;
30 import org.sonar.db.dialect.PostgreSql;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.api.Assertions.assertThatThrownBy;
34 import static org.mockito.Mockito.mock;
35 import static org.mockito.Mockito.when;
37 public class DropPrimaryKeySqlGeneratorTest {
39 private static final String TABLE_NAME = "issues";
40 private static final String PK_COLUMN = "id";
41 private static final String CONSTRAINT = "pk_id";
43 private static final PostgreSql POSTGRESQL = new PostgreSql();
44 private static final MsSql MS_SQL = new MsSql();
45 private static final Oracle ORACLE = new Oracle();
46 private static final org.sonar.db.dialect.H2 H2 = new H2();
48 private final Database db = mock(Database.class);
49 private final DbPrimaryKeyConstraintFinder dbConstraintFinder = mock(DbPrimaryKeyConstraintFinder.class);
51 private final DropPrimaryKeySqlGenerator underTest = new DropPrimaryKeySqlGenerator(db, dbConstraintFinder);
54 public void generate_unknown_dialect() {
55 Dialect mockDialect = mock(Dialect.class);
56 when(mockDialect.getId()).thenReturn("unknown-db-vendor");
57 when(db.getDialect()).thenReturn(mockDialect);
59 assertThatThrownBy(() -> underTest.generate(TABLE_NAME, PK_COLUMN, true))
60 .isInstanceOf(IllegalStateException.class);
64 public void generate_for_postgres_sql() throws SQLException {
65 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(CONSTRAINT);
66 when(dbConstraintFinder.getPostgresSqlSequence(TABLE_NAME, "id")).thenReturn(TABLE_NAME + "_id_seq");
67 when(db.getDialect()).thenReturn(POSTGRESQL);
69 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
71 assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
72 "DROP SEQUENCE issues_id_seq",
73 "ALTER TABLE issues DROP CONSTRAINT pk_id");
77 public void generate_for_postgres_sql_no_seq() throws SQLException {
78 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(CONSTRAINT);
79 when(dbConstraintFinder.getPostgresSqlSequence(TABLE_NAME, "id")).thenReturn(null);
80 when(db.getDialect()).thenReturn(POSTGRESQL);
82 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
84 assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
85 "ALTER TABLE issues DROP CONSTRAINT pk_id");
89 public void generate_for_ms_sql() throws SQLException {
90 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(CONSTRAINT);
91 when(db.getDialect()).thenReturn(MS_SQL);
93 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
95 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");
99 public void generate_for_oracle_autogenerated_true() throws SQLException {
100 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(CONSTRAINT);
101 when(db.getDialect()).thenReturn(ORACLE);
103 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
105 assertThat(sqls).containsExactly("DROP TRIGGER issues_IDT",
106 "DROP SEQUENCE issues_SEQ",
107 "ALTER TABLE issues DROP CONSTRAINT pk_id DROP INDEX");
111 public void generate_for_oracle_autogenerated_false() throws SQLException {
112 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(CONSTRAINT);
113 when(db.getDialect()).thenReturn(ORACLE);
115 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, false);
117 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id DROP INDEX");
121 public void generate_for_h2() throws SQLException {
122 when(dbConstraintFinder.findConstraintName(TABLE_NAME)).thenReturn(CONSTRAINT);
123 when(db.getDialect()).thenReturn(H2);
125 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
127 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");