Hi everybody!
I posted this yesterday in the mgo-users group, but since more people read here, I'll give it a try:
I'm wondering, what do people use to init database data? I'm speaking of master collections (countries, ACL (roles, features, main admin user...) and so)
Coming from PHP, I've used PHPMig and Laravel Artisan migrations (both for seeding and schema changes). How do you achieve this with MongoDB and Go?
The idea is to have a directory with a file per db operation (bulk field updates, master collection data insertion...) and run them when the app inits (or on demand via CLI command)
A collection called migrations will keep track of which ones have already been applied.
I've been thinking about it, and my three ideas are:
1. Have .js files in the directory, and run them from my Go app using
mgo.Database.Run(bson.M{"eval", jsCode}) , where jsCode would be the file contents, something along
db.users.insert({name: "admin", email: "ad...@app.com", ...}Pros: very easy to implement.
Cons: eval() has been deprecated in MongoDB 3.0. It bypasses the app logic that could be present when CRUD operations are triggered.
2. Regular JSON files in the directory. A JSON document modeling a operation ("insert", "update"...), containing an array with data to be unmarshaled to the model structs, and persisted to database using the app repositories (userRepo.Insert(userData))
Pros: uses app regular path to resource creation / deletion / update
Cons: could be that in the future, some changes are introduced in the code (model structs) that break JSON unmarshaling to the structs.
Pros: independence of the app code (no need for JSON / Go struct parity)
Cons: bypasses app logic (as point 1). Need to third party package to support extended JSON unmarsharling.
Pros: same as solution 2
Cons: data in the code, doesn't look very good to me.
Any insights?
Thanks in advance!
Dani