feat: Add H2 in-memory database dependency for fallback scenarios.
This commit is contained in:
@@ -43,6 +43,13 @@
|
|||||||
<groupId>org.springframework.security</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>spring-security-crypto</artifactId>
|
<artifactId>spring-security-crypto</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- H2 in-memory DB (fallback когда нет настроенных тенантов) -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.magistr.app.config.tenant;
|
|||||||
|
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
@@ -24,6 +25,9 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* Конфигурация мультитенантного DataSource.
|
* Конфигурация мультитенантного DataSource.
|
||||||
* Загружает тенанты из JSON-файла и регистрирует TenantInterceptor.
|
* Загружает тенанты из JSON-файла и регистрирует TenantInterceptor.
|
||||||
|
*
|
||||||
|
* Если нет ни одного настроенного тенанта — создаёт H2 in-memory БД
|
||||||
|
* как заглушку, чтобы Spring JPA мог инициализироваться.
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class TenantDataSourceConfig implements WebMvcConfigurer {
|
public class TenantDataSourceConfig implements WebMvcConfigurer {
|
||||||
@@ -68,10 +72,18 @@ public class TenantDataSourceConfig implements WebMvcConfigurer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Если всё ещё нет ни одного тенанта — H2 in-memory заглушка
|
||||||
if (routingDataSource.getTenantConfigs().isEmpty()) {
|
if (routingDataSource.getTenantConfigs().isEmpty()) {
|
||||||
log.warn("=== НЕТ НАСТРОЕННЫХ ТЕНАНТОВ ===");
|
log.warn("=== НЕТ НАСТРОЕННЫХ ТЕНАНТОВ ===");
|
||||||
log.warn("Backend запустится, но API не будет работать без подключения к БД.");
|
log.warn("Создаём H2 in-memory заглушку для запуска приложения.");
|
||||||
log.warn("Добавьте тенант через POST /api/database/tenants или настройте tenants.json");
|
log.warn("Добавьте тенант через POST /api/database/tenants");
|
||||||
|
|
||||||
|
TenantConfig h2Fallback = new TenantConfig(
|
||||||
|
"H2 Placeholder", "default",
|
||||||
|
"jdbc:h2:mem:placeholder;DB_CLOSE_DELAY=-1",
|
||||||
|
"sa", ""
|
||||||
|
);
|
||||||
|
routingDataSource.addTenant(h2Fallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
return routingDataSource;
|
return routingDataSource;
|
||||||
@@ -91,7 +103,6 @@ public class TenantDataSourceConfig implements WebMvcConfigurer {
|
|||||||
|
|
||||||
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
|
||||||
vendorAdapter.setGenerateDdl(true);
|
vendorAdapter.setGenerateDdl(true);
|
||||||
vendorAdapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
|
|
||||||
em.setJpaVendorAdapter(vendorAdapter);
|
em.setJpaVendorAdapter(vendorAdapter);
|
||||||
|
|
||||||
Map<String, Object> props = new HashMap<>();
|
Map<String, Object> props = new HashMap<>();
|
||||||
|
|||||||
Reference in New Issue
Block a user