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.
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.
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
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.
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
);
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.
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.
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;
Integrate Flyway migration with your CI/CD pipeline:
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 our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.
Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.
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.
With the Bootstrapped platform, managing projects and developers has never been easier.
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.
Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.
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.
Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.
Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.
Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.
Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.