Skip to content

Commit

Permalink
add postgis driver (#328)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu authored May 5, 2024
1 parent 728aa2f commit 901af90
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 121 deletions.
2 changes: 1 addition & 1 deletion gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (
}
)

var Drivers = []string{"mysql", "postgres", "mariadb", "sqlite", "mssql", "clickhouse"}
var Drivers = []string{"mysql", "postgres", "postgis", "mariadb", "sqlite", "mssql", "clickhouse"}

func validateDriver(s string) error {
if !slices.Contains(Drivers, s) {
Expand Down
2 changes: 2 additions & 0 deletions gen/services.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
dev-url: 'mysql://root:pass@localhost:3306/dev'
{{- else if eq .Driver "postgres" -}}
dev-url: 'postgres://postgres:pass@localhost:5432/dev?search_path=public&sslmode=disable'
{{- else if eq .Driver "postgis" -}}
dev-url: 'docker://postgis/latest/dev'
{{- else if eq .Driver "mariadb" -}}
dev-url: 'maria://root:pass@localhost:3306/dev'
{{- else if eq .Driver "sqlite" -}}
Expand Down
38 changes: 38 additions & 0 deletions gen/testdata/postgis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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:
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: 'docker://postgis/latest/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: 'docker://postgis/latest/dev'
2 changes: 1 addition & 1 deletion github.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (r *Repository) ConfigFiles(ctx context.Context) ([]string, error) {
return paths, nil
}

// ReadContent of the file at the given path.
// Implementation of the ContentReader interface.
func (r *Repository) ReadContent(ctx context.Context, path string) (string, error) {
fileContents, _, _, err := r.client.Repositories.GetContents(ctx, r.owner, r.name, path, nil)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,mssql,clickhouse" default:"mysql" help:"Driver of the migration directory (mysql,postgres,mariadb,sqlite,mssql,clickhouse)."`
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."`
Expand Down Expand Up @@ -96,7 +96,15 @@ func (i *InitActionCmd) Run(ctx context.Context, client *githubClient, current r
return err
}
repo := NewRepository(client, current, repoData.GetDefaultBranch())
if err = i.setParams(ctx, repo); err != nil {
dirs, err := repo.MigrationDirectories(ctx)
if err != nil {
return err
}
configs, err := repo.ConfigFiles(ctx)
if err != nil {
return err
}
if err = i.setParams(ctx, dirs, configs, repo); err != nil {
return err
}
cloud := cloudapi.New(i.cloudURL, i.Token)
Expand Down
21 changes: 13 additions & 8 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ func TestRunInitActionCmd(t *testing.T) {
{
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set up the terminal interaction
r, w, err := os.Pipe()
require.NoError(t, err)
_, err = w.WriteString(tt.prompt)
Expand All @@ -459,15 +460,19 @@ func TestRunInitActionCmd(t *testing.T) {
return
}
require.NoError(t, err)
require.Equal(t, tt.expected.Token, tt.cmd.Token)
require.Equal(t, tt.expected.Driver, tt.cmd.Driver)
require.Equal(t, tt.expected.DirPath, tt.cmd.DirPath)
require.Equal(t, tt.expected.DirName, tt.cmd.DirName)
require.Equal(t, tt.expected.Replace, tt.cmd.Replace)
require.Equal(t, tt.expected.ConfigPath, tt.cmd.ConfigPath)
require.Equal(t, tt.expected.ConfigEnv, tt.cmd.ConfigEnv)
require.Equal(t, tt.expected.HasDevURL, tt.cmd.HasDevURL)
requireCommandsEqual(t, tt.expected, tt.cmd)
})
}
}
}

func requireCommandsEqual(t *testing.T, a, b *InitActionCmd) {
require.Equal(t, a.DirPath, b.DirPath)
require.Equal(t, a.DirName, b.DirName)
require.Equal(t, a.Driver, b.Driver)
require.Equal(t, a.Token, b.Token)
require.Equal(t, a.ConfigPath, b.ConfigPath)
require.Equal(t, a.ConfigEnv, b.ConfigEnv)
require.Equal(t, a.HasDevURL, b.HasDevURL)
require.Equal(t, a.Replace, b.Replace)
}
127 changes: 127 additions & 0 deletions pormpt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package main

import (
"context"
"os"
"testing"

"github.com/stretchr/testify/require"
)

func TestSetConfigPath(t *testing.T) {
var tests = []struct {
name string
configs []string // input to the setConfigPath function
cr ContentReader // input to the setConfigPath function
prompt string // user interaction with the terminal
expected *InitActionCmd // expected command after user interaction
}{
{
name: "no config files",
configs: []string{},
expected: &InitActionCmd{},
},
{
name: "1 config file, dont use it",
cr: &mockContentReader{
content: `env "local" {}`,
},
configs: []string{"atlas.hcl"},
// arrow key down and then enter
prompt: "\x1b[B\n\n",
expected: &InitActionCmd{
ConfigPath: "",
},
},
{
name: "1 config file, use it",
cr: &mockContentReader{
content: `env "local" {
dev = "postgres://localhost:5432/dev"
}`,
},
configs: []string{"atlas.hcl"},
prompt: "\n",
expected: &InitActionCmd{
ConfigPath: "atlas.hcl",
ConfigEnv: "local",
HasDevURL: true,
},
},
{
name: "1 config file, witout dev url",
cr: &mockContentReader{
content: `env "local" {}`,
},
configs: []string{"atlas.hcl"},
prompt: "\n",
expected: &InitActionCmd{
ConfigPath: "atlas.hcl",
ConfigEnv: "local",
},
},
{
name: "2 config files, use second",
cr: &mockContentReader{
content: `env "local" {
dev = "postgres://localhost:5432/dev"
}`,
},
configs: []string{"atlas.hcl", "atlas2.hcl"},
// arrow key down, enter, enter
prompt: "\x1b[B\n\n",
expected: &InitActionCmd{
ConfigPath: "atlas2.hcl",
ConfigEnv: "local",
HasDevURL: true,
},
},
{
name: " 2 config files, multiple envs, select first file and second env",
cr: &mockContentReader{
content: `
env "local" {
dev = "postgres://localhost:5432/dev"
}
env "prod" {
dev = "postgres://localhost:5432/dev"
}`,
},
configs: []string{"atlas.hcl", "atlas2.hcl"},
// enter, arrow key down, enter
prompt: "\n\x1b[B\n",
expected: &InitActionCmd{
ConfigPath: "atlas.hcl",
ConfigEnv: "prod",
HasDevURL: true,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// set up the terminal interaction
r, w, err := os.Pipe()
require.NoError(t, err)
_, err = w.WriteString(tt.prompt)
require.NoError(t, err)
err = w.Close()
require.NoError(t, err)

// run the command
cmd := &InitActionCmd{
stdin: &stdinBuffer{r},
}
err = cmd.setConfigPath(context.Background(), tt.configs, tt.cr)
require.NoError(t, err)
requireCommandsEqual(t, tt.expected, cmd)
})
}
}

type mockContentReader struct {
content string
}

func (m *mockContentReader) ReadContent(_ context.Context, _ string) (string, error) {
return m.content, nil
}
Loading

0 comments on commit 901af90

Please sign in to comment.