Skip to content

supabase db dump creates sql that cannot run in certain generated column cases #3921

@jleclanche

Description

@jleclanche

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Running supabase db dump generates sql that does not cleanly apply on db reset in certain cases with generated columns

To Reproduce

  1. Create the following two migration files in supabase/migrations/:
-- 001_initial.sql
SET
	default_tablespace = '';

CREATE OR REPLACE FUNCTION "public"."b58encode" ("data" "bytea") RETURNS "text" LANGUAGE "plpgsql" IMMUTABLE STRICT AS $$
BEGIN
	RETURN '';
END;
$$;

CREATE OR REPLACE FUNCTION "public"."b58encode" ("data" "uuid") RETURNS "text" LANGUAGE "sql" IMMUTABLE STRICT AS $$
	SELECT b58encode(decode(replace(data::text, '-', ''), 'hex'));
$$;

ALTER FUNCTION "public"."b58encode" ("data" "uuid") OWNER TO "postgres";

CREATE TABLE IF NOT EXISTS "public"."example" (
	"id" "uuid" DEFAULT 'a62f8a10-683b-4f31-8ec8-3211770ae257'::uuid NOT NULL
);

RESET ALL;

-- 002_bug.sql
ALTER TABLE public.example
ADD COLUMN test text GENERATED ALWAYS AS b58encode (id) STORED;
  1. Run supabase db reset to create the database.
  2. Run supabase db dump --local --file 001_initial.sql to create an 001_initial.sql file which reproduces the issue
  3. Remove the two old migration files and replace them with the new 001_initial.sql file
  4. Run supabase db reset again.

What happens:

Resetting local database...
Recreating database...
Initialising schema...
Seeding globals from roles.sql...
Applying migration 001_initial.sql...
ERROR: function b58encode(bytea) does not exist (SQLSTATE 42883)                
At statement: 25                                                                
CREATE TABLE IF NOT EXISTS "public"."example" (                                 
    "id" "uuid" DEFAULT 'a62f8a10-683b-4f31-8ec8-3211770ae257'::"uuid" NOT NULL,
    "test" "text" GENERATED ALWAYS AS ("public"."b58encode" ("id")) STORED      
)                                                                               
Try rerunning the command with --debug to troubleshoot the error.
error: "supabase" exited with code 1

Expected behavior

Should generate valid sql.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions