根据数据库验证 Grails 域类
验证 grails 域类与数据库同步的最佳方法是什么?它是遗留数据库,我无法从域类构建它。一个有趣的想法 here 这意味着获取每个域的一行。但是,它感觉不像是一个完整的解决方案,主要是因为我验证的测试数据库的数据可能不丰富,以至于在所有表中都有数据。
预先感谢您花时间阅读/回复。
What's the best way to validate that the grails domain classes are in sync with a database? It's legacy database and i can't build it from the domain classes. An interesting idea here which implies fetching one row of each of the domains. However, it doesn't feel like a complete solution mainly because the test database against which I validate may not be so data rich as to have data in all tables.
Thanks in advance for taking time to read/reply.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个很好的方法,即使对于空表也必须有效 - 如果表是空的,您就不必担心验证遗留数据,对吧?或者,如果您想测试 Grails 约束与 DB 约束的兼容性,请创建该类的新实例并尝试在事务中
save()
它 - 并始终回滚事务。如果数据库很小,我什至会从
list()
中删除max:1
- 以验证每条记录,因为只有一些记录的记录可能违反限制。我还将
println "${it}"
替换为assert it.validate()
。最后一项优化,我将仅测试那些我知道可能违反某些约束的类。这将节省此类测试的很大一部分 - 而且测试将花费大量时间,你知道 - 读取所有数据库并产生 GORM 开销。
That's a nice approach and must work even for empty tables - if a table is empty, you have no legacy data to worry about validating, right? Or, if you want to test Grails constraints for compatibility with DB constraints, create a new instance of the class and try to
save()
it in transaction - and always roll the transaction back.If the database is small, I'd even go and remove
max:1
fromlist()
- to validate every record, because only some of the records may violate constraints.I'd also replace
println "${it}"
withassert it.validate()
.One last optimization, I'd limit the classes tested only to those that I know can violate some constraints. This will save a good part of such a test - and the test is going to take a plenty of time, you know - reading all the database with GORM overhead.