Skip to content

Commit

Permalink
feat: atlas and golang-migrate integration (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanmoysrt authored Apr 13, 2024
1 parent eb08e6e commit 5eeec27
Show file tree
Hide file tree
Showing 15 changed files with 700 additions and 66 deletions.
14 changes: 14 additions & 0 deletions .github/scripts/check_if_migration_missed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env sh

# get output of atlas migrate diff --env gorm
output=$(atlas migrate diff --env gorm --dev-url postgres://postgres:pass@localhost:5432/dev?sslmode=disable)

# check if output contains `no changes to be made`
if echo "$output" | grep -q 'no changes to be made'; then
echo "No migration changes detected"
exit 0
else
echo "Migration changes detected and need to be committed"
echo "Please run ./generate_migration_records.sh <name> to generate new migration files"
exit 1
fi
34 changes: 34 additions & 0 deletions .github/workflows/db-migration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: DB Migration
on:
[push, pull_request]
jobs:
check-uncommitted-db-migrations:
services:
# Spin up a postgres:15 container to be used as the dev-database.
postgres15:
image: postgres:15
env:
POSTGRES_DB: dev
POSTGRES_PASSWORD: pass
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.22
- name: Install atlas
uses: ariga/setup-atlas@master
- name: Install Dependencies
run: go mod download
- name: Check if migration is missed
run: ./.github/scripts/check_if_migration_missed.sh
22 changes: 22 additions & 0 deletions atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
data "external_schema" "gorm" {
program = [
"go",
"run",
"-mod=mod",
"./swiftwave_service/db_models_loader",
]
}

env "gorm" {
src = data.external_schema.gorm.url
dev = "docker://postgres/15/dev"
migration {
dir = "file://swiftwave_service/db/migrations"
format = golang-migrate
}
format {
migrate {
diff = "{{ sql . \" \" }}"
}
}
}
33 changes: 33 additions & 0 deletions generate_migration_records.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env sh

# join of all arguments from first to last using _ as separator
migration_name=$(echo "$@" | tr ' ' '_')

# ask for the migration name
if [ -z "$migration_name" ]; then
read -p "Enter the migration name: " migration_name
fi

# if the migration name is empty, exit
if [ -z "$migration_name" ]; then
echo "Migration name cannot be empty"
exit 1
fi

echo "Migration name: $migration_name"

# anything except a-z0-9_ is not allowed
if echo "$migration_name" | grep -q '[^a-z0-9_]'; then
echo "Migration name can only contain lowercase alphabets, numbers, and underscores"
exit 1
fi

# check if atlas is installed
if ! command -v atlas > /dev/null; then
echo "atlas is not installed"
echo "Run \`curl -sSf https://atlasgo.sh | sh\` to install atlas or visit https://atlasgo.io/getting-started/ to learn more"
exit 1
fi

# create a new migration file
atlas migrate diff --env gorm $migration_name
19 changes: 16 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ module github.com/swiftwave-org/swiftwave
go 1.21.7

require (
ariga.io/atlas-provider-gorm v0.3.2
github.com/99designs/gqlgen v0.17.45
github.com/aws/aws-sdk-go v1.51.21
github.com/docker/docker v26.0.1+incompatible
github.com/fatih/color v1.16.0
github.com/go-git/go-git/v5 v5.12.0
github.com/go-redis/redis/v8 v8.11.5
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang-migrate/migrate/v4 v4.17.0
github.com/hashicorp/go-set v0.1.14
github.com/labstack/echo-jwt/v4 v4.2.0
github.com/labstack/echo/v4 v4.11.4
Expand Down Expand Up @@ -57,7 +59,8 @@ require (
)

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
ariga.io/atlas-go-sdk v0.2.3 // indirect
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.12 // indirect
Expand All @@ -69,8 +72,13 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
Expand All @@ -79,6 +87,8 @@ require (
github.com/klauspost/compress v1.16.7 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
Expand All @@ -99,10 +109,13 @@ require (
go.uber.org/goleak v1.3.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/sync v0.6.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231030173426-d783a09b4405 // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gorm.io/driver/mysql v1.5.1 // indirect
gorm.io/driver/sqlite v1.5.2 // indirect
gorm.io/driver/sqlserver v1.5.2 // indirect
)

require (
Expand Down
Loading

0 comments on commit 5eeec27

Please sign in to comment.