Skip to content

Commit

Permalink
Make description optional for col_not_null(schema, table, column) (yu…
Browse files Browse the repository at this point in the history
  • Loading branch information
nasbyj authored Nov 19, 2019
1 parent a170aa4 commit 15975a0
Show file tree
Hide file tree
Showing 6 changed files with 382 additions and 247 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Revision history for pgTAP
* Fix pgtap_version(), which incorrectly returned 0.99 when upgrading from
version 0.99.0 to 1.0.0.
* Fix issue with using pg_upgrade to Postgres 11+ with pgTap installed.
* Make description optional for col_not_null().

1.0.0 2019-02-21T22:39:42Z
--------------------------
Expand Down
2 changes: 2 additions & 0 deletions doc/pgtap.mmd
Original file line number Diff line number Diff line change
Expand Up @@ -4196,6 +4196,7 @@ view or composite type.
### `col_not_null()` ###

SELECT col_not_null( :schema, :table, :column, :description );
SELECT col_not_null( :schema, :table, :column );
SELECT col_not_null( :table, :column, :description );
SELECT col_not_null( :table, :column );

Expand Down Expand Up @@ -4225,6 +4226,7 @@ first, eh?
### `col_is_null()` ###

SELECT col_is_null( :schema, :table, :column, :description );
SELECT col_is_null( :schema, :table, :column );
SELECT col_is_null( :table, :column, :description );
SELECT col_is_null( :table, :column );

Expand Down
100 changes: 100 additions & 0 deletions sql/pgtap--1.0.0--1.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,103 @@ RETURNS NUMERIC AS 'SELECT 1.1;'
LANGUAGE SQL IMMUTABLE;


-- These are now obsolete
DROP FUNCTION col_not_null ( NAME, NAME );
DROP FUNCTION col_is_null ( NAME, NAME, NAME );
DROP FUNCTION col_is_null ( NAME, NAME );

-- _col_is_null( schema, table, column, desc, null )
CREATE OR REPLACE FUNCTION _col_is_null ( NAME, NAME, NAME, TEXT, bool )
RETURNS TEXT AS $$
DECLARE
qcol CONSTANT text := quote_ident($1) || '.' || quote_ident($2) || '.' || quote_ident($3);
c_desc CONSTANT text := coalesce(
$4,
'Column ' || qcol || ' should '
|| CASE WHEN $5 THEN 'be NOT' ELSE 'allow' END || ' NULL'
);
BEGIN
IF NOT _cexists( $1, $2, $3 ) THEN
RETURN fail( c_desc ) || E'\n'
|| diag (' Column ' || qcol || ' does not exist' );
END IF;
RETURN ok(
EXISTS(
SELECT true
FROM pg_catalog.pg_namespace n
JOIN pg_catalog.pg_class c ON n.oid = c.relnamespace
JOIN pg_catalog.pg_attribute a ON c.oid = a.attrelid
WHERE n.nspname = $1
AND c.relname = $2
AND a.attnum > 0
AND NOT a.attisdropped
AND a.attname = $3
AND a.attnotnull = $5
), c_desc
);
END;
$$ LANGUAGE plpgsql;

-- _col_is_null( table, column, desc, null )
CREATE OR REPLACE FUNCTION _col_is_null ( NAME, NAME, TEXT, bool )
RETURNS TEXT AS $$
DECLARE
qcol CONSTANT text := quote_ident($1) || '.' || quote_ident($2);
c_desc CONSTANT text := coalesce(
$3,
'Column ' || qcol || ' should '
|| CASE WHEN $4 THEN 'be NOT' ELSE 'allow' END || ' NULL'
);
BEGIN
IF NOT _cexists( $1, $2 ) THEN
RETURN fail( c_desc ) || E'\n'
|| diag (' Column ' || qcol || ' does not exist' );
END IF;
RETURN ok(
EXISTS(
SELECT true
FROM pg_catalog.pg_class c
JOIN pg_catalog.pg_attribute a ON c.oid = a.attrelid
WHERE pg_catalog.pg_table_is_visible(c.oid)
AND c.relname = $1
AND a.attnum > 0
AND NOT a.attisdropped
AND a.attname = $2
AND a.attnotnull = $4
), c_desc
);
END;
$$ LANGUAGE plpgsql;

-- col_not_null( schema, table, column, description )
-- col_not_null( schema, table, column )
CREATE OR REPLACE FUNCTION col_not_null (
schema_name NAME, table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, $4, true );
$$ LANGUAGE SQL;

-- col_not_null( table, column, description )
-- col_not_null( table, column )
CREATE OR REPLACE FUNCTION col_not_null (
table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, true );
$$ LANGUAGE SQL;

-- col_is_null( schema, table, column, description )
-- col_is_null( schema, table, column )
CREATE OR REPLACE FUNCTION col_is_null (
schema_name NAME, table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, $4, false );
$$ LANGUAGE SQL;

-- col_is_null( table, column, description )
-- col_is_null( table, column )
CREATE OR REPLACE FUNCTION col_is_null (
table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, false );
$$ LANGUAGE SQL;

63 changes: 37 additions & 26 deletions sql/pgtap.sql.in
Original file line number Diff line number Diff line change
Expand Up @@ -1298,10 +1298,17 @@ $$ LANGUAGE SQL;
-- _col_is_null( schema, table, column, desc, null )
CREATE OR REPLACE FUNCTION _col_is_null ( NAME, NAME, NAME, TEXT, bool )
RETURNS TEXT AS $$
DECLARE
qcol CONSTANT text := quote_ident($1) || '.' || quote_ident($2) || '.' || quote_ident($3);
c_desc CONSTANT text := coalesce(
$4,
'Column ' || qcol || ' should '
|| CASE WHEN $5 THEN 'be NOT' ELSE 'allow' END || ' NULL'
);
BEGIN
IF NOT _cexists( $1, $2, $3 ) THEN
RETURN fail( $4 ) || E'\n'
|| diag (' Column ' || quote_ident($1) || '.' || quote_ident($2) || '.' || quote_ident($3) || ' does not exist' );
RETURN fail( c_desc ) || E'\n'
|| diag (' Column ' || qcol || ' does not exist' );
END IF;
RETURN ok(
EXISTS(
Expand All @@ -1315,18 +1322,25 @@ BEGIN
AND NOT a.attisdropped
AND a.attname = $3
AND a.attnotnull = $5
), $4
), c_desc
);
END;
$$ LANGUAGE plpgsql;

