Skip to content

Extend ENSDb migrations#1899

Open
tk-o wants to merge 5 commits intomainfrom
feat/extend-ensdb-migrations
Open

Extend ENSDb migrations#1899
tk-o wants to merge 5 commits intomainfrom
feat/extend-ensdb-migrations

Conversation

@tk-o
Copy link
Copy Markdown
Contributor

@tk-o tk-o commented Apr 8, 2026

Lite PR

Tip: Review docs on the ENSNode PR process

Summary

  • Added migration that enables the pg_trgm extension.
  • Added migrations that creates a trigger in ENSDb that is executed each time a schema is dropped from ENSDb. When that happens, we take the name of the schema being dropped, and we delete all records referencing this schema name from ENSNode Metadata table.

Why

  • We needed a way to run certain queries against the ENSDb instance just once. The database migrations guarantee that the queries are executed exactly-once.
  • Each time ENSIndexer Schema would be dropped from ENSDb, for example, during regular maintenance, the ENSNode Metadata table would still include records referencing the dropped schema. We want to make the ENSDb data model intuitive, and having orphaned data does not support that goal.

Testing

  • After creating migration files, I've ran a local ENSIndexer instance and observed that both migrations were executed correctly.
    image
  • I ran three local instances of ENSIndexer
    • ENSINDEXER_SCHEMA_NAME=abc1 pnpm -F ensindexer start
    • ENSINDEXER_SCHEMA_NAME=abc2 pnpm -F ensindexer start
    • ENSINDEXER_SCHEMA_NAME=abc3 pnpm -F ensindexer start
  • I saw 9 records in ENSNode Metadata table, 3 records for each ENSIndexer Schema Name.
  • I stopped all three local ENSIndexer instances
  • I deleted the abc2 schema
  • I observed how only 6 records were remaining in ENSNode Metadata table
    image

Notes for Reviewer (Optional)

  • Drizzle migrations are guaranteed to run just once for the given ENSDb instance.

Pre-Review Checklist (Blocking)

  • This PR does not introduce significant changes and is low-risk to review quickly.
  • Relevant changesets are included (or are not required)

Copilot AI review requested due to automatic review settings April 8, 2026 17:47
@tk-o tk-o requested a review from a team as a code owner April 8, 2026 17:47
@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Apr 8, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
admin.ensnode.io Ready Ready Preview, Comment Apr 8, 2026 6:10pm
ensnode.io Ready Ready Preview, Comment Apr 8, 2026 6:10pm
ensrainbow.io Ready Ready Preview, Comment Apr 8, 2026 6:10pm

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Apr 8, 2026

🦋 Changeset detected

Latest commit: 9de6a26

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 24 packages
Name Type
@ensnode/ensdb-sdk Major
ensapi Major
ensindexer Major
@ensnode/integration-test-env Patch
ensadmin Major
ensrainbow Major
fallback-ensapi Major
enssdk Major
enscli Major
enskit Major
ensskills Major
@ensnode/datasources Major
@ensnode/ensrainbow-sdk Major
@ensnode/ensnode-react Major
@ensnode/ensnode-sdk Major
@ensnode/ponder-sdk Major
@ensnode/ponder-subgraph Major
@ensnode/shared-configs Major
@docs/ensnode Major
@docs/ensrainbow Major
@docs/mintlify Major
@namehash/ens-referrals Major
@namehash/namehash-ui Major
@ensnode/enskit-react-example Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 8, 2026

Warning

Rate limit exceeded

@tk-o has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 7 minutes and 53 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 7 minutes and 53 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 80e7ce58-8a0e-4ed9-99e9-e35f036a35d1

📥 Commits

Reviewing files that changed from the base of the PR and between 68d5926 and 9de6a26.

📒 Files selected for processing (7)
  • .changeset/metal-donkeys-bake.md
  • .changeset/six-garlics-wish.md
  • packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql
  • packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql
  • packages/ensdb-sdk/migrations/meta/0001_snapshot.json
  • packages/ensdb-sdk/migrations/meta/0002_snapshot.json
  • packages/ensdb-sdk/migrations/meta/_journal.json
📝 Walkthrough

Walkthrough

This PR introduces two database migrations to the ENSDb SDK: one to enable PostgreSQL's trigram extension for text search capabilities, and another to establish automatic cleanup of orphaned metadata when database schemas are dropped via an event trigger.

Changes

