Skip to content

Commit

Permalink
Add is_indexed() to upgrade script.
Browse files Browse the repository at this point in the history
And record it in Changes.
  • Loading branch information
theory committed Jun 29, 2016
1 parent 1a924f2 commit fb203a7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Changes
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ Revision history for pgTAP
---------------------------
* Fixed the default description for `hasnt_opclass()` to say "should not
exist" instead of "should exist", thanks to Rodolphe Quiédeville (PR #99).

* Added `is_indexed()`, which test to see that specific table columns are
indexed, thanks to Rodolphe Quiédeville (PR #103).

0.96.0 2016-05-16T20:53:57Z
---------------------------
* Added an optional `:exclude_pattern` parameter to `findfuncs()` to prevent
Expand Down
94 changes: 93 additions & 1 deletion sql/pgtap--0.96.0--0.97.0.sql
Original file line number Diff line number Diff line change
@@ -1 +1,93 @@
-- No changes yet.
CREATE OR REPLACE FUNCTION _is_indexed( NAME, NAME, NAME[])
RETURNS BOOLEAN AS $$
WITH cols AS (
SELECT x.indexrelid, x.indrelid, unnest(x.indkey) as colid
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class r ON r.oid = x.indrelid
JOIN pg_catalog.pg_namespace n ON n.oid = r.relnamespace
WHERE n.nspname = $1
AND r.relname = $2),
colsdef AS (
SELECT cols.indexrelid, cols.indrelid, array_agg(a.attname) as cols
FROM cols
JOIN pg_catalog.pg_attribute a ON (a.attrelid = cols.indrelid
AND a.attnum = cols.colid)
GROUP BY 1, 2)
SELECT EXISTS (
SELECT TRUE
FROM colsdef
WHERE colsdef.cols::NAME[] = $3
);
$$ LANGUAGE sql;

CREATE OR REPLACE FUNCTION _is_indexed( NAME, NAME[])
RETURNS BOOLEAN AS $$
WITH cols AS (
SELECT x.indexrelid, x.indrelid, unnest(x.indkey) as colid
FROM pg_catalog.pg_index x
JOIN pg_catalog.pg_class r ON r.oid = x.indrelid
WHERE r.relname = $1),
colsdef AS (
SELECT cols.indexrelid, cols.indrelid, array_agg(a.attname) as cols
FROM cols
JOIN pg_catalog.pg_attribute a ON (a.attrelid = cols.indrelid
AND a.attnum = cols.colid)
GROUP BY 1, 2)
SELECT EXISTS (
SELECT TRUE
FROM colsdef
WHERE colsdef.cols::NAME[] = $2
);
$$ LANGUAGE sql;

-- is_indexed( schema, table, columns[], description )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT ok ( _is_indexed( $1, $2, $3), $4);
$$ LANGUAGE sql;

-- is_indexed( schema, table, columns[] )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME, NAME[] )
RETURNS TEXT AS $$
SELECT ok ( _is_indexed( $1, $2, $3), 'An index on ' || quote_ident($1) || '.' || quote_ident($2) || ' with ' || $3::text || ' should exist');
$$ LANGUAGE sql;

-- is_indexed( table, columns[], description )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME[], TEXT )
RETURNS TEXT AS $$
SELECT ok ( _is_indexed( $1, $2), $3);
$$ LANGUAGE sql;

-- is_indexed( table, columns[] )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME[] )
RETURNS TEXT AS $$
SELECT ok ( _is_indexed( $1, $2), 'An index on ' || quote_ident($1) || ' with ' || $2::text || ' should exist');
$$ LANGUAGE sql;

-- is_indexed( schema, table, column, description )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME, NAME, TEXT )
RETURNS TEXT AS $$
SELECT ok ( _is_indexed( $1, $2, ARRAY[$3]::NAME[]), $4);
$$ LANGUAGE sql;

-- is_indexed( schema, table, column )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME, NAME )
RETURNS TEXT AS $$
SELECT CASE WHEN _is_schema( $1 ) THEN
-- Looking for schema.table index.
ok ( _is_indexed( $1, $2, ARRAY[$3]::NAME[]),
'An index on ' || quote_ident($1) || '.' || quote_ident($2)
|| ' on column ' || quote_ident($3) || ' should exist')
ELSE
-- Looking for particular columns.
ok ( _is_indexed( $1, ARRAY[$2]::NAME[]), $3)
END;
$$ LANGUAGE sql;

-- is_indexed( table, column )
CREATE OR REPLACE FUNCTION is_indexed ( NAME, NAME )
RETURNS TEXT AS $$
SELECT ok ( _is_indexed( $1, ARRAY[$2]::NAME[]),
'An index on ' || quote_ident($1) || ' on column '
|| $2::text || ' should exist');
$$ LANGUAGE sql;

0 comments on commit fb203a7

Please sign in to comment.