Grails 应用程序的种子数据
将种子(初始或测试)数据加载到 grails 应用程序中的最佳方法是什么?我正在考虑 3 个选项
- 将所有内容放入 *BootStrap.groovy 文件中。如果领域类和测试数据很多,这会很乏味。
- 编写自定义功能以通过 xml 加载它。借助 groovy 出色的 xml 支持,可能不会太困难,但需要针对不同域类的大量 switch 语句。
- 使用 Liquibase LoadData API。我发现您可以相当轻松地从 csv 文件加载数据。
选择 3 似乎是最简单的。但是,我对 Liquibase 不熟悉。在这种情况下它好吗,或者仅用于迁移、数据库更改等。如果有人可以提供更好的 sol,或者指出 Liquibase 的示例,那将会有很大的帮助。
What is the best way to load seed (initial or test) data into grails application. I'm considering 3 options
- Putting everything in *BootStrap.groovy files. This is tedious if the domain classes and test data are many.
- Write custom functionality to load it through xml. May not be too difficult with the excellent xml support by groovy, but lot of switch statements for different domain classes.
- Use Liquibase LoadData api. I see you can load the data fairly easy from csv files.
Choice 3 seems the easiest. But, I'm not familiar with Liquibase. Is it good in this scenario, or only used for migration, db changes etc. If anyone could provide a better sol, or point to an example with Liquibase, it would be great help..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
另一个答案是利用 grails run-script。这将允许您移动可能放入引导程序中的内容并将其保留在文件系统上您想要的位置(可能在代码库之外)。同样,您可以安装控制台插件并通过该插件在正在运行的应用程序上加载代码。
根据您的数据需求,还可以查看出色的构建测试数据插件。
Another answer would be to leverage grails run-script. This would allow you to move what you might put in bootstrap and keep it where you want on your file system (possibly outside of the codebase). Similarly, you could install the console plugin and load code through that on a running application.
Depending on your data needs, check out the great build-test-data plugin as well.
我正在使用 Fixtures 插件加载测试/初始数据,它对我有用。
http://www.grails.org/plugin/fixtures
I'm using the Fixtures plugin to load test/initial data, it works for me.
http://www.grails.org/plugin/fixtures
查看 http://www.dbunit.org/ 和 http://www.grails.org/DBUnit+Plugin
look at http://www.dbunit.org/ and http://www.grails.org/DBUnit+Plugin
查看 SeedMe 插件:
https://github.com/bertramdev/seed-me
look into SeedMe plugin:
https://github.com/bertramdev/seed-me
我生成种子数据的一种方法是使用服务。我创建了一个类,我们称之为 SeederService。我可以在 Bootstrap.groovy 中注入此服务并调用我想要的任何方法。
SeederService 的优点在于您还可以在单元测试中使用相同的服务。只需将服务类注入单元测试并生成种子数据即可。
One way I have generated seed data is using a service. I created a class, lets call it SeederService. I can inject this service in the Bootstrap.groovy and call whatever method I would want.
The beauty of SeederService is that you can also use the same service in your unit-tests. Simply inject the service class in your unit test and generate your seed data.