3 * Copyright (C) 2009-2020 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.version.v84.util;
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.H2;
27 import org.sonar.db.dialect.MsSql;
28 import org.sonar.db.dialect.Oracle;
29 import org.sonar.db.dialect.PostgreSql;
31 import static org.assertj.core.api.Assertions.assertThat;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
35 public class DropPrimaryKeySqlGeneratorTest {
37 private static final String TABLE_NAME = "issues";
38 private static final String PK_COLUMN = "id";
39 private static final String CONSTRAINT = "pk_id";
41 private static final PostgreSql POSTGRESQL = new PostgreSql();
42 private static final MsSql MS_SQL = new MsSql();
43 private static final Oracle ORACLE = new Oracle();
44 private static final org.sonar.db.dialect.H2 H2 = new H2();
46 private Database db = mock(Database.class);
47 private SqlHelper sqlHelper = mock(SqlHelper.class);
49 private DropPrimaryKeySqlGenerator underTest = new DropPrimaryKeySqlGenerator(db, sqlHelper);
52 public void generate_for_postgres_sql() throws SQLException {
53 when(sqlHelper.getPostgresSqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
54 when(sqlHelper.getPostgresSqlSequence(TABLE_NAME, "id")).thenReturn(TABLE_NAME + "_id_seq");
55 when(db.getDialect()).thenReturn(POSTGRESQL);
57 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
59 assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
60 "DROP SEQUENCE issues_id_seq",
61 "ALTER TABLE issues DROP CONSTRAINT pk_id");
65 public void generate_for_ms_sql() throws SQLException {
66 when(sqlHelper.getMssqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
67 when(db.getDialect()).thenReturn(MS_SQL);
69 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
71 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");
75 public void generate_for_oracle() throws SQLException {
76 when(sqlHelper.getOracleConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
77 when(db.getDialect()).thenReturn(ORACLE);
79 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
81 assertThat(sqls).containsExactly("DROP TRIGGER issues_IDT",
82 "DROP SEQUENCE issues_SEQ",
83 "ALTER TABLE issues DROP CONSTRAINT pk_id DROP INDEX");
87 public void generate_for_h2() throws SQLException {
88 when(sqlHelper.getH2Constraint(TABLE_NAME)).thenReturn(CONSTRAINT);
89 when(db.getDialect()).thenReturn(H2);
91 List<String> sqls = underTest.generate(TABLE_NAME, PK_COLUMN, true);
93 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id",
94 "ALTER TABLE issues ALTER COLUMN id INTEGER NOT NULL");