Skip to content

Commit

Permalink
gen: ask user if he manages single schema or multiple (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu authored May 6, 2024
1 parent 901af90 commit 3a1c947
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 123 deletions.
1 change: 1 addition & 0 deletions gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type (
ConfigPath string
Env string
CreateDevURL bool
SchemaScope bool
}
)

Expand Down
5 changes: 5 additions & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ func TestGen(t *testing.T) {
SecretName: "ATLAS_CLOUD_TOKEN",
Driver: name,
CreateDevURL: true,
SchemaScope: false,
}
if strings.Contains(name, "atlas_config") {
cfg.Driver = strings.Split(name, "_")[0]
cfg.ConfigPath = "atlas.hcl"
cfg.Env = "dev"
cfg.CreateDevURL = false
}
if strings.Contains(name, "schema_scope") {
cfg.Driver = strings.Split(name, "_")[0]
cfg.SchemaScope = true
}
actual, err := Generate(cfg)
require.NoError(t, err)
require.Equal(t, strings.TrimSpace(string(expected)), strings.TrimSpace(string(actual)))
Expand Down
24 changes: 24 additions & 0 deletions gen/services.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,42 @@

{{- define "UseServices" }}
{{- if eq .Driver "mysql" -}}
{{- if .SchemaScope -}}
dev-url: 'mysql://root:pass@localhost:3306/dev'
{{- else -}}
dev-url: 'mysql://root:pass@localhost:3306'
{{- end -}}
{{- else if eq .Driver "postgres" -}}
{{- if .SchemaScope -}}
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
{{- else -}}
dev-url: 'postgres://postgres:pass@localhost:5432/dev?sslmode=disable'
{{- end -}}
{{- else if eq .Driver "postgis" -}}
{{- if .SchemaScope -}}
dev-url: 'docker://postgis/latest/postgres?search_path=public'
{{- else -}}
dev-url: 'docker://postgis/latest/dev'
{{- end -}}
{{- else if eq .Driver "mariadb" -}}
{{- if .SchemaScope -}}
dev-url: 'maria://root:pass@localhost:3306/dev'
{{- else -}}
dev-url: 'maria://root:pass@localhost:3306'
{{- end -}}
{{- else if eq .Driver "sqlite" -}}
dev-url: 'sqlite://dev?mode=memory'
{{- else if eq .Driver "clickhouse" -}}
{{- if .SchemaScope -}}
dev-url: 'clickhouse://root:pass@localhost:9000/test'
{{- else -}}
dev-url: 'clickhouse://root:pass@localhost:9000'
{{- end -}}
{{- else if eq .Driver "mssql" -}}
{{- if .SchemaScope -}}
dev-url: 'sqlserver://sa:P@ssw0rd0995@localhost:1433/test?mode=schema'
{{- else -}}
dev-url: 'sqlserver://sa:P@ssw0rd0995@localhost:1433/test'
{{- end -}}
{{- end -}}
{{- end -}}
4 changes: 2 additions & 2 deletions gen/testdata/clickhouse.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ jobs:
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'clickhouse://root:pass@localhost:9000/test'
dev-url: 'clickhouse://root:pass@localhost:9000'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'clickhouse://root:pass@localhost:9000/test'
dev-url: 'clickhouse://root:pass@localhost:9000'
4 changes: 2 additions & 2 deletions gen/testdata/mariadb.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ jobs:
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'maria://root:pass@localhost:3306/dev'
dev-url: 'maria://root:pass@localhost:3306'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'maria://root:pass@localhost:3306/dev'
dev-url: 'maria://root:pass@localhost:3306'
4 changes: 2 additions & 2 deletions gen/testdata/mysql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ jobs:
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'mysql://root:pass@localhost:3306/dev'
dev-url: 'mysql://root:pass@localhost:3306'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'mysql://root:pass@localhost:3306/dev'
dev-url: 'mysql://root:pass@localhost:3306'
53 changes: 53 additions & 0 deletions gen/testdata/mysql_schema_scope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Atlas
on:
push:
branches:
- master
paths:
- .github/workflows/ci-atlas.yaml
- 'migrations/*'
pull_request:
paths:
- 'migrations/*'
# Permissions to write comments on the pull request.
permissions:
contents: read
pull-requests: write
jobs:
atlas:
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 }}
- uses: ariga/atlas-action/migrate/lint@v1
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'mysql://root:pass@localhost:3306/dev'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'mysql://root:pass@localhost:3306/dev'
4 changes: 2 additions & 2 deletions gen/testdata/postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ jobs:
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?sslmode=disable'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?sslmode=disable'
53 changes: 53 additions & 0 deletions gen/testdata/postgres_schema_scope.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Atlas
on:
push:
branches:
- master
paths:
- .github/workflows/ci-atlas.yaml
- 'migrations/*'
pull_request:
paths:
- 'migrations/*'
# Permissions to write comments on the pull request.
permissions:
contents: read
pull-requests: write
jobs:
atlas:
services:
# Spin up a postgres:15 container to be used as the dev-database for analysis.
postgres:
image: postgres:15
env:
POSTGRES_DB: dev
POSTGRES_PASSWORD: pass
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 5
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 }}
- uses: ariga/atlas-action/migrate/lint@v1
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
env:
GITHUB_TOKEN: ${{ github.token }}
- uses: ariga/atlas-action/migrate/push@v1
if: github.ref == 'refs/heads/master'
with:
dir: 'file://migrations'
dir-name: 'name'
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
24 changes: 13 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ var cli struct {

// InitActionCmd is the command for initializing a new Atlas CI workflow.
type InitActionCmd struct {
DirPath string `arg:"" optional:"" type:"-path" help:"Path inside repository containing the migration files."`
Driver string `enum:"mysql,postgres,postgis,mariadb,sqlite,mssql,clickhouse" default:"mysql" help:"Driver of the migration directory (mysql,postgres,postgis,mariadb,sqlite,mssql,clickhouse)."`
Token string `short:"t" help:"Atlas authentication token."`
Repo string `short:"R" help:"GitHub repository owner/name, defaults to the current repository."`
ConfigPath string `optional:"" help:"Path to atlas.hcl configuration file."`
ConfigEnv string `optional:"" help:"The environment to use from the Atlas configuration file."`
HasDevURL bool `optional:"" help:"Whether the environment config has a dev_url attribute." default:"false"`
DirName string `optional:"" help:"Name of target migration directory in Atlas Cloud."`
Replace bool `optional:"" help:"Replace existing Atlas CI workflow."`
stdin io.ReadCloser `hidden:""`
cloudURL string `hidden:""`
DirPath string `arg:"" optional:"" type:"-path" help:"Path inside repository containing the migration files."`
Driver string `enum:"mysql,postgres,postgis,mariadb,sqlite,mssql,clickhouse" default:"mysql" help:"Driver of the migration directory (mysql,postgres,postgis,mariadb,sqlite,mssql,clickhouse)."`
Token string `short:"t" help:"Atlas authentication token."`
Repo string `short:"R" help:"GitHub repository owner/name, defaults to the current repository."`
ConfigPath string `optional:"" help:"Path to atlas.hcl configuration file."`
ConfigEnv string `optional:"" help:"The environment to use from the Atlas configuration file."`
HasDevURL bool `optional:"" help:"Whether the environment config has a dev_url attribute." default:"false"`
SchemaScope bool `optional:"" help:"Limit the scope of the work done by Atlas (inspection, diffing, etc.) to one schema."`
DirName string `optional:"" help:"Name of target migration directory in Atlas Cloud."`
Replace bool `optional:"" help:"Replace existing Atlas CI workflow."`
stdin io.ReadCloser `hidden:""`
cloudURL string `hidden:""`
}

func (i *InitActionCmd) Help() string {
Expand Down Expand Up @@ -139,6 +140,7 @@ func (i *InitActionCmd) Run(ctx context.Context, client *githubClient, current r
ConfigPath: i.ConfigPath,
Env: i.ConfigEnv,
CreateDevURL: !i.HasDevURL,
SchemaScope: i.SchemaScope,
}
if err = repo.AddAtlasYAML(ctx, cfg, branchName, commitMsg, i.Replace); err != nil {
return err
Expand Down
Loading

0 comments on commit 3a1c947

Please sign in to comment.