separate atlas command

This commit is contained in:
dpurbosakti
2025-09-12 08:57:05 +07:00
parent dfaf6e8d29
commit 20bc8419d0
10 changed files with 155 additions and 130 deletions
+18
View File
@@ -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)
+59
View File
@@ -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 dont 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
```
-47
View File
@@ -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
+18
View File
@@ -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)
+59
View File
@@ -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 dont 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
```
@@ -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
@@ -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
);
@@ -1,2 +0,0 @@
h1:dPpFMJ+ZSlHizKRHShYcjJZJNZXi4UuZXZUHME8zPb4=
20250911060006.sql h1:G3YiyY/tCTZijQWqyvyDAaMVCFkhh/gD3v/1gz/eBb8=
@@ -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"`
-24
View File
@@ -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")
}