Skip to content

Commit

Permalink
feat(template): add schema push & schema apply
Browse files Browse the repository at this point in the history
  • Loading branch information
luantranminh committed Sep 17, 2024
1 parent be05417 commit 9e94d71
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 9 deletions.
2 changes: 0 additions & 2 deletions gen/atlas.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ permissions:
{{- end }}

{{- define "job-setup" }}
jobs:
atlas:
{{- if .CreateDevURL }}
{{- template "services" . }}
{{- end }}
Expand Down
58 changes: 56 additions & 2 deletions gen/declarative.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ on:
branches: [ {{ .DefaultBranch }} ]
workflow_dispatch:
{{- template "permissions" . }}
{{- template "job-setup" . }}
- uses: ariga/atlas-action/schema/plan@v1
jobs:
plan:
if: {{`${{ github.event_name == 'pull_request' }}`}}
{{- template "job-setup" . }}
- uses: ariga/atlas-action/schema/plan@master
with:
{{- if .From }}
from: {{ .From }}
Expand All @@ -24,5 +27,56 @@ on:
{{- with .Env }}
env: '{{ . }}'
{{- end }}
env:
GITHUB_TOKEN: {{`${{ github.token }}`}}
apply:
if: {{`${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}`}}
{{- template "job-setup" . }}
- uses: ariga/atlas-action/schema/push@master
with:
{{- if .CreateDevURL }}
{{ template "UseServices" . }}
{{- end -}}
{{- with .ConfigPath }}
config: 'file://{{ . }}'
{{- end }}
{{- with .Env }}
env: '{{ . }}'
{{- end }}
env:
GITHUB_TOKEN: {{`${{ github.token }}`}}
- uses: ariga/atlas-action/schema/plan@master
id: schema-plan
with:
{{- if .From }}
from: {{ .From }}
{{- end }}
{{- if .To }}
to: {{ .To }}
{{- end }}
{{- if .CreateDevURL }}
{{ template "UseServices" . }}
{{- end -}}
{{- with .ConfigPath }}
config: 'file://{{ . }}'
{{- end }}
{{- with .Env }}
env: '{{ . }}'
{{- end }}
env:
GITHUB_TOKEN: {{`${{ github.token }}`}}
- uses: ariga/atlas-action/schema/apply@master
if: {{`${{ steps.schema-plan.outputs.status == 'APPROVED' }}`}}
with:
{{- if .CreateDevURL }}
{{ template "UseServices" . }}
{{- end -}}
{{- with .ConfigPath }}
config: 'file://{{ . }}'
{{- end }}
{{- with .Env }}
env: '{{ . }}'
{{- end }}
plan: {{`${{ steps.schema-plan.outputs.plan }}`}}
env:
GITHUB_TOKEN: {{`${{ github.token }}`}}
1 change: 1 addition & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestDeclarativeFlowGen(t *testing.T) {
expected, err := os.ReadFile("testdata/declarative/" + tc.filename)
require.NoError(t, err)
actual, err := Generate(tc.config)
os.WriteFile("testdata/declarative1/"+tc.filename, actual, 0644)

Check failure on line 92 in gen/gen_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

Error return value of `os.WriteFile` is not checked (errcheck)
require.NoError(t, err)
require.Equal(t, strings.TrimSpace(string(expected)), strings.TrimSpace(string(actual)))
})
Expand Down
56 changes: 54 additions & 2 deletions gen/testdata/declarative/plan_full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ permissions:
contents: read
pull-requests: write
jobs:
atlas:
plan:
if: ${{ github.event_name == 'pull_request' }}
services:
# Spin up a mysql:8 container to be used as the dev-database for analysis.
mysql:
Expand All @@ -34,12 +35,63 @@ jobs:
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN_X1 }}
- uses: ariga/atlas-action/schema/plan@v1
- uses: ariga/atlas-action/schema/plan@master
with:
from: atlas://myrepo:v1
to: atlas://myrepo:v2
dev-url: 'mysql://root:pass@localhost:3306'
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
env:
GITHUB_TOKEN: ${{ github.token }}
apply:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
services:
# Spin up a mysql:8 container to be used as the dev-database for analysis.
mysql:
image: mysql:8
env:
MYSQL_DATABASE: dev
MYSQL_ROOT_PASSWORD: pass
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -ppass"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN_X1 }}
- uses: ariga/atlas-action/schema/push@master
with:
dev-url: 'mysql://root:pass@localhost:3306'
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/schema/plan@master
id: schema-plan
with:
from: atlas://myrepo:v1
to: atlas://myrepo:v2
dev-url: 'mysql://root:pass@localhost:3306'
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/schema/apply@master
if: ${{ steps.schema-plan.outputs.status == 'APPROVED' }}
with:
dev-url: 'mysql://root:pass@localhost:3306'
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
plan: ${{ steps.schema-plan.outputs.plan }}
env:
GITHUB_TOKEN: ${{ github.token }}
37 changes: 35 additions & 2 deletions gen/testdata/declarative/plan_only_from.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ permissions:
contents: read
pull-requests: write
jobs:
atlas:
plan:
if: ${{ github.event_name == 'pull_request' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -19,10 +20,42 @@ jobs:
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN_X3 }}
- uses: ariga/atlas-action/schema/plan@v1
- uses: ariga/atlas-action/schema/plan@master
with:
from: atlas://myrepo:latest
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
env:
GITHUB_TOKEN: ${{ github.token }}
apply:
if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/master' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: ariga/setup-atlas@v0
with:
cloud-token: ${{ secrets.ATLAS_CLOUD_TOKEN_X3 }}
- uses: ariga/atlas-action/schema/push@master
with:
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/schema/plan@master
id: schema-plan
with:
from: atlas://myrepo:latest
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/schema/apply@master
if: ${{ steps.schema-plan.outputs.status == 'APPROVED' }}
with:
config: 'file://atlas.hcl'
env: 'GORM_MIGRATION_SOURCE'
plan: ${{ steps.schema-plan.outputs.plan }}
env:
GITHUB_TOKEN: ${{ github.token }}
4 changes: 3 additions & 1 deletion gen/versioned.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ on:
paths:
- '{{ .Path }}/*'
{{- template "permissions" . }}
{{- template "job-setup" . }}
jobs:
atlas:
{{- template "job-setup" . }}
- uses: ariga/atlas-action/migrate/lint@v1
with:
dir: 'file://{{ .Path }}'
Expand Down

0 comments on commit 9e94d71

Please sign in to comment.