Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.cloudfoundry.credhub.config

import org.flywaydb.core.api.ResourceProvider
import org.flywaydb.core.api.configuration.FluentConfiguration
import org.flywaydb.core.api.migration.JavaMigration
import org.flywaydb.core.api.resource.LoadableResource
import org.flywaydb.core.internal.scanner.Scanner
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile
import org.springframework.core.Ordered
import org.springframework.core.annotation.Order
import java.io.Reader
import java.io.StringReader

/**
* For unit tests against PostgreSQL, strip `CONCURRENTLY` from index DDL in Flyway SQL migrations so
* migrations do not block on other connections (e.g. Flyway bookkeeping). Production migrations are
* unchanged on disk; only the in-memory script content is transformed.
*
* Every `*.sql` resource is wrapped (Flyway relative paths omit `db/migration/postgres/`); replacements are
* no-ops for scripts that do not use concurrent index DDL.
Comment on lines +18 to +23
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: curious why this needed now, is it due to changes in Flyway or recent changes to the tests?

Is this related to flyway/flyway#3508 or flyway/flyway#3682
workaround suggests applying config flyway.postgresql.transactional.lock=false

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

@hsinn0 hsinn0 Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: curious why this needed now, is it due to changes in Flyway or recent changes to the tests?

The unit-tests-postgres-* jobs in Coucourse have been failing frequently with timeouts in the past, too. It seems that the timeout is just more frequent (or consistent) these days. I've never bothered looked into those CI timeouts, thinking they are flakes, as rerunning usually made the jobs pass.
For the matter of local run in my dev box, I do not regularly run the tests against postgresql database unless there is something postgresql-specific that need to be looked into. So no idea when the problem started to occur there. It is possible that this was caused by some changes in other dependencies such as hikari pool or hibernate.

Is this related to flyway/flyway#3508 or flyway/flyway#3682 workaround suggests applying config flyway.postgresql.transactional.lock=false

Not sure if our current issue is related to those old flyway issues. Sounds similar, though. And yes, we are already setting transitionalLock to false (in FlywayMigrationStrategyTestConfig for tests).

As the goal is to prevent the hang for the tests and the issue is not to be expected to occur in production, and the change only affect test run with postgresql database, I think that just removing concurrently from the create/drop index statements for test only is the way to go.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the latest issue: flyway/flyway#3961
Seems like there's no other solutions yet.

*/
@Configuration
@Profile("unit-test-postgres")
class FlywayPostgresTestStripConcurrentlyConfiguration {
@Bean
@Order(Ordered.LOWEST_PRECEDENCE)
fun stripConcurrentlyFlywayCustomizer(): FlywayConfigurationCustomizer =
FlywayConfigurationCustomizer { configuration: FluentConfiguration ->
val locations = configuration.locations
val delegateProvider: ResourceProvider =
Scanner(JavaMigration::class.java, configuration, locations)
configuration.resourceProvider(
StripConcurrentlyResourceProvider(delegateProvider),
)
}
}

internal fun stripConcurrentIndexDdl(sql: String): String =
sql
.replace(CREATE_INDEX_CONCURRENTLY, "CREATE INDEX")
.replace(DROP_INDEX_CONCURRENTLY, "DROP INDEX")

private val CREATE_INDEX_CONCURRENTLY = Regex("(?i)CREATE\\s+INDEX\\s+CONCURRENTLY\\b")
private val DROP_INDEX_CONCURRENTLY = Regex("(?i)DROP\\s+INDEX\\s+CONCURRENTLY\\b")

private class StripConcurrentlyResourceProvider(
private val delegate: ResourceProvider,
) : ResourceProvider {
override fun getResource(name: String): LoadableResource? = delegate.getResource(name)?.let { wrap(it) }

override fun getResources(
prefix: String,
suffixes: Array<String>,
): Collection<LoadableResource> = delegate.getResources(prefix, suffixes).map { wrap(it) }

private fun wrap(resource: LoadableResource): LoadableResource =
if (resource.filename.endsWith(".sql")) {
TransformingPostgresSqlResource(resource)
} else {
resource
}
}

private class TransformingPostgresSqlResource(
private val delegate: LoadableResource,
) : LoadableResource() {
override fun read(): Reader {
val original = delegate.read().use { it.readText() }
val transformed = stripConcurrentIndexDdl(original)
return StringReader(transformed)
}

override fun getAbsolutePath(): String = delegate.absolutePath

override fun getAbsolutePathOnDisk(): String = delegate.absolutePathOnDisk

override fun getFilename(): String = delegate.filename

override fun getRelativePath(): String = delegate.relativePath
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.cloudfoundry.credhub.config

import com.zaxxer.hikari.HikariDataSource
import org.springframework.boot.jdbc.DataSourceBuilder
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary
import org.springframework.context.annotation.Profile
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.jdbc.datasource.DriverManagerDataSource
import java.sql.ResultSet
import javax.sql.DataSource

Expand All @@ -17,10 +19,9 @@ class ParallelPostgresTestDataSourceConfiguration {
private fun createTestDatabaseForWorker(workerId: String) {
val workerDatabaseName = "credhub_test_$workerId"
val tempDataSource =
DataSourceBuilder
.create()
.url("jdbc:postgresql://localhost:5432/credhub_test?user=pivotal")
.build()
DriverManagerDataSource(
"jdbc:postgresql://localhost:5432/credhub_test?user=pivotal&connectTimeout=10",
)

val jdbcTemplate = JdbcTemplate(tempDataSource)
val noDb =
Expand All @@ -33,8 +34,6 @@ class ParallelPostgresTestDataSourceConfiguration {
if (noDb) {
jdbcTemplate.execute("CREATE DATABASE $workerDatabaseName")
}

tempDataSource.connection.close()
}

@Primary
Expand All @@ -47,9 +46,14 @@ class ParallelPostgresTestDataSourceConfiguration {
val dataSource =
DataSourceBuilder
.create()
.url("jdbc:postgresql://localhost:5432/credhub_test_$workerId?user=pivotal")
.type(HikariDataSource::class.java)
.url("jdbc:postgresql://localhost:5432/credhub_test_$workerId?user=pivotal&connectTimeout=10")
.build()

dataSource.maximumPoolSize = 5
dataSource.minimumIdle = 1
dataSource.connectionInitSql = "SET statement_timeout = '120s'"

return dataSource
}
}