Cohort / File(s) Summary
Changeset Documentation
.changeset/metal-donkeys-bake.md, .changeset/six-garlics-wish.md
Two changelog entries documenting minor version bumps for migrations adding trigram extension support and orphaned metadata cleanup.
Database Migrations
packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql, packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql
Adds PostgreSQL pg_trgm extension for trigram indexing and creates an event trigger with PL/pgSQL function to automatically clean up ensnode.metadata records when schemas are dropped.
Migration Metadata
packages/ensdb-sdk/migrations/meta/0001_snapshot.json, packages/ensdb-sdk/migrations/meta/0002_snapshot.json, packages/ensdb-sdk/migrations/meta/_journal.json
Schema snapshots documenting state after each migration and journal entries tracking the execution of both migrations.

Possibly related PRs

Poem

🐰 Trigrams hop through schemas with glee,
Cleaning up orphans automatically,
When drops occur and schemas disappear,
Our trusty trigger makes metadata clear!

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Extend ENSDb migrations' accurately captures the main change—adding new database migrations to ENSDb. It is concise, clear, and clearly summarizes the primary objective.
Description check ✅ Passed The description comprehensively covers all required template sections: Summary (2 bullets), Why (clear rationale with context), Testing (detailed local testing with screenshots), Notes for Reviewer, and completed Pre-Review Checklist. All essential information is present and well-structured.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/extend-ensdb-migrations

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 8, 2026

Greptile Summary

This PR adds two new Drizzle migrations to ensdb-sdk: one enables the pg_trgm PostgreSQL extension, and the other installs a PL/pgSQL event trigger that automatically deletes orphaned ensnode.metadata rows whenever a schema is dropped from ENSDb.

The migration comment now correctly documents the superuser requirement for event triggers. The pg_trgm migration is fully idempotent (IF NOT EXISTS). The function logic correctly uses obj.object_type = 'schema' and obj.object_name from pg_event_trigger_dropped_objects() to match the right rows.

Confidence Score: 5/5

Safe to merge; the one new finding is a P2 defensive hardening suggestion that does not block correctness under the intended usage.

Both migrations are functionally correct for the described use case. Prior P0/P1 concerns from earlier review rounds are addressed (superuser requirement is now documented). The only new finding is a P2 edge case where dropping the ensnode schema itself would orphan the event trigger — not a normal operational scenario and does not affect day-to-day schema cleanup.

packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql — the event trigger function could silently break if the ensnode schema is ever dropped.

Vulnerabilities

No security concerns identified. The event trigger function only performs a targeted DELETE scoped to ensnode.metadata using the exact dropped schema name, with no dynamic SQL or user-controlled input.

Important Files Changed

