bbdb351e5e
What: Use the github.com/jackc/pgx postgresql driver in place of github.com/lib/pq. Why: github.com/lib/pq has some problems with error handling and context cancellations (i.e. it might even issue queries or DML statements more than once! see https://github.com/lib/pq/issues/939). The github.com/jackx/pgx library appears not to have these problems, and also appears to be better engineered and implemented (in particular, it doesn't use "exceptions by panic"). It should also give us some performance improvements in some cases, and even more so if we can use it directly instead of going through the database/sql layer. Change-Id: Ia696d220f340a097dee9550a312d37de14ed2044
110 lines
3.1 KiB
Go
110 lines
3.1 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package pgutil_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
_ "github.com/jackc/pgx/v4/stdlib"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"storj.io/common/testcontext"
|
|
"storj.io/storj/private/dbutil"
|
|
"storj.io/storj/private/dbutil/dbschema"
|
|
"storj.io/storj/private/dbutil/pgtest"
|
|
"storj.io/storj/private/dbutil/pgutil"
|
|
"storj.io/storj/private/dbutil/tempdb"
|
|
)
|
|
|
|
func TestQuery(t *testing.T) {
|
|
pgtest.Run(t, func(ctx *testcontext.Context, t *testing.T, connstr string) {
|
|
db, err := tempdb.OpenUnique(ctx, connstr, "pgutil-query")
|
|
require.NoError(t, err)
|
|
defer ctx.Check(db.Close)
|
|
|
|
emptySchema, err := pgutil.QuerySchema(ctx, db)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, &dbschema.Schema{}, emptySchema)
|
|
|
|
_, err = db.ExecContext(ctx, `
|
|
CREATE TABLE users (
|
|
a bigint NOT NULL,
|
|
b bigint NOT NULL,
|
|
c text,
|
|
UNIQUE (c),
|
|
PRIMARY KEY (a)
|
|
);
|
|
CREATE TABLE names (
|
|
users_a bigint REFERENCES users( a ) ON DELETE CASCADE,
|
|
a text NOT NULL,
|
|
x text,
|
|
b text,
|
|
PRIMARY KEY (a, x),
|
|
UNIQUE ( x ),
|
|
UNIQUE ( a, b )
|
|
);
|
|
`)
|
|
require.NoError(t, err)
|
|
|
|
schema, err := pgutil.QuerySchema(ctx, db)
|
|
require.NoError(t, err)
|
|
|
|
expected := &dbschema.Schema{
|
|
Tables: []*dbschema.Table{
|
|
{
|
|
Name: "users",
|
|
Columns: []*dbschema.Column{
|
|
{Name: "a", Type: "bigint", IsNullable: false, Reference: nil},
|
|
{Name: "b", Type: "bigint", IsNullable: false, Reference: nil},
|
|
{Name: "c", Type: "text", IsNullable: true, Reference: nil},
|
|
},
|
|
PrimaryKey: []string{"a"},
|
|
Unique: [][]string{
|
|
{"c"},
|
|
},
|
|
},
|
|
{
|
|
Name: "names",
|
|
Columns: []*dbschema.Column{
|
|
{Name: "users_a", Type: "bigint", IsNullable: true,
|
|
Reference: &dbschema.Reference{
|
|
Table: "users",
|
|
Column: "a",
|
|
OnDelete: "CASCADE",
|
|
}},
|
|
{Name: "a", Type: "text", IsNullable: false, Reference: nil},
|
|
{Name: "x", Type: "text", IsNullable: false, Reference: nil}, // not null, because primary key
|
|
{Name: "b", Type: "text", IsNullable: true, Reference: nil},
|
|
},
|
|
PrimaryKey: []string{"a", "x"},
|
|
Unique: [][]string{
|
|
{"a", "b"},
|
|
{"x"},
|
|
},
|
|
},
|
|
},
|
|
Indexes: []*dbschema.Index{
|
|
{Name: "names_a_b_key", Table: "names", Columns: []string{"a", "b"}, Unique: true, Partial: ""},
|
|
{Name: "names_pkey", Table: "names", Columns: []string{"a", "x"}, Unique: true, Partial: ""},
|
|
{Name: "names_x_key", Table: "names", Columns: []string{"x"}, Unique: true, Partial: ""},
|
|
{Name: "users_c_key", Table: "users", Columns: []string{"c"}, Unique: true, Partial: ""},
|
|
{Name: "users_pkey", Table: "users", Columns: []string{"a"}, Unique: true, Partial: ""},
|
|
},
|
|
}
|
|
|
|
if db.Implementation == dbutil.Cockroach {
|
|
expected.Indexes = append(expected.Indexes, &dbschema.Index{
|
|
Name: "names_auto_index_fk_users_a_ref_users",
|
|
Table: "names",
|
|
Columns: []string{"users_a"},
|
|
})
|
|
}
|
|
|
|
expected.Sort()
|
|
schema.Sort()
|
|
assert.Equal(t, expected, schema)
|
|
})
|
|
}
|