perseas / Pyrseas

Provides utilities for Postgres database schema versioning.
https://perseas.github.io/
BSD 3-Clause "New" or "Revised" License
395 stars 67 forks source link

Conditionally inject dependencies of functions #189

Open feikesteenbergen opened 6 years ago

feikesteenbergen commented 6 years ago

For some functions, it is possible that the functions it depends upon are not explicitly present, e.g. for the following aggregate function, the dependency is upon pg_catalog.array_cat, so should not even be made explicit.

CREATE AGGREGATE array_agg_mult(anyarray) (
    SFUNC = array_cat,
    STYPE = anyarray,
    INITCOND = '{}'
);

We got the following error when trying to dbtoyaml this database

KeyError: (u'public', 'array_cat', u'anyarray, anyarray')
feikesteenbergen commented 6 years ago

Arguably, this is still wrong, as the following definition is perfectly valid, yet cause dbtoyaml to raise an error:

SET search_path = a, pg_catalog;

--
-- Name: _group_concat(text, text); Type: FUNCTION; Schema: a; Owner: postgres
--

CREATE FUNCTION _group_concat(text, text) RETURNS text
    LANGUAGE sql IMMUTABLE
    AS $_$
SELECT CASE
  WHEN $2 IS NULL THEN $1
  WHEN $1 IS NULL THEN $2
  ELSE $1 || ', ' || $2
END
$_$;

ALTER FUNCTION a._group_concat(text, text) OWNER TO postgres;

SET search_path = b, pg_catalog;

--
-- Name: group_concat(text); Type: AGGREGATE; Schema: b; Owner: postgres
--

CREATE AGGREGATE group_concat(text) (
    SFUNC = a._group_concat,
    STYPE = text
);

ALTER AGGREGATE b.group_concat(text) OWNER TO postgres;

The error raised is:

  File "/Users/feike/Library/Python/2.7/lib/python/site-packages/Pyrseas-0.8.0-py2.7.egg/pyrseas/dbobject/__init__.py", line 567, in unqualify
    assert sch == self.schema
AssertionError
jmafc commented 6 years ago

Yes, this is essentially the same problem raised by #175. Unless we read all of pg_catalog, we can't properly determine how to deal with it. Hence the need for #185--the trick is to fetch the pg_catalog information (and use it internally), but do not dump the pg_catalog schema. I'm going to hold off on merging this for a while.