supabase / cli

Supabase CLI. Manage postgres migrations, run Supabase locally, deploy edge functions. Postgres backups. Generating types from your database schema.
https://supabase.com/docs/reference/cli/about
MIT License
1.02k stars 201 forks source link

Unexpected migration occurs when executing `supabase db diff --local --schema storage --use-migra -f {name}` #2283

Closed azurechen closed 3 months ago

azurechen commented 4 months ago

I always use the local dev server to develop. And I also create storage policies locally. When I prepare to deploy these policies to the remote server. I executed the following command to generate a migration file for storage policies.

supabase db diff --local --schema storage --use-migra -f {name}

And the output includes some functions that were created by Supabase not myself.

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION storage.extension(name text)
 RETURNS text
 LANGUAGE plpgsql
AS $function$
DECLARE
_parts text[];
_filename text;
BEGIN
    select string_to_array(name, '/') into _parts;
    select _parts[array_length(_parts,1)] into _filename;
    -- @todo return the last part instead of 2
    return split_part(_filename, '.', 2);
END
$function$
;

CREATE OR REPLACE FUNCTION storage.filename(name text)
 RETURNS text
 LANGUAGE plpgsql
AS $function$
DECLARE
_parts text[];
BEGIN
    select string_to_array(name, '/') into _parts;
    return _parts[array_length(_parts,1)];
END
$function$
;

CREATE OR REPLACE FUNCTION storage.foldername(name text)
 RETURNS text[]
 LANGUAGE plpgsql
AS $function$
DECLARE
_parts text[];
BEGIN
    select string_to_array(name, '/') into _parts;
    return _parts[1:array_length(_parts,1)-1];
END
$function$
;

I guess if I run migrations and replace these functions it may cause some issues even if I don't change any logic. (Such as if Supabase official will change these functions' logic someday)

I am also curious as to why only extension, filename and foldername these three functions should be migrated. Other functions and tables under the storage schema won't be included in the migration file. Is it caused by permissions?

I know I can remove them from the migration file by myself. But next time I run the command to generate another migration file for storage policies update. These three functions will be included in the new migration file again. Therefore I think remove them by myself may not be a good solution.

To Reproduce

  1. run supabase db diff --local --schema storage --use-migra -f {name}
  2. check the migration file

Expected behavior

The generated migration file doesn't include extension, filename and foldername functions

System information

sweatybridge commented 3 months ago

This is quite strange indeed because I can't reproduce this on a fresh local project. The steps I took

  1. supabase init
  2. supabase start
  3. go to http://localhost:54323/project/default/storage/policies and add bucket and object policies
  4. supabase db diff --local --schema storage
Diffing schemas: storage
Finished supabase db diff on branch develop.

create policy "Enable read access for all users"
on "storage"."buckets"
as permissive
for select
to public
using (true);

create policy "Enable read access for all users"
on "storage"."objects"
as permissive
for select
to public
using (true);

Output only contains the changed policies and not other functions.

sweatybridge commented 3 months ago

If you are still running into this error, try supabase db reset to remove any potential changes to storage schema on local database.