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..
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.
I'm using the Fixtures plugin to load test/initial data, it works for me.
http://www.grails.org/plugin/fixtures
look at http://www.dbunit.org/ and http://www.grails.org/DBUnit+Plugin
look into SeedMe plugin:
https://github.com/bertramdev/seed-me
seed = {
author(meta:[key:'name'], name: 'David', description: 'Author Bio Here')
}
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.