import java.sql.SQLException;
import org.sonar.db.Database;
+import org.sonar.db.dialect.Oracle;
import org.sonar.server.platform.db.migration.sql.AddPrimaryKeyBuilder;
import org.sonar.server.platform.db.migration.step.DdlChange;
@Override
public void execute(Context context) throws SQLException {
- context.execute(new AddPrimaryKeyBuilder(TABLE_NAME, "kee").build());
+ if (Oracle.ID.equals(getDialect().getId())) {
+ context.execute(new AddPrimaryKeyBuilder(TABLE_NAME, "kee").build());
+ }
}
}
import java.sql.SQLException;
import org.sonar.db.Database;
+import org.sonar.db.dialect.Oracle;
import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator;
import org.sonar.server.platform.db.migration.step.DdlChange;
@Override
public void execute(Context context) throws SQLException {
- context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, "kee", false));
+ if (Oracle.ID.equals(getDialect().getId())) {
+ context.execute(dropPrimaryKeySqlGenerator.generate(TABLE_NAME, "kee", false));
+ }
}
}
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.Oracle;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.only;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class AddPrimaryKeyOnKeeColumnOfIssuesTableTest {
@Rule
private final AddPrimaryKeyOnKeeColumnOfIssuesTable underTest = new AddPrimaryKeyOnKeeColumnOfIssuesTable(db.database());
@Test
- public void migration_should_drop_PK_on_ce_activity() throws SQLException {
- db.assertNoPrimaryKey("issues");
+ public void migration_should_not_create_another_PK_on_issues_when_using_H2_database() throws SQLException {
+ db.assertPrimaryKey("issues", "pk_issues", "kee");
+
underTest.execute();
+
+ db.assertPrimaryKey("issues", "pk_issues", "kee");
+ }
+
+ @Test
+ public void migration_should_have_no_effect_when_using_h2_database_and_running_it_twice() throws SQLException {
db.assertPrimaryKey("issues", "pk_issues", "kee");
+
+ underTest.execute();
+ underTest.execute();
+
+ db.assertPrimaryKey("issues", "pk_issues", "kee");
+ }
+
+ @Test
+ public void migration_should_execute_when_using_oracle_database() throws SQLException {
+ Database mockedDb = mock(Database.class);
+ AddPrimaryKeyOnKeeColumnOfIssuesTable underTest = new AddPrimaryKeyOnKeeColumnOfIssuesTable(mockedDb);
+
+ DdlChange.Context context = mock(DdlChange.Context.class);
+ Dialect dialect = mock(Dialect.class);
+ when(dialect.getId()).thenReturn(Oracle.ID);
+ when(mockedDb.getDialect()).thenReturn(dialect);
+
+ underTest.execute(context);
+
+ verify(context, only()).execute("ALTER TABLE issues ADD CONSTRAINT pk_issues PRIMARY KEY (kee)");
}
}
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.CoreDbTester;
+import org.sonar.db.Database;
+import org.sonar.db.dialect.Dialect;
+import org.sonar.db.dialect.Oracle;
import org.sonar.server.platform.db.migration.sql.DbPrimaryKeyConstraintFinder;
import org.sonar.server.platform.db.migration.sql.DropPrimaryKeySqlGenerator;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.only;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
public class DropPrimaryKeyOnKeeColumnOfIssuesTableTest {
new DropPrimaryKeySqlGenerator(db.database(), new DbPrimaryKeyConstraintFinder(db.database())));
@Test
- public void migration_should_drop_PK_on_ce_activity() throws SQLException {
+ public void migration_should_have_no_effect_when_using_h2_database() throws SQLException {
db.assertPrimaryKey("issues", "pk_issues", "kee");
underTest.execute();
- db.assertNoPrimaryKey("issues");
+ db.assertPrimaryKey("issues", "pk_issues", "kee");
+ }
+
+ @Test
+ public void migration_should_call_generator_when_using_oracle_database() throws SQLException {
+ DropPrimaryKeySqlGenerator generator = mock(DropPrimaryKeySqlGenerator.class);
+ Database mockedDb = mock(Database.class);
+ DropPrimaryKeyOnKeeColumnOfIssuesTable underTest = new DropPrimaryKeyOnKeeColumnOfIssuesTable(
+ mockedDb, generator);
+
+ DdlChange.Context context = mock(DdlChange.Context.class);
+ Dialect dialect = mock(Dialect.class);
+ when(dialect.getId()).thenReturn(Oracle.ID);
+ when(mockedDb.getDialect()).thenReturn(dialect);
+
+ underTest.execute(context);
+
+ verify(generator, only()).generate("issues", "kee", false);
}
@Test
underTest.execute();
// re-entrant
underTest.execute();
- db.assertNoPrimaryKey("issues");
+ db.assertPrimaryKey("issues", "pk_issues", "kee");
}
}
"PROJECT_UUID" VARCHAR(50),
"LOCATIONS" BLOB,
"ISSUE_TYPE" TINYINT,
- "FROM_HOTSPOT" BOOLEAN
+ "FROM_HOTSPOT" BOOLEAN,
+ CONSTRAINT pk_issues PRIMARY KEY (KEE)
);
CREATE INDEX "ISSUES_ASSIGNEE" ON "ISSUES"("ASSIGNEE");
CREATE INDEX "ISSUES_COMPONENT_UUID" ON "ISSUES"("COMPONENT_UUID");