Skip to content

Commit 0c65914

Browse files
committed
Make ENSDb Writer Worker to execute database migrations
1 parent 49c7deb commit 0c65914

4 files changed

Lines changed: 62 additions & 6 deletions

File tree

apps/ensindexer/src/lib/ensdb-client/ensdb-client.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
2-
import { and, eq, sql } from "drizzle-orm/sql";
2+
import { migrate } from "drizzle-orm/node-postgres/migrator";
3+
import { and, eq } from "drizzle-orm/sql";
34

45
import * as ensNodeSchema from "@ensnode/ensnode-schema/ensnode";
56
import {
@@ -8,6 +9,7 @@ import {
89
deserializeEnsIndexerPublicConfig,
910
type EnsDbClientMutation,
1011
type EnsDbClientQuery,
12+
type EnsDbMigration,
1113
type EnsIndexerPublicConfig,
1214
EnsNodeMetadataKeys,
1315
type SerializedEnsNodeMetadata,
@@ -38,7 +40,7 @@ interface DrizzleDb extends NodePgDatabase<typeof ensNodeSchema> {}
3840
* - ENSDb version
3941
* - ENSIndexer Public Config, and Indexing Status Snapshot and CrossChainIndexingStatusSnapshot.
4042
*/
41-
export class EnsDbClient implements EnsDbClientQuery, EnsDbClientMutation {
43+
export class EnsDbClient implements EnsDbClientQuery, EnsDbClientMutation, EnsDbMigration {
4244
/**
4345
* Drizzle database instance for ENSDb.
4446
*/
@@ -191,4 +193,13 @@ export class EnsDbClient implements EnsDbClientQuery, EnsDbClientMutation {
191193
set: { value: metadata.value },
192194
});
193195
}
196+
197+
/**
198+
* @inheritdoc
199+
*/
200+
async migrate(migrationsDirPath: string): Promise<void> {
201+
return migrate(this.db, {
202+
migrationsFolder: migrationsDirPath,
203+
});
204+
}
194205
}

apps/ensindexer/src/lib/ensdb-writer-worker/ensdb-writer-worker.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ const INDEXING_STATUS_RECORD_UPDATE_INTERVAL: Duration = 1;
2424
/**
2525
* ENSDb Writer Worker
2626
*
27-
* A worker responsible for writing ENSIndexer-related metadata into ENSDb, including:
28-
* - ENSDb version
29-
* - ENSIndexer Public Config
30-
* - ENSIndexer Indexing Status Snapshots
27+
* A worker responsible for:
28+
* - executing ENSDb database migrations on startup, and
29+
* - writing ENSNode-related metadata into ENSDb, including:
30+
* - ENSDb version
31+
* - ENSIndexer Public Config
32+
* - Indexing Status Snapshots
3133
*/
3234
export class EnsDbWriterWorker {
3335
/**
@@ -50,32 +52,42 @@ export class EnsDbWriterWorker {
5052
*/
5153
private publicConfigBuilder: PublicConfigBuilder;
5254

55+
/**
56+
* Path to the directory containing ENSDb migrations to be executed by the worker on startup.
57+
*/
58+
private migrationsDirPath: string;
59+
5360
/**
5461
* @param ensDbClient ENSDb Client instance used by the worker to interact with ENSDb.
5562
* @param publicConfigBuilder ENSIndexer Public Config Builder instance used by the worker to read ENSIndexer Public Config.
5663
* @param indexingStatusBuilder Indexing Status Builder instance used by the worker to read ENSIndexer Indexing Status.
64+
* @param migrationsDirPath Path to the directory containing ENSDb migrations to be executed by the worker on startup.
5765
*/
5866
constructor(
5967
ensDbClient: EnsDbClient,
6068
publicConfigBuilder: PublicConfigBuilder,
6169
indexingStatusBuilder: IndexingStatusBuilder,
70+
migrationsDirPath: string,
6271
) {
6372
this.ensDbClient = ensDbClient;
6473
this.publicConfigBuilder = publicConfigBuilder;
6574
this.indexingStatusBuilder = indexingStatusBuilder;
75+
this.migrationsDirPath = migrationsDirPath;
6676
}
6777

6878
/**
6979
* Run the ENSDb Writer Worker
7080
*
7181
* The worker performs the following tasks:
82+
* 0) Execute pending ENSDb migrations.
7283
* 1) A single attempt to upsert ENSDb version into ENSDb.
7384
* 2) A single attempt to upsert serialized representation of
7485
* {@link EnsIndexerPublicConfig} into ENSDb.
7586
* 3) A recurring attempt to upsert serialized representation of
7687
* {@link CrossChainIndexingStatusSnapshot} into ENSDb.
7788
*
7889
* @throws Error if the worker is already running, or
90+
* if database migrations execution fails, or
7991
* if the in-memory ENSIndexer Public Config could not be fetched, or
8092
* if the in-memory ENSIndexer Public Config is incompatible with the stored config in ENSDb.
8193
*/
@@ -85,6 +97,11 @@ export class EnsDbWriterWorker {
8597
throw new Error("EnsDbWriterWorker is already running");
8698
}
8799

100+
// Task 0: execute database migrations
101+
console.log(`[EnsDbWriterWorker]: Executing database migrations...`);
102+
await this.executeMigrations();
103+
console.log(`[EnsDbWriterWorker]: Database migrations executed successfully`);
104+
88105
// Fetch data required for task 1 and task 2.
89106
const inMemoryConfig = await this.getValidatedEnsIndexerPublicConfig();
90107

@@ -126,6 +143,17 @@ export class EnsDbWriterWorker {
126143
}
127144
}
128145

146+
/**
147+
* Execute database migrations for the ENSDb Writer Worker.
148+
*
149+
* Runs all pending migrations in the defined migrations directory.
150+
*
151+
* @throws Error if any migration fails to execute.
152+
*/
153+
private async executeMigrations(): Promise<void> {
154+
await this.ensDbClient.migrate(this.migrationsDirPath);
155+
}
156+
129157
/**
130158
* Get validated ENSIndexer Public Config object for the ENSDb Writer Worker.
131159
*

apps/ensindexer/src/lib/ensdb-writer-worker/singleton.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fileURLToPath } from "node:url";
2+
13
import { ensDbClient } from "@/lib/ensdb-client/singleton";
24
import { indexingStatusBuilder } from "@/lib/indexing-status-builder/singleton";
35
import { publicConfigBuilder } from "@/lib/public-config-builder/singleton";
@@ -20,10 +22,15 @@ export function startEnsDbWriterWorker() {
2022
throw new Error("EnsDbWriterWorker has already been initialized");
2123
}
2224

25+
const migrationsDirPath = fileURLToPath(
26+
new URL("../../../drizzle-kit/migrations/", import.meta.url),
27+
);
28+
2329
ensDbWriterWorker = new EnsDbWriterWorker(
2430
ensDbClient,
2531
publicConfigBuilder,
2632
indexingStatusBuilder,
33+
migrationsDirPath,
2734
);
2835

2936
ensDbWriterWorker

packages/ensnode-sdk/src/ensdb/client.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,13 @@ export interface EnsDbClientMutation {
5656
*/
5757
upsertIndexingStatusSnapshot(indexingStatus: CrossChainIndexingStatusSnapshot): Promise<void>;
5858
}
59+
60+
export interface EnsDbMigration {
61+
/**
62+
* Execute pending migrations for ENSDb.
63+
*
64+
* @param migrationsDirPath - The file path to the directory containing migration files.
65+
* @throws error when migration execution fails.
66+
*/
67+
migrate(migrationsDirPath: string): Promise<void>;
68+
}

0 commit comments

Comments
 (0)