Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/metal-donkeys-bake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ensnode/ensdb-sdk": minor
---

Added `enable_ext_pg_trgm` migration.
5 changes: 5 additions & 0 deletions .changeset/six-garlics-wish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ensnode/ensdb-sdk": minor
---

Added `manage_orphaned_ensnode_metadata` migration.
3 changes: 3 additions & 0 deletions packages/ensdb-sdk/migrations/0001_enable_ext_pg_trgm.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- This migration enables the pg_trgm extension, which is used for
-- trigram-based indexing and searching in PostgreSQL.
CREATE EXTENSION IF NOT EXISTS pg_trgm;
Comment thread
tk-o marked this conversation as resolved.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Does this action also require special priviliges?

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- This migration adds an event trigger to handle cleanup of orphaned records
-- in the `ensnode.metadata` table when an ENSIndexer Schema is dropped.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I note that this triggers on any schema that is not the ENSNode schema. We should document this more precisely.

-- Note: Running this migration requires superuser privileges due to
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should explicitly document what the consequence is of running this migration without superuser priviliges.

-- the use of event triggers.

CREATE OR REPLACE FUNCTION "ensnode"."handle_schema_dropped"()
RETURNS event_trigger
AS $$
DECLARE
obj RECORD;
BEGIN
FOR obj IN
SELECT *
FROM pg_event_trigger_dropped_objects()
LOOP
IF obj.object_type = 'schema'
AND obj.object_name <> 'ensnode'
AND to_regclass('"ensnode"."metadata"') IS NOT NULL THEN

DELETE FROM "ensnode"."metadata"
WHERE "ens_indexer_schema_name" = obj.object_name;

END IF;
END LOOP;
END;
$$ LANGUAGE plpgsql;


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;
Comment on lines +36 to +42
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 Silent success masks permanently absent cleanup feature

When insufficient_privilege is caught, the DO block exits cleanly with only a RAISE NOTICE. Drizzle records this migration as applied and will never re-run it. In non-superuser managed environments, ensnode.handle_schema_dropped is installed but the event trigger is never wired up — orphan cleanup is silently non-functional with no mechanism to retry short of a manual SQL intervention.

Consider replacing RAISE NOTICE with RAISE WARNING and adding a post-migration check (or logging at application start) so operators know this feature is inactive. Alternatively, document an upgrade path (e.g., a separate idempotent script) for when superuser access becomes available later.

$$;
55 changes: 55 additions & 0 deletions packages/ensdb-sdk/migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"id": "899ad861-685e-4639-8f31-cf4130b730e6",
"prevId": "d661dcae-f64d-4ecd-a4da-3d5783e17e2c",
"version": "7",
"dialect": "postgresql",
"tables": {
"ensnode.metadata": {
"name": "metadata",
"schema": "ensnode",
"columns": {
"ens_indexer_schema_name": {
"name": "ens_indexer_schema_name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"key": {
"name": "key",
"type": "text",
"primaryKey": false,
"notNull": true
},
"value": {
"name": "value",
"type": "jsonb",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"metadata_pkey": {
"name": "metadata_pkey",
"columns": ["ens_indexer_schema_name", "key"]
}
},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"views": {},
"sequences": {},
"roles": {},
"policies": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
55 changes: 55 additions & 0 deletions packages/ensdb-sdk/migrations/meta/0002_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"id": "1403ee88-a1d3-4192-a73c-f99b034c97b2",
"prevId": "899ad861-685e-4639-8f31-cf4130b730e6",
"version": "7",
"dialect": "postgresql",
"tables": {
"ensnode.metadata": {
"name": "metadata",
"schema": "ensnode",
"columns": {
"ens_indexer_schema_name": {
"name": "ens_indexer_schema_name",
"type": "text",
"primaryKey": false,
"notNull": true
},
"key": {
"name": "key",
"type": "text",
"primaryKey": false,
"notNull": true
},
"value": {
"name": "value",
"type": "jsonb",
"primaryKey": false,
"notNull": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"metadata_pkey": {
"name": "metadata_pkey",
"columns": ["ens_indexer_schema_name", "key"]
}
},
"uniqueConstraints": {},
"policies": {},
"checkConstraints": {},
"isRLSEnabled": false
}
},
"enums": {},
"schemas": {},
"views": {},
"sequences": {},
"roles": {},
"policies": {},
"_meta": {
"columns": {},
"schemas": {},
"tables": {}
}
}
14 changes: 14 additions & 0 deletions packages/ensdb-sdk/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@
"when": 1773743108514,
"tag": "0000_cultured_captain_cross",
"breakpoints": true
},
{
"idx": 1,
"version": "7",
"when": 1775668778695,
"tag": "0001_enable_ext_pg_trgm",
"breakpoints": true
},
{
"idx": 2,
"version": "7",
"when": 1775668818089,
"tag": "0002_manage_orphaned_ensnode_metadata",
"breakpoints": true
}
]
}
Loading