MOBILE-3640 db: Migration db helper function

main
Pau Ferrer Ocaña 2021-03-18 16:01:05 +01:00
parent 9b0e4f0f0e
commit 2d53e954a0
4 changed files with 56 additions and 74 deletions

View File

@ -201,7 +201,6 @@ export const CALENDAR_SITE_SCHEMA: CoreSiteSchema = {
],
async migrate(db: SQLiteDB, oldVersion: number): Promise<void> {
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<AddonCalendarEventDBRecord>(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);
}
},
};

View File

@ -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<void> {
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<SQLiteDBRecordValues>(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

View File

@ -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<void> {
async migrate(db: SQLiteDB, oldVersion: number): Promise<void> {
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<CoreH5PLibraryDBRecord>(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);
},
};

View File

@ -87,28 +87,7 @@ export const APP_SCHEMA: CoreAppSchema = {
],
async migrate(db: SQLiteDB, oldVersion: number): Promise<void> {
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<SiteDBEntry>(oldTable);
const promises: Promise<number>[] = [];
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<void> {
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,
}));
}
},
};