我正在编写集成测试,并希望在每次测试后都清理(postgres)数据库。因此,我认为对所有(实际上仅是大多数)表进行级联截断操作是可行的方法。
[我正在开发一个使用Kotlin,Spring和Jooq的应用程序,这就是为什么我用Truncator
成像一个truncateCascade
类的原因,可以将其自动装配到SpringBootTest
类中。
import org.jooq.DSLContext
import org.jooq.Table
@Service
class Truncator(private val dsl: DSLContext) {
fun truncateCascade(vararg tables: Table<*>) {
dsl.truncate ...
}
// single truncate work only for tables without foreign key constraints
// so I can't simply iterate over all tables and call this method.
// fun truncate(table: Table<*>) {
// dsl.truncate(table).execute()
// }
}
基本上我正在寻找truncateCascade
的实现(假设这不是错误的方法)。
我在研究此问题时发现了Jooq的TruncateCascadeStep文档,并提到了continueIdentity或restartIdentity,但通常没有足够的Jooq或数据库经验来将其组合在一起。
缺少的是在cascade()
命令上调用truncate()
:
fun truncate(table: Table<*>) {
dsl.truncate(table).cascade().execute()
}
另一种选择是简单地完全删除您的模式,然后从头开始重新创建它。对于测试而言,这可能更健壮,并且对于中小型架构,不应花费更多的时间。