I need to ensure existence of table(s) upon application startup.
I also want to create secondary index on the table if table does not exist and needs to be created.
It is easily done in Go but I would like to do it in ReQL in one statement. So I came up with this:
func ensureTableIndex(ses *r.Session, name string, index string) (err error) {
err = r.TableList().Contains(name).Do(r.Branch(r.Row, r.Expr(nil), r.Do(func() r.Term {
return r.TableCreate(name).Do(func() r.Term {
return r.Table(name).IndexCreate(index)
})
}))).Exec(ses)
return
}
Seems to pass the test.
My question is whether this is a correct/efficient way to do table and index creation in one go? Is using ReQL Do() function the correct way to sequence multiple write commands?
Thanks
Yeah, do
is the normal way you sequence operations in ReQL. The r.Do
as the third argument to branch
is superfluous, though; you can just put the tableCreate
call there directly.