Search code examples
postgresqlsupabase

How can I reference a local file to be used in a Supabase migration?


Using Supabase locally, how can I reference a local .sql file to be used within a Supabase migration file?

For example:

This is my directory structure:

- db
  - supabase
   - types
    - custom_type.sql
   - migrations
    - some_timestamp_migration.sql

Then within my some_timestamp_migration.sql, if I tried to reference the custom_type.sql using:

\i db/supabase/custom_type.sql

I'm not sure if this usage is incorrect or it's an issue with using local Supabase migration commands.

supabase migration up

This results in: Error: ERROR: syntax error at or near "\" (SQLSTATE 42601)

I could of course copy & paste the contents of these files directly to the migrations but I don't think that would be very good DX.


Solution

  • Well, unfortuately supabase doesn't support \i directives like postgresql cmd client does. A way to get around this would be to temporarily concatenate your SQL files using a command like cat and then migrate that up.

    let's take your two sql scripts for example-

    cat db/supabase/custom_type.sql db/supabase/migrations/some_timestamp_migration.sql > combined_migration.sql
    supabase migration up
    

    this would make a new sql that combines the content of the two and push it up for migration. However, there are some nice thirdparty's that does this, like flyway - do check out Migrate - Flyway - if you want it more DX friendly