Skip to content

Commit

Permalink
remove mysql r2dbc support
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeWang1127 committed Dec 2, 2022
1 parent 8fc4c02 commit 0cb8ffe
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 108 deletions.
15 changes: 1 addition & 14 deletions spring-cloud-gcp-autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,20 +117,7 @@
<scope>test</scope>
</dependency>

<!--R2DBC : mySQL connector and driver -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>cloud-sql-connector-r2dbc-mysql</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<optional>true</optional>
</dependency>


<!--R2DBC : PostgreSQL connector and driver -->
<!--R2DBC : PostgresSQL connector and driver -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>cloud-sql-connector-r2dbc-postgres</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,21 @@ String createUrl(DatabaseType databaseType, GcpCloudSqlProperties sqlProperties)
}

/**
* Returns {@link DatabaseType} constant based on whether mySQL or postgreSQL R2DBC driver and
* Returns {@link DatabaseType} constant based on whether postgresSQL R2DBC driver and
* connector dependencies are present on the classpath. Returns null if Cloud SQL is not enabled
* in Spring Cloud GCP, CredentialFactory is not present or ConnectionFactory (which is used to
* enable Spring R2DBC auto-configuration) is not present.
* enable Spring R2DBC autoconfiguration) is not present.
*
* @param environment environment to post-process
* @return database type
*/
DatabaseType getEnabledDatabaseType(ConfigurableEnvironment environment) {
if (isR2dbcEnabled(environment)
&& isOnClasspath("com.google.cloud.sql.CredentialFactory")
&& isOnClasspath("io.r2dbc.spi.ConnectionFactory")) {
if (isOnClasspath("com.google.cloud.sql.core.GcpConnectionFactoryProviderMysql")
&& isOnClasspath("dev.miku.r2dbc.mysql.MySqlConnectionFactoryProvider")) {
return DatabaseType.MYSQL;
} else if (isOnClasspath("com.google.cloud.sql.core.GcpConnectionFactoryProviderPostgres")
&& isOnClasspath("io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider")) {
return DatabaseType.POSTGRESQL;
}
&& isOnClasspath("io.r2dbc.spi.ConnectionFactory")
&& isOnClasspath("com.google.cloud.sql.core.GcpConnectionFactoryProviderPostgres")
&& isOnClasspath("io.r2dbc.postgresql.PostgresqlConnectionFactoryProvider")) {
return DatabaseType.POSTGRESQL;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.springframework.boot.SpringApplication;
Expand All @@ -29,7 +30,7 @@
/** Tests for {@link R2dbcCloudSqlEnvironmentPostProcessor}. */
class R2dbcCloudSqlEnvironmentPostProcessorTests {

private R2dbcCloudSqlEnvironmentPostProcessor r2dbcPostProcessor =
private final R2dbcCloudSqlEnvironmentPostProcessor r2dbcPostProcessor =
new R2dbcCloudSqlEnvironmentPostProcessor();

ApplicationContextRunner contextRunner =
Expand All @@ -39,15 +40,6 @@ class R2dbcCloudSqlEnvironmentPostProcessorTests {
r2dbcPostProcessor.postProcessEnvironment(
configurableApplicationContext.getEnvironment(), new SpringApplication()));

@Test
void testCreateUrl_mySql() {
GcpCloudSqlProperties properties = new GcpCloudSqlProperties();
properties.setDatabaseName("my-database");
properties.setInstanceConnectionName("my-instance-connection-name");
String r2dbcUrl = r2dbcPostProcessor.createUrl(DatabaseType.MYSQL, properties);
assertThat(r2dbcUrl).isEqualTo("r2dbc:gcp:mysql://my-instance-connection-name/my-database");
}

@Test
void testCreateUrl_postgres() {
GcpCloudSqlProperties properties = new GcpCloudSqlProperties();
Expand All @@ -57,65 +49,13 @@ void testCreateUrl_postgres() {
assertThat(r2dbcUrl).isEqualTo("r2dbc:gcp:postgres://my-instance-connection-name/my-database");
}

@Test
void testSetR2dbcProperty_mySql_defaultUsername() {
this.contextRunner
.withPropertyValues(
"spring.cloud.gcp.sql.databaseName=my-database",
"spring.cloud.gcp.sql.instanceConnectionName=my-project:region:my-instance")
.run(
context -> {
assertThat(context.getEnvironment().getProperty("spring.r2dbc.url"))
.isEqualTo("r2dbc:gcp:mysql://my-project:region:my-instance/my-database");
assertThat(context.getEnvironment().getProperty("spring.r2dbc.username"))
.isEqualTo("root");
});
}

@Test
void testSetR2dbcProperty_mySql_usernameProvided() {
this.contextRunner
.withPropertyValues(
"spring.cloud.gcp.sql.databaseName=my-database",
"spring.cloud.gcp.sql.instanceConnectionName=my-project:region:my-instance",
"spring.r2dbc.username=my-username")
.run(
context -> {
assertThat(context.getEnvironment().getProperty("spring.r2dbc.url"))
.isEqualTo("r2dbc:gcp:mysql://my-project:region:my-instance/my-database");
assertThat(context.getEnvironment().getProperty("spring.r2dbc.username"))
.isEqualTo("my-username");
});
}

@Test
void testSetR2dbcProperty_mySql_urlProvidedByUserIgnored() {
this.contextRunner
.withPropertyValues(
"spring.cloud.gcp.sql.databaseName=my-database",
"spring.cloud.gcp.sql.instanceConnectionName=my-project:region:my-instance",
"spring.r2dbc.url=ignored")
.run(
context -> {
assertThat(context.getEnvironment().getProperty("spring.r2dbc.url"))
.isEqualTo("r2dbc:gcp:mysql://my-project:region:my-instance/my-database");
});
}

@Test
void testSetR2dbcProperty_postgres() {
verifyThatCorrectUrlAndUsernameSet(new String[] {"io.r2dbc.postgresql"},
"postgres",
"r2dbc:gcp:postgres://my-project:region:my-instance/my-database");
}

@Test
void testSetR2dbcProperty_mysql() {
verifyThatCorrectUrlAndUsernameSet(new String[] {"dev.miku.r2dbc.mysql"},
"root",
"r2dbc:gcp:mysql://my-project:region:my-instance/my-database");
}

/**
* Verifies that correct database properties got injected into context, given a passed-in list of
* packages to retain on the classpath.
Expand All @@ -125,14 +65,13 @@ void testSetR2dbcProperty_mysql() {
* @param url expected {@code spring.r2dbc.username} value to verify
*/
private void verifyThatCorrectUrlAndUsernameSet(
String[] driverPackagesToInclude, String username, String url) {
String[] driverPackagesToInclude,
String username,
String url) {
// Because `FilteredClassLoader` accepts a list of packages to remove from classpath,
// `driverPackagesToInclude` is used to calculate the inverse list of packages to _exclude_.
Set<String> driverPackagesToExclude = new HashSet<>(Arrays.asList(
"dev.miku.r2dbc.mysql",
"io.r2dbc.postgresql"
));
driverPackagesToExclude.removeAll(Arrays.asList(driverPackagesToInclude));
Set<String> driverPackagesToExclude = new HashSet<>(List.of("io.r2dbc.postgresql"));
Arrays.asList(driverPackagesToInclude).forEach(driverPackagesToExclude::remove);

this.contextRunner
.withPropertyValues(
Expand Down
16 changes: 0 additions & 16 deletions spring-cloud-gcp-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -271,22 +271,6 @@
<version>0.8.12.RELEASE</version>
</dependency>

<!-- spring-cloud-gcp-starter-sql-mysql-r2dbc -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
<artifactId>cloud-sql-connector-r2dbc-mysql</artifactId>
<version>${cloud-sql-socket-factory.version}</version>
</dependency>
<!-- Spring Boot 2.7 no longer manages this dependency.
Replace with MariaDB driver once it's supported.
https://github.com/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory/issues/990
-->
<dependency>
<groupId>dev.miku</groupId>
<artifactId>r2dbc-mysql</artifactId>
<version>${r2dbc-mysql.version}</version>
</dependency>

<!-- spring-cloud-gcp-starter-sql-postgres-r2dbc -->
<dependency>
<groupId>com.google.cloud.sql</groupId>
Expand Down

0 comments on commit 0cb8ffe

Please sign in to comment.