Skip to content

Commit 676fe74

Browse files
authored
CLI: run migration with -mod=mod and add validation (#137)
* CLI: disable read only mod and add validation * reorder * use -mod=mod flag * update codecov action
1 parent 3aebb7b commit 676fe74

File tree

5 files changed

+54
-6
lines changed

5 files changed

+54
-6
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,4 @@ jobs:
122122
POSTGRESQL_DATABASE: postgres://rel:rel@localhost/rel_test
123123
run: go test -race -tags=all -coverprofile=coverage.txt -covermode=atomic ./...
124124
- name: Codecov
125-
uses: codecov/codecov-action@v1.0.13
125+
uses: codecov/codecov-action@v1

cmd/rel/internal/migrate.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"flag"
7+
"fmt"
78
"html/template"
89
"io"
910
"io/ioutil"
@@ -110,6 +111,10 @@ func ExecMigrate(ctx context.Context, args []string) error {
110111

111112
fs.Parse(args[2:])
112113

114+
if *adapter == "" || *driver == "" || *dsn == "" {
115+
return fmt.Errorf("rel: missing required parameters:\n\tadapter: %s\n\tdriver: %s\n\tdsn: %s", *adapter, *driver, *dsn)
116+
}
117+
113118
file, err := ioutil.TempFile(tempdir, "rel-*.go")
114119
check(err)
115120
defer os.Remove(file.Name())
@@ -139,7 +144,7 @@ func ExecMigrate(ctx context.Context, args []string) error {
139144
check(err)
140145
check(file.Close())
141146

142-
cmd := exec.CommandContext(ctx, "go", "run", "-mod=readonly", file.Name(), "migrate")
147+
cmd := exec.CommandContext(ctx, "go", "run", "-mod=mod", file.Name(), "migrate")
143148
cmd.Stdout = stdout
144149
cmd.Stderr = stderr
145150
return cmd.Run()

cmd/rel/internal/migrate_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,27 @@ import (
1111
)
1212

1313
func TestExecMigrate(t *testing.T) {
14+
t.Run("missing required parameters", func(t *testing.T) {
15+
var (
16+
ctx = context.TODO()
17+
args = []string{
18+
"rel",
19+
"migrate",
20+
}
21+
)
22+
23+
assert.Equal(t, errors.New("rel: missing required parameters:\n\tadapter: \n\tdriver: \n\tdsn: "), ExecMigrate(ctx, args))
24+
})
25+
1426
t.Run("invalid migration dir", func(t *testing.T) {
1527
var (
1628
ctx = context.TODO()
1729
args = []string{
1830
"rel",
1931
"migrate",
32+
"-adapter=github.com/go-rel/rel/adapter/sqlite3",
33+
"-driver=github.com/mattn/go-sqlite3",
34+
"-dsn=:memory:",
2035
"-dir=db",
2136
}
2237
)

cmd/rel/internal/util.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ func getDatabaseInfo() (string, string, string) {
2121
adapter = os.Getenv("DATABASE_ADAPTER")
2222
driver = os.Getenv("DATABASE_DRIVER")
2323
dsn = os.Getenv("DATABASE_URL")
24+
case os.Getenv("SQLITE3_DATABASE") != "":
25+
adapter = "github.com/go-rel/rel/adapter/sqlite3"
26+
driver = "github.com/mattn/go-sqlite3"
27+
dsn = os.Getenv("SQLITE3_DATABASE")
2428
case os.Getenv("MYSQL_HOST") != "":
2529
adapter = "github.com/go-rel/rel/adapter/mysql"
2630
driver = "github.com/go-sql-driver/mysql"
@@ -39,10 +43,15 @@ func getDatabaseInfo() (string, string, string) {
3943
os.Getenv("POSTGRES_HOST"),
4044
os.Getenv("POSTGRES_PORT"),
4145
os.Getenv("POSTGRES_DATABASE"))
42-
case os.Getenv("SQLITE3_DATABASE") != "":
43-
adapter = "github.com/go-rel/rel/adapter/sqlite3"
44-
driver = "github.com/mattn/go-sqlite3"
45-
dsn = os.Getenv("SQLITE3_DATABASE")
46+
case os.Getenv("POSTGRESQL_HOST") != "":
47+
adapter = "github.com/go-rel/rel/adapter/postgres"
48+
driver = "github.com/lib/pq"
49+
dsn = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
50+
os.Getenv("POSTGRESQL_USERNAME"),
51+
os.Getenv("POSTGRESQL_PASSWORD"),
52+
os.Getenv("POSTGRESQL_HOST"),
53+
os.Getenv("POSTGRESQL_PORT"),
54+
os.Getenv("POSTGRESQL_DATABASE"))
4655
}
4756

4857
return adapter, driver, dsn

cmd/rel/internal/util_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ func TestGetDatabaseInfo(t *testing.T) {
6363
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=disable", url)
6464
})
6565

66+
t.Run("postgresql alternative", func(t *testing.T) {
67+
os.Setenv("POSTGRESQL_HOST", "localhost")
68+
os.Setenv("POSTGRESQL_PORT", "5432")
69+
os.Setenv("POSTGRESQL_DATABASE", "db")
70+
os.Setenv("POSTGRESQL_USERNAME", "user")
71+
os.Setenv("POSTGRESQL_PASSWORD", "password")
72+
73+
defer os.Setenv("POSTGRESQL_HOST", "")
74+
defer os.Setenv("POSTGRESQL_PORT", "")
75+
defer os.Setenv("POSTGRESQL_DATABASE", "")
76+
defer os.Setenv("POSTGRESQL_USERNAME", "")
77+
defer os.Setenv("POSTGRESQL_PASSWORD", "")
78+
79+
adapter, driver, url := getDatabaseInfo()
80+
assert.Equal(t, "github.com/go-rel/rel/adapter/postgres", adapter)
81+
assert.Equal(t, "github.com/lib/pq", driver)
82+
assert.Equal(t, "postgres://user:password@localhost:5432/db?sslmode=disable", url)
83+
})
84+
6685
t.Run("sqlite3", func(t *testing.T) {
6786
os.Setenv("SQLITE3_DATABASE", "test.db")
6887
defer os.Setenv("SQLITE3_DATABASE", "")

0 commit comments

Comments
 (0)