mfvanek / pg-index-health

pg-index-health is a Java library for analyzing and maintaining indexes and tables health in Postgresql databases.
Apache License 2.0
129 stars 15 forks source link

[pg-index-health-test-starter] Add ability to customize name of dataSource bean and databaseUrl #521

Open mfvanek opened 1 day ago

mfvanek commented 1 day ago

See https://github.com/mfvanek/pg-index-health/blob/006fd3db74fc677411c8843084aa8b3fac3ec6d7/spring-boot-integration/pg-index-health-test-starter/src/main/java/io/github/mfvanek/pg/spring/DatabaseStructureHealthAutoConfiguration.java#L59

    @ConditionalOnBean(name = "dataSource")
    @ConditionalOnMissingBean
    public PgConnection pgConnection(@Qualifier("dataSource") final DataSource dataSource,
                                     @Value("${spring.datasource.url:#{null}}") final String databaseUrl) {

In some projects bean might be customized very much

    @Bean
    @ConditionalOnBean(name = ["mainDataSource"])
    fun pgConnection(@Qualifier("mainDataSource") dataSource: DataSource,
                     @Value("\${datasource.jdbc-url:#{null}}") databaseUrl: String?): PgConnection {

Need a way to do it without copying whole code for pgConnection bean

mfvanek commented 1 day ago

An additional special constructor for PgConnection? https://github.com/mfvanek/pg-index-health/blob/f1958cf4466217199885b3fe1ad427089842780e/pg-index-health-jdbc-connection/src/main/java/io/github/mfvanek/pg/connection/PgConnectionImpl.java#L63

mfvanek commented 1 day ago

Relates to https://github.com/mfvanek/pg-index-health/issues/517

mfvanek commented 5 hours ago

Qualifier annotation cannot use SpEL. Only way to use context/beanFactory

@Bean
@ConditionalOnProperty(name = "custom.datasource.bean-name")
@ConditionalOnMissingBean
public PgConnection pgConnection(
        @Value("${custom.datasource.bean-name:dataSource}") String dataSourceBeanName,
        ConfigurableListableBeanFactory beanFactory,
        @Value("${spring.datasource.url:#{null}}") String databaseUrl) {
    DataSource dataSource = beanFactory.getBean(dataSourceBeanName, DataSource.class);
    return PgConnectionImpl.ofUrl(dataSource, databaseUrl);
}