Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

examples: Updates example rules to match the latest format of Cursor rules and add declarative schema example (#34992)

Tylere3b03513e984
diff --git a/apps/ui-library/registry/default/ai-editor-rules/create-db-functions.mdc b/apps/ui-library/registry/default/ai-editor-rules/create-db-functions.mdc
index 65de4858f83..a0e6c8a57f6 100644
--- a/apps/ui-library/registry/default/ai-editor-rules/create-db-functions.mdc
+++ b/apps/ui-library/registry/default/ai-editor-rules/create-db-functions.mdc
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Supabase database functions
-globs: **/*.sql
+alwaysApply: false
---
# Database: Create functions
diff --git a/apps/ui-library/registry/default/ai-editor-rules/create-migration.mdc b/apps/ui-library/registry/default/ai-editor-rules/create-migration.mdc
index def2cf68841..57c9f13d53d 100644
--- a/apps/ui-library/registry/default/ai-editor-rules/create-migration.mdc
+++ b/apps/ui-library/registry/default/ai-editor-rules/create-migration.mdc
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Postgres migrations
-globs: supabase/migrations/**/*.sql
+alwaysApply: false
---
# Database: Create migration
diff --git a/apps/ui-library/registry/default/ai-editor-rules/create-rls-policies.mdc b/apps/ui-library/registry/default/ai-editor-rules/create-rls-policies.mdc
index 70b4f597f46..b6f6fcd1d5c 100644
--- a/apps/ui-library/registry/default/ai-editor-rules/create-rls-policies.mdc
+++ b/apps/ui-library/registry/default/ai-editor-rules/create-rls-policies.mdc
@@ -1,6 +1,5 @@
---
description: Guidelines for writing Postgres Row Level Security policies
-globs: **/*.sql
alwaysApply: false
---
diff --git a/apps/ui-library/registry/default/ai-editor-rules/declarative-schema.mdc b/apps/ui-library/registry/default/ai-editor-rules/declarative-schema.mdc
new file mode 100644
index 00000000000..40ef3eeb0dc
--- /dev/null
+++ b/apps/ui-library/registry/default/ai-editor-rules/declarative-schema.mdc
@@ -0,0 +1,78 @@
+---
+# Specify the following for Cursor rules
+description: For when modifying the Supabase database schema.
+alwaysApply: false
+---
+
+# Database: Declarative Database Schema
+
+Mandatory Instructions for Supabase Declarative Schema Management
+
+## 1. **Exclusive Use of Declarative Schema**
+
+-**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.
+
+## 2. **Schema Declaration**
+
+-For each database entity (e.g., tables, views, functions), create or update a corresponding `.sql` file in the `supabase/schemas/` directory
+-Ensure that each `.sql` file accurately represents the desired final state of the entity
+
+## 3. **Migration Generation**
+
+- Before generating migrations, **stop the local Supabase development environment**
+ ```bash
+ supabase stop
+ ```
+- Generate migration files by diffing the declared schema against the current database state
+ ```bash
+ supabase db diff -f <migration_name>
+ ```
+ Replace `<migration_name>` with a descriptive name for the migration
+
+## 4. **Schema File Organization**
+
+- Schema files are executed in lexicographic order. To manage dependencies (e.g., foreign keys), name files to ensure correct execution order
+- When adding new columns, append them to the end of the table definition to prevent unnecessary diffs
+
+## 5. **Rollback Procedures**
+
+- To revert changes
+ - Manually update the relevant `.sql` files in `supabase/schemas/` to reflect the desired state
+ - Generate a new migration file capturing the rollback
+ ```bash
+ supabase db diff -f <rollback_migration_name>
+ ```
+ - Review the generated migration file carefully to avoid unintentional data loss
+
+## 6. **Known caveats**
+
+The migra diff tool used for generating schema diff is capable of tracking most database changes. However, there are edge cases where it can fail.
+
+If you need to use any of the entities below, remember to add them through versioned migrations instead.
+
+### Data manipulation language
+
+- DML statements such as insert, update, delete, etc., are not captured by schema diff
+
+### View ownership
+
+- view owner and grants
+- security invoker on views
+- materialized views
+- doesn’t recreate views when altering column type
+
+### RLS policies
+
+- alter policy statements
+- column privileges
+- Other entities#
+- schema privileges are not tracked because each schema is diffed separately
+- comments are not tracked
+- partitions are not tracked
+- alter publication ... add table ...
+- create domain statements are ignored
+- grant statements are duplicated from default privileges
+
+---
+
+**Non-compliance with these instructions may lead to inconsistent database states and is strictly prohibited.**
diff --git a/apps/ui-library/registry/default/ai-editor-rules/postgres-sql-style-guide.mdc b/apps/ui-library/registry/default/ai-editor-rules/postgres-sql-style-guide.mdc
index 6b8082dc8d4..b09a71fb0be 100644
--- a/apps/ui-library/registry/default/ai-editor-rules/postgres-sql-style-guide.mdc
+++ b/apps/ui-library/registry/default/ai-editor-rules/postgres-sql-style-guide.mdc
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Postgres SQL
-globs: **/*.sql
+alwaysApply: false
---
# Postgres SQL Style Guide
diff --git a/apps/ui-library/registry/default/ai-editor-rules/writing-supabase-edge-functions.mdc b/apps/ui-library/registry/default/ai-editor-rules/writing-supabase-edge-functions.mdc
index 24a59f0ef1b..eb08df027c7 100644
--- a/apps/ui-library/registry/default/ai-editor-rules/writing-supabase-edge-functions.mdc
+++ b/apps/ui-library/registry/default/ai-editor-rules/writing-supabase-edge-functions.mdc
@@ -1,6 +1,6 @@
---
description: Coding rules for Supabase Edge Functions
-globs: supabase/functions/**/*.ts
+alwaysApply: false
---
# Writing Supabase Edge Functions
diff --git a/examples/prompts/code-format-sql.md b/examples/prompts/code-format-sql.md
index 4bd5314a541..e865fcd1d65 100644
--- a/examples/prompts/code-format-sql.md
+++ b/examples/prompts/code-format-sql.md
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Postgres SQL
-globs: "**/*.sql"
+alwaysApply: false
---
# Postgres SQL Style Guide
diff --git a/examples/prompts/database-create-migration.md b/examples/prompts/database-create-migration.md
index 3f975638d6c..84cc98cf609 100644
--- a/examples/prompts/database-create-migration.md
+++ b/examples/prompts/database-create-migration.md
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Postgres migrations
-globs: "supabase/migrations/**/*.sql"
+alwaysApply: false
---
# Database: Create migration
diff --git a/examples/prompts/database-functions.md b/examples/prompts/database-functions.md
index 81df2d66039..a0e6c8a57f6 100644
--- a/examples/prompts/database-functions.md
+++ b/examples/prompts/database-functions.md
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Supabase database functions
-globs: "**/*.sql"
+alwaysApply: false
---
# Database: Create functions
diff --git a/examples/prompts/database-rls-policies.md b/examples/prompts/database-rls-policies.md
index 76818ff1b08..3272c5cceb5 100644
--- a/examples/prompts/database-rls-policies.md
+++ b/examples/prompts/database-rls-policies.md
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Postgres Row Level Security policies
-globs: "**/*.sql"
+alwaysApply: false
---
# Database: Create RLS policies
diff --git a/examples/prompts/declarative-database-schema.md b/examples/prompts/declarative-database-schema.md
new file mode 100644
index 00000000000..40ef3eeb0dc
--- /dev/null
+++ b/examples/prompts/declarative-database-schema.md
@@ -0,0 +1,78 @@
+---
+# Specify the following for Cursor rules
+description: For when modifying the Supabase database schema.
+alwaysApply: false
+---
+
+# Database: Declarative Database Schema
+
+Mandatory Instructions for Supabase Declarative Schema Management
+
+## 1. **Exclusive Use of Declarative Schema**
+
+-**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.
+
+## 2. **Schema Declaration**
+
+-For each database entity (e.g., tables, views, functions), create or update a corresponding `.sql` file in the `supabase/schemas/` directory
+-Ensure that each `.sql` file accurately represents the desired final state of the entity
+
+## 3. **Migration Generation**
+
+- Before generating migrations, **stop the local Supabase development environment**
+ ```bash
+ supabase stop
+ ```
+- Generate migration files by diffing the declared schema against the current database state
+ ```bash
+ supabase db diff -f <migration_name>
+ ```
+ Replace `<migration_name>` with a descriptive name for the migration
+
+## 4. **Schema File Organization**
+
+- Schema files are executed in lexicographic order. To manage dependencies (e.g., foreign keys), name files to ensure correct execution order
+- When adding new columns, append them to the end of the table definition to prevent unnecessary diffs
+
+## 5. **Rollback Procedures**
+
+- To revert changes
+ - Manually update the relevant `.sql` files in `supabase/schemas/` to reflect the desired state
+ - Generate a new migration file capturing the rollback
+ ```bash
+ supabase db diff -f <rollback_migration_name>
+ ```
+ - Review the generated migration file carefully to avoid unintentional data loss
+
+## 6. **Known caveats**
+
+The migra diff tool used for generating schema diff is capable of tracking most database changes. However, there are edge cases where it can fail.
+
+If you need to use any of the entities below, remember to add them through versioned migrations instead.
+
+### Data manipulation language
+
+- DML statements such as insert, update, delete, etc., are not captured by schema diff
+
+### View ownership
+
+- view owner and grants
+- security invoker on views
+- materialized views
+- doesn’t recreate views when altering column type
+
+### RLS policies
+
+- alter policy statements
+- column privileges
+- Other entities#
+- schema privileges are not tracked because each schema is diffed separately
+- comments are not tracked
+- partitions are not tracked
+- alter publication ... add table ...
+- create domain statements are ignored
+- grant statements are duplicated from default privileges
+
+---
+
+**Non-compliance with these instructions may lead to inconsistent database states and is strictly prohibited.**
diff --git a/examples/prompts/edge-functions.md b/examples/prompts/edge-functions.md
index 49cec5f6614..b59ba754bc2 100644
--- a/examples/prompts/edge-functions.md
+++ b/examples/prompts/edge-functions.md
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Coding rules for Supabase Edge Functions
-globs: "supabase/functions/**/*.ts"
+alwaysApply: false
---
# Writing Supabase Edge Functions
diff --git a/examples/prompts/nextjs-supabase-auth.md b/examples/prompts/nextjs-supabase-auth.md
index 8e1b4f40496..2dfefd20561 100644
--- a/examples/prompts/nextjs-supabase-auth.md
+++ b/examples/prompts/nextjs-supabase-auth.md
@@ -1,7 +1,7 @@
---
# Specify the following for Cursor rules
description: Guidelines for writing Next.js apps with Supabase Auth
-globs: "**/*.ts, **/*.tsx, **/*.js, **/*.jsx"
+alwaysApply: false
---
# Bootstrap Next.js app with Supabase Auth