AI Prompt: Database: Declarative Database Schema
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.
You can also load the prompt directly into your IDE via the following links:
Prompt
1# Database: Declarative Database Schema23Mandatory Instructions for Supabase Declarative Schema Management45## 1. **Exclusive Use of Declarative Schema**67-**All database schema modifications must be defined within `.sql` files located in the `supabase/schemas/` directory. -**Do not\*\* create or modify files directly in the `supabase/migrations/` directory unless the modification is about the known caveats below. Migration files are to be generated automatically through the CLI.89## 2. **Schema Declaration**1011-For each database entity (e.g., tables, views, functions), create or update a corresponding `.sql` file in the `supabase/schemas/` directory12-Ensure that each `.sql` file accurately represents the desired final state of the entity1314## 3. **Migration Generation**1516- Before generating migrations, **stop the local Supabase development environment**17 ```bash18 supabase stop19 ```20- Generate migration files by diffing the declared schema against the current database state21 ```bash22 supabase db diff -f <migration_name>23 ```24 Replace `<migration_name>` with a descriptive name for the migration2526## 4. **Schema File Organization**2728- Schema files are executed in lexicographic order. To manage dependencies (e.g., foreign keys), name files to ensure correct execution order29- When adding new columns, append them to the end of the table definition to prevent unnecessary diffs3031## 5. **Rollback Procedures**3233- To revert changes34 - Manually update the relevant `.sql` files in `supabase/schemas/` to reflect the desired state35 - Generate a new migration file capturing the rollback36 ```bash37 supabase db diff -f <rollback_migration_name>38 ```39 - Review the generated migration file carefully to avoid unintentional data loss4041## 6. **Known caveats**4243The migra diff tool used for generating schema diff is capable of tracking most database changes. However, there are edge cases where it can fail.4445If you need to use any of the entities below, remember to add them through versioned migrations instead.4647### Data manipulation language4849- DML statements such as insert, update, delete, etc., are not captured by schema diff5051### View ownership5253- view owner and grants54- security invoker on views55- materialized views56- doesn’t recreate views when altering column type5758### RLS policies5960- alter policy statements61- column privileges62- Other entities#63- schema privileges are not tracked because each schema is diffed separately64- comments are not tracked65- partitions are not tracked66- alter publication ... add table ...67- create domain statements are ignored68- grant statements are duplicated from default privileges6970---7172**Non-compliance with these instructions may lead to inconsistent database states and is strictly prohibited.**