Skip to content

Commit 5eeec27

Browse files
authored
feat: atlas and golang-migrate integration (#515)
1 parent eb08e6e commit 5eeec27

15 files changed

+700
-66
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env sh
2+
3+
# get output of atlas migrate diff --env gorm
4+
output=$(atlas migrate diff --env gorm --dev-url postgres://postgres:pass@localhost:5432/dev?sslmode=disable)
5+
6+
# check if output contains `no changes to be made`
7+
if echo "$output" | grep -q 'no changes to be made'; then
8+
echo "No migration changes detected"
9+
exit 0
10+
else
11+
echo "Migration changes detected and need to be committed"
12+
echo "Please run ./generate_migration_records.sh <name> to generate new migration files"
13+
exit 1
14+
fi

.github/workflows/db-migration.yml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: DB Migration
2+
on:
3+
[push, pull_request]
4+
jobs:
5+
check-uncommitted-db-migrations:
6+
services:
7+
# Spin up a postgres:15 container to be used as the dev-database.
8+
postgres15:
9+
image: postgres:15
10+
env:
11+
POSTGRES_DB: dev
12+
POSTGRES_PASSWORD: pass
13+
ports:
14+
- 5432:5432
15+
options: >-
16+
--health-cmd pg_isready
17+
--health-interval 10s
18+
--health-timeout 5s
19+
--health-retries 5
20+
runs-on: ubuntu-latest
21+
steps:
22+
- uses: actions/checkout@v4
23+
with:
24+
submodules: recursive
25+
- name: Set up Go
26+
uses: actions/setup-go@v4
27+
with:
28+
go-version: 1.22
29+
- name: Install atlas
30+
uses: ariga/setup-atlas@master
31+
- name: Install Dependencies
32+
run: go mod download
33+
- name: Check if migration is missed
34+
run: ./.github/scripts/check_if_migration_missed.sh

atlas.hcl

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
data "external_schema" "gorm" {
2+
program = [
3+
"go",
4+
"run",
5+
"-mod=mod",
6+
"./swiftwave_service/db_models_loader",
7+
]
8+
}
9+
10+
env "gorm" {
11+
src = data.external_schema.gorm.url
12+
dev = "docker://postgres/15/dev"
13+
migration {
14+
dir = "file://swiftwave_service/db/migrations"
15+
format = golang-migrate
16+
}
17+
format {
18+
migrate {
19+
diff = "{{ sql . \" \" }}"
20+
}
21+
}
22+
}

generate_migration_records.sh

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env sh
2+
3+
# join of all arguments from first to last using _ as separator
4+
migration_name=$(echo "$@" | tr ' ' '_')
5+
6+
# ask for the migration name
7+
if [ -z "$migration_name" ]; then
8+
read -p "Enter the migration name: " migration_name
9+
fi
10+
11+
# if the migration name is empty, exit
12+
if [ -z "$migration_name" ]; then
13+
echo "Migration name cannot be empty"
14+
exit 1
15+
fi
16+
17+
echo "Migration name: $migration_name"
18+
19+
# anything except a-z0-9_ is not allowed
20+
if echo "$migration_name" | grep -q '[^a-z0-9_]'; then
21+
echo "Migration name can only contain lowercase alphabets, numbers, and underscores"
22+
exit 1
23+
fi
24+
25+
# check if atlas is installed
26+
if ! command -v atlas > /dev/null; then
27+
echo "atlas is not installed"
28+
echo "Run \`curl -sSf https://atlasgo.sh | sh\` to install atlas or visit https://atlasgo.io/getting-started/ to learn more"
29+
exit 1
30+
fi
31+
32+
# create a new migration file
33+
atlas migrate diff --env gorm $migration_name

go.mod

+16-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ module github.com/swiftwave-org/swiftwave
33
go 1.21.7
44

55
require (
6+
ariga.io/atlas-provider-gorm v0.3.2
67
github.com/99designs/gqlgen v0.17.45
78
github.com/aws/aws-sdk-go v1.51.21
89
github.com/docker/docker v26.0.1+incompatible
910
github.com/fatih/color v1.16.0
1011
github.com/go-git/go-git/v5 v5.12.0
1112
github.com/go-redis/redis/v8 v8.11.5
1213
github.com/golang-jwt/jwt/v5 v5.2.1
14+
github.com/golang-migrate/migrate/v4 v4.17.0
1315
github.com/hashicorp/go-set v0.1.14
1416
github.com/labstack/echo-jwt/v4 v4.2.0
1517
github.com/labstack/echo/v4 v4.11.4
@@ -57,7 +59,8 @@ require (
5759
)
5860

5961
require (
60-
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
62+
ariga.io/atlas-go-sdk v0.2.3 // indirect
63+
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
6164
github.com/Microsoft/hcsshim v0.11.4 // indirect
6265
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
6366
github.com/containerd/containerd v1.7.12 // indirect
@@ -69,8 +72,13 @@ require (
6972
github.com/go-logr/logr v1.4.1 // indirect
7073
github.com/go-logr/stdr v1.2.2 // indirect
7174
github.com/go-ole/go-ole v1.2.6 // indirect
75+
github.com/go-sql-driver/mysql v1.7.0 // indirect
76+
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
77+
github.com/golang-sql/sqlexp v0.1.0 // indirect
7278
github.com/golang/protobuf v1.5.3 // indirect
7379
github.com/google/go-cmp v0.6.0 // indirect
80+
github.com/hashicorp/errwrap v1.1.0 // indirect
81+
github.com/hashicorp/go-multierror v1.1.1 // indirect
7482
github.com/inconshreveable/mousetrap v1.1.0 // indirect
7583
github.com/jackc/puddle/v2 v2.2.1 // indirect
7684
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
@@ -79,6 +87,8 @@ require (
7987
github.com/klauspost/compress v1.16.7 // indirect
8088
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
8189
github.com/magiconair/properties v1.8.7 // indirect
90+
github.com/mattn/go-sqlite3 v1.14.17 // indirect
91+
github.com/microsoft/go-mssqldb v1.6.0 // indirect
8292
github.com/moby/docker-image-spec v1.3.1 // indirect
8393
github.com/moby/patternmatcher v0.6.0 // indirect
8494
github.com/moby/sys/sequential v0.5.0 // indirect
@@ -99,10 +109,13 @@ require (
99109
go.uber.org/goleak v1.3.0 // indirect
100110
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
101111
golang.org/x/sync v0.6.0 // indirect
102-
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
103-
google.golang.org/grpc v1.58.3 // indirect
112+
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
113+
google.golang.org/grpc v1.59.0 // indirect
104114
google.golang.org/protobuf v1.33.0 // indirect
105115
gopkg.in/warnings.v0 v0.1.2 // indirect
116+
gorm.io/driver/mysql v1.5.1 // indirect
117+
gorm.io/driver/sqlite v1.5.2 // indirect
118+
gorm.io/driver/sqlserver v1.5.2 // indirect
106119
)
107120

108121
require (

0 commit comments

Comments
 (0)