pgschema icon indicating copy to clipboard operation
pgschema copied to clipboard

Wrong dependency order from dump

Open FFX3 opened this issue 10 months ago • 2 comments

get_dzi_not_linked_to_plan should be after the table. Don't ask me why this isn't a view, it probably should be.


--
-- Name: get_dzi_not_linked_to_plan(uuid); Type: FUNCTION; Schema: -; Owner: -
--

CREATE OR REPLACE FUNCTION get_dzi_not_linked_to_plan(
    _plan_id uuid
)
RETURNS SETOF dzi
LANGUAGE plpgsql
STABLE
AS $$
    begin
        return query (
            select * from dzi
            where id not in (
                select dzi_id from plan_dzi
                where plan_id = _plan_id
            )
        );
    end;

$$;
--
-- Name: dzi; Type: TABLE; Schema: -; Owner: -
--

CREATE TABLE IF NOT EXISTS dzi (
    id uuid DEFAULT extensions.uuid_generate_v4(),
    pdf_id uuid,
    project_id uuid NOT NULL,
    uri text NOT NULL,
    thumbnail_uri text NOT NULL,
    created_at timestamptz DEFAULT now(),
    updated_at timestamptz,
    page integer,
    CONSTRAINT dzi_pkey PRIMARY KEY (id),
    CONSTRAINT dzi_pdf_id_fkey FOREIGN KEY (pdf_id) REFERENCES pdfs (id) ON DELETE CASCADE,
    CONSTRAINT dzi_project_id_fkey FOREIGN KEY (project_id) REFERENCES projects (id)
);

--- etc

```SQL
--
-- pgschema database dump
--

-- Dumped from database version PostgreSQL 15.8
-- Dumped by pgschema version 1.6.1


\i types/permission.sql
\i functions/update_updated_at_column.sql
\i functions/ensure_non_pending_has_worker_id.sql
\i functions/get_dzi_not_linked_to_plan.sql
\i functions/is_in_org.sql
\i functions/org_permissions_access_token_hook.sql
\i functions/upsert_grading_annotations.sql
\i functions/upsert_plan_dzi_pivot.sql
\i functions/ensure_failed_has_error_message.sql
\i functions/ensure_processing_ended_at.sql
\i functions/get_plan_scale.sql
\i functions/link_user_to_project_using_token.sql
\i functions/org_permissions.sql
\i tables/dzi.sql
-- etc

FFX3 avatar Jan 31 '26 00:01 FFX3

Seems I can get around this by updating main.sql but I fear if I run a dump again it might reset the order.

FFX3 avatar Jan 31 '26 00:01 FFX3

When we dump in in multi-file. The object type order is fixed. See https://github.com/pgschema/pgschema/blob/cc76ca31ccfcf29ded048f1783bb9f46c271bd7e/internal/dump/formatter.go#L110

Because we may have function that depends on table and vice versa.

Unlike single file mode, where we can use topological sort to order them properly. Doing this in multi-file is hard.

The idea of using dump is to baseline the schema, so usually you only need to do it upon init and occasionally when schema drifts occur. Re-arrange it manually is expected.

tianzhou avatar Jan 31 '26 06:01 tianzhou