-
Notifications
You must be signed in to change notification settings - Fork 793
fix: EXPOSED-1004 SpringBoot-Starter Run DatabaseInitializer DDL during bean initialization #2820
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Jiwoo Kim (zbqmgldjfh)
wants to merge
3
commits into
JetBrains:main
Choose a base branch
from
zbqmgldjfh:zbqmgldjfh/exposed-1004
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
b7b37bf
fix: EXPOSED-1004 Execute DatabaseInitializer DDL during bean init ph…
zbqmgldjfh 2fbc080
fix: EXPOSED-1004 Port DatabaseInitializer InitializingBean fix to sp…
zbqmgldjfh 34e81e5
docs: EXPOSED-1004 Update Spring Boot guide and sample to Initializin…
zbqmgldjfh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
.../org/jetbrains/exposed/v1/spring/boot/autoconfigure/ExposedDatabaseInitializerDetector.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package org.jetbrains.exposed.v1.spring.boot.autoconfigure | ||
|
|
||
| import org.jetbrains.exposed.v1.spring.boot.DatabaseInitializer | ||
| import org.springframework.beans.factory.config.ConfigurableListableBeanFactory | ||
| import org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector | ||
|
|
||
| /** | ||
| * Registers Exposed's [DatabaseInitializer] as a database initializer bean for Spring Boot's automatic | ||
| * dependency ordering, so that beans annotated with | ||
| * [org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitialization] are initialized after the | ||
| * schema has been created by [DatabaseInitializer.afterPropertiesSet]. | ||
| */ | ||
| class ExposedDatabaseInitializerDetector : DatabaseInitializerDetector { | ||
|
|
||
| override fun detect(beanFactory: ConfigurableListableBeanFactory): Set<String> { | ||
| return beanFactory.getBeanNamesForType( | ||
| DatabaseInitializer::class.java, true, false | ||
| ).toSet() | ||
| } | ||
| } |
2 changes: 2 additions & 0 deletions
2
exposed-spring-boot-starter/src/main/resources/META-INF/spring.factories
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector=\ | ||
| org.jetbrains.exposed.v1.spring.boot.autoconfigure.ExposedDatabaseInitializerDetector |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...in/org/jetbrains/exposed/v1/spring/boot/autoconfigure/DatabaseInitializerEarlyInitTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package org.jetbrains.exposed.v1.spring.boot.autoconfigure | ||
|
|
||
| import org.jetbrains.exposed.v1.jdbc.selectAll | ||
| import org.jetbrains.exposed.v1.spring.boot.Application | ||
| import org.jetbrains.exposed.v1.spring.boot.DatabaseInitializer | ||
| import org.jetbrains.exposed.v1.spring.boot.tables.TestTable | ||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
| import org.springframework.beans.factory.InitializingBean | ||
| import org.springframework.beans.factory.annotation.Autowired | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty | ||
| import org.springframework.boot.sql.init.dependency.DependsOnDatabaseInitialization | ||
| import org.springframework.boot.test.context.SpringBootTest | ||
| import org.springframework.boot.test.context.TestConfiguration | ||
| import org.springframework.context.annotation.Bean | ||
| import org.springframework.transaction.PlatformTransactionManager | ||
| import org.springframework.transaction.support.TransactionTemplate | ||
|
|
||
| /** | ||
| * @author zbqmgldjfh@gmail.com | ||
| * Verifies that [DatabaseInitializer] is registered as a database initializer via Spring Boot's | ||
| * [DatabaseInitializerDetector][org.springframework.boot.sql.init.dependency.DatabaseInitializerDetector] SPI, | ||
| * so that beans annotated with [DependsOnDatabaseInitialization] automatically wait for DDL to complete | ||
| * without requiring explicit `@DependsOn("databaseInitializer")`. | ||
| */ | ||
| @SpringBootTest( | ||
| classes = [Application::class, DatabaseInitializerEarlyInitTest.EarlyInitConfig::class], | ||
| properties = [ | ||
| "spring.datasource.url=jdbc:h2:mem:test-early-init;DB_CLOSE_DELAY=-1", | ||
| "spring.datasource.driver-class-name=org.h2.Driver", | ||
| "spring.exposed.generate-ddl=true" | ||
| ] | ||
| ) | ||
| open class DatabaseInitializerEarlyInitTest { | ||
|
|
||
| @TestConfiguration | ||
| open class EarlyInitConfig { | ||
| @Bean | ||
| @DependsOnDatabaseInitialization | ||
| @ConditionalOnProperty("spring.exposed.generate-ddl", havingValue = "true") | ||
| open fun schemaVerifier(transactionManager: PlatformTransactionManager): SchemaVerifierBean = | ||
| SchemaVerifierBean(transactionManager) | ||
| } | ||
|
|
||
| /** | ||
| * A bean that queries the TestTable during its own initialization. | ||
| * Uses [DependsOnDatabaseInitialization] (Spring Boot standard) instead of | ||
| * `@DependsOn("databaseInitializer")` to verify SPI-based automatic ordering. | ||
| */ | ||
| class SchemaVerifierBean( | ||
| private val transactionManager: PlatformTransactionManager | ||
| ) : InitializingBean { | ||
| var tableRowCount: Long = -1 | ||
|
|
||
| override fun afterPropertiesSet() { | ||
| TransactionTemplate(transactionManager).execute { | ||
| tableRowCount = TestTable.selectAll().count() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Autowired | ||
| private lateinit var schemaVerifier: SchemaVerifierBean | ||
|
|
||
| @Test | ||
| fun `schema should be available during bean initialization via SPI`() { | ||
| assertEquals( | ||
| 0L, | ||
| schemaVerifier.tableRowCount, | ||
| "TestTable should be queryable during afterPropertiesSet with @DependsOnDatabaseInitialization" | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `DatabaseInitializer should be an InitializingBean`() { | ||
| assertTrue( | ||
| InitializingBean::class.java.isAssignableFrom(DatabaseInitializer::class.java), | ||
| "DatabaseInitializer should implement InitializingBean" | ||
| ) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moving to TransactionTemplate is mandatory, not optional.
The AOP proxy isn't active during afterPropertiesSet(), so Transactional gets silently ignored.
If this was overlooked, the DDL likely executed without a transaction or failed.