Relax the definition of unique on columns (psql)

- Fix formatting on giant postgres query
- Invert the section that looks for unique constraints to look for a
  constraint and join to the constraint_column_usage to be more in line
  with the mysql query and also it makes a little bit more sense.
- Add more checks to ensure the schema is being enforced in the postgres
  side of things as several pieces in the unique checks were missing it
  which would lead to false positives.
- Fix #77 for postgres
This commit is contained in:
Aaron L 2017-01-03 19:43:01 -08:00
parent 23b6221f8b
commit 5f7bee14a0

View file

@ -155,16 +155,17 @@ func (p *PostgresDriver) Columns(schema, tableName string) ([]bdb.Column, error)
c.is_nullable = 'YES' as is_nullable, c.is_nullable = 'YES' as is_nullable,
(select exists( (select exists(
select 1 select 1
from information_schema.constraint_column_usage as ccu from information_schema.table_constraints tc
inner join information_schema.table_constraints tc on ccu.constraint_name = tc.constraint_name inner join information_schema.constraint_column_usage as ccu on tc.constraint_name = ccu.constraint_name
where ccu.table_name = c.table_name and ccu.column_name = c.column_name and tc.constraint_type = 'UNIQUE' where tc.table_schema = $1 and tc.constraint_type = 'UNIQUE' and ccu.constraint_schema = $1 and ccu.table_name = c.table_name and ccu.column_name = c.column_name and
)) OR (select exists( (select count(*) from information_schema.constraint_column_usage where constraint_schema = $1 and constraint_name = tc.constraint_name) = 1
)) OR
(select exists(
select 1 select 1
from from pg_indexes pgix
pg_indexes pgix inner join pg_class pgc on pgix.indexname = pgc.relname and pgc.relkind = 'i' and pgc.relnatts = 1
inner join pg_class pgc on pgix.indexname = pgc.relname and pgc.relkind = 'i' inner join pg_index pgi on pgi.indexrelid = pgc.oid
inner join pg_index pgi on pgi.indexrelid = pgc.oid inner join pg_attribute pga on pga.attrelid = pgi.indrelid and pga.attnum = ANY(pgi.indkey)
inner join pg_attribute pga on pga.attrelid = pgi.indrelid and pga.attnum = ANY(pgi.indkey)
where where
pgix.schemaname = $1 and pgix.tablename = c.table_name and pga.attname = c.column_name and pgi.indisunique = true pgix.schemaname = $1 and pgix.tablename = c.table_name and pga.attname = c.column_name and pgi.indisunique = true
)) as is_unique )) as is_unique
@ -173,7 +174,7 @@ func (p *PostgresDriver) Columns(schema, tableName string) ([]bdb.Column, error)
left join information_schema.element_types e left join information_schema.element_types e
on ((c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier) on ((c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier)
= (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier)) = (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier))
where c.table_name=$2 and c.table_schema = $1; where c.table_name = $2 and c.table_schema = $1;
`, schema, tableName) `, schema, tableName)
if err != nil { if err != nil {