@mastra/libsql 1.24.0-alpha.1 → 1.24.0-alpha.3

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
@@ -423,9 +423,14 @@ function handleLogicalOperator(key, value, parentPath) {
423
423
  }
424
424
  const values = [];
425
425
  const joinOperator = key === "$or" || key === "$nor" ? "OR" : "AND";
426
- const joined = (Array.isArray(value) ? value.map((f) => {
427
- return (!!f ? Object.entries(f) : []).map(([k, v]) => buildCondition$1(k, v, key));
428
- }) : [buildCondition$1(key, value, parentPath)]).flat().map((c) => {
426
+ const joined = (Array.isArray(value) ? value.flatMap((f) => {
427
+ const branch = (!!f ? Object.entries(f) : []).map(([k, v]) => buildCondition$1(k, v, key));
428
+ if (branch.length <= 1) return branch;
429
+ return [{
430
+ sql: `(${branch.map((c) => c.sql).join(" AND ")})`,
431
+ values: branch.flatMap((c) => c.values)
432
+ }];
433
+ }) : [buildCondition$1(key, value, parentPath)]).map((c) => {
429
434
  values.push(...c.values);
430
435
  return c.sql;
431
436
  }).join(` ${joinOperator} `);
@@ -11226,6 +11231,7 @@ var SchedulesLibSQL = class SchedulesLibSQL extends SchedulesStorage {
11226
11231
  });
11227
11232
  }
11228
11233
  async init() {
11234
+ await this.#migrateLegacyTriggersTable();
11229
11235
  await this.#db.createTable({
11230
11236
  tableName: TABLE_SCHEDULES,
11231
11237
  schema: TABLE_SCHEMAS[TABLE_SCHEDULES]
@@ -11234,6 +11240,15 @@ var SchedulesLibSQL = class SchedulesLibSQL extends SchedulesStorage {
11234
11240
  tableName: TABLE_SCHEDULE_TRIGGERS,
11235
11241
  schema: TABLE_SCHEMAS[TABLE_SCHEDULE_TRIGGERS]
11236
11242
  });
11243
+ try {
11244
+ await this.#db.alterTable({
11245
+ tableName: TABLE_SCHEDULES,
11246
+ schema: TABLE_SCHEMAS[TABLE_SCHEDULES],
11247
+ ifNotExists: ["owner_type", "owner_id"]
11248
+ });
11249
+ } catch (error) {
11250
+ if (!(await this.#db.hasColumn(TABLE_SCHEDULES, "owner_type") && await this.#db.hasColumn(TABLE_SCHEDULES, "owner_id"))) throw error;
11251
+ }
11237
11252
  await this.#client.batch([{
11238
11253
  sql: `CREATE INDEX IF NOT EXISTS idx_schedules_status_next_fire ON "${TABLE_SCHEDULES}" ("status", "next_fire_at")`,
11239
11254
  args: []
@@ -11242,6 +11257,42 @@ var SchedulesLibSQL = class SchedulesLibSQL extends SchedulesStorage {
11242
11257
  args: []
11243
11258
  }], "write");
11244
11259
  }
11260
+ /**
11261
+ * Trigger tables created before the ownership/audit schema change are keyed
11262
+ * by `run_id` (NOT NULL) and store `status` instead of `outcome`. SQLite
11263
+ * cannot change a primary key or rename-and-retype in place, so the table is
11264
+ * rebuilt in the current shape inside one write transaction. Legacy run ids
11265
+ * are unique, so they become the new row ids.
11266
+ */
11267
+ async #migrateLegacyTriggersTable() {
11268
+ const isLegacy = async () => await this.#db.hasColumn(TABLE_SCHEDULE_TRIGGERS, "status") && !await this.#db.hasColumn(TABLE_SCHEDULE_TRIGGERS, "outcome");
11269
+ if (!await isLegacy()) return;
11270
+ const shadow = `${TABLE_SCHEDULE_TRIGGERS}__legacy_rebuild`;
11271
+ try {
11272
+ await this.#client.batch([
11273
+ `DROP TABLE IF EXISTS "${shadow}"`,
11274
+ `CREATE TABLE "${shadow}" (
11275
+ "id" TEXT NOT NULL PRIMARY KEY,
11276
+ "schedule_id" TEXT NOT NULL,
11277
+ "run_id" TEXT,
11278
+ "scheduled_fire_at" INTEGER NOT NULL,
11279
+ "actual_fire_at" INTEGER NOT NULL,
11280
+ "outcome" TEXT NOT NULL,
11281
+ "error" TEXT,
11282
+ "trigger_kind" TEXT NOT NULL,
11283
+ "parent_trigger_id" TEXT,
11284
+ "metadata" TEXT
11285
+ )`,
11286
+ `INSERT INTO "${shadow}" ("id", "schedule_id", "run_id", "scheduled_fire_at", "actual_fire_at", "outcome", "error", "trigger_kind")
11287
+ SELECT "run_id", "schedule_id", "run_id", "scheduled_fire_at", "actual_fire_at", "status", "error", 'schedule-fire'
11288
+ FROM "${TABLE_SCHEDULE_TRIGGERS}"`,
11289
+ `DROP TABLE "${TABLE_SCHEDULE_TRIGGERS}"`,
11290
+ `ALTER TABLE "${shadow}" RENAME TO "${TABLE_SCHEDULE_TRIGGERS}"`
11291
+ ], "write");
11292
+ } catch (error) {
11293
+ if (await isLegacy()) throw error;
11294
+ }
11295
+ }
11245
11296
  async dangerouslyClearAll() {
11246
11297
  await this.#db.deleteData({ tableName: TABLE_SCHEDULE_TRIGGERS });
11247
11298
  await this.#db.deleteData({ tableName: TABLE_SCHEDULES });