Merge pull request #4 from dikstub-rssa/moko

Moko
This commit is contained in:
Munawwirul Jamal
2025-08-13 15:33:08 +07:00
committed by GitHub
5 changed files with 155 additions and 3 deletions
+47
View File
@@ -0,0 +1,47 @@
appCfg:
fullName: BPJS Bridge
codeName: simrs-vx
version: 0.1.0
env: development
lang: en
httpCfg:
host:
port:
dbCfg:
dsn:
maxOpenConns: 5
maxIdleConns: 5
maxIdleTime: 100
loggerCfg:
hideTime:
hideLevel:
msCfg:
dsn:
langCfg:
active:
path:
fileName:
minioCfg:
endpoint:
region:
accessKey:
secretKey:
useSsl:
bucketName:
- patient
corsCfg:
allowedOrigin:
allowedMethod:
satuSehatCfg:
host: localhsot:8200
bpjsCfg:
host: localhsot:8200
+9
View File
@@ -0,0 +1,9 @@
package main
import (
m "simrs-vx/internal/interface/migration"
)
func main() {
m.Migrate()
}
+4 -3
View File
@@ -7,7 +7,11 @@ toolchain go1.23.11
require (
github.com/karincake/apem v0.0.16-g
github.com/karincake/dodol v0.0.1
github.com/karincake/getuk v0.1.0
github.com/karincake/lepet v0.0.1
golang.org/x/crypto v0.40.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/postgres v1.5.7
gorm.io/gorm v1.25.10
)
@@ -24,11 +28,8 @@ require (
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/nxadm/tail v1.4.11 // indirect
github.com/rs/zerolog v1.33.0 // indirect
golang.org/x/crypto v0.40.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 // indirect
golang.org/x/net v0.42.0 // indirect
golang.org/x/sys v0.34.0 // indirect
golang.org/x/text v0.27.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.7 // indirect
)
+2
View File
@@ -24,6 +24,8 @@ github.com/karincake/apem v0.0.16-g h1:jPIr/YiaJhVSftdA1PyB2tlDiQtFeTVZohO1qf0qp
github.com/karincake/apem v0.0.16-g/go.mod h1:cQP2sJfDrLRIiwWoaLWw/z8uAya+DWu/FpmYeinMQXM=
github.com/karincake/dodol v0.0.1 h1:jUXmJh1r0Ei4fmHPZ6IUkoplW/V9d27L63JEl6zudL0=
github.com/karincake/dodol v0.0.1/go.mod h1:2f1NcvkvY0J3GMUkwILNDYVvRUpz0W3lpPp/Ha/Ld24=
github.com/karincake/getuk v0.1.0 h1:jcIsASrr0UDE528GN7Ua6n9UFyRgUypsWh8Or8wzCO0=
github.com/karincake/getuk v0.1.0/go.mod h1:NVnvxSGAkQ/xuq99FzWACvY5efyKPLFla1cKB8czm7c=
github.com/karincake/lepet v0.0.1 h1:eq/cwn5BBg0jWZ1c/MmvhFIBma0zBpVs2LwkfDOncy4=
github.com/karincake/lepet v0.0.1/go.mod h1:U84w7olXO3BPJw2Hu6MBonFmJmPKaFjtyAj1HTu3z1A=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
+93
View File
@@ -0,0 +1,93 @@
package migration
import (
"flag"
"fmt"
"log"
"os"
"reflect"
"gopkg.in/yaml.v3"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
type DbConf struct {
Dsn string
Dialect string
}
// Migrate all tables at once, one time only for exam purpose
func Migrate() {
// use default config file location or use flat
cfgFile := "./config.yml"
flag.StringVar(&cfgFile, "config-file", "./config.yml", "Configuration path (default=./config.yaml)")
flag.Parse()
// read the config file
yamlFile, err := os.ReadFile(cfgFile)
if err != nil {
log.Fatalf("%v", err)
}
// parse into config struct
var dbConf DbConf
err = yaml.Unmarshal(yamlFile, &dbConf)
if err != nil {
log.Fatal(err)
}
log.Print("config is loaded successfully")
// create database connection
db, err := gorm.Open(postgres.Open(dbConf.Dsn), &gorm.Config{
NamingStrategy: schema.NamingStrategy{
SingularTable: true,
NoLowerCase: true,
},
})
if err != nil {
log.Fatal(err)
}
log.Print("database-connection is established successfully")
// migrate all the tables
modelList := []any{
// &single.Single{}}, // example
}
argsWithProg := os.Args
if len(argsWithProg) > 1 {
if argsWithProg[1] == "gradually" {
for _, v := range modelList {
name := ""
if t := reflect.TypeOf(v); t.Kind() == reflect.Ptr {
name = "*" + t.Elem().Name()
} else {
name = t.Name()
}
fmt.Println("Migrating ", name)
db.AutoMigrate(v)
}
} else {
for _, v := range modelList {
name := ""
if t := reflect.TypeOf(v); t.Kind() == reflect.Ptr {
name = "*" + t.Elem().Name()
} else {
name = t.Name()
}
if name == argsWithProg[1] {
fmt.Println("Migrating ", name)
db.AutoMigrate(v)
}
}
}
} else {
fmt.Println("Migrating all tables")
db.AutoMigrate(modelList...)
}
log.Printf("migration is complete")
}