From 728aa2f53a3b8606f251420f534e85da720860d9 Mon Sep 17 00:00:00 2001 From: Ronen Lubin <63970571+ronenlu@users.noreply.github.com> Date: Thu, 22 Feb 2024 09:38:59 +0200 Subject: [PATCH] support clickhouse and mssql (#327) --- gen/gen.go | 9 ++++--- gen/services.tmpl | 37 +++++++++++++++++++++++++ gen/testdata/clickhouse.yml | 54 +++++++++++++++++++++++++++++++++++++ gen/testdata/mssql.yml | 53 ++++++++++++++++++++++++++++++++++++ main.go | 2 +- prompt.go | 3 ++- 6 files changed, 152 insertions(+), 6 deletions(-) create mode 100644 gen/testdata/clickhouse.yml create mode 100644 gen/testdata/mssql.yml diff --git a/gen/gen.go b/gen/gen.go index 07984b0..14d3447 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -5,6 +5,7 @@ import ( "embed" _ "embed" "fmt" + "slices" "text/template" ) @@ -23,13 +24,13 @@ type ( } ) +var Drivers = []string{"mysql", "postgres", "mariadb", "sqlite", "mssql", "clickhouse"} + func validateDriver(s string) error { - switch s { - case "postgres", "mysql", "mariadb", "sqlite": - return nil - default: + if !slices.Contains(Drivers, s) { return fmt.Errorf("unknown driver %q", s) } + return nil } var ( diff --git a/gen/services.tmpl b/gen/services.tmpl index 1a236e6..44baac3 100644 --- a/gen/services.tmpl +++ b/gen/services.tmpl @@ -47,6 +47,39 @@ --health-start-period 10s --health-timeout 5s --health-retries 10 + {{- else if eq .Driver "clickhouse" }} + services: + # Spin up a clickhouse:23.10 container to be used as the dev-database for analysis. + clickhouse: + image: clickhouse/clickhouse-server:23.10 + env: + CLICKHOUSE_DB: test + CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1 + CLICKHOUSE_PASSWORD: pass + CLICKHOUSE_USER: root + ports: + - 9000:9000 + options: >- + --health-cmd "clickhouse-client --host localhost --query 'SELECT 1'" + --health-interval 10s + --health-timeout 5s + --health-retries 5 + {{- else if eq .Driver "mssql" }} + services: + # Spin up a mcr.microsoft.com/mssql/server:2022-latest container to be used as the dev-database for analysis. + sqlserver: + image: mcr.microsoft.com/mssql/server:2022-latest + env: + ACCEPT_EULA: Y + MSSQL_PID: Developer + MSSQL_SA_PASSWORD: P@ssw0rd0995 + ports: + - 1433:1433 + options: >- + --health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P P@ssw0rd0995 -Q \"SELECT 1\"" + --health-interval 10s + --health-timeout 5s + --health-retries 5 {{- end }} {{- end }} @@ -59,5 +92,9 @@ dev-url: 'maria://root:pass@localhost:3306/dev' {{- else if eq .Driver "sqlite" -}} dev-url: 'sqlite://dev?mode=memory' +{{- else if eq .Driver "clickhouse" -}} + dev-url: 'clickhouse://root:pass@localhost:9000/test' +{{- else if eq .Driver "mssql" -}} + dev-url: 'sqlserver://sa:P@ssw0rd0995@localhost:1433/test' {{- end -}} {{- end -}} \ No newline at end of file diff --git a/gen/testdata/clickhouse.yml b/gen/testdata/clickhouse.yml new file mode 100644 index 0000000..4cae5de --- /dev/null +++ b/gen/testdata/clickhouse.yml @@ -0,0 +1,54 @@ +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 clickhouse:23.10 container to be used as the dev-database for analysis. + clickhouse: + image: clickhouse/clickhouse-server:23.10 + env: + CLICKHOUSE_DB: test + CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT: 1 + CLICKHOUSE_PASSWORD: pass + CLICKHOUSE_USER: root + ports: + - 9000:9000 + options: >- + --health-cmd "clickhouse-client --host localhost --query 'SELECT 1'" + --health-interval 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: 'clickhouse://root:pass@localhost:9000/test' + 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' diff --git a/gen/testdata/mssql.yml b/gen/testdata/mssql.yml new file mode 100644 index 0000000..d131533 --- /dev/null +++ b/gen/testdata/mssql.yml @@ -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 mcr.microsoft.com/mssql/server:2022-latest container to be used as the dev-database for analysis. + sqlserver: + image: mcr.microsoft.com/mssql/server:2022-latest + env: + ACCEPT_EULA: Y + MSSQL_PID: Developer + MSSQL_SA_PASSWORD: P@ssw0rd0995 + ports: + - 1433:1433 + options: >- + --health-cmd "/opt/mssql-tools/bin/sqlcmd -U sa -P P@ssw0rd0995 -Q \"SELECT 1\"" + --health-interval 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: 'sqlserver://sa:P@ssw0rd0995@localhost:1433/test' + 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: 'sqlserver://sa:P@ssw0rd0995@localhost:1433/test' diff --git a/main.go b/main.go index 7c36f20..5386cba 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,7 @@ 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,mariadb,sqlite" default:"mysql" help:"Driver of the migration directory (mysql,postgres,mariadb,sqlite)."` + Driver string `enum:"mysql,postgres,mariadb,sqlite,mssql,clickhouse" default:"mysql" help:"Driver of the migration directory (mysql,postgres,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."` diff --git a/prompt.go b/prompt.go index 61706a2..248fc26 100644 --- a/prompt.go +++ b/prompt.go @@ -6,6 +6,7 @@ import ( "fmt" "strings" + "ariga.io/gh-atlas/gen" "github.com/1lann/promptui" "github.com/hashicorp/hcl/v2/hclparse" "github.com/hashicorp/hcl/v2/hclsyntax" @@ -25,7 +26,7 @@ func (i *InitActionCmd) setParams(ctx context.Context, repo *Repository) error { prompt := promptui.Select{ Label: "Choose driver", HideHelp: true, - Items: []string{"mysql", "postgres", "mariadb", "sqlite"}, + Items: gen.Drivers, Stdin: i.stdin, } if _, i.Driver, err = prompt.Run(); err != nil {