@hatk/hatk 0.0.1-alpha.52 → 0.0.1-alpha.53

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/database/db.js +13 -16
  2. package/package.json +1 -1
@@ -214,13 +214,16 @@ function buildChildExpectedCols(columns) {
214
214
  }
215
215
  export async function migrateSchema(tableSchemas) {
216
216
  const changes = [];
217
+ const newCollections = new Set();
217
218
  for (const schema of tableSchemas) {
218
219
  if (schema.columns.length === 0)
219
220
  continue; // generic JSON storage, skip
220
221
  const tableName = schema.collection;
221
222
  const existingCols = await getExistingColumns(tableName);
222
- if (existingCols.size === 0)
223
+ if (existingCols.size === 0) {
224
+ newCollections.add(schema.collection);
223
225
  continue; // table just created, nothing to migrate
226
+ }
224
227
  // Expected columns: base columns (uri, cid, did, indexed_at) + schema columns
225
228
  const expectedCols = new Map();
226
229
  expectedCols.set('uri', 'TEXT');
@@ -281,22 +284,16 @@ export async function migrateSchema(tableSchemas) {
281
284
  if (changes.length > 0) {
282
285
  await applyMigrationChanges(changes);
283
286
  }
284
- // Check for empty collection tables these are newly added and need backfill
285
- // Skip on fresh DB (no repos yet) since backfill runs naturally
286
- const [hasRepos] = await all(`SELECT 1 FROM _repos LIMIT 1`);
287
- if (hasRepos) {
288
- for (const schema of tableSchemas) {
289
- if (schema.columns.length === 0)
290
- continue;
291
- try {
292
- const [row] = await all(`SELECT 1 FROM ${schema.tableName} LIMIT 1`);
293
- if (!row) {
294
- await run(`UPDATE _repos SET status = 'pending' WHERE status = 'active'`);
295
- emit('migration', 'new_collection', { collection: schema.collection });
296
- break; // only need to mark once
297
- }
287
+ // Trigger backfill only for genuinely new collections (tables created this startup)
288
+ // Previously this checked ALL empty tables, which caused infinite resync loops
289
+ // for collections that are legitimately empty (e.g. blocks when nobody has blocked)
290
+ if (newCollections.size > 0) {
291
+ const [hasRepos] = await all(`SELECT 1 FROM _repos LIMIT 1`);
292
+ if (hasRepos) {
293
+ await run(`UPDATE _repos SET status = 'pending' WHERE status = 'active'`);
294
+ for (const collection of newCollections) {
295
+ emit('migration', 'new_collection', { collection });
298
296
  }
299
- catch { }
300
297
  }
301
298
  }
302
299
  return changes;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatk/hatk",
3
- "version": "0.0.1-alpha.52",
3
+ "version": "0.0.1-alpha.53",
4
4
  "license": "MIT",
5
5
  "bin": {
6
6
  "hatk": "dist/cli.js"