dialect/sql/schema: add pg user defined type for schema introspect (#994)

* Added user defined fields

* pr feedback

* added integration test

* run generate

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

* Update dialect/sql/schema/postgres_test.go

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>

Co-authored-by: Ariel Mashraki <7413593+a8m@users.noreply.github.com>
This commit is contained in:
Ciaran Liedeman
2020-12-27 20:00:47 +02:00
committed by GitHub
parent 6355f5dac3
commit 7320b0d16c
33 changed files with 4785 additions and 84 deletions

View File

@@ -87,7 +87,7 @@ func (d *Postgres) setRange(ctx context.Context, tx dialect.Tx, t *Table, value
func (d *Postgres) table(ctx context.Context, tx dialect.Tx, name string) (*Table, error) {
rows := &sql.Rows{}
query, args := sql.Dialect(dialect.Postgres).
Select("column_name", "data_type", "is_nullable", "column_default").
Select("column_name", "data_type", "is_nullable", "column_default", "udt_name").
From(sql.Table("columns").Schema("information_schema")).
Where(sql.And(
sql.EQ("table_schema", sql.Raw("CURRENT_SCHEMA()")),
@@ -216,8 +216,9 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
var (
nullable sql.NullString
defaults sql.NullString
udt sql.NullString
)
if err := rows.Scan(&c.Name, &c.typ, &nullable, &defaults); err != nil {
if err := rows.Scan(&c.Name, &c.typ, &nullable, &defaults, &udt); err != nil {
return fmt.Errorf("scanning column description: %v", err)
}
if nullable.Valid {
@@ -251,6 +252,14 @@ func (d *Postgres) scanColumn(c *Column, rows *sql.Rows) error {
c.Type = field.TypeUUID
case "cidr", "inet", "macaddr", "macaddr8":
c.Type = field.TypeOther
case "USER-DEFINED":
c.Type = field.TypeOther
if !udt.Valid {
return fmt.Errorf("missing user defined type")
}
c.SchemaType = map[string]string{dialect.Postgres: udt.String}
}
switch {
case !defaults.Valid || c.Type == field.TypeTime || seqfunc(defaults.String):

View File

@@ -192,10 +192,38 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "nextval('users_colname_seq'::regclass)"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "nextval('users_colname_seq'::regclass)", "int4"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
mock.ExpectCommit()
},
},
{
name: "scan table with custom type",
tables: []*Table{
{
Name: "users",
Columns: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
{Name: "custom", Type: field.TypeOther, SchemaType: map[string]string{dialect.Postgres: "customtype"}},
},
PrimaryKey: []*Column{
{Name: "id", Type: field.TypeInt, Increment: true},
},
},
},
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "nextval('users_colname_seq'::regclass)", "NULL").
AddRow("custom", "USER-DEFINED", "NO", "NULL", "customtype"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -229,20 +257,20 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character varying", "YES", "NULL").
AddRow("uuid", "uuid", "YES", "NULL").
AddRow("created_at", "date", "NO", "CURRENT_DATE").
AddRow("updated_at", "timestamp", "YES", "NULL").
AddRow("deleted_at", "date", "YES", "NULL").
AddRow("text", "text", "YES", "NULL").
AddRow("cidr", "cidr", "NO", "NULL").
AddRow("inet", "inet", "YES", "NULL").
AddRow("macaddr", "macaddr", "YES", "NULL").
AddRow("macaddr8", "macaddr8", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character varying", "YES", "NULL", "varchar").
AddRow("uuid", "uuid", "YES", "NULL", "uuid").
AddRow("created_at", "date", "NO", "CURRENT_DATE", "date").
AddRow("updated_at", "timestamp", "YES", "NULL", "timestamptz").
AddRow("deleted_at", "date", "YES", "NULL", "date").
AddRow("text", "text", "YES", "NULL", "text").
AddRow("cidr", "cidr", "NO", "NULL", "cidr").
AddRow("inet", "inet", "YES", "NULL", "inet").
AddRow("macaddr", "macaddr", "YES", "NULL", "macaddr").
AddRow("macaddr8", "macaddr8", "YES", "NULL", "macaddr8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -270,12 +298,12 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "YES", "NULL").
AddRow("doc", "jsonb", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar").
AddRow("doc", "jsonb", "YES", "NULL", "jsonb"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -304,12 +332,12 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "YES", "NULL").
AddRow("doc", "jsonb", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar").
AddRow("doc", "jsonb", "YES", "NULL", "jsonb"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -336,11 +364,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -367,11 +395,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -398,11 +426,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -428,11 +456,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -458,11 +486,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("name", "character", "NO", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("name", "character", "NO", "NULL", "bpchar"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -488,11 +516,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("age", "bigint", "NO", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("age", "bigint", "NO", "NULL", "int8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -518,11 +546,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("age", "bigint", "NO", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("age", "bigint", "NO", "NULL", "int8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0).
@@ -548,11 +576,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("age", "bigint", "NO", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("age", "bigint", "NO", "NULL", "int8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0).
@@ -602,12 +630,12 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("age", "bigint", "NO", "NULL").
AddRow("score", "bigint", "NO", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("age", "bigint", "NO", "NULL", "int8").
AddRow("score", "bigint", "NO", "NULL", "int8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0).
@@ -623,11 +651,11 @@ func TestPostgres_Create(t *testing.T) {
mock.ExpectExec(escape(`CREATE UNIQUE INDEX "user_score" ON "users"("score")`)).
WillReturnResult(sqlmock.NewResult(0, 1))
mock.tableExists("equipment", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("equipment").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "NO", "NULL").
AddRow("score", "bigint", "NO", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "NO", "NULL", "int8").
AddRow("score", "bigint", "NO", "NULL", "int8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "equipment"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0).
@@ -664,11 +692,11 @@ func TestPostgres_Create(t *testing.T) {
before: func(mock pgMock) {
mock.start("120000")
mock.tableExists("users", true)
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "YES", "NULL").
AddRow("name", "character", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "YES", "NULL", "int8").
AddRow("name", "character", "YES", "NULL", "bpchar"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))
@@ -730,10 +758,10 @@ func TestPostgres_Create(t *testing.T) {
// query users table.
mock.tableExists("users", true)
// users table has no changes.
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
mock.ExpectQuery(escape(`SELECT "column_name", "data_type", "is_nullable", "column_default", "udt_name" FROM "information_schema"."columns" WHERE "table_schema" = CURRENT_SCHEMA() AND "table_name" = $1`)).
WithArgs("users").
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default"}).
AddRow("id", "bigint", "YES", "NULL"))
WillReturnRows(sqlmock.NewRows([]string{"column_name", "data_type", "is_nullable", "column_default", "udt_name"}).
AddRow("id", "bigint", "YES", "NULL", "int8"))
mock.ExpectQuery(escape(fmt.Sprintf(indexesQuery, "users"))).
WillReturnRows(sqlmock.NewRows([]string{"index_name", "column_name", "primary", "unique", "seq_in_index"}).
AddRow("users_pkey", "id", "t", "t", 0))