Skip to content

Commit

Permalink
Fix CHECK handling for PostgreSQL 12
Browse files Browse the repository at this point in the history
  • Loading branch information
k0kubun committed Mar 12, 2021
1 parent 98ca670 commit 0b1421f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
14 changes: 4 additions & 10 deletions adapter/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ func buildDumpTableDDL(table string, columns []column, pkeyCols, indexDefs, fore
fmt.Fprintf(&queryBuilder, " DEFAULT %s", col.Default)
}
if col.Check != "" {
fmt.Fprintf(&queryBuilder, " CHECK %s", col.Check)
if col.CheckNoInherit {
fmt.Fprint(&queryBuilder, " NO INHERIT")
}
fmt.Fprintf(&queryBuilder, " %s", col.Check)
}
}
if len(pkeyCols) > 0 {
Expand Down Expand Up @@ -169,7 +166,6 @@ type column struct {
IsAutoIncrement bool
IsUnique bool
Check string
CheckNoInherit bool
}

func (c *column) GetDataType() string {
Expand Down Expand Up @@ -206,8 +202,7 @@ func (d *PostgresDatabase) getColumns(table string) ([]column, error) {
query := `SELECT s.column_name, s.column_default, s.is_nullable, s.character_maximum_length,
CASE WHEN s.data_type = 'ARRAY' THEN format_type(f.atttypid, f.atttypmod) ELSE s.data_type END,
CASE WHEN p.contype = 'u' THEN true ELSE false END AS uniquekey,
CASE WHEN pc.contype = 'c' THEN pc.consrc ELSE NULL END AS check,
CASE WHEN pc.connoinherit = 't' THEN true ELSE false END AS no_inherit
CASE WHEN pc.contype = 'c' THEN pg_get_constraintdef(pc.oid, true) ELSE NULL END AS check
FROM pg_attribute f
JOIN pg_class c ON c.oid = f.attrelid JOIN pg_type t ON t.oid = f.atttypid
LEFT JOIN pg_attrdef d ON d.adrelid = c.oid AND d.adnum = f.attnum
Expand All @@ -229,8 +224,8 @@ WHERE c.relkind = 'r'::char AND n.nspname = $1 AND c.relname = $2 AND f.attnum >
col := column{}
var colName, isNullable, dataType string
var maxLenStr, colDefault, check *string
var isUnique, noInherit bool
err = rows.Scan(&colName, &colDefault, &isNullable, &maxLenStr, &dataType, &isUnique, &check, &noInherit)
var isUnique bool
err = rows.Scan(&colName, &colDefault, &isNullable, &maxLenStr, &dataType, &isUnique, &check)
if err != nil {
return nil, err
}
Expand All @@ -254,7 +249,6 @@ WHERE c.relkind = 'r'::char AND n.nspname = $1 AND c.relname = $2 AND f.attnum >
col.Length = maxLen
if check != nil {
col.Check = *check
col.CheckNoInherit = noInherit
}
cols = append(cols, col)
}
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ services:
ports:
- '3306:3306'
postgres:
image: postgres:11
image: postgres:12
environment:
POSTGRES_USER: postgres
POSTGRES_HOST_AUTH_METHOD: trust
Expand Down

0 comments on commit 0b1421f

Please sign in to comment.