@@ -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 */
3234export 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 *
0 commit comments