@h-ai/reldb 0.1.0-alpha.71 → 0.1.0-alpha.73

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.
package/dist/index.js CHANGED
@@ -3424,14 +3424,14 @@ async function migrateSchema(db, module, steps, options = {}) {
3424
3424
  return err(HaiCommonError.VALIDATION_ERROR, core.i18n.coreM("core_errorValidation"));
3425
3425
  const table = "hai_schema_migrations";
3426
3426
  const targetVersion = steps.length;
3427
- const failed = () => err({ ...HaiCommonError.INIT_FAILED, message: core.i18n.coreM("core_runtimeStartFailed", { params: { module } }), ext: { module, phase: "migration" } });
3427
+ const failed = (cause) => err({ ...HaiCommonError.INIT_FAILED, message: core.i18n.coreM("core_runtimeStartFailed", { params: { module } }), ext: { module, phase: "migration" }, cause });
3428
3428
  async function inspect(sql) {
3429
3429
  const columns = await schemaColumns(db, table, sql);
3430
3430
  if (!columns.success)
3431
- return failed();
3431
+ return failed(columns.error);
3432
3432
  const row = columns.data.length ? await sql.get(`SELECT version FROM ${table} WHERE module=?`, [module]) : ok(null);
3433
3433
  if (!row.success)
3434
- return failed();
3434
+ return failed(row.error);
3435
3435
  const currentVersion = Number(row.data?.version ?? 0);
3436
3436
  if (currentVersion > targetVersion)
3437
3437
  return failed();
@@ -3439,7 +3439,7 @@ async function migrateSchema(db, module, steps, options = {}) {
3439
3439
  for (const step of steps) {
3440
3440
  const needed = await step.needed(sql);
3441
3441
  if (!needed.success)
3442
- return failed();
3442
+ return failed(needed.error);
3443
3443
  if (needed.data)
3444
3444
  changes.push(step.name);
3445
3445
  }
@@ -3449,13 +3449,13 @@ async function migrateSchema(db, module, steps, options = {}) {
3449
3449
  return inspect(db.sql);
3450
3450
  const created = await db.sql.execute(`CREATE TABLE IF NOT EXISTS ${table} (module VARCHAR(64) PRIMARY KEY, version INTEGER NOT NULL)`);
3451
3451
  if (!created.success)
3452
- return failed();
3452
+ return failed(created.error);
3453
3453
  const seeded = await db.sql.execute(db.config?.type === "mysql" ? `INSERT IGNORE INTO ${table}(module,version) VALUES (?,0)` : `INSERT INTO ${table}(module,version) VALUES (?,0) ON CONFLICT(module) DO NOTHING`, [module]);
3454
3454
  if (!seeded.success)
3455
- return failed();
3455
+ return failed(seeded.error);
3456
3456
  const started = await db.tx.begin();
3457
3457
  if (!started.success)
3458
- return failed();
3458
+ return failed(started.error);
3459
3459
  const tx = started.data;
3460
3460
  let mysqlLock = false;
3461
3461
  let committed = false;
@@ -3463,40 +3463,40 @@ async function migrateSchema(db, module, steps, options = {}) {
3463
3463
  if (db.config?.type === "mysql") {
3464
3464
  const lock = await tx.get("SELECT GET_LOCK(?,30) AS acquired", [`hai-schema:${module}`]);
3465
3465
  if (!lock.success || Number(lock.data?.acquired) !== 1)
3466
- return failed();
3466
+ return failed(lock.success ? new Error("Migration lock unavailable") : lock.error);
3467
3467
  mysqlLock = true;
3468
3468
  }
3469
3469
  const locked = await tx.execute(`UPDATE ${table} SET version=version WHERE module=?`, [module]);
3470
3470
  if (!locked.success)
3471
- return failed();
3471
+ return failed(locked.error);
3472
3472
  const plan = await inspect(tx);
3473
3473
  if (!plan.success)
3474
3474
  return plan;
3475
3475
  for (const step of steps) {
3476
3476
  const needed = await step.needed(tx);
3477
3477
  if (!needed.success)
3478
- return failed();
3478
+ return failed(needed.error);
3479
3479
  if (needed.data) {
3480
3480
  const applied = await step.apply(tx);
3481
3481
  if (!applied.success)
3482
- return failed();
3482
+ return failed(applied.error);
3483
3483
  const remaining = await step.needed(tx);
3484
3484
  if (!remaining.success || remaining.data)
3485
- return failed();
3485
+ return failed(remaining.success ? new Error("Migration step did not converge") : remaining.error);
3486
3486
  }
3487
3487
  }
3488
3488
  const recorded = await tx.execute(`UPDATE ${table} SET version=? WHERE module=?`, [targetVersion, module]);
3489
3489
  if (!recorded.success)
3490
- return failed();
3490
+ return failed(recorded.error);
3491
3491
  if (mysqlLock) {
3492
3492
  await tx.get("SELECT RELEASE_LOCK(?)", [`hai-schema:${module}`]);
3493
3493
  mysqlLock = false;
3494
3494
  }
3495
3495
  const result = await tx.commit();
3496
3496
  committed = result.success;
3497
- return result.success ? plan : failed();
3498
- } catch {
3499
- return failed();
3497
+ return result.success ? plan : failed(result.error);
3498
+ } catch (error) {
3499
+ return failed(error);
3500
3500
  } finally {
3501
3501
  if (mysqlLock)
3502
3502
  await tx.get("SELECT RELEASE_LOCK(?)", [`hai-schema:${module}`]);