-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_management.go
40 lines (34 loc) · 1.04 KB
/
db_management.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package api
import (
"fmt"
"github.com/go-pg/pg/orm"
)
func createTables() error {
tx, err := dbHandle.Begin()
if err != nil {
return fmt.Errorf("Could not start table creation transaction: '%s'", err.Error())
}
defer tx.Rollback()
// Table users
err = dbHandle.CreateTable(&userModel{}, &orm.CreateTableOptions{IfNotExists: true})
if err != nil {
return fmt.Errorf("Error while creating users table: '%s'", err.Error())
}
// Table sessions
err = dbHandle.CreateTable(&sessionModel{}, &orm.CreateTableOptions{IfNotExists: true})
if err != nil {
return fmt.Errorf("Error while creating sessions table: '%s'", err.Error())
}
// Admin Account Creation
exists, err := userExists(apiConfig.General.AdminEmail)
if err != nil {
return fmt.Errorf("Error check admin existance: '%s'", err.Error())
}
if !exists {
err = createUser(apiConfig.General.AdminEmail, "Administrator", []string{"admin"}, apiConfig.General.AdminPassword)
if err != nil {
return fmt.Errorf("Could not create admin: '%s'", err.Error())
}
}
return tx.Commit()
}