@mastra/libsql 1.17.0-alpha.3 → 1.17.0

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
@@ -13363,8 +13363,10 @@ function assertIdentifier(kind, name) {
13363
13363
  }
13364
13364
  }
13365
13365
  function isUniqueViolation(error) {
13366
+ const code = typeof error === "object" && error !== null ? error.code : void 0;
13367
+ if (code === "SQLITE_CONSTRAINT_UNIQUE" || code === "SQLITE_CONSTRAINT_PRIMARYKEY") return true;
13366
13368
  const message = error instanceof Error ? error.message : String(error);
13367
- return message.includes("UNIQUE constraint failed") || message.includes("SQLITE_CONSTRAINT");
13369
+ return message.includes("UNIQUE constraint failed") || message.includes("SQLITE_CONSTRAINT_UNIQUE") || message.includes("SQLITE_CONSTRAINT_PRIMARYKEY");
13368
13370
  }
13369
13371
  function primaryKeyOf(schema) {
13370
13372
  const pks = Object.entries(schema.columns).filter(([, spec]) => spec.type === "uuid-pk" || spec.primaryKey);
@@ -13708,6 +13710,35 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
13708
13710
  if (spec.default !== void 0) ddl += ` DEFAULT ${serializeDefault(spec.default)}`;
13709
13711
  return ddl;
13710
13712
  }
13713
+ /**
13714
+ * A table created by an older schema may still say NOT NULL on a column the
13715
+ * current schema declares nullable — inserts of null then fail on databases
13716
+ * that predate the change. SQLite has no `ALTER COLUMN DROP NOT NULL`, so
13717
+ * relaxation swaps in a table rebuilt from the current schema (create shadow
13718
+ * → copy → drop → rename) atomically.
13719
+ */
13720
+ async #relaxDriftedNotNulls(schema) {
13721
+ const info = await this.#client.execute(`PRAGMA table_info("${schema.name}")`);
13722
+ const hasDrift = info.rows.some((row) => {
13723
+ const spec = schema.columns[String(row.name)];
13724
+ if (!spec || spec.type === "uuid-pk" || spec.primaryKey) return false;
13725
+ return spec.nullable === true && Number(row.notnull) === 1;
13726
+ });
13727
+ if (!hasDrift) return;
13728
+ const ddl = Object.entries(schema.columns).map(([name, spec]) => this.#columnDdl(name, spec));
13729
+ const shadow = `${schema.name}__nullable_rebuild`;
13730
+ const cols = Object.keys(schema.columns).map((name) => `"${name}"`).join(", ");
13731
+ await this.#client.batch(
13732
+ [
13733
+ `DROP TABLE IF EXISTS "${shadow}"`,
13734
+ `CREATE TABLE "${shadow}" (${ddl.join(", ")})`,
13735
+ `INSERT INTO "${shadow}" (${cols}) SELECT ${cols} FROM "${schema.name}"`,
13736
+ `DROP TABLE "${schema.name}"`,
13737
+ `ALTER TABLE "${shadow}" RENAME TO "${schema.name}"`
13738
+ ],
13739
+ "write"
13740
+ );
13741
+ }
13711
13742
  async #ensureCollection(schema) {
13712
13743
  assertIdentifier("collection", schema.name);
13713
13744
  primaryKeyOf(schema);
@@ -13719,6 +13750,7 @@ var LibSQLFactoryStorage = class extends FactoryStorage {
13719
13750
  if (existing.has(name)) continue;
13720
13751
  await this.#client.execute(`ALTER TABLE "${schema.name}" ADD COLUMN ${this.#columnDdl(name, spec)}`);
13721
13752
  }
13753
+ await this.#relaxDriftedNotNulls(schema);
13722
13754
  for (const index of schema.uniqueIndexes ?? []) {
13723
13755
  assertIdentifier("index", index.name);
13724
13756
  index.columns.forEach((column) => assertIdentifier("column", column));