@minatojs/driver-sqlite
Advanced tools
Comparing version 3.3.11 to 3.4.1
@@ -28,3 +28,3 @@ /// <reference types="node" /> | ||
/** synchronize table schema */ | ||
prepare(table: string): Promise<void>; | ||
prepare(table: string, dropKeys?: string[]): Promise<void>; | ||
init(buffer: ArrayLike<number> | null): void; | ||
@@ -31,0 +31,0 @@ load(): Promise<Buffer | null>; |
@@ -133,4 +133,4 @@ "use strict"; | ||
/** synchronize table schema */ | ||
async prepare(table) { | ||
const info = __privateMethod(this, _all, all_fn).call(this, `PRAGMA table_info(${(0, import_sql_utils.escapeId)(table)})`); | ||
async prepare(table, dropKeys) { | ||
const columns = __privateMethod(this, _all, all_fn).call(this, `PRAGMA table_info(${(0, import_sql_utils.escapeId)(table)})`); | ||
const model = this.model(table); | ||
@@ -143,4 +143,9 @@ const columnDefs = []; | ||
for (const key in model.fields) { | ||
if (model.fields[key].deprecated) { | ||
if (dropKeys == null ? void 0 : dropKeys.includes(key)) | ||
shouldMigrate = true; | ||
continue; | ||
} | ||
const legacy = [key, ...model.fields[key].legacy || []]; | ||
const column = info.find(({ name }) => legacy.includes(name)); | ||
const column = columns.find(({ name }) => legacy.includes(name)); | ||
const { initial, nullable = true } = model.fields[key]; | ||
@@ -177,17 +182,17 @@ const typedef = getTypeDef(model.fields[key]); | ||
} | ||
if (!info.length) { | ||
if (!columns.length) { | ||
logger.info("auto creating table %c", table); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(table)} (${[...columnDefs, ...indexDefs].join(", ")})`); | ||
} else if (shouldMigrate) { | ||
for (const column of info) { | ||
if (mapping[column.name]) | ||
for (const { name, type, notnull, pk, dflt_value } of columns) { | ||
if (mapping[name] || (dropKeys == null ? void 0 : dropKeys.includes(name))) | ||
continue; | ||
let def = `${(0, import_sql_utils.escapeId)(column.name)} ${column.type}`; | ||
def += (column.notnull ? " NOT " : " ") + "NULL"; | ||
if (column.pk) | ||
let def = `${(0, import_sql_utils.escapeId)(name)} ${type}`; | ||
def += (notnull ? " NOT " : " ") + "NULL"; | ||
if (pk) | ||
def += " PRIMARY KEY"; | ||
if (column.dflt_value !== null) | ||
def += " DEFAULT " + this.sql.escape(column.dflt_value); | ||
if (dflt_value !== null) | ||
def += " DEFAULT " + this.sql.escape(dflt_value); | ||
columnDefs.push(def); | ||
mapping[column.name] = column.name; | ||
mapping[name] = name; | ||
} | ||
@@ -197,11 +202,11 @@ const temp = table + "_temp"; | ||
logger.info("auto migrating table %c", table); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(temp)} (${columnDefs.join(", ")})`); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(temp)} (${[...columnDefs, ...indexDefs].join(", ")})`); | ||
try { | ||
__privateMethod(this, _run, run_fn).call(this, `INSERT INTO ${(0, import_sql_utils.escapeId)(temp)} SELECT ${fields} FROM ${(0, import_sql_utils.escapeId)(table)}`); | ||
__privateMethod(this, _run, run_fn).call(this, `DROP TABLE ${(0, import_sql_utils.escapeId)(table)}`); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(table)} (${[...columnDefs, ...indexDefs].join(", ")})`); | ||
__privateMethod(this, _run, run_fn).call(this, `INSERT INTO ${(0, import_sql_utils.escapeId)(table)} SELECT * FROM ${(0, import_sql_utils.escapeId)(temp)}`); | ||
} finally { | ||
} catch (error) { | ||
__privateMethod(this, _run, run_fn).call(this, `DROP TABLE ${(0, import_sql_utils.escapeId)(temp)}`); | ||
throw error; | ||
} | ||
__privateMethod(this, _run, run_fn).call(this, `ALTER TABLE ${(0, import_sql_utils.escapeId)(temp)} RENAME TO ${(0, import_sql_utils.escapeId)(table)}`); | ||
} else if (alter.length) { | ||
@@ -213,2 +218,15 @@ logger.info("auto updating table %c", table); | ||
} | ||
if (dropKeys) | ||
return; | ||
dropKeys = []; | ||
this.migrate(table, { | ||
error: logger.warn, | ||
before: (keys) => keys.every((key) => columns.some(({ name }) => name === key)), | ||
after: (keys) => dropKeys.push(...keys), | ||
finalize: () => { | ||
if (!dropKeys.length) | ||
return; | ||
this.prepare(table, dropKeys); | ||
} | ||
}); | ||
} | ||
@@ -215,0 +233,0 @@ init(buffer) { |
{ | ||
"name": "@minatojs/driver-sqlite", | ||
"version": "3.3.11", | ||
"version": "3.4.1", | ||
"description": "SQLite Driver for Minato", | ||
@@ -28,9 +28,9 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.2.2" | ||
"@minatojs/core": "^2.3.1" | ||
}, | ||
"devDependencies": { | ||
"@minatojs/tests": "^1.4.0" | ||
"@minatojs/tests": "^1.5.0" | ||
}, | ||
"dependencies": { | ||
"@minatojs/sql-utils": "^4.0.2", | ||
"@minatojs/sql-utils": "^4.0.3", | ||
"@minatojs/sql.js": "^2.0.0", | ||
@@ -37,0 +37,0 @@ "cosmokit": "^1.4.1", |
Sorry, the diff of this file is not supported yet
40976
444
Updated@minatojs/sql-utils@^4.0.3