RED-4515: Create schema on createTenant
This commit is contained in:
parent
94aa4287ba
commit
8a0591d4b9
@ -28,6 +28,8 @@ import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.StatementCallback;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@ -113,7 +115,9 @@ public class TenantManagementService {
|
||||
|
||||
if (tenantRepository.findById(tenantRequest.getTenantId()).isEmpty()) {
|
||||
|
||||
var jdbcUrl = JDBCUtils.buildJdbcUrl(tenantRequest.getDatabaseConnection());
|
||||
createSchema(tenantRequest);
|
||||
|
||||
var jdbcUrl = JDBCUtils.buildJdbcUrlWithSchema(tenantRequest.getDatabaseConnection());
|
||||
validateJdbcUrl(jdbcUrl);
|
||||
|
||||
try (Connection connection = DriverManager.getConnection(jdbcUrl,
|
||||
@ -199,6 +203,23 @@ public class TenantManagementService {
|
||||
}
|
||||
|
||||
|
||||
|
||||
private void createSchema(TenantRequest tenantRequest){
|
||||
|
||||
var jdbcUrl = JDBCUtils.buildJdbcUrl(tenantRequest.getDatabaseConnection());
|
||||
try (Connection connection = DriverManager.getConnection(jdbcUrl,
|
||||
tenantRequest.getDatabaseConnection().getUsername(),
|
||||
tenantRequest.getDatabaseConnection().getPassword())) {
|
||||
DataSource tenantDataSource = new SingleConnectionDataSource(connection, false);
|
||||
JdbcTemplate jdbcTemplate = new JdbcTemplate(tenantDataSource);
|
||||
jdbcTemplate.execute((StatementCallback<Boolean>) stmt -> stmt.execute("CREATE SCHEMA " + tenantRequest.getDatabaseConnection().getSchema()));
|
||||
jdbcTemplate.execute((StatementCallback<Boolean>) stmt -> stmt.execute("GRANT USAGE ON SCHEMA " + tenantRequest.getDatabaseConnection().getSchema() + " TO " + tenantRequest.getDatabaseConnection().getUsername()));
|
||||
} catch (Exception e) {
|
||||
log.info("Could not create schema, ignoring");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private boolean tryToAccessRealm(String tenantId) {
|
||||
|
||||
try {
|
||||
|
||||
@ -28,6 +28,23 @@ public class JDBCUtils {
|
||||
|
||||
|
||||
public String buildJdbcUrl(DatabaseConnection databaseConnection){
|
||||
StringBuilder sb = new StringBuilder("jdbc:")
|
||||
.append(databaseConnection.getDriver())
|
||||
.append("://")
|
||||
.append(databaseConnection.getHost())
|
||||
.append(':')
|
||||
.append(databaseConnection.getPort())
|
||||
.append('/')
|
||||
.append(databaseConnection.getDatabase());
|
||||
if(databaseConnection.getParams() != null) {
|
||||
sb.append('?');
|
||||
databaseConnection.getParams().forEach((k, v) -> sb.append('&').append(k).append(v));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
public String buildJdbcUrlWithSchema(DatabaseConnection databaseConnection){
|
||||
StringBuilder sb = new StringBuilder("jdbc:")
|
||||
.append(databaseConnection.getDriver())
|
||||
.append("://")
|
||||
|
||||
@ -2,8 +2,6 @@ package com.iqser.red.service.peristence.v1.server.integration.utils;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
@ -37,7 +35,6 @@ import org.springframework.context.annotation.Import;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.StatementCallback;
|
||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
@ -103,7 +100,6 @@ import com.iqser.red.storage.commons.StorageAutoConfiguration;
|
||||
import com.iqser.red.storage.commons.service.StorageService;
|
||||
|
||||
import io.micrometer.prometheus.PrometheusMeterRegistry;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@ -312,7 +308,6 @@ public abstract class AbstractPersistenceServerServiceTest {
|
||||
var port = postgreSQLContainerMaster.getJdbcUrl().substring(0, postgreSQLContainerMaster.getJdbcUrl().lastIndexOf('/')).split(":")[3];
|
||||
|
||||
createDatabase("redaction", "redaction");
|
||||
createSchema(jdbcUrl, "redaction", "redaction");
|
||||
|
||||
var tenantRequest = TenantRequest.builder()
|
||||
.tenantId("redaction")
|
||||
@ -360,18 +355,6 @@ public abstract class AbstractPersistenceServerServiceTest {
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
public void createSchema(String jdbcUrl, String username, String password) {
|
||||
|
||||
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
|
||||
DataSource tenantDataSource = new SingleConnectionDataSource(connection, false);
|
||||
JdbcTemplate insert = new JdbcTemplate(tenantDataSource);
|
||||
insert.execute((StatementCallback<Boolean>) stmt -> stmt.execute("CREATE SCHEMA myschema"));
|
||||
insert.execute((StatementCallback<Boolean>) stmt -> stmt.execute("GRANT USAGE ON SCHEMA myschema TO " + username));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@AfterEach
|
||||
public void cleanupStorage() {
|
||||
|
||||
@ -448,7 +431,9 @@ public abstract class AbstractPersistenceServerServiceTest {
|
||||
TestPropertyValues.of("spring.redis.port=" + redisContainer.getFirstMappedPort(),
|
||||
"multitenancy.master.datasource.url=" + postgreSQLContainerMaster.getJdbcUrl() + connectionStringDetails,
|
||||
"multitenancy.master.datasource.username=" + postgreSQLContainerMaster.getUsername(),
|
||||
"multitenancy.master.datasource.password=" + postgreSQLContainerMaster.getPassword(), "keycloak.auth-server-url=" + kcInstance.getAuthServerUrl(), "commons.keycloak.serverUrl=" + kcInstance.getAuthServerUrl()).applyTo(configurableApplicationContext.getEnvironment());
|
||||
"multitenancy.master.datasource.password=" + postgreSQLContainerMaster.getPassword(),
|
||||
"keycloak.auth-server-url=" + kcInstance.getAuthServerUrl(),
|
||||
"commons.keycloak.serverUrl=" + kcInstance.getAuthServerUrl()).applyTo(configurableApplicationContext.getEnvironment());
|
||||
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user