Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix goosex migration pgx not imported / update to crdbpgxv5 and pgx/v5 #217

Merged
merged 3 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions goosex/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
92 changes: 92 additions & 0 deletions goosex/cobra_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
Loading