From 96a2ae9251bd7b8ab9300a8aa45d997a5c2196ca Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Thu, 11 Sep 2025 12:19:06 +0700 Subject: [PATCH 1/3] initiate multi db config --- cmd/main-api/config.yml-example | 18 +++++++--- go.mod | 2 ++ internal/infra/ss-db/ss-db.go | 33 ++----------------- internal/infra/ss-db/tycovar.go | 10 +----- .../interface/main-handler/main-handler.go | 2 +- 5 files changed, 20 insertions(+), 45 deletions(-) diff --git a/cmd/main-api/config.yml-example b/cmd/main-api/config.yml-example index aaf986f0..f131a725 100644 --- a/cmd/main-api/config.yml-example +++ b/cmd/main-api/config.yml-example @@ -15,11 +15,19 @@ dbCfg: maxIdleConns: 5 maxIdleTime: 100 -ssDbCfg: - dsn: - maxOpenConns: 5 - maxIdleConns: 5 - maxIdleTime: 100 +multiDbCfg: + - dbs : + name: + dsn: + maxOpenConns: + maxIdleConns: + maxIdleTime: + - bpjs + name: bpjs + dsn: + maxOpenConns: + maxIdleConns: + maxIdleTime: loggerCfg: hideTime: diff --git a/go.mod b/go.mod index da8ef314..f85c51cf 100644 --- a/go.mod +++ b/go.mod @@ -49,3 +49,5 @@ require ( gorm.io/driver/sqlite v1.5.7 // indirect gorm.io/driver/sqlserver v1.5.4 // indirect ) + +replace github.com/karincake/apem => D:\Kuli\Sabbi\external\apem \ No newline at end of file diff --git a/internal/infra/ss-db/ss-db.go b/internal/infra/ss-db/ss-db.go index 3c239540..ac9c13ee 100644 --- a/internal/infra/ss-db/ss-db.go +++ b/internal/infra/ss-db/ss-db.go @@ -1,36 +1,9 @@ package ssdb import ( - "log" - - a "github.com/karincake/apem" - lo "github.com/karincake/apem/loggero" - "gorm.io/driver/postgres" - "gorm.io/gorm" - "gorm.io/gorm/schema" + dg "github.com/karincake/apem/db-gorm-pg" ) -func Init() { - SetConfig() - NewInstance() -} - -func SetConfig() { - a.ParseSingleCfg(&Cfg) -} - -func NewInstance() { - // create database connection - db, err := gorm.Open(postgres.Open(Cfg.Dsn), &gorm.Config{ - NamingStrategy: schema.NamingStrategy{ - SingularTable: true, - NoLowerCase: true, - }, - }) - if err != nil { - log.Fatal(err) - } - - SSDb = db - lo.I.Println("Instantiation for satu-sehat database-connetion using db-gorm-pg, status: DONE!!") +func SetInstance() { + I = dg.IS["satu-sehat"] } diff --git a/internal/infra/ss-db/tycovar.go b/internal/infra/ss-db/tycovar.go index ff084508..55fc90fd 100644 --- a/internal/infra/ss-db/tycovar.go +++ b/internal/infra/ss-db/tycovar.go @@ -2,12 +2,4 @@ package ssdb import "gorm.io/gorm" -type SsDbCfg struct { - Dsn string - MaxOpenConns int `yaml:"maxOpenConns"` - MaxIdleConns int `yaml:"maxIdleConns"` - MaxIdleTime int `yaml:"maxIdleTime"` -} - -var Cfg SsDbCfg = SsDbCfg{} -var SSDb *gorm.DB +var I *gorm.DB diff --git a/internal/interface/main-handler/main-handler.go b/internal/interface/main-handler/main-handler.go index 476dacc7..5ef7d2c4 100644 --- a/internal/interface/main-handler/main-handler.go +++ b/internal/interface/main-handler/main-handler.go @@ -80,7 +80,7 @@ func SetRoutes() http.Handler { ///// a.RegisterExtCall(gs.Adjust) a.RegisterExtCall(zlc.Adjust) - a.RegisterExtCall(ssdb.Init) + a.RegisterExtCall(ssdb.SetInstance) a.RegisterExtCall(lh.Populate) a.RegisterExtCall(validation.RegisterValidation) From dfaf6e8d29a066875da96cb2b2508b7dc42a2116 Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Thu, 11 Sep 2025 13:09:30 +0700 Subject: [PATCH 2/3] remove replaced go mod --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index 48086e5a..e717f960 100644 --- a/go.mod +++ b/go.mod @@ -49,5 +49,3 @@ require ( gorm.io/driver/sqlite v1.5.7 // indirect gorm.io/driver/sqlserver v1.5.4 // indirect ) - -replace github.com/karincake/apem => D:\Kuli\Sabbi\external\apem \ No newline at end of file From 20bc8419d00a25ca2d5a49c85be0f49314cec1a2 Mon Sep 17 00:00:00 2001 From: dpurbosakti Date: Fri, 12 Sep 2025 08:57:05 +0700 Subject: [PATCH 3/3] separate atlas command --- cmd/main-migration/Makefile | 18 ++++++ cmd/main-migration/README-ATLAS.MD | 59 +++++++++++++++++++ cmd/main-migration/config.yml-example | 47 --------------- cmd/satusehat-migration/Makefile | 18 ++++++ cmd/satusehat-migration/README-ATLAS.MD | 59 +++++++++++++++++++ cmd/satusehat-migration/config.yml-example | 47 --------------- .../migrations/20250911060006.sql | 9 --- cmd/satusehat-migration/migrations/atlas.sum | 2 - .../satusehat-entities/patient/entity.go | 2 +- internal/interface/migration/migration.go | 24 -------- 10 files changed, 155 insertions(+), 130 deletions(-) create mode 100644 cmd/main-migration/Makefile create mode 100644 cmd/main-migration/README-ATLAS.MD delete mode 100644 cmd/main-migration/config.yml-example create mode 100644 cmd/satusehat-migration/Makefile create mode 100644 cmd/satusehat-migration/README-ATLAS.MD delete mode 100644 cmd/satusehat-migration/config.yml-example delete mode 100644 cmd/satusehat-migration/migrations/20250911060006.sql delete mode 100644 cmd/satusehat-migration/migrations/atlas.sum diff --git a/cmd/main-migration/Makefile b/cmd/main-migration/Makefile new file mode 100644 index 00000000..92a4be84 --- /dev/null +++ b/cmd/main-migration/Makefile @@ -0,0 +1,18 @@ +# Makefile for Atlas migrations + +# Default environment +ENV ?= gorm + +.PHONY: diff apply hash + +## Generate a new migration diff +diff: + atlas migrate diff --env $(ENV) + +## Apply migrations to the database +apply: + atlas migrate apply --env $(ENV) + +## Calculate the schema hash +hash: + atlas schema hash --env $(ENV) diff --git a/cmd/main-migration/README-ATLAS.MD b/cmd/main-migration/README-ATLAS.MD new file mode 100644 index 00000000..da249823 --- /dev/null +++ b/cmd/main-migration/README-ATLAS.MD @@ -0,0 +1,59 @@ +# Database Migration with Atlas + +This project uses [Atlas](https://atlasgo.io/) for database schema management and migrations. + +## 📋 Prerequisites + +1. **Download and Install Atlas CLI** + Run the following command in PowerShell or Git Bash: + + ```sh + curl -sSf https://atlasgo.sh | sh + ``` + Verify installation: + + ```sh + atlas version + ``` + +2. Install GORM Provider + Run inside your Go project: + + ```sh + go get -u ariga.io/atlas-provider-gorm + ``` + +3. Create atlas.hcl configuration file + Just create an atlas.hcl file in your project root as example given at atlas.hcl.example +4. Create migrations folder + ```sh + mkdir migrations + ``` +5. Usage +You can use the provided Makefile for common commands: + + Generate a migration diff + ```sh + make diff + ``` + + Apply migrations + ```sh + make apply + ``` + + Compute schema hash + ```sh + make hash + ``` + + If you don’t have make installed, you can run the Atlas commands directly: + ```sh + atlas migrate diff --env gorm + ``` + ```sh + atlas migrate apply --env gorm + ``` + ```sh + atlas migrate hash + ``` \ No newline at end of file diff --git a/cmd/main-migration/config.yml-example b/cmd/main-migration/config.yml-example deleted file mode 100644 index 19893fd7..00000000 --- a/cmd/main-migration/config.yml-example +++ /dev/null @@ -1,47 +0,0 @@ -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 diff --git a/cmd/satusehat-migration/Makefile b/cmd/satusehat-migration/Makefile new file mode 100644 index 00000000..92a4be84 --- /dev/null +++ b/cmd/satusehat-migration/Makefile @@ -0,0 +1,18 @@ +# Makefile for Atlas migrations + +# Default environment +ENV ?= gorm + +.PHONY: diff apply hash + +## Generate a new migration diff +diff: + atlas migrate diff --env $(ENV) + +## Apply migrations to the database +apply: + atlas migrate apply --env $(ENV) + +## Calculate the schema hash +hash: + atlas schema hash --env $(ENV) diff --git a/cmd/satusehat-migration/README-ATLAS.MD b/cmd/satusehat-migration/README-ATLAS.MD new file mode 100644 index 00000000..da249823 --- /dev/null +++ b/cmd/satusehat-migration/README-ATLAS.MD @@ -0,0 +1,59 @@ +# Database Migration with Atlas + +This project uses [Atlas](https://atlasgo.io/) for database schema management and migrations. + +## 📋 Prerequisites + +1. **Download and Install Atlas CLI** + Run the following command in PowerShell or Git Bash: + + ```sh + curl -sSf https://atlasgo.sh | sh + ``` + Verify installation: + + ```sh + atlas version + ``` + +2. Install GORM Provider + Run inside your Go project: + + ```sh + go get -u ariga.io/atlas-provider-gorm + ``` + +3. Create atlas.hcl configuration file + Just create an atlas.hcl file in your project root as example given at atlas.hcl.example +4. Create migrations folder + ```sh + mkdir migrations + ``` +5. Usage +You can use the provided Makefile for common commands: + + Generate a migration diff + ```sh + make diff + ``` + + Apply migrations + ```sh + make apply + ``` + + Compute schema hash + ```sh + make hash + ``` + + If you don’t have make installed, you can run the Atlas commands directly: + ```sh + atlas migrate diff --env gorm + ``` + ```sh + atlas migrate apply --env gorm + ``` + ```sh + atlas migrate hash + ``` \ No newline at end of file diff --git a/cmd/satusehat-migration/config.yml-example b/cmd/satusehat-migration/config.yml-example deleted file mode 100644 index 19893fd7..00000000 --- a/cmd/satusehat-migration/config.yml-example +++ /dev/null @@ -1,47 +0,0 @@ -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 diff --git a/cmd/satusehat-migration/migrations/20250911060006.sql b/cmd/satusehat-migration/migrations/20250911060006.sql deleted file mode 100644 index b1671f9a..00000000 --- a/cmd/satusehat-migration/migrations/20250911060006.sql +++ /dev/null @@ -1,9 +0,0 @@ --- Create "Patient" table -CREATE TABLE "public"."Patient" ( - "ResourceType" text NULL, - "Active" boolean NULL, - "Gender" text NULL, - "BirthDate" text NULL, - "DeceasedBool" boolean NULL, - "MultipleBirthInteger" bigint NULL -); diff --git a/cmd/satusehat-migration/migrations/atlas.sum b/cmd/satusehat-migration/migrations/atlas.sum deleted file mode 100644 index d8f010f3..00000000 --- a/cmd/satusehat-migration/migrations/atlas.sum +++ /dev/null @@ -1,2 +0,0 @@ -h1:dPpFMJ+ZSlHizKRHShYcjJZJNZXi4UuZXZUHME8zPb4= -20250911060006.sql h1:G3YiyY/tCTZijQWqyvyDAaMVCFkhh/gD3v/1gz/eBb8= diff --git a/internal/domain/satusehat-entities/patient/entity.go b/internal/domain/satusehat-entities/patient/entity.go index 12efaa93..21c6075b 100644 --- a/internal/domain/satusehat-entities/patient/entity.go +++ b/internal/domain/satusehat-entities/patient/entity.go @@ -7,7 +7,7 @@ type Patient struct { Active bool `json:"active"` // Name []HumanName `json:"name"` // Telecom []ContactPoint `json:"telecom"` - Gender string `json:"gender"` + Gender string `json:"gender" gorm:"size:10"` BirthDate string `json:"birthDate"` DeceasedBool bool `json:"deceasedBoolean"` // Address []Address `json:"address"` diff --git a/internal/interface/migration/migration.go b/internal/interface/migration/migration.go index 66018a21..6cec68dc 100644 --- a/internal/interface/migration/migration.go +++ b/internal/interface/migration/migration.go @@ -4,7 +4,6 @@ import ( "fmt" "io" "os" - "os/exec" "ariga.io/atlas-provider-gorm/gormschema" "gorm.io/gorm" @@ -36,30 +35,7 @@ func getEntities(input string) []any { return nil } -func runAtlas(args ...string) error { - cmd := exec.Command("atlas", args...) - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -} - -func logMsg(msg string) { - fmt.Fprintln(os.Stderr, msg) -} - func Migrate(input string) { loader(input) - logMsg("Running atlas migrate diff...") - if err := runAtlas("migrate", "diff", "--env", "gorm"); err != nil { - logMsg(fmt.Sprintf("Failed to run diff: %v", err)) - return - } - logMsg("Running atlas migrate apply...") - if err := runAtlas("migrate", "apply", "--env", "gorm"); err != nil { - logMsg(fmt.Sprintf("Failed to run apply: %v", err)) - return - } - - logMsg("Migration complete") }