Skip to content

Commit

Permalink
SONAR-20296 Update minimum database version checks for Oracle and Pos…
Browse files Browse the repository at this point in the history
…tgres
  • Loading branch information
matteo-mara-sonarsource authored and sonartech committed Jun 20, 2024
1 parent 255d54f commit d351497
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 77 deletions.
1 change: 1 addition & 0 deletions server/sonar-db-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies {
}

test {
useJUnitPlatform()
if (System.hasProperty('orchestrator.configUrl'))
systemProperty 'orchestrator.configUrl', System.getProperty('orchestrator.configUrl')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
public class Oracle extends AbstractDialect {
public static final String ID = "oracle";
private static final List<String> INIT_STATEMENTS = List.of("ALTER SESSION SET NLS_SORT='BINARY'");
private static final Version MIN_SUPPORTED_VERSION = Version.create(11, 0, 0);
private static final Version MIN_SUPPORTED_VERSION = Version.create(19, 0, 0);

public Oracle() {
super(ID, "oracle.jdbc.OracleDriver", "1", "0", "SELECT 1 FROM DUAL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.Version;

import static com.google.common.base.Preconditions.checkState;

public class PostgreSql extends AbstractDialect {
public static final String ID = "postgresql";
static final List<String> INIT_STATEMENTS = List.of("SET standard_conforming_strings=on", "SET backslash_quote=off");
private static final Version MIN_SUPPORTED_VERSION = Version.create(9, 3, 0);
private static final Version MIN_UPSERT_VERSION = Version.create(9, 5, 0);
private static final Version MIN_SUPPORTED_VERSION = Version.create(11, 0, 0);
private static final Version MIN_NULL_NOT_DISTINCT_VERSION = Version.create(15, 0, 0);

private boolean initialized = false;
private boolean supportsUpsert = false;
private boolean supportsNullNotDistinct = false;

public PostgreSql() {
Expand All @@ -60,8 +57,7 @@ public boolean supportsMigration() {

@Override
public boolean supportsUpsert() {
checkState(initialized, "onInit() must be called before calling supportsUpsert()");
return supportsUpsert;
return true;
}

@Override
Expand All @@ -76,11 +72,7 @@ public void init(DatabaseMetaData metaData) throws SQLException {

Version version = checkDbVersion(metaData, MIN_SUPPORTED_VERSION);

supportsUpsert = version.compareTo(MIN_UPSERT_VERSION) >= 0;
supportsNullNotDistinct = version.compareTo(MIN_NULL_NOT_DISTINCT_VERSION) >= 0;
if (!supportsUpsert) {
LoggerFactory.getLogger(getClass()).warn("Upgrading PostgreSQL to {} or greater is recommended for better performances", MIN_UPSERT_VERSION);
}

initialized = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.sonar.api.utils.MessageException;

Expand All @@ -32,96 +32,92 @@

public class OracleTest {

private Oracle underTest = new Oracle();
private final Oracle underTest = new Oracle();

@Test
public void matchesJdbcURL() {
void matchesJdbcURL() {
assertThat(underTest.matchesJdbcUrl("jdbc:oracle:thin:@localhost/XE")).isTrue();
assertThat(underTest.matchesJdbcUrl("jdbc:hsql:foo")).isFalse();
}

@Test
public void testBooleanSqlValues() {
void testBooleanSqlValues() {
assertThat(underTest.getTrueSqlValue()).isEqualTo("1");
assertThat(underTest.getFalseSqlValue()).isEqualTo("0");
}

@Test
public void should_configure() {
void should_configure() {
assertThat(underTest.getId()).isEqualTo("oracle");
assertThat(underTest.getDefaultDriverClassName()).isEqualTo("oracle.jdbc.OracleDriver");
assertThat(underTest.getValidationQuery()).isEqualTo("SELECT 1 FROM DUAL");
}

@Test
public void testFetchSizeForScrolling() {
void testFetchSizeForScrolling() {
assertThat(underTest.getScrollDefaultFetchSize()).isEqualTo(200);
}

@Test
public void oracle_does_supportMigration() {
void oracle_does_supportMigration() {
assertThat(underTest.supportsMigration()).isTrue();
}

@Test
public void getSqlFromDual() {
void getSqlFromDual() {
assertThat(underTest.getSqlFromDual()).isEqualTo("from dual");
}

@Test
public void test_db_versions() throws Exception {
void test_db_versions() throws Exception {
// oracle 11.0 is ok
DatabaseMetaData metadata = newMetadata( 11, 0, "12.1.0.1.0");
DatabaseMetaData metadata = newMetadata( 19, 0, "12.1.0.1.0");
underTest.init(metadata);

// oracle 11.1 is noit
metadata = newMetadata(11, 1, "12.1.0.1.0");
// oracle 11.1 is ok
metadata = newMetadata(19, 1, "12.1.0.1.0");
underTest.init(metadata);

// oracle 11.2 is ok
metadata = newMetadata(11, 2, "12.1.0.1.0");
metadata = newMetadata(19, 2, "12.1.0.1.0");
underTest.init(metadata);

// oracle 12 is ok
metadata = newMetadata(12, 0, "12.1.0.1.0");
// oracle 21 is ok
metadata = newMetadata(21, 1, "12.1.0.1.0");
underTest.init(metadata);

// oracle 18 is ok
metadata = newMetadata(18, 0, "18.3.0.0.0");
underTest.init(metadata);

// oracle 10 is not supported
metadata = newMetadata(10, 2, "12.1.0.1.0");
// oracle 18 is not supported
metadata = newMetadata(18, 17, "12.1.0.1.0");
try {
underTest.init(metadata);
fail();
} catch (MessageException e) {
assertThat(e).hasMessage("Unsupported oracle version: 10.2. Minimal supported version is 11.0.");
assertThat(e).hasMessage("Unsupported oracle version: 18.17. Minimal supported version is 19.0.");
}
}

@Test
public void test_driver_versions() throws Exception {
DatabaseMetaData metadata = newMetadata( 11, 2, "18.3.0.0.0");
void test_driver_versions() throws Exception {
DatabaseMetaData metadata = newMetadata( 19, 2, "18.3.0.0.0");
underTest.init(metadata);

metadata = newMetadata(11, 2, "12.2.0.1.0");
metadata = newMetadata(19, 2, "12.2.0.1.0");
underTest.init(metadata);
// no error

metadata = newMetadata(11, 2, "12.1.0.2.0");
metadata = newMetadata(19, 2, "12.1.0.2.0");
underTest.init(metadata);
// no error

metadata = newMetadata(11, 2, "12.1.0.1.0");
metadata = newMetadata(19, 2, "12.1.0.1.0");
underTest.init(metadata);
// no error

metadata = newMetadata(11, 2, "12.0.2");
metadata = newMetadata(19, 2, "12.0.2");
underTest.init(metadata);
// no error

metadata = newMetadata(11, 2, "11.1.0.2");
metadata = newMetadata(19, 2, "11.1.0.2");
try {
underTest.init(metadata);
fail();
Expand All @@ -131,7 +127,7 @@ public void test_driver_versions() throws Exception {
}

@Test
public void supportsUpsert_returns_false() {
void supportsUpsert_returns_false() {
assertThat(underTest.supportsUpsert()).isFalse();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,95 +21,92 @@

import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mockito;
import org.slf4j.event.Level;
import org.sonar.api.testfixtures.log.LogTester;
import org.sonar.api.testfixtures.log.LogTesterJUnit5;
import org.sonar.api.utils.MessageException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class PostgreSqlTest {

@Rule
public LogTester logs = new LogTester();
@RegisterExtension
public final LogTesterJUnit5 logs = new LogTesterJUnit5();

private PostgreSql underTest = new PostgreSql();
private final PostgreSql underTest = new PostgreSql();

@Test
public void matchesJdbcURL() {
void matchesJdbcURL() {
assertThat(underTest.matchesJdbcUrl("jdbc:postgresql://localhost/sonar")).isTrue();
assertThat(underTest.matchesJdbcUrl("jdbc:hsql:foo")).isFalse();
}

@Test
public void should_set_connection_properties() {
void should_set_connection_properties() {
assertThat(underTest.getConnectionInitStatements()).isEqualTo(PostgreSql.INIT_STATEMENTS);
}

@Test
public void testBooleanSqlValues() {
void testBooleanSqlValues() {
assertThat(underTest.getTrueSqlValue()).isEqualTo("true");
assertThat(underTest.getFalseSqlValue()).isEqualTo("false");
}

@Test
public void should_configure() {
void should_configure() {
assertThat(underTest.getId()).isEqualTo("postgresql");
assertThat(underTest.getDefaultDriverClassName()).isEqualTo("org.postgresql.Driver");
assertThat(underTest.getValidationQuery()).isEqualTo("SELECT 1");
}

@Test
public void testFetchSizeForScrolling() {
void testFetchSizeForScrolling() {
assertThat(underTest.getScrollDefaultFetchSize()).isEqualTo(200);
}

@Test
public void postgres_does_supportMigration() {
void postgres_does_supportMigration() {
assertThat(underTest.supportsMigration()).isTrue();
}

@Test
public void getSqlFromDual() {
void postgres_does_supportUpsert() {
assertThat(underTest.supportsUpsert()).isTrue();
}

@Test
void getSqlFromDual() {
assertThat(underTest.getSqlFromDual()).isEmpty();
}

@Test
public void postgresql_9_2_is_not_supported() throws Exception {
void postgresql_9_2_is_not_supported() {
assertThatThrownBy(() -> {
DatabaseMetaData metadata = newMetadata(9, 2);
underTest.init(metadata);
})
.isInstanceOf(MessageException.class)
.hasMessage("Unsupported postgresql version: 9.2. Minimal supported version is 9.3.");
}

@Test
public void postgresql_9_3_is_supported_without_upsert() throws Exception {
DatabaseMetaData metadata = newMetadata(9, 3);
underTest.init(metadata);

assertThat(underTest.supportsUpsert()).isFalse();
assertThat(logs.logs(Level.WARN)).contains("Upgrading PostgreSQL to 9.5 or greater is recommended for better performances");
.hasMessage("Unsupported postgresql version: 9.2. Minimal supported version is 11.0.");
}

@Test
public void postgresql_9_5_is_supported_with_upsert() throws Exception {
DatabaseMetaData metadata = newMetadata(9, 5);
void postgresql_11_0_is_supported_with_upsert() throws Exception {
DatabaseMetaData metadata = newMetadata(11, 0);
underTest.init(metadata);

assertThat(underTest.supportsUpsert()).isTrue();
assertThat(logs.logs(Level.WARN)).isEmpty();
}

@Test
public void init_throws_ISE_if_called_twice() throws Exception {
DatabaseMetaData metaData = newMetadata(9, 5);
void init_throws_ISE_if_called_twice() throws Exception {
DatabaseMetaData metaData = newMetadata(11, 0);
underTest.init(metaData);

assertThatThrownBy(() -> underTest.init(metaData))
Expand All @@ -118,28 +115,26 @@ public void init_throws_ISE_if_called_twice() throws Exception {
}

@Test
public void supportsUpsert_throws_ISE_if_not_initialized() {
assertThatThrownBy(() -> underTest.supportsUpsert())
.isInstanceOf(IllegalStateException.class)
.hasMessage("onInit() must be called before calling supportsUpsert()");
void supportsUpsert_returns_true_even_if_not_initialized() {
assertTrue(underTest.supportsUpsert());
}

@Test
public void supportsNullNotDistinct_throws_ISE_if_not_initialized() {
assertThatThrownBy(() -> underTest.supportsNullNotDistinct())
void supportsNullNotDistinct_throws_ISE_if_not_initialized() {
assertThatThrownBy(underTest::supportsNullNotDistinct)
.isInstanceOf(IllegalStateException.class)
.hasMessage("onInit() must be called before calling supportsNullNotDistinct()");
}

@Test
public void supportsNullNotDistinct_shouldReturnTrue_WhenPostgres15OrGreater() throws SQLException {
void supportsNullNotDistinct_shouldReturnTrue_WhenPostgres15OrGreater() throws SQLException {
DatabaseMetaData metadata = newMetadata(15, 0);
underTest.init(metadata);
assertThat(underTest.supportsNullNotDistinct()).isTrue();
}

@Test
public void supportsNullNotDistinct_shouldReturnFalse_WhenPostgres14OrLesser() throws SQLException {
void supportsNullNotDistinct_shouldReturnFalse_WhenPostgres14OrLesser() throws SQLException {
DatabaseMetaData metadata = newMetadata(14, 0);
underTest.init(metadata);
assertThat(underTest.supportsNullNotDistinct()).isFalse();
Expand Down

0 comments on commit d351497

Please sign in to comment.