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") }