Merge pull request #147 from dikstub-rssa/migration-vanilia

Migration vanilia
This commit is contained in:
Dwi Atmoko Purbo Sakti
2025-11-14 09:54:32 +07:00
committed by GitHub
13 changed files with 210 additions and 2 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 migrate hash
+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
```
@@ -0,0 +1,22 @@
data "external_schema" "gorm" {
program = [
"go",
"run",
"-mod=mod",
".",
]
}
env "gorm" {
src = data.external_schema.gorm.url
dev = "" // dsn db to check the diff
migration {
dir = "file://migrations"
}
url = "" // dsn db to apply
format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}
+9
View File
@@ -0,0 +1,9 @@
package main
import (
m "simrs-vx/internal/interface/migration"
)
func main() {
m.Migrate(m.SimgosSync)
}
@@ -0,0 +1,36 @@
-- Create "InstallationLink" table
CREATE TABLE "public"."InstallationLink" (
"Id" bigserial NOT NULL,
"CreatedAt" timestamptz NULL,
"UpdatedAt" timestamptz NULL,
"DeletedAt" timestamptz NULL,
"Simx_Id" bigint NULL,
"Simgos_Id" bigint NULL,
PRIMARY KEY ("Id"),
CONSTRAINT "uni_InstallationLink_Simgos_Id" UNIQUE ("Simgos_Id"),
CONSTRAINT "uni_InstallationLink_Simx_Id" UNIQUE ("Simx_Id")
);
-- Create "InstallationSimgosLog" table
CREATE TABLE "public"."InstallationSimgosLog" (
"Id" bigserial NOT NULL,
"CreatedAt" timestamptz NULL,
"UpdatedAt" timestamptz NULL,
"DeletedAt" timestamptz NULL,
"Value" text NULL,
"Date" timestamptz NULL,
"Status" text NULL,
"ErrMessage" text NULL,
PRIMARY KEY ("Id")
);
-- Create "InstallationSimxLog" table
CREATE TABLE "public"."InstallationSimxLog" (
"Id" bigserial NOT NULL,
"CreatedAt" timestamptz NULL,
"UpdatedAt" timestamptz NULL,
"DeletedAt" timestamptz NULL,
"Value" text NULL,
"Date" timestamptz NULL,
"Status" text NULL,
"ErrMessage" text NULL,
PRIMARY KEY ("Id")
);
@@ -0,0 +1,2 @@
h1:8jMmMBxSEls9jaOUrpQQV0wUPlORRwJWd5g9742Z2fQ=
20251113035508.sql h1:rjDlu6yDdy5xv6nrCOr7NialrLSLT23pzduYNq29Hf0=
@@ -16,6 +16,7 @@ type (
DataVerifiedCode string
CrudCode string
DataApprovalCode string
ProcessStatusCode string
)
const (
@@ -102,6 +103,9 @@ const (
DACNew DataApprovalCode = "new"
DACApproved DataApprovalCode = "approved"
DACRejected DataApprovalCode = "rejected"
PSCSuccess ProcessStatusCode = "success"
PSCFailed ProcessStatusCode = "failed"
)
func GetDayCodes() map[DayCode]string {
@@ -0,0 +1,12 @@
package installation
type MInstalasi struct {
No_Instalasi uint `json:"no_instalasi" gorm:"primaryKey;autoIncrement;column:no_instalasi"`
Nama_Instalasi string `json:"nama_instalasi" gorm:"column:nama_instalasi"`
Status_Rawat_Inap uint `json:"status_rawat_inap" gorm:"column:status_rawat_inap"`
St_Aktif uint `json:"st_aktif" gorm:"column:st_aktif"`
}
func (MInstalasi) TableName() string {
return "m_instalasi"
}
@@ -0,0 +1,29 @@
package installation
import (
ecore "simrs-vx/internal/domain/base-entities/core"
erc "simrs-vx/internal/domain/references/common"
"time"
)
type InstallationLink struct {
ecore.Main
Simx_Id uint `json:"simx_id" gorm:"unique"`
Simgos_Id uint `json:"simgos_id" gorm:"unique"`
}
type InstallationSimxLog struct {
ecore.Main
Value *string `json:"value"`
Date *time.Time `json:"date"`
Status erc.ProcessStatusCode `json:"status"`
ErrMessage *string `json:"errMessage"`
}
type InstallationSimgosLog struct {
ecore.Main
Value *string `json:"value"`
Date *time.Time `json:"date"`
Status erc.ProcessStatusCode `json:"status"`
ErrMessage *string `json:"errMessage"`
}
@@ -31,6 +31,8 @@ func getEntities(input string) []any {
return getMainEntities()
case "satusehat":
return getSatuSehatEntities()
case "simgossync":
return getSimgosSyncEntities()
}
return nil
}
@@ -0,0 +1,14 @@
package migration
import (
/************** Source ***************/
installation "simrs-vx/internal/domain/sync-entities/installation"
)
func getSimgosSyncEntities() []any {
return []any{
&installation.InstallationLink{},
&installation.InstallationSimxLog{},
&installation.InstallationSimgosLog{},
}
}
+3 -2
View File
@@ -2,6 +2,7 @@
package migration
const (
Main = "main"
SatuSehat = "satusehat"
Main = "main"
SatuSehat = "satusehat"
SimgosSync = "simgossync"
)