Last active
August 30, 2023 07:46
-
-
Save hkakutalua/948f7b795e70b9e72dc7d6284470b4b3 to your computer and use it in GitHub Desktop.
PostgresDbCleanerExtension
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
class PostgresDbCleanerExtension : BeforeEachCallback { | |
companion object { | |
private val LOGGER = LoggerFactory.getLogger(PostgresDbCleanerExtension::class.java) | |
private val TABLES_TO_IGNORE = listOf( | |
TableData("databasechangelog"), | |
TableData("databasechangeloglock") | |
) | |
} | |
@Throws(Exception::class) | |
override fun beforeEach(context: ExtensionContext) { | |
val dataSource = getDataSourceFromYamlProperties("application.yml") | |
cleanDatabase(dataSource) | |
} | |
... | |
private fun cleanDatabase(dataSource: DataSource) { | |
try { | |
dataSource.connection.use { connection -> | |
connection.autoCommit = false | |
val tablesToClean = loadTablesToClean(connection) | |
cleanTablesData(tablesToClean, connection) | |
connection.commit() | |
} | |
} catch (e: SQLException) { | |
LOGGER.error(String.format("Failed to clean database due to error: \"%s\"", e.message)) | |
e.printStackTrace() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
one method missing. please update or delete
https://github.com/hkakutalua/avoid-transactional-in-tests/blob/without-transactional-replacement/src/test/kotlin/com/example/avoidtransactional/utilities/PostgresDbCleanerExtension.kt