The App
Seeds
Rails's db/seeds.rb is a script that runs top to bottom every time, and stays safe because each line is a find_or_create_by!. Bogie's seeds work the same way: plain Go functions in db/seeds, no version table, all of them run on every db:seed. Since v0.2.6.
Write a seed
bogie g seed demo_posts
create db/seeds/demo_posts.go
insert db/seeds/seeds.go: func() error { return SeedDemoPosts(ctx, s) },
The new file is one function with the store in hand, so a seed calls the same methods a controller would, validation included:
// SeedDemoPosts seeds demo_posts. db seed can run this any number of times, so
// make it safe to run twice: insert with ON CONFLICT DO NOTHING, or
// check before creating.
func SeedDemoPosts(ctx context.Context, s *models.Store) error {
// TODO: s.Create...(ctx, ...), written so a second run does nothing new.
return nil
}
Run them
bogie db:seed # or: make db-seed, blog db seed
bogie db:prepare # create if needed, migrate, then seed
db/seeds/seeds.go ships with every app from bogie new. Its Run calls each registered seed in the order it sits above // bogie:seeds, and the first error stops the rest, since a later seed may depend on rows an earlier one wrote.
Safe to run twice
There is no record of which seeds have run, so a seed that blindly inserts will insert again. Either check first (look the row up by a natural key, create it only if domain.ErrNotFound), or write a query with ON CONFLICT DO NOTHING in db/queries and call it from the seed. That is the same discipline db/seeds.rb already asks of you.
Taking one out
bogie d seed demo_posts
removes the file and its registration line together. bogie doctor checks that // bogie:seeds is present exactly once.
Coming from an older app
Before v0.2.6 seeds were goose SQL files with their own version table and a db:seed:rollback; both are gone. Commit your work, port any existing seed SQL into db/seeds/*.go by hand, then run bogie app:update to bring in the new runner. app:update moves the layout; it does not translate seed content. See Upgrading an app.