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

[pgx,postgres] Drop also drops custom PostgreSQL types #1049

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS "custom_users";
DROP TYPE IF EXISTS "custom_user_type";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TYPE "custom_user_type" AS ENUM('foo', 'bar', 'qux');

CREATE TABLE "custom_users" (
"user_id" integer unique,
"name" text,
"email" text,
"user_type" custom_user_type
);
58 changes: 51 additions & 7 deletions database/pgx/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,13 +535,57 @@ func (p *Postgres) Drop() (err error) {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(tableNames) > 0 {
// delete one by one ...
for _, t := range tableNames {
query = `DROP TABLE IF EXISTS ` + quoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
for _, t := range tableNames {
query = `DROP TABLE IF EXISTS ` + quoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
}

// select all types
query = `
SELECT t.typname as type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
AND n.nspname = current_schema()
`
types, err := p.conn.QueryContext(context.Background(), query)
if err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
defer func() {
if errClose := types.Close(); errClose != nil {
err = multierror.Append(err, errClose)
}
}()

// delete one table after another
typeNames := make([]string, 0)
for types.Next() {
var typeName string
if err := types.Scan(&typeName); err != nil {
return err
}

// do not drop lock table
if typeName == p.config.LockTable && p.config.LockStrategy == LockStrategyTable {
continue
}

if len(typeName) > 0 {
typeNames = append(typeNames, typeName)
}
}
if err := types.Err(); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

for _, t := range typeNames {
query = `DROP TYPE IF EXISTS ` + quoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
}

Expand Down
52 changes: 45 additions & 7 deletions database/pgx/v5/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,51 @@ func (p *Postgres) Drop() (err error) {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(tableNames) > 0 {
// delete one by one ...
for _, t := range tableNames {
query = `DROP TABLE IF EXISTS ` + quoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
for _, t := range tableNames {
query = `DROP TABLE IF EXISTS ` + quoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
}

// select all custom types
query = `
SELECT t.typname as type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
AND n.nspname = current_schema()
`
types, err := p.conn.QueryContext(context.Background(), query)
if err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
defer func() {
if errClose := types.Close(); errClose != nil {
err = multierror.Append(err, errClose)
}
}()

// delete one table after another
typeNames := make([]string, 0)
for types.Next() {
var tableName string
if err := types.Scan(&tableName); err != nil {
return err
}
if len(tableName) > 0 {
typeNames = append(typeNames, tableName)
}
}
if err := types.Err(); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

for _, t := range typeNames {
query = `DROP TYPE IF EXISTS ` + quoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS "custom_users";
DROP TYPE IF EXISTS "custom_user_type";
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TYPE "custom_user_type" AS ENUM('foo', 'bar', 'qux');

CREATE TABLE "custom_users" (
"user_id" integer unique,
"name" text,
"email" text,
"user_type" custom_user_type
);
52 changes: 45 additions & 7 deletions database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,13 +436,51 @@ func (p *Postgres) Drop() (err error) {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

if len(tableNames) > 0 {
// delete one by one ...
for _, t := range tableNames {
query = `DROP TABLE IF EXISTS ` + pq.QuoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
for _, t := range tableNames {
query = `DROP TABLE IF EXISTS ` + pq.QuoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
}

// select all custom types
query = `
SELECT t.typname as type
FROM pg_type t
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid))
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid)
AND n.nspname = current_schema()
`
types, err := p.conn.QueryContext(context.Background(), query)
if err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
defer func() {
if errClose := types.Close(); errClose != nil {
err = multierror.Append(err, errClose)
}
}()

// delete one type after another
typeNames := make([]string, 0)
for types.Next() {
var typeName string
if err := types.Scan(&typeName); err != nil {
return err
}
if len(typeName) > 0 {
typeNames = append(typeNames, typeName)
}
}
if err := types.Err(); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}

for _, t := range typeNames {
query = `DROP TYPE IF EXISTS ` + pq.QuoteIdentifier(t) + ` CASCADE`
if _, err := p.conn.ExecContext(context.Background(), query); err != nil {
return &database.Error{OrigErr: err, Query: []byte(query)}
}
}

Expand Down
Loading