Filename Overview
packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql Enables the pg_trgm extension idempotently; straightforward and safe.
packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql Adds an event trigger for schema-drop cleanup; function logic is correct, but if the ensnode schema itself is ever dropped the trigger becomes permanently broken for all future DROP SCHEMA commands.
packages/ensdb-sdk/migrations/meta/_journal.json Journal correctly registers both new migrations with sequential indices and matching tags.
packages/ensdb-sdk/migrations/meta/0001_snapshot.json Snapshot correctly reflects no schema-level changes (extension install is invisible to Drizzle's schema tracker).
packages/ensdb-sdk/migrations/meta/0002_snapshot.json Snapshot correctly reflects no schema-level changes (event trigger and function are invisible to Drizzle's schema tracker).

Sequence Diagram

sequenceDiagram
    participant Client
    participant PostgreSQL
    participant EventTrigger as Event Trigger cleanup_ensnode_metadata_on_schema_drop
    participant Function as ensnode.handle_schema_dropped()
    participant Metadata as ensnode.metadata

    Client->>PostgreSQL: DROP SCHEMA abc2 [CASCADE]
    PostgreSQL->>PostgreSQL: Execute schema drop
    PostgreSQL->>EventTrigger: Fire sql_drop (TAG = DROP SCHEMA)
    EventTrigger->>Function: EXECUTE FUNCTION
    Function->>PostgreSQL: pg_event_trigger_dropped_objects()
    PostgreSQL-->>Function: object_type=schema, object_name=abc2
    Function->>Metadata: DELETE WHERE ens_indexer_schema_name = 'abc2'
    Metadata-->>Function: rows deleted
    Function-->>EventTrigger: done
    EventTrigger-->>PostgreSQL: trigger complete
    PostgreSQL-->>Client: DROP SCHEMA succeeded
Loading

Reviews (2): Last reviewed commit: "docs(changeset): Added `manage_orphaned_..." | Re-trigger Greptile

Comment on lines +18 to +21
CREATE EVENT TRIGGER cleanup_ensnode_metadata_on_schema_drop
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE FUNCTION handle_schema_dropped();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 CREATE EVENT TRIGGER requires superuser privileges

In PostgreSQL, event triggers can only be created by a superuser. If the database user running these migrations does not hold the SUPERUSER role — which is common on managed providers like AWS RDS, Neon, or Supabase — this migration will fail with ERROR: permission denied to create event trigger. The function definition on line 3 will succeed, but the trigger creation will abort the migration. Consider either documenting this requirement explicitly, providing a fallback, or checking whether the target deployment always runs as a superuser-equivalent role.

Comment on lines +18 to +21
CREATE EVENT TRIGGER cleanup_ensnode_metadata_on_schema_drop
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE FUNCTION handle_schema_dropped();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Event trigger has no idempotency guard

The function uses CREATE OR REPLACE FUNCTION, but the event trigger itself uses a bare CREATE EVENT TRIGGER with no IF NOT EXISTS. PostgreSQL does not support OR REPLACE for event triggers before version 14 and never supports IF NOT EXISTS. If this migration is ever re-applied manually (e.g., disaster-recovery replay), it will fail with ERROR: event trigger "cleanup_ensnode_metadata_on_schema_drop" already exists. A DO $$ BEGIN ... EXCEPTION WHEN duplicate_object THEN NULL; END $$; guard around the trigger creation would make it resilient.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR extends the @ensnode/ensdb-sdk Drizzle migration set to (1) enable the pg_trgm Postgres extension and (2) add an event trigger that cleans up ensnode.metadata rows when an ENSIndexer schema is dropped, preventing orphaned metadata.

Changes:

  • Add a migration to enable pg_trgm.
  • Add an event-trigger-based cleanup migration for orphaned ensnode.metadata records on DROP SCHEMA.
  • Update Drizzle migration metadata (_journal.json + snapshots) to include the new migrations.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql Enables pg_trgm extension for trigram indexing/search.
packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql Adds event trigger + handler function to delete metadata rows when schemas are dropped.
packages/ensdb-sdk/migrations/meta/_journal.json Registers the two new migrations in Drizzle’s journal.
packages/ensdb-sdk/migrations/meta/0001_snapshot.json Snapshot metadata for migration 0001.
packages/ensdb-sdk/migrations/meta/0002_snapshot.json Snapshot metadata for migration 0002.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql`:
- Around line 1-3: The comment above the migration contains a minor typo
("PostgreSQL.g"); edit the SQL file comment so the sentence ends with a normal
period. Locate the top comment block where the CREATE EXTENSION IF NOT EXISTS
pg_trgm; statement appears and replace "PostgreSQL.g" with "PostgreSQL." to
remove the stray 'g'.

In `@packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql`:
- Around line 18-21: Ensure creation of the event trigger
cleanup_ensnode_metadata_on_schema_drop is idempotent by preceding the CREATE
EVENT TRIGGER call with a DROP EVENT TRIGGER IF EXISTS
cleanup_ensnode_metadata_on_schema_drop CASCADE; so re-running migrations or
recovering from a corrupted migrations table won’t fail, and add a note in
deployment/setup docs that creating event triggers (e.g.,
cleanup_ensnode_metadata_on_schema_drop which EXECUTE FUNCTION
handle_schema_dropped()) requires PostgreSQL superuser privileges so the DB role
running migrations must be superuser.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 50756160-ec47-442a-8ad0-d29e445c9c0b

📥 Commits

Reviewing files that changed from the base of the PR and between 0cc51b3 and 68d5926.

📒 Files selected for processing (7)
  • .changeset/metal-donkeys-bake.md
  • .changeset/six-garlics-wish.md
  • packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql
  • packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql
  • packages/ensdb-sdk/migrations/meta/0001_snapshot.json
  • packages/ensdb-sdk/migrations/meta/0002_snapshot.json
  • packages/ensdb-sdk/migrations/meta/_journal.json

Comment on lines +18 to +21
CREATE EVENT TRIGGER cleanup_ensnode_metadata_on_schema_drop
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE FUNCTION handle_schema_dropped();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

Does PostgreSQL CREATE EVENT TRIGGER support IF NOT EXISTS?

💡 Result:

No. PostgreSQL’s CREATE EVENT TRIGGER syntax does not include IF NOT EXISTS (nor OR REPLACE), so attempting CREATE EVENT TRIGGER IF NOT EXISTS ... is a syntax error. [1]

Workarounds:

  • Check the system catalog first (pg_catalog.pg_event_trigger) and only create if missing. [2]
  • Or wrap the CREATE EVENT TRIGGER in a DO block and ignore the duplicate_object exception.
  • If your goal is idempotency in migrations, DROP EVENT TRIGGER IF EXISTS ... is supported. [3]

Sources: [1] [2] [3]


🏁 Script executed:

# Find the migrations directory and other migration files
fd -e sql . packages/ensdb-sdk/migrations/ | head -20

Repository: namehash/ensnode

Length of output: 251


🏁 Script executed:

# Check the migration file to see the full context
cat -n packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql

Repository: namehash/ensnode

Length of output: 858


🏁 Script executed:

# Search for Drizzle configuration files
fd -e config -e json -e ts | grep -i drizzle | head -10

Repository: namehash/ensnode

Length of output: 217


🏁 Script executed:

# Search for other event trigger creations in the codebase
rg "CREATE EVENT TRIGGER" --type sql

Repository: namehash/ensnode

Length of output: 192


🏁 Script executed:

# Search for database role/permission setup documentation
fd -e md | xargs grep -l "privilege\|superuser\|role" 2>/dev/null | head -5

Repository: namehash/ensnode

Length of output: 42


🏁 Script executed:

# Check if there are any other migrations that handle similar idempotency concerns
rg "DROP.*IF EXISTS" packages/ensdb-sdk/migrations/ --type sql

Repository: namehash/ensnode

Length of output: 42


🏁 Script executed:

# Check drizzle configuration
cat packages/ensdb-sdk/drizzle.config.ts

Repository: namehash/ensnode

Length of output: 290


🏁 Script executed:

# Check drizzle test file to see migration patterns
cat packages/ensdb-sdk/src/lib/drizzle.test.ts

Repository: namehash/ensnode

Length of output: 7929


🏁 Script executed:

# Search for README or setup documentation
fd -e md . | head -20

Repository: namehash/ensnode

Length of output: 688


🏁 Script executed:

# Look for Docker or environment configuration
fd -e dockerfile -e docker-compose -o -name ".env*" -o -name "*.env" | head -10

Repository: namehash/ensnode

Length of output: 290


🏁 Script executed:

# Search for any documentation about database setup or roles
rg "superuser|privilege|role|permission" --type md | head -20

Repository: namehash/ensnode

Length of output: 42


🏁 Script executed:

# Check if migrations can be re-run and what happens
rg "migration" --type ts packages/ensdb-sdk/src/ | head -15

Repository: namehash/ensnode

Length of output: 1936


🏁 Script executed:

# Check the ensdb-writer implementation for migration execution
cat packages/ensdb-sdk/src/client/ensdb-writer.ts | head -100

Repository: namehash/ensnode

Length of output: 2876


🏁 Script executed:

# Look for migration execution utilities
rg "runMigrations|migrate\(" --type ts packages/ensdb-sdk/src/

Repository: namehash/ensnode

Length of output: 146


🏁 Script executed:

# Check the main README for database setup instructions
head -100 README.md

Repository: namehash/ensnode

Length of output: 5155


🏁 Script executed:

# Search for any migration runner or database setup documentation
fd -e ts packages/ensdb-sdk/src/client/ | xargs grep -l "migration" 2>/dev/null

Repository: namehash/ensnode

Length of output: 487


🏁 Script executed:

# Look for any scripts or documentation about database initialization
fd -e sh -o -e md | xargs grep -l "migrate\|migration" 2>/dev/null | head -10

Repository: namehash/ensnode

Length of output: 176


🏁 Script executed:

# Check if there's any documentation about database setup or privileges
cat README.md | grep -A 20 -B 5 "database\|privilege\|role\|setup" | head -50

Repository: namehash/ensnode

Length of output: 42


🏁 Script executed:

# Look for any deployment or infrastructure documentation
fd -e md apps/ensindexer/ | head -5 | xargs cat | grep -A 10 -B 5 "database\|privilege\|migration" | head -30

Repository: namehash/ensnode

Length of output: 429


🏁 Script executed:

# Search for any documentation about running migrations
rg "migrateEnsNodeSchema\|migrate" --type md | head -20

Repository: namehash/ensnode

Length of output: 42


🏁 Script executed:

# Check if there's any setup documentation in the ensdb-sdk package
fd -e md packages/ensdb-sdk/ | xargs cat 2>/dev/null | head -100

Repository: namehash/ensnode

Length of output: 441


🏁 Script executed:

# Look for any environment or deployment configuration
fd -e env -o -e yaml -o -e yml | head -10

Repository: namehash/ensnode

Length of output: 176


🏁 Script executed:

# Check docker configuration if it exists for database setup hints
fd -i dockerfile

Repository: namehash/ensnode

Length of output: 161


🏁 Script executed:

# Check one of the Dockerfiles for database setup hints
cat apps/ensindexer/Dockerfile | head -80

Repository: namehash/ensnode

Length of output: 480


🏁 Script executed:

# Look for any scripts that set up the database
fd . . -type f -name "*setup*" -o -name "*init*" | grep -i sql

Repository: namehash/ensnode

Length of output: 230


🏁 Script executed:

# Search for documentation about how migrations are tracked
rg "migrations" --type md apps/ensindexer/ | head -20

Repository: namehash/ensnode

Length of output: 365


🏁 Script executed:

# Look for any PostgreSQL initialization scripts
fd . . -type f -name "*init*.sql" -o -name "*setup*.sql"

Repository: namehash/ensnode

Length of output: 230


🏁 Script executed:

# Check for environment-specific configuration
fd -e json -e config.ts -o -e config.js | head -10

Repository: namehash/ensnode

Length of output: 176


🏁 Script executed:

# Search for any comments about database requirements or setup
rg "superuser\|CREATE EVENT TRIGGER\|privilege" --type ts --type sql

Repository: namehash/ensnode

Length of output: 42


Consider event trigger idempotency in recovery scenarios and document superuser privilege requirement.

Two points to address:

  1. Idempotency: While Drizzle tracks executed migrations, CREATE EVENT TRIGGER lacks IF NOT EXISTS support. If migrations are manually re-run or the migration tracking table is corrupted, creation will fail. For robustness, consider using DROP EVENT TRIGGER IF EXISTS cleanup_ensnode_metadata_on_schema_drop CASCADE; before the CREATE EVENT TRIGGER statement to ensure recovery scenarios don't fail.

  2. Privilege requirements: Creating event triggers requires PostgreSQL superuser privileges. Document this requirement in deployment/setup documentation so operators know the database role running migrations must have superuser access.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/ensdb-sdk/migrations/0002_manage_orphaned_ensnode_metadata.sql`
around lines 18 - 21, Ensure creation of the event trigger
cleanup_ensnode_metadata_on_schema_drop is idempotent by preceding the CREATE
EVENT TRIGGER call with a DROP EVENT TRIGGER IF EXISTS
cleanup_ensnode_metadata_on_schema_drop CASCADE; so re-running migrations or
recovering from a corrupted migrations table won’t fail, and add a note in
deployment/setup docs that creating event triggers (e.g.,
cleanup_ensnode_metadata_on_schema_drop which EXECUTE FUNCTION
handle_schema_dropped()) requires PostgreSQL superuser privileges so the DB role
running migrations must be superuser.

@tk-o
Copy link
Copy Markdown
Contributor Author

tk-o commented Apr 8, 2026

@greptile review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 8, 2026

Tip:

Greploops — Automatically fix all review issues by running /greploops in Claude Code. It iterates: fix, push, re-review, repeat until 5/5 confidence.

Use the Greptile plugin for Claude Code to query reviews, search comments, and manage custom context directly from your terminal.

Comment on lines +20 to +23
CREATE EVENT TRIGGER cleanup_ensnode_metadata_on_schema_drop
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE FUNCTION "ensnode"."handle_schema_dropped"();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
CREATE EVENT TRIGGER cleanup_ensnode_metadata_on_schema_drop
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE FUNCTION "ensnode"."handle_schema_dropped"();
DO $
BEGIN
CREATE EVENT TRIGGER cleanup_ensnode_metadata_on_schema_drop
ON sql_drop
WHEN TAG IN ('DROP SCHEMA')
EXECUTE FUNCTION "ensnode"."handle_schema_dropped"();
EXCEPTION WHEN insufficient_privilege THEN
-- Event trigger creation requires superuser privileges.
-- This is expected in managed PostgreSQL environments.
RAISE NOTICE 'Event trigger "cleanup_ensnode_metadata_on_schema_drop" could not be created due to insufficient privileges. This feature requires superuser access.';
END $;

CREATE EVENT TRIGGER requires superuser privileges in PostgreSQL, causing migrations to fail in managed database environments that don't grant superuser access to application users

Fix on Vercel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants