Database Seeding

Hitrix supports seeds that can be used to populate database or apply changes on it.

To Seed your database (wikiopen in new window), you can use the seeding feature that is implemented as script (DBSeedScript). This DBSeedScript needs to be provided a Seeds map[string][]Seed field, Where the string key is the identifier of the project that is used. This measn you can have different project in one code base The Script can be implemented in your app by making a type that satisfies the Seed interface:

type Seed interface {
    Execute(*beeorm.Engine)
    Environments() []string
    Name() string
}
1
2
3
4
5

Example:

users_seed.go

type UsersSeed struct{}

func (seed *UsersSeed) Name() string {
    return "UsersSeed"
}

func (seed *UsersSeed) Environments() []string {
    return []string{app.ModeTest, app.ModeLocal, app.ModeDev, app.ModeDemo, app.ModeProd}
}

func (seed *UsersSeed) Execute(ormService *beeorm.Engine) {
	// TODO insert a new user entity to the db
}
1
2
3
4
5
6
7
8
9
10
11
12
13

And after your server definition (such as the one in server.go), you could use it to run the seeds just like you would run any script (explained earlier in the scripts seciton) as follows:

// Server Definition
s,_ := hitrix.New()

...

s.RunBackgroundProcess(func(b *hitrix.BackgroundProcessor) {
  // seed database
  go b.RunScript(&hitrixScripts.DBSeedScript{
    SeedsPerProject: map[string][]hitrixScripts.Seed{
        "project_name": {
            &script.UserProfileAttributesSeed{},
        },
    })

  // ... other scripts
})
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

This seed will only run if no seeds has ever been ran with that name (in db table seeder, no row with 'name'='UsersSeed' )