Supabase

How to handle Supabase database migrations with Flyway?

Discover the art of handling Supabase database migrations effortlessly using Flyway. Dive into this guide for setup steps, configuration tricks, and deployment advice to keep your database versioning smooth and hassle-free.

Developer profile skeleton
a developer thinking

Overview

Ensuring database migrations in your application is essential for keeping it stable and reliable. Supabase, known for its solid PostgreSQL database, is a great backend-as-a-service platform. Flyway, on the other hand, excels at managing database migrations. Marrying the strengths of these two simplifies handling database changes. This guide breaks down how to set up Flyway for a Supabase project. It covers everything from installation to configuration, and executing migrations, helping you keep updates smooth while reducing hiccups.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to handle Supabase database migrations with Flyway?

Prerequisites

  • A Supabase project up and running.
  • Flyway installed on your local machine.
  • Basic knowledge of SQL and command-line interface.

Step 1: Install Flyway CLI

First, download and install Flyway CLI from the official Flyway website:

curl -LO https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/8.0.5/flyway-commandline-8.0.5-linux-x64.tar.gz
tar -xvf flyway-commandline-8.0.5-linux-x64.tar.gz
sudo ln -s `pwd`/flyway-8.0.5/flyway /usr/local/bin

To make sure it's installed correctly, run:

flyway -v

Step 2: Configure Flyway

Create a new directory for Flyway configuration files and SQL scripts:

mkdir flyway_migrations
cd flyway_migrations

Inside this directory, create a flyway.conf file with the following content:

flyway.url=jdbc:postgresql://<your-supabase-instance-url>:5432/postgres
flyway.user=<your-supabase-db-user>
flyway.password=<your-supabase-db-password>
flyway.schemas=public
flyway.locations=filesystem:./sql

Replace <your-supabase-instance-url>, <your-supabase-db-user>, and <your-supabase-db-password> with the appropriate values from your Supabase project settings.

Step 3: Write Migration Scripts

Create a subdirectory called sql within flyway_migrations:

mkdir sql

Write your SQL migration scripts inside this sql directory. Name the files following Flyway's naming convention: V1__create_table.sql, V2__add_column.sql, etc. Here's an example for V1__create_table.sql:

CREATE TABLE users (
  id SERIAL PRIMARY KEY,
  name VARCHAR(100) NOT NULL,
  email VARCHAR(100) UNIQUE NOT NULL
);

Step 4: Run Migrations

Run the migrations using Flyway from the flyway_migrations directory:

flyway migrate

Check the output logs to make sure the migrations were successful. This command will apply all the new migration scripts in the sql directory to the Supabase database.

Step 5: Verify Migrations

Log in to your Supabase dashboard, navigate to the SQL editor, and run:

SELECT * FROM flyway_schema_history;

This should show a table with the history of all the migrations that Flyway has applied, ensuring the records match your scripts.

Step 6: Rollback Migrations (if needed)

To rollback the last applied migration, use:

flyway undo

However, Flyway Community Edition doesn't support automatic undo functionality. Instead, create specific rollback scripts using Flyway Pro or Enterprise Edition or execute the SQL manually:

DROP TABLE users;

Step 7: Automating Migrations

Integrate Flyway migration with your CI/CD pipeline:

  1. Add Flyway configurations to your pipeline script.
  2. Run flyway migrate as part of the build/deployment process.

Example using GitHub Actions:

jobs:
  flyway:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK 11
        uses: actions/setup-java@v1
        with:
          java-version: 11
      - name: Run Flyway Migrations
        run: flyway -configFiles=flyway_migrations/flyway.conf migrate

This setup will trigger database migrations automatically during deployment processes, ensuring your database schema is always up-to-date.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.