Dependency missed with a function used as default value

Open
#191 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
go, postgresql
Domain
databases

Research direction

Reproduce the issue with the supplied PostgreSQL function and table definitions, then trace the dependency validation and migration statement ordering that produces the DROP FUNCTION and DROP TABLE statements. Done means the explicit function use in the column default is recognized as a dependency and validation produces a safe ordering without the reported error.

Written by the indexing model from the issue text.

Description

Hello,

I have an issue with the following schema:

CREATE FUNCTION public.demo() RETURNS text
    LANGUAGE plpgsql
    AS $$
declare
begin
   return 'hi';
end;

$$;

CREATE TABLE public.demo2 (
    demo3 text DEFAULT public.demo() NOT NULL
);

The dependency between demo2 and demo is not detected, validation is not passing.

validating migration plan: running migration plan: executing migration statement: {DROP FUNCTION "public"."demo"() 3s 3s [HAS_UNTRACKABLE_DEPENDENCIES: Dependencies, i.e. other functions used in the function body, of non-sql functions cannot be tracked. As a result, we cannot guarantee that function dependencies are ordered properly relative to this statement. For drops, this means you need to ensure that all functions this function depends on are dropped after this statement.]}: ERROR: cannot drop function demo() because other objects depend on it (SQLSTATE 2BP01)

With generated statements lookings like this:

        {
            DDL:         "DROP FUNCTION \"public\".\"demo\"()",
            Timeout:     3000000000,
            LockTimeout: 3000000000,
            Hazards:     {
                {Type:"HAS_UNTRACKABLE_DEPENDENCIES", Message:"Dependencies, i.e. other functions used in the function body, of non-sql functions cannot be tracked. As a result, we cannot guarantee that function dependencies are ordered properly relative to this statement. For drops, this means you need to ensure that all functions this function depends on are dropped after this statement."},
            },
        },
        {
            DDL:         "DROP TABLE \"public\".\"demo2\"",
            Timeout:     1200000000000,
            LockTimeout: 3000000000,
            Hazards:     {
                {Type:"DELETES_DATA", Message:"Deletes all rows in the table (and the table itself)"},
            },
        },

I do understand that there are complexities tracking dependencies of functions, but could a 'obvious' dependency like that one, when the function is explicitly used somewhere else be detected?

I tried to dig a into the code but I was not sure where / how to find relevents part, if you have some pointers I would be happy to make a PR to improve that :)

Dominant language
Go
Stars
884
Forks
82
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from stripe/pg-schema-diff

All issues in stripe/pg-schema-diff

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.