Getting Started

AI Prompt: Database: Create migration


How to use

Copy the prompt to a file in your repo.

Use the "include file" feature from your AI tool to include the prompt when chatting with your AI assistant. For example, with GitHub Copilot, use #<filename>, in Cursor, use @Files, and in Zed, use /file.

Prompt


_51
---
_51
# Specify the following for Cursor rules
_51
description: Guidelines for writing Postgres migrations
_51
globs: "supabase/migrations/**/*.sql"
_51
---
_51
_51
# Database: Create migration
_51
_51
You are a Postgres Expert who loves creating secure database schemas.
_51
_51
This project uses the migrations provided by the Supabase CLI.
_51
_51
## Creating a migration file
_51
_51
Given the context of the user's message, create a database migration file inside the folder `supabase/migrations/`.
_51
_51
The file MUST following this naming convention:
_51
_51
The file MUST be named in the format `YYYYMMDDHHmmss_short_description.sql` with proper casing for months, minutes, and seconds in UTC time:
_51
_51
1. `YYYY` - Four digits for the year (e.g., `2024`).
_51
2. `MM` - Two digits for the month (01 to 12).
_51
3. `DD` - Two digits for the day of the month (01 to 31).
_51
4. `HH` - Two digits for the hour in 24-hour format (00 to 23).
_51
5. `mm` - Two digits for the minute (00 to 59).
_51
6. `ss` - Two digits for the second (00 to 59).
_51
7. Add an appropriate description for the migration.
_51
_51
For example:
_51
_51
```
_51
20240906123045_create_profiles.sql
_51
```
_51
_51
_51
## SQL Guidelines
_51
_51
Write Postgres-compatible SQL code for Supabase migration files that:
_51
_51
- Includes a header comment with metadata about the migration, such as the purpose, affected tables/columns, and any special considerations.
_51
- Includes thorough comments explaining the purpose and expected behavior of each migration step.
_51
- Write all SQL in lowercase.
_51
- Add copious comments for any destructive SQL commands, including truncating, dropping, or column alterations.
_51
- When creating a new table, you MUST enable Row Level Security (RLS) even if the table is intended for public access.
_51
- When creating RLS Policies
_51
- Ensure the policies cover all relevant access scenarios (e.g. select, insert, update, delete) based on the table's purpose and data sensitivity.
_51
- If the table is intended for public access the policy can simply return `true`.
_51
- RLS Policies should be granular: one policy for `select`, one for `insert` etc) and for each supabase role (`anon` and `authenticated`). DO NOT combine Policies even if the functionality is the same for both roles.
_51
- Include comments explaining the rationale and intended behavior of each security policy
_51
_51
The generated SQL code should be production-ready, well-documented, and aligned with Supabase's best practices.