From 8da2332c74787a3650d6cfd31c01960e6e346161 Mon Sep 17 00:00:00 2001 From: 18alantom <2.alan.tom@gmail.com> Date: Wed, 1 Dec 2021 14:37:35 +0530 Subject: [PATCH] feat: sqlite schema update hack --- backends/sqlite.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/backends/sqlite.js b/backends/sqlite.js index 24247f74..ddfd54d8 100644 --- a/backends/sqlite.js +++ b/backends/sqlite.js @@ -8,16 +8,16 @@ class SqliteDatabase extends Database { this.connectionParams = { client: 'sqlite3', connection: { - filename: this.dbPath + filename: this.dbPath, }, pool: { afterCreate(conn, done) { conn.run('PRAGMA foreign_keys=ON'); done(); - } + }, }, useNullAsDefault: true, - asyncStackTraces: process.env.NODE_ENV === 'development' + asyncStackTraces: process.env.NODE_ENV === 'development', }; } @@ -48,12 +48,12 @@ class SqliteDatabase extends Database { } async getTableColumns(doctype) { - return (await this.sql(`PRAGMA table_info(${doctype})`)).map(d => d.name); + return (await this.sql(`PRAGMA table_info(${doctype})`)).map((d) => d.name); } async getForeignKeys(doctype) { return (await this.sql(`PRAGMA foreign_key_list(${doctype})`)).map( - d => d.from + (d) => d.from ); } @@ -103,6 +103,17 @@ class SqliteDatabase extends Database { } return errorType; } + + async prestigeTheTable(tableName, tableRows) { + // Alter table hacx for sqlite in case of schema change. + const tempName = `__${tableName}`; + await this.knex.raw('PRAGMA foreign_keys=OFF'); + await this.createTable(tableName, tempName); + await this.knex.batchInsert(tempName, tableRows); + await this.knex.schema.dropTable(tableName); + await this.knex.schema.renameTable(tempName, tableName); + await this.knex.raw('PRAGMA foreign_keys=ON'); + } } module.exports = SqliteDatabase;