diff --git a/go.mod b/go.mod index 9c2667e..08aef0f 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/gin-gonic/gin v1.9.1 github.com/go-jose/go-jose/v3 v3.0.3 github.com/golang-jwt/jwt/v5 v5.2.1 + github.com/jackc/pgx/v5 v5.5.5 github.com/jaevor/go-nanoid v1.3.0 github.com/jmoiron/sqlx v1.3.5 github.com/labstack/echo-contrib v0.16.0 @@ -76,10 +77,12 @@ require ( github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-ole/go-ole v1.2.6 // indirect github.com/go-openapi/inflect v0.19.0 // indirect + github.com/gofrs/flock v0.8.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang-jwt/jwt v3.2.2+incompatible // indirect github.com/google/go-cmp v0.6.0 // indirect github.com/hashicorp/hcl/v2 v2.13.0 // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect github.com/klauspost/compress v1.17.7 // indirect github.com/klauspost/cpuid/v2 v2.2.7 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect diff --git a/goosex/cobra.go b/goosex/cobra.go index 2ceddbe..aa9d72b 100644 --- a/goosex/cobra.go +++ b/goosex/cobra.go @@ -18,8 +18,9 @@ import ( "context" "io/fs" - _ "github.com/cockroachdb/cockroach-go/v2/crdb/crdbpgx" // crdb retries and postgres interface - _ "github.com/lib/pq" // Register the Postgres driver. + _ "github.com/cockroachdb/cockroach-go/v2/crdb/crdbpgxv5" // crdb retries and postgres interface + _ "github.com/jackc/pgx/v5/stdlib" // Register pgx driver. + _ "github.com/lib/pq" // Register the Postgres driver. "github.com/pressly/goose/v3" "github.com/spf13/cobra" "go.uber.org/zap" @@ -28,8 +29,9 @@ import ( ) var ( - dbURI string - logger *zap.SugaredLogger + dbURI string + + logger = zap.NewNop().Sugar() ) // RegisterCobraCommand will add a migrate command to the cobra command provided diff --git a/goosex/cobra_test.go b/goosex/cobra_test.go new file mode 100644 index 0000000..133be7c --- /dev/null +++ b/goosex/cobra_test.go @@ -0,0 +1,92 @@ +// Copyright 2023 The Infratographer Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package goosex_test + +import ( + "context" + "testing" + "testing/fstest" + + "github.com/cockroachdb/cockroach-go/v2/testserver" + "github.com/pressly/goose/v3" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap" + + "go.infratographer.com/x/goosex" +) + +var ( + migrations = fstest.MapFS{ + "migrations/0001_create-testtable.sql": &fstest.MapFile{ + Data: []byte(` +-- +goose Up +-- create "testtable" table +CREATE TABLE "testtable" ( + "id" character varying NOT NULL, + "name" character varying(64) NOT NULL, + PRIMARY KEY ("id") +); +-- +goose Down +-- reverse: create "testtable" table +DROP TABLE "testtable"; + `), + }, + } +) + +func TestMigrateUpContext(t *testing.T) { + logger, err := zap.NewDevelopmentConfig().Build() + require.NoError(t, err) + + server, err := testserver.NewTestServer() + require.NoError(t, err) + + defer server.Stop() + + ctx := context.Background() + + goosex.SetLogger(logger.Sugar()) + goosex.MigrateUpContext(ctx, server.PGURL().String(), migrations) + + db, err := goose.OpenDBWithDriver("postgres", server.PGURL().String()) + require.NoError(t, err) + + rows, err := db.QueryContext(ctx, "SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = 'public'") + require.NoError(t, err) + + var ( + testTableFound bool + versionsTableFound bool + ) + + for rows.Next() { + var tableName string + + if err := rows.Scan(&tableName); err != nil { + require.NoError(t, err) + } + + switch tableName { + case "testtable": + testTableFound = true + case "goose_db_version": + versionsTableFound = true + } + } + + assert.True(t, testTableFound, "expected test table to exist") + assert.True(t, versionsTableFound, "expected goose versions table to exist") +}