-
Notifications
You must be signed in to change notification settings - Fork 1
/
createEnum.go
60 lines (50 loc) · 1.63 KB
/
createEnum.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"log"
"time"
"github.com/mayur-tolexo/contour/adapter/psql"
shifter "github.com/mayur-tolexo/pg-shifter"
)
func main() {
if conn, err := psql.Conn(true); err == nil {
s := shifter.NewShifter()
//1. Create enum by passing table model
err = s.CreateEnum(conn, &TestAddress{}, "address_status")
logIfError(err)
//2. Create enum by passing table name
err = s.SetTableModel(&TestAddress{})
logIfError(err)
err = s.CreateEnum(conn, "test_address", "address_status")
logIfError(err)
//1. Create all enum by passing table model
err = s.CreateAllEnum(conn, &TestAddress{})
logIfError(err)
//2. Create all enum by pasing table name
err = s.SetTableModel(&TestAddress{})
logIfError(err)
err = s.CreateAllEnum(conn, "test_address")
logIfError(err)
}
}
//TestAddress Table structure as in DB
type TestAddress struct {
tableName struct{} `sql:"test_address"`
AddressID int `json:"address_id,omitempty" sql:"address_id,type:serial PRIMARY KEY"`
City string `json:"city" sql:"city,type:varchar(25) UNIQUE"`
Status string `json:"status,omitempty" sql:"status,type:address_status"`
CreatedBy int `json:"created_by" sql:"created_by,type:int NOT NULL UNIQUE REFERENCES test_user(user_id)"`
CreatedAt time.Time `json:"-" sql:"created_at,type:time NOT NULL DEFAULT NOW()"`
UpdatedAt time.Time `json:"-" sql:"updated_at,type:timetz NOT NULL DEFAULT NOW()"`
}
//Enum of the table.
func (TestAddress) Enum() map[string][]string {
enm := map[string][]string{
"address_status": {"enable", "disable"},
}
return enm
}
func logIfError(err error) {
if err != nil {
log.Println(err)
}
}