|
1 | 1 | package ca.bc.gov.restapi.results.common.configuration; |
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
3 | 6 | import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer; |
| 7 | +import org.springframework.context.ApplicationContext; |
4 | 8 | import org.springframework.context.annotation.Bean; |
5 | 9 | import org.springframework.context.annotation.Configuration; |
| 10 | +import org.springframework.context.annotation.ImportRuntimeHints; |
6 | 11 | import org.springframework.core.env.Environment; |
7 | 12 |
|
| 13 | +import lombok.extern.slf4j.Slf4j; |
| 14 | + |
| 15 | +/** |
| 16 | + * Configures Flyway migration locations based on the active primary database. |
| 17 | + * |
| 18 | + * <p>In GraalVM native image, @ConditionalOnProperty is evaluated at AOT build time (not |
| 19 | + * runtime), so the PRIMARY_DB value must be set as a Docker build arg so Spring Boot AOT bakes |
| 20 | + * the correct beans into the binary. This class reads PRIMARY_DB at runtime (via |
| 21 | + * Environment.getProperty) which is safe — it's a plain value lookup, not a conditional. |
| 22 | + * |
| 23 | + * <p>Flyway's ClassPathScanner fails in native image with "unsupported protocol: resource". |
| 24 | + * {@link FlywayRuntimeHints} pre-registers migration resources with Spring Boot's AOT processor, |
| 25 | + * and {@link NativeImageResourceProvider} replaces Flyway's scanner with Spring's |
| 26 | + * ApplicationContext.getResources() which uses the AOT-generated resource index. |
| 27 | + */ |
| 28 | +@Slf4j |
8 | 29 | @Configuration |
| 30 | +@ImportRuntimeHints(FlywayRuntimeHints.class) |
9 | 31 | public class FlywayConfiguration { |
10 | 32 |
|
11 | 33 | @Bean |
12 | | - public FlywayConfigurationCustomizer flywayConfigurationCustomizer(Environment env) { |
| 34 | + public FlywayConfigurationCustomizer flywayConfigurationCustomizer( |
| 35 | + Environment env, ApplicationContext context) { |
13 | 36 | return configuration -> { |
14 | | - String primaryDatabase = env.getProperty("server.primary-db", |
15 | | - env.getProperty("PRIMARY_DB", "oracle")); |
16 | | - if (primaryDatabase.equals("oracle")) { |
17 | | - configuration.locations("classpath:db/migration"); |
18 | | - } else if (primaryDatabase.equals("postgres")) { |
19 | | - configuration.locations("classpath:db/migration", "classpath:db/migration-dev"); |
20 | | - } else { |
| 37 | + |
| 38 | + String primaryDb = env.getProperty("PRIMARY_DB", |
| 39 | + env.getProperty("server.primary-db", "oracle")); |
| 40 | + log.info("Configuring Flyway for primary database: {}", primaryDb); |
| 41 | + |
| 42 | + List<String> locations = new ArrayList<>(); |
| 43 | + locations.add("classpath:db/migration"); |
| 44 | + |
| 45 | + if ("postgres".equals(primaryDb)) { |
| 46 | + locations.add("classpath:db/migration-dev"); |
| 47 | + } else if (!"oracle".equals(primaryDb)) { |
21 | 48 | throw new IllegalStateException("Unsupported value for primary database configuration " + |
22 | 49 | "(property 'server.primary-db' or env var 'PRIMARY_DB'): '" + |
23 | | - primaryDatabase + "'. Expected one of: oracle, postgres." |
| 50 | + primaryDb + "'. Expected one of: oracle, postgres." |
24 | 51 | ); |
25 | 52 | } |
| 53 | + |
| 54 | + configuration |
| 55 | + .schemas("silva") |
| 56 | + .defaultSchema("silva") |
| 57 | + .locations(locations.toArray(String[]::new)) |
| 58 | + .resourceProvider(new NativeImageResourceProvider(context, locations)); |
26 | 59 | }; |
27 | 60 | } |
28 | 61 | } |
0 commit comments