RED-9658: Fixed bug in mongo on the fly migration

This commit is contained in:
Dominique Eifländer 2024-07-15 15:31:26 +02:00
parent fc3e44b66e
commit 4d8cbfb2a7

View File

@ -20,6 +20,7 @@ import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
// This is just for migration from 4.0 to 4.1 and can be removed afterward.
@ -50,7 +51,7 @@ public class MongoDbOntheFlyMigrationService {
@Transactional
public TenantEntity updateMongoDatabaseConnection(TenantEntity tenantEntity) {
if ((tenantEntity.getMongoDBConnection() == null || tenantEntity.getMongoDBConnection().getUsername() == null) && defaultMongoDBUsername != null) {
if ((tenantEntity.getMongoDBConnection() == null || tenantEntity.getMongoDBConnection().getUsername() == null) && defaultMongoDBUsername != null && !defaultMongoDBUsername.isEmpty()) {
tenantEntity.setMongoDBConnection(MongoDBConnectionEntity.builder()
.prefix(defaultMongoDBPrefix)
.username(defaultMongoDBUsername)
@ -62,6 +63,7 @@ public class MongoDbOntheFlyMigrationService {
tenantRepository.save(tenantEntity);
createMongoDBDatabase(convert(tenantEntity.getMongoDBConnection()));
}
return tenantEntity;
}
@ -87,14 +89,16 @@ public class MongoDbOntheFlyMigrationService {
private void createMongoDBDatabase(MongoDBConnection mongoDBConnection) {
mongoDBConnection.setPassword(encryptionService.decrypt(mongoDBConnection.getPassword()));
try (MongoClient mongoClient = MongoClients.create(MongoConnectionStringHelper.buildGenericMongoConnectionString(mongoDBConnection))) {
String databaseName = mongoDBConnection.getDatabase();
String username = mongoDBConnection.getUsername();
String password = defaultMongoDBPassword;
MongoDatabase database = mongoClient.getDatabase(databaseName);
BsonDocument createUserCommand = new BsonDocument();
createUserCommand.append("createUser", new BsonString(username));
createUserCommand.append("pwd", new BsonString(password));
createUserCommand.append("pwd", new BsonString(mongoDBConnection.getPassword()));
BsonArray roles = new BsonArray();
roles.add(new BsonDocument("role", new BsonString("dbOwner")).append("db", new BsonString(databaseName)));
createUserCommand.append("roles", roles);