/
generate.go
62 lines (57 loc) · 1.62 KB
/
generate.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
61
62
package migrate
import (
"os"
"github.com/uptrace/bun"
"github.com/yulog/mi-diary/app"
"github.com/yulog/mi-diary/infra"
"github.com/yulog/mi-diary/model"
)
// https://techblog.enechain.com/entry/bun-atlas-migration-setup-guide
func modelsToByte(db *bun.DB, models []interface{}) []byte {
var data []byte
for _, model := range models {
query := db.NewCreateTable().Model(model).WithForeignKeys()
rawQuery, err := query.AppendQuery(db.Formatter(), nil)
if err != nil {
panic(err)
}
data = append(data, rawQuery...)
data = append(data, ";\n"...)
}
return data
}
// https://techblog.enechain.com/entry/bun-atlas-migration-setup-guide
func indexesToByte(db *bun.DB, idxCreators []model.IndexQueryCreator) []byte {
var data []byte
for _, idxCreator := range idxCreators {
idx := idxCreator(db)
rawQuery, err := idx.AppendQuery(db.Formatter(), nil)
if err != nil {
panic(err)
}
data = append(data, rawQuery...)
data = append(data, ";\n"...)
}
return data
}
func GenerateSchema() {
app := app.New()
infra := infra.New(app)
models := []interface{}{
(*model.Note)(nil),
(*model.User)(nil),
(*model.Reaction)(nil),
(*model.HashTag)(nil),
(*model.NoteToTag)(nil),
(*model.Month)(nil),
(*model.Day)(nil),
}
var data []byte
for k := range app.Config.Profiles {
data = append(data, modelsToByte(infra.DB(k), models)...)
data = append(data, indexesToByte(infra.DB(k), model.IdxCreators)...)
break // schemaの生成は1つだけやれば良さそう
}
// TODO: 権限これで良いの?
os.WriteFile("migrate/schema.sql", data, 0666)
}