added run liquibase on initialization logic for tenant creation
This commit is contained in:
parent
4ae2118372
commit
d9177d9cac
@ -12,7 +12,7 @@ import com.knecon.fforesight.tenantcommons.TenantProvider;
|
||||
public class MongoLiquibaseConfig {
|
||||
|
||||
@Bean
|
||||
@ConfigurationProperties("multitenancy.tenant.liquibase")
|
||||
@ConfigurationProperties("multitenancy.tenant.mongo.liquibase")
|
||||
public LiquibaseProperties tenantLiquibaseProperties() {
|
||||
|
||||
return new LiquibaseProperties();
|
||||
|
||||
@ -0,0 +1,14 @@
|
||||
package com.knecon.fforesight.mongo.database.commons.liquibase;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class TenantCreatedEvent {
|
||||
|
||||
private String tenantId;
|
||||
|
||||
}
|
||||
@ -0,0 +1,37 @@
|
||||
package com.knecon.fforesight.mongo.database.commons.liquibase;
|
||||
|
||||
import org.springframework.amqp.rabbit.annotation.RabbitListener;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TenantCreatedListener {
|
||||
|
||||
private final TenantMongoLiquibaseExecutor tenantMongoLiquibaseExecutor;
|
||||
|
||||
@Value("${fforesight.multitenancy.tenant-created-queue:tenant-created}")
|
||||
private String tenantCreatedQueue;
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void postConstruct() {
|
||||
|
||||
log.info("Listener for tenant-created started for queue: {}", tenantCreatedQueue);
|
||||
}
|
||||
|
||||
|
||||
@SneakyThrows
|
||||
@RabbitListener(queues = "${fforesight.multitenancy.tenant-created-queue:tenant-created}")
|
||||
public void createTenant(TenantCreatedEvent tenantRequest) {
|
||||
|
||||
tenantMongoLiquibaseExecutor.initializeTenant(tenantRequest.getTenantId());
|
||||
}
|
||||
|
||||
}
|
||||
@ -49,28 +49,38 @@ public class TenantMongoLiquibaseExecutor implements InitializingBean {
|
||||
@SneakyThrows
|
||||
protected void runOnAllTenants(List<TenantResponse> tenants) {
|
||||
|
||||
for (var tenant : tenants) {
|
||||
tenants.forEach(this::runLiquibase);
|
||||
}
|
||||
|
||||
MongoDBConnection mongoDBConnection = tenant.getMongoDBConnection();
|
||||
var mongoUrl = MongoUtils.buildMongoUrl(mongoDBConnection);
|
||||
log.info("Initializing MongoDB liquibase for tenant {} / {}", tenant.getTenantId(), mongoUrl);
|
||||
|
||||
try (MongoLiquibaseDatabase database = (MongoLiquibaseDatabase) DatabaseFactory.getInstance()
|
||||
.openDatabase(mongoUrl, mongoDBConnection.getUsername(), encryptionService.decrypt(mongoDBConnection.getPassword()), null, null)) {
|
||||
private void runLiquibase(TenantResponse tenant) {
|
||||
|
||||
try (Liquibase liquibase = new Liquibase(tenantLiquibaseProperties.getChangeLog(), new ClassLoaderResourceAccessor(), database)) {
|
||||
Contexts contexts = new Contexts(tenantLiquibaseProperties.getContexts());
|
||||
List<ChangeSet> changeSetsList = liquibase.listUnrunChangeSets(contexts, null);
|
||||
if (!changeSetsList.isEmpty()) {
|
||||
liquibase.update(contexts);
|
||||
}
|
||||
MongoDBConnection mongoDBConnection = tenant.getMongoDBConnection();
|
||||
var mongoUrl = MongoUtils.buildMongoUrl(mongoDBConnection);
|
||||
log.info("Initializing MongoDB liquibase for tenant {} / {}", tenant.getTenantId(), mongoUrl);
|
||||
|
||||
try (MongoLiquibaseDatabase database = (MongoLiquibaseDatabase) DatabaseFactory.getInstance()
|
||||
.openDatabase(mongoUrl, mongoDBConnection.getUsername(), encryptionService.decrypt(mongoDBConnection.getPassword()), null, null)) {
|
||||
|
||||
try (Liquibase liquibase = new Liquibase(tenantLiquibaseProperties.getChangeLog(), new ClassLoaderResourceAccessor(), database)) {
|
||||
Contexts contexts = new Contexts(tenantLiquibaseProperties.getContexts());
|
||||
List<ChangeSet> changeSetsList = liquibase.listUnrunChangeSets(contexts, null);
|
||||
if (!changeSetsList.isEmpty()) {
|
||||
liquibase.update(contexts);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to run liquibase migration on MongoDB for tenant: {}", tenant.getTenantId(), e);
|
||||
}
|
||||
log.info("Liquibase ran on MongoDB for tenant " + tenant.getTenantId());
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to run liquibase migration on MongoDB for tenant: {}", tenant.getTenantId(), e);
|
||||
}
|
||||
log.info("Liquibase ran on MongoDB for tenant " + tenant.getTenantId());
|
||||
}
|
||||
|
||||
|
||||
public void initializeTenant(String tenantId) {
|
||||
|
||||
runLiquibase(tenantProvider.getTenant(tenantId));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user