Skip to content

Commit cebe3d4

Browse files
authored
Update database migration jar (#1737)
* Update database migration jar LMCROSSITXSADEPLOY-3283
1 parent 950bfd5 commit cebe3d4

File tree

5 files changed

+118
-29
lines changed

5 files changed

+118
-29
lines changed

multiapps-controller-database-migration/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44

55
<artifactId>multiapps-controller-database-migration</artifactId>
@@ -45,6 +45,10 @@
4545
<groupId>org.cloudfoundry.multiapps</groupId>
4646
<artifactId>multiapps-controller-persistence</artifactId>
4747
</dependency>
48+
<dependency>
49+
<groupId>org.cloudfoundry.multiapps</groupId>
50+
<artifactId>multiapps-common</artifactId>
51+
</dependency>
4852
<dependency>
4953
<groupId>io.pivotal.cfenv</groupId>
5054
<artifactId>java-cfenv</artifactId>

multiapps-controller-database-migration/src/main/java/org/cloudfoundry/multiapps/controller/database/migration/DatabaseMigration.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,38 @@
11
package org.cloudfoundry.multiapps.controller.database.migration;
22

3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import javax.sql.DataSource;
6+
37
import org.apache.logging.log4j.LogManager;
48
import org.apache.logging.log4j.core.Logger;
59
import org.apache.logging.log4j.core.LoggerContext;
610
import org.apache.logging.log4j.core.config.ConfigurationSource;
11+
import org.cloudfoundry.multiapps.common.util.JsonUtil;
712
import org.cloudfoundry.multiapps.controller.database.migration.executor.DatabaseSequenceMigrationExecutor;
813
import org.cloudfoundry.multiapps.controller.database.migration.executor.DatabaseTableMigrationExecutor;
914
import org.cloudfoundry.multiapps.controller.database.migration.executor.ImmutableDatabaseSequenceMigrationExecutor;
1015
import org.cloudfoundry.multiapps.controller.database.migration.executor.ImmutableDatabaseTableMigrationExecutor;
1116
import org.cloudfoundry.multiapps.controller.database.migration.extractor.DataSourceEnvironmentExtractor;
12-
13-
import java.io.IOException;
14-
import java.io.InputStream;
15-
import javax.sql.DataSource;
17+
import org.cloudfoundry.multiapps.controller.persistence.dto.DatabaseServiceKey;
1618

1719
public class DatabaseMigration {
1820

1921
private static final Logger LOGGER = (Logger) LogManager.getLogger(DatabaseMigration.class);
22+
private static final String DATABASE_TARGET_SERVICE_KEY = "DATABASE_TARGET_SERVICE_KEY";
2023

2124
public static void main(String[] args) {
2225
configureLogger();
23-
LOGGER.info("Starting database migration...");
26+
DatabaseServiceKey databaseServiceKey = getServiceKeyFromEnvironment();
2427
DataSourceEnvironmentExtractor environmentExtractor = new DataSourceEnvironmentExtractor();
25-
DataSource sourceDataSource = environmentExtractor.extractDataSource("deploy-service-database-source");
2628
DataSource targetDataSource = environmentExtractor.extractDataSource("deploy-service-database");
29+
DataSource sourceDataSource = environmentExtractor.extractDataSource(databaseServiceKey);
2730

2831
DatabaseSequenceMigrationExecutor sequenceMigrationExecutor = ImmutableDatabaseSequenceMigrationExecutor.builder()
29-
.sourceDataSource(sourceDataSource)
30-
.targetDataSource(targetDataSource)
32+
.sourceDataSource(
33+
sourceDataSource)
34+
.targetDataSource(
35+
targetDataSource)
3136
.build();
3237

3338
DatabaseTableMigrationExecutor tableMigrationExecutor = ImmutableDatabaseTableMigrationExecutor.builder()
@@ -36,13 +41,17 @@ public static void main(String[] args) {
3641
.build();
3742
sequenceMigrationExecutor.executeMigration("configuration_entry_sequence");
3843
sequenceMigrationExecutor.executeMigration("configuration_subscription_sequence");
39-
4044
tableMigrationExecutor.executeMigration("configuration_registry");
4145
tableMigrationExecutor.executeMigration("configuration_subscription");
4246

4347
LOGGER.info("Database migration completed.");
4448
}
4549

50+
private static DatabaseServiceKey getServiceKeyFromEnvironment() {
51+
String databaseTargetServiceKey = System.getenv(DATABASE_TARGET_SERVICE_KEY);
52+
return new DatabaseServiceKey(JsonUtil.convertJsonToMap(databaseTargetServiceKey));
53+
}
54+
4655
private static void configureLogger() {
4756
ClassLoader classLoader = DatabaseMigration.class.getClassLoader();
4857
if (classLoader != null) {

multiapps-controller-database-migration/src/main/java/org/cloudfoundry/multiapps/controller/database/migration/extractor/DataSourceEnvironmentExtractor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import javax.sql.DataSource;
44

5+
import io.pivotal.cfenv.jdbc.CfJdbcService;
6+
import org.cloudfoundry.multiapps.controller.persistence.dto.DatabaseServiceKey;
57
import org.cloudfoundry.multiapps.controller.persistence.util.DataSourceFactory;
68
import org.cloudfoundry.multiapps.controller.persistence.util.EnvironmentServicesFinder;
79
import org.slf4j.Logger;
810
import org.slf4j.LoggerFactory;
911

10-
import io.pivotal.cfenv.jdbc.CfJdbcService;
11-
1212
public class DataSourceEnvironmentExtractor {
1313

1414
private static final Logger LOGGER = LoggerFactory.getLogger(DataSourceEnvironmentExtractor.class);
@@ -19,6 +19,10 @@ public DataSource extractDataSource(String serviceName) {
1919
return createDataSource(service);
2020
}
2121

22+
public DataSource extractDataSource(DatabaseServiceKey databaseServiceKey) {
23+
return createDataSource(databaseServiceKey);
24+
}
25+
2226
private CfJdbcService findService(String serviceName) {
2327
return new EnvironmentServicesFinder().findJdbcService(serviceName);
2428
}
@@ -27,4 +31,7 @@ private DataSource createDataSource(CfJdbcService service) {
2731
return new DataSourceFactory().createDataSource(service);
2832
}
2933

34+
private DataSource createDataSource(DatabaseServiceKey databaseServiceKey) {
35+
return new DataSourceFactory().createDataSource(databaseServiceKey);
36+
}
3037
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.cloudfoundry.multiapps.controller.persistence.dto;
2+
3+
import java.text.MessageFormat;
4+
import java.util.Map;
5+
6+
public class DatabaseServiceKey {
7+
8+
private final Map<String, Object> credentials;
9+
private static final String JDBC_URI_TEMPLATE = "jdbc:postgresql://{0}:{1}/{2}?user={3}&password={4}";
10+
11+
public DatabaseServiceKey(Map<String, Object> credentials) {
12+
this.credentials = credentials;
13+
}
14+
15+
public String getUsername() {
16+
return getValueFromCredentials("username");
17+
}
18+
19+
public String getPassword() {
20+
return getValueFromCredentials("password");
21+
}
22+
23+
public String getJdbcUri() {
24+
return MessageFormat.format(JDBC_URI_TEMPLATE, getHostname(), getPort(), getDbName(), getUsername(), getPassword());
25+
}
26+
27+
private String getHostname() {
28+
return getValueFromCredentials("hostname");
29+
}
30+
31+
private String getPort() {
32+
return getValueFromCredentials("port");
33+
}
34+
35+
private String getDbName() {
36+
return getValueFromCredentials("dbname");
37+
}
38+
39+
private String getValueFromCredentials(String key) {
40+
return getCredentialsObject().get(key)
41+
.toString();
42+
}
43+
44+
private Map<String, Object> getCredentialsObject() {
45+
return (Map<String, Object>) credentials.get("credentials");
46+
}
47+
}
Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,61 @@
11
package org.cloudfoundry.multiapps.controller.persistence.util;
22

33
import java.nio.file.Path;
4-
54
import javax.sql.DataSource;
65

7-
import org.cloudfoundry.multiapps.controller.persistence.Constants;
8-
96
import com.zaxxer.hikari.HikariConfig;
107
import com.zaxxer.hikari.HikariDataSource;
11-
128
import io.pivotal.cfenv.jdbc.CfJdbcService;
139
import jakarta.inject.Named;
10+
import org.apache.logging.log4j.LogManager;
11+
import org.apache.logging.log4j.core.Logger;
12+
import org.cloudfoundry.multiapps.controller.persistence.Constants;
13+
import org.cloudfoundry.multiapps.controller.persistence.dto.DatabaseServiceKey;
1414

1515
@Named
1616
public class DataSourceFactory {
1717

18+
private static final Logger LOGGER = (Logger) LogManager.getLogger(DataSourceFactory.class);
19+
1820
public DataSource createDataSource(CfJdbcService service) {
1921
return createDataSource(service, null, null);
2022
}
2123

24+
public DataSource createDataSource(DatabaseServiceKey databaseServiceKey) {
25+
DatabaseConfig databaseConfig = createDatabaseConfigFromDatabaseServiceKey(databaseServiceKey);
26+
return new HikariDataSource(createHikariConfig(databaseConfig, null, "null"));
27+
}
28+
2229
public DataSource createDataSource(CfJdbcService service, Integer maximumPoolSize, String appInstanceTemplate) {
23-
return new HikariDataSource(createHikariConfig(service, maximumPoolSize, appInstanceTemplate));
30+
DatabaseConfig databaseConfig = createDatabaseConfigFromCfJdbcService(service);
31+
return new HikariDataSource(createHikariConfig(databaseConfig, maximumPoolSize, appInstanceTemplate));
2432
}
2533

26-
private HikariConfig createHikariConfig(CfJdbcService service, Integer maximumPoolSize, String appInstanceTemplate) {
34+
private HikariConfig createHikariConfig(DatabaseConfig databaseConfig, Integer maximumPoolSize, String appInstanceTemplate) {
2735
HikariConfig hikariConfig = new HikariConfig();
28-
hikariConfig.setUsername(service.getUsername());
29-
hikariConfig.setPassword(service.getPassword());
30-
hikariConfig.setJdbcUrl(service.getJdbcUrl());
36+
hikariConfig.setUsername(databaseConfig.username);
37+
hikariConfig.setPassword(databaseConfig.password);
38+
hikariConfig.setJdbcUrl(databaseConfig.jdbcUrl);
3139
hikariConfig.setConnectionTimeout(60000);
3240
hikariConfig.setIdleTimeout(60000);
3341
hikariConfig.setMinimumIdle(10);
3442
hikariConfig.addDataSourceProperty("tcpKeepAlive", true);
35-
hikariConfig.addDataSourceProperty("ApplicationName", appInstanceTemplate);
3643

37-
configureSSLClientKeyIfExists(service, hikariConfig);
44+
configureSSLClientKeyIfExists(databaseConfig, hikariConfig);
3845

46+
if (appInstanceTemplate != null) {
47+
hikariConfig.addDataSourceProperty("ApplicationName", appInstanceTemplate);
48+
}
3949
if (maximumPoolSize != null) {
4050
hikariConfig.setMaximumPoolSize(maximumPoolSize);
4151
}
4252
hikariConfig.setRegisterMbeans(true);
4353
return hikariConfig;
4454
}
4555

46-
private void configureSSLClientKeyIfExists(CfJdbcService service, HikariConfig hikariConfig) {
47-
String clientKey = (String) service.getCredentials()
48-
.getMap()
49-
.get("sslkey");
50-
if (clientKey != null) {
51-
configureClientCertificate(clientKey, hikariConfig);
56+
private void configureSSLClientKeyIfExists(DatabaseConfig service, HikariConfig hikariConfig) {
57+
if (service.sslKey != null) {
58+
configureClientCertificate(service.sslKey, hikariConfig);
5259
}
5360
}
5461

@@ -58,4 +65,19 @@ private void configureClientCertificate(String clientKey, HikariConfig hikariCon
5865
hikariConfig.addDataSourceProperty("sslkey", encodedKeyPath.toAbsolutePath());
5966
}
6067

68+
private DatabaseConfig createDatabaseConfigFromCfJdbcService(CfJdbcService service) {
69+
String clientKey = (String) service.getCredentials()
70+
.getMap()
71+
.get("sslkey");
72+
return new DatabaseConfig(service.getUsername(), service.getPassword(), service.getJdbcUrl(), clientKey);
73+
}
74+
75+
private DatabaseConfig createDatabaseConfigFromDatabaseServiceKey(DatabaseServiceKey databaseServiceKey) {
76+
return new DatabaseConfig(databaseServiceKey.getUsername(), databaseServiceKey.getPassword(), databaseServiceKey.getJdbcUri(),
77+
null);
78+
}
79+
80+
private record DatabaseConfig(String username, String password, String jdbcUrl, String sslKey) {
81+
}
82+
6183
}

0 commit comments

Comments
 (0)