Seed data for grails application

2019-05-24 14:19发布

问题:

What is the best way to load seed (initial or test) data into grails application. I'm considering 3 options

  1. Putting everything in *BootStrap.groovy files. This is tedious if the domain classes and test data are many.
  2. 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.
  3. 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..

回答1:

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.



回答2:

I'm using the Fixtures plugin to load test/initial data, it works for me.

http://www.grails.org/plugin/fixtures



回答3:

look at http://www.dbunit.org/ and http://www.grails.org/DBUnit+Plugin



回答4:

look into SeedMe plugin: https://github.com/bertramdev/seed-me

seed = {
    author(meta:[key:'name'], name: 'David', description: 'Author Bio Here')
}


回答5:

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.



标签: grails seed