-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: atlas and golang-migrate integration (#515)
- Loading branch information
Showing
15 changed files
with
700 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 . \" \" }}" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.