From 2d53e954a05eac7ae69062e9e704518f3d8705ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Thu, 18 Mar 2021 16:01:05 +0100 Subject: [PATCH] MOBILE-3640 db: Migration db helper function --- .../calendar/services/database/calendar.ts | 15 +----- src/core/classes/sqlitedb.ts | 43 +++++++++++++++ .../features/h5p/services/database/h5p.ts | 20 ++----- src/core/services/database/sites.ts | 52 ++++--------------- 4 files changed, 56 insertions(+), 74 deletions(-) diff --git a/src/addons/calendar/services/database/calendar.ts b/src/addons/calendar/services/database/calendar.ts index 711239fbc..c6d3acd3d 100644 --- a/src/addons/calendar/services/database/calendar.ts +++ b/src/addons/calendar/services/database/calendar.ts @@ -201,7 +201,6 @@ export const CALENDAR_SITE_SCHEMA: CoreSiteSchema = { ], async migrate(db: SQLiteDB, oldVersion: number): Promise { if (oldVersion < 3) { - const newTable = EVENTS_TABLE; let oldTable = 'addon_calendar_events_2'; try { @@ -211,19 +210,7 @@ export const CALENDAR_SITE_SCHEMA: CoreSiteSchema = { oldTable = 'addon_calendar_events'; } - try { - await db.tableExists(oldTable); - - // Move the records from the old table. - const events = await db.getAllRecords(oldTable); - const promises = events.map((event) => db.insertRecord(newTable, event)); - - await Promise.all(promises); - - db.dropTable(oldTable); - } catch { - // Old table does not exist, ignore. - } + await db.migrateTable(oldTable, EVENTS_TABLE); } }, }; diff --git a/src/core/classes/sqlitedb.ts b/src/core/classes/sqlitedb.ts index 4aa39378c..4c1deb91b 100644 --- a/src/core/classes/sqlitedb.ts +++ b/src/core/classes/sqlitedb.ts @@ -856,6 +856,49 @@ export class SQLiteDB { await this.execute(`INSERT INTO ${table} SELECT ${fields} FROM ${source} ${select}`, params); } + /** + * Helper migration function for tables. + * It will check if old table exists and drop it when finished. + * + * @param oldTable Old table name. + * @param newTable New table name. + * @param mapCallback Mapping callback to migrate each record. + * @return Resolved when done. + */ + async migrateTable( + oldTable: string, + newTable: string, + mapCallback?: (record: SQLiteDBRecordValues) => SQLiteDBRecordValues, + ): Promise { + try { + await this.tableExists(oldTable); + } catch (error) { + // Old table does not exist, ignore. + return; + } + + // Move the records from the old table. + if (mapCallback) { + const records = await this.getAllRecords(oldTable); + const promises = records.map((record) => { + record = mapCallback(record); + + return this.insertRecord(newTable, record); + }); + + await Promise.all(promises); + } else { + // No changes needed. + await this.insertRecordsFrom(newTable, oldTable); + } + + try { + await this.dropTable(oldTable); + } catch (error) { + // Error deleting old table, ignore. + } + } + /** * Ensures that limit params are numeric and positive integers, to be passed to the database. * We explicitly treat null, '' and -1 as 0 in order to provide compatibility with how limit diff --git a/src/core/features/h5p/services/database/h5p.ts b/src/core/features/h5p/services/database/h5p.ts index f6605d529..a324a1ae2 100644 --- a/src/core/features/h5p/services/database/h5p.ts +++ b/src/core/features/h5p/services/database/h5p.ts @@ -244,27 +244,13 @@ export const SITE_SCHEMA: CoreSiteSchema = { ], }, ], - // eslint-disable-next-line @typescript-eslint/no-unused-vars - async migrate(db: SQLiteDB, oldVersion: number, siteId: string): Promise { + async migrate(db: SQLiteDB, oldVersion: number): Promise { if (oldVersion >= 2) { return; } - const newTable = LIBRARIES_TABLE_NAME; - const oldTable = 'h5p_libraries'; - - try { - await db.tableExists(oldTable); - - // Move the records from the old table. - const entries = await db.getAllRecords(oldTable); - - await Promise.all(entries.map((entry) => db.insertRecord(newTable, entry))); - - await db.dropTable(oldTable); - } catch { - // Old table does not exist, ignore. - } + // Move the records from the old table. + await db.migrateTable('h5p_libraries', LIBRARIES_TABLE_NAME); }, }; diff --git a/src/core/services/database/sites.ts b/src/core/services/database/sites.ts index 653db3620..4f4968ec6 100644 --- a/src/core/services/database/sites.ts +++ b/src/core/services/database/sites.ts @@ -87,28 +87,7 @@ export const APP_SCHEMA: CoreAppSchema = { ], async migrate(db: SQLiteDB, oldVersion: number): Promise { if (oldVersion < 2) { - const newTable = SITES_TABLE_NAME; - const oldTable = 'sites'; - - try { - // Check if V1 table exists. - await db.tableExists(oldTable); - - // Move the records from the old table. - const sites = await db.getAllRecords(oldTable); - const promises: Promise[] = []; - - sites.forEach((site) => { - promises.push(db.insertRecord(newTable, site)); - }); - - await Promise.all(promises); - - // Data moved, drop the old table. - await db.dropTable(oldTable); - } catch (error) { - // Old table does not exist, ignore. - } + await db.migrateTable('sites', SITES_TABLE_NAME); } }, }; @@ -166,27 +145,14 @@ export const SITE_SCHEMA: CoreSiteSchema = { ], async migrate(db: SQLiteDB, oldVersion: number): Promise { if (oldVersion && oldVersion < 2) { - const newTable = CoreSite.WS_CACHE_TABLE; - const oldTable = 'wscache'; - - try { - await db.tableExists(oldTable); - } catch (error) { - // Old table does not exist, ignore. - return; - } - // Cannot use insertRecordsFrom because there are extra fields, so manually code INSERT INTO. - await db.execute( - 'INSERT INTO ' + newTable + ' ' + - 'SELECT id, data, key, expirationTime, NULL as component, NULL as componentId ' + - 'FROM ' + oldTable, - ); - - try { - await db.dropTable(oldTable); - } catch (error) { - // Error deleting old table, ignore. - } + await db.migrateTable('wscache', CoreSite.WS_CACHE_TABLE, (record) => ({ + id: record.id, + data: record.data, + key: record.key, + expirationTime: record.expirationTime, + component: null, + componentId: null, + })); } }, };