-- _col_is_null( table, column, desc, null )
CREATE OR REPLACE FUNCTION _col_is_null ( NAME, NAME, TEXT, bool )
RETURNS TEXT AS $$
DECLARE
qcol CONSTANT text := quote_ident($1) || '.' || quote_ident($2);
c_desc CONSTANT text := coalesce(
$3,
'Column ' || qcol || ' should '
|| CASE WHEN $4 THEN 'be NOT' ELSE 'allow' END || ' NULL'
);
BEGIN
IF NOT _cexists( $1, $2 ) THEN
RETURN fail( $3 ) || E'\n'
|| diag (' Column ' || quote_ident($1) || '.' || quote_ident($2) || ' does not exist' );
RETURN fail( c_desc ) || E'\n'
|| diag (' Column ' || qcol || ' does not exist' );
END IF;
RETURN ok(
EXISTS(
Expand All @@ -1339,46 +1353,43 @@ BEGIN
AND NOT a.attisdropped
AND a.attname = $2
AND a.attnotnull = $4
), $3
), c_desc
);
END;
$$ LANGUAGE plpgsql;

-- col_not_null( schema, table, column, description )
CREATE OR REPLACE FUNCTION col_not_null ( NAME, NAME, NAME, TEXT )
RETURNS TEXT AS $$
-- col_not_null( schema, table, column )
CREATE OR REPLACE FUNCTION col_not_null (
schema_name NAME, table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, $4, true );
$$ LANGUAGE SQL;

-- col_not_null( table, column, description )
CREATE OR REPLACE FUNCTION col_not_null ( NAME, NAME, TEXT )
RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, true );
$$ LANGUAGE SQL;

-- col_not_null( table, column )
CREATE OR REPLACE FUNCTION col_not_null ( NAME, NAME )
RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, 'Column ' || quote_ident($1) || '.' || quote_ident($2) || ' should be NOT NULL', true );
CREATE OR REPLACE FUNCTION col_not_null (
table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, true );
$$ LANGUAGE SQL;

-- col_is_null( schema, table, column, description )
CREATE OR REPLACE FUNCTION col_is_null ( NAME, NAME, NAME, TEXT )
RETURNS TEXT AS $$
-- col_is_null( schema, table, column )
CREATE OR REPLACE FUNCTION col_is_null (
schema_name NAME, table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, $4, false );
$$ LANGUAGE SQL;

-- col_is_null( schema, table, column )
CREATE OR REPLACE FUNCTION col_is_null ( NAME, NAME, NAME )
RETURNS TEXT AS $$
-- col_is_null( table, column, description )
-- col_is_null( table, column )
CREATE OR REPLACE FUNCTION col_is_null (
table_name NAME, column_name NAME, description TEXT DEFAULT NULL
) RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, $3, false );
$$ LANGUAGE SQL;

-- col_is_null( table, column )
CREATE OR REPLACE FUNCTION col_is_null ( NAME, NAME )
RETURNS TEXT AS $$
SELECT _col_is_null( $1, $2, 'Column ' || quote_ident($1) || '.' || quote_ident($2) || ' should allow NULL', false );
$$ LANGUAGE SQL;

CREATE OR REPLACE FUNCTION _get_col_type ( NAME, NAME, NAME )
RETURNS TEXT AS $$
Expand Down
Loading

0 comments on commit 15975a0

Please sign in to comment.