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.v83.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 ORIGINAL_TABLE_NAME = "original_issues";
39 private static final String PK_COLUMN = "id";
40 private static final String CONSTRAINT = "pk_id";
42 private static final PostgreSql POSTGRESQL = new PostgreSql();
43 private static final MsSql MS_SQL = new MsSql();
44 private static final Oracle ORACLE = new Oracle();
45 private static final org.sonar.db.dialect.H2 H2 = new H2();
47 private Database db = mock(Database.class);
48 private GetConstraintHelper getConstraintHelper = mock(GetConstraintHelper.class);
50 private DropPrimaryKeySqlGenerator underTest = new DropPrimaryKeySqlGenerator(db, getConstraintHelper);
53 public void generate_for_postgres_sql() throws SQLException {
54 when(getConstraintHelper.getPostgresSqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
55 when(db.getDialect()).thenReturn(POSTGRESQL);
57 List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
59 assertThat(sqls).containsExactly("ALTER TABLE issues ALTER COLUMN id DROP DEFAULT",
60 "DROP SEQUENCE original_issues_id_seq",
61 "ALTER TABLE issues DROP CONSTRAINT pk_id");
65 public void generate_for_ms_sql() throws SQLException {
66 when(getConstraintHelper.getMssqlConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
67 when(db.getDialect()).thenReturn(MS_SQL);
69 List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
71 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT pk_id");
75 public void generate_for_oracle() throws SQLException {
76 when(getConstraintHelper.getOracleConstraint(TABLE_NAME)).thenReturn(CONSTRAINT);
77 when(db.getDialect()).thenReturn(ORACLE);
79 List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
81 assertThat(sqls).containsExactly("DROP TRIGGER issues_IDT",
82 "DROP SEQUENCE issues_SEQ",
83 "ALTER TABLE issues DROP CONSTRAINT pk_id");
87 public void generate_for_h2() throws SQLException {
88 when(getConstraintHelper.getH2Constraint(TABLE_NAME)).thenReturn(CONSTRAINT);
89 when(db.getDialect()).thenReturn(H2);
91 List<String> sqls = underTest.generate(TABLE_NAME, ORIGINAL_TABLE_NAME, PK_COLUMN);
93 assertThat(sqls).containsExactly("ALTER TABLE issues DROP CONSTRAINT PK_original_issues",
94 "ALTER TABLE issues ALTER COLUMN id INTEGER NOT NULL");