Testy backendu przechodza - poprawki schematu tylko na PostgreSQL
SchemaFixer wykonuje SQL w skladni PostgreSQL (split_part, DO $$, to_regclass), wiec kazdy @SpringBootTest wywracal sie przy starcie na testowym H2. Teraz runner sprawdza rodzaj bazy i na innej niz PostgreSQL nic nie robi - w testach schemat i tak tworzy Hibernate z encji. mvn test: 22 testy, 0 bledow (bylo 3 bledy). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,10 @@
|
|||||||
package pl.polskalokalnie.config;
|
package pl.polskalokalnie.config;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.boot.ApplicationRunner;
|
import org.springframework.boot.ApplicationRunner;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -8,9 +13,32 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class SchemaFixer {
|
public class SchemaFixer {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(SchemaFixer.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Laty schematu pisane sa skladnia PostgreSQL (DO $$, to_regclass, lo_get, split_part...),
|
||||||
|
* bo taka baza dziala na serwerze. W testach schemat tworzy Hibernate z encji na H2,
|
||||||
|
* wiec nie ma czego latac - bez tego warunku kazdy @SpringBootTest wywracal sie przy starcie.
|
||||||
|
*/
|
||||||
|
private static boolean isPostgres(JdbcTemplate jdbcTemplate) {
|
||||||
|
DataSource dataSource = jdbcTemplate.getDataSource();
|
||||||
|
if (dataSource == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try (Connection connection = dataSource.getConnection()) {
|
||||||
|
return "PostgreSQL".equalsIgnoreCase(connection.getMetaData().getDatabaseProductName());
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
log.warn("Nie udalo sie rozpoznac bazy danych, pomijam poprawki schematu: {}", ex.getMessage());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
ApplicationRunner ensureViewsCountColumn(JdbcTemplate jdbcTemplate) {
|
ApplicationRunner ensureViewsCountColumn(JdbcTemplate jdbcTemplate) {
|
||||||
return args -> {
|
return args -> {
|
||||||
|
if (!isPostgres(jdbcTemplate)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
jdbcTemplate.execute(
|
jdbcTemplate.execute(
|
||||||
"ALTER TABLE property_listings ADD COLUMN IF NOT EXISTS views_count BIGINT NOT NULL DEFAULT 0"
|
"ALTER TABLE property_listings ADD COLUMN IF NOT EXISTS views_count BIGINT NOT NULL DEFAULT 0"
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user