From 03614f1a2554160c856ad55e77a2eadf62772cf8 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Wed, 14 Apr 2021 10:06:34 +0200 Subject: [PATCH] MOBILE-3656 db: Add addColumn method and use it in h5p --- src/addons/mod/data/services/database/data.ts | 9 ----- src/core/classes/sqlitedb.ts | 40 ++++++++++++++----- .../features/h5p/services/database/h5p.ts | 6 +-- src/core/services/app.ts | 2 +- src/core/services/database/sites.ts | 2 +- src/core/services/sites.ts | 2 +- 6 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/addons/mod/data/services/database/data.ts b/src/addons/mod/data/services/database/data.ts index 203274e78..8b4c8a150 100644 --- a/src/addons/mod/data/services/database/data.ts +++ b/src/addons/mod/data/services/database/data.ts @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { SQLiteDB } from '@classes/sqlitedb'; import { CoreSiteSchema } from '@services/sites'; import { AddonModDataAction } from '../data'; @@ -59,14 +58,6 @@ export const ADDON_MOD_DATA_OFFLINE_SITE_SCHEMA: CoreSiteSchema = { primaryKeys: ['dataid', 'entryid', 'action'], }, ], - async migrate(db: SQLiteDB, oldVersion: number): Promise { - if (oldVersion > 0) { - return; - } - - // Move the records from the old table. - await db.migrateTable('addon_mod_data_entry', DATA_ENTRY_TABLE); - }, }; /** diff --git a/src/core/classes/sqlitedb.ts b/src/core/classes/sqlitedb.ts index 4c1deb91b..9455e4c6a 100644 --- a/src/core/classes/sqlitedb.ts +++ b/src/core/classes/sqlitedb.ts @@ -17,6 +17,8 @@ import { SQLiteObject } from '@ionic-native/sqlite/ngx'; import { SQLite, Platform } from '@singletons'; import { CoreError } from '@classes/errors/error'; +type SQLiteDBColumnType = 'INTEGER' | 'REAL' | 'TEXT' | 'BLOB'; + /** * Schema of a table. */ @@ -64,7 +66,7 @@ export interface SQLiteDBColumnSchema { /** * Column's type. */ - type?: 'INTEGER' | 'REAL' | 'TEXT' | 'BLOB'; + type?: SQLiteDBColumnType; /** * Whether the column is a primary key. Use it only if primary key is a single column. @@ -145,6 +147,30 @@ export class SQLiteDB { this.init(); } + /** + * Add a column to an existing table. + * + * @param table Table name. + * @param column Name of the column to add. + * @param type Type of the column to add. + * @param constraints Other constraints (e.g. NOT NULL). + * @return Promise resolved when done. + */ + async addColumn(table: string, column: string, type: SQLiteDBColumnType, constraints?: string): Promise { + constraints = constraints || ''; + + try { + await this.execute(`ALTER TABLE ${table} ADD COLUMN ${column} ${type} ${constraints}`); + } catch (error) { + if (error && error.code == 5 && error?.message.indexOf('duplicate column name') != -1) { + // Column already exists. + return; + } + + throw error; + } + } + /** * Helper function to create a table if it doesn't exist. * @@ -839,25 +865,19 @@ export class SQLiteDB { * * @param table The database table to be inserted into. * @param source The database table to get the records from. - * @param conditions The conditions to build the where clause. Must not contain numeric indexes. - * @param fields A comma separated list of fields to return. * @return Promise resolved when done. */ async insertRecordsFrom( table: string, source: string, - conditions?: SQLiteDBRecordValues, - fields: string = '*', ): Promise { - const selectAndParams = this.whereClause(conditions); - const select = selectAndParams.sql ? 'WHERE ' + selectAndParams.sql : ''; - const params = selectAndParams.params; + const records = await this.getAllRecords(source); - await this.execute(`INSERT INTO ${table} SELECT ${fields} FROM ${source} ${select}`, params); + await Promise.all(records.map((record) => this.insertRecord(table, record))); } /** - * Helper migration function for tables. + * Migrate all the data from a table to another table. * It will check if old table exists and drop it when finished. * * @param oldTable Old table name. diff --git a/src/core/features/h5p/services/database/h5p.ts b/src/core/features/h5p/services/database/h5p.ts index a324a1ae2..4ff57daa5 100644 --- a/src/core/features/h5p/services/database/h5p.ts +++ b/src/core/features/h5p/services/database/h5p.ts @@ -20,7 +20,7 @@ import { CoreSiteSchema } from '@services/sites'; */ // DB table names. export const CONTENT_TABLE_NAME = 'h5p_content'; // H5P content. -export const LIBRARIES_TABLE_NAME = 'h5p_libraries_2'; // Installed libraries. +export const LIBRARIES_TABLE_NAME = 'h5p_libraries'; // Installed libraries. export const LIBRARY_DEPENDENCIES_TABLE_NAME = 'h5p_library_dependencies'; // Library dependencies. export const CONTENTS_LIBRARIES_TABLE_NAME = 'h5p_contents_libraries'; // Which library is used in which content. export const LIBRARIES_CACHEDASSETS_TABLE_NAME = 'h5p_libraries_cachedassets'; // H5P cached library assets. @@ -249,8 +249,8 @@ export const SITE_SCHEMA: CoreSiteSchema = { return; } - // Move the records from the old table. - await db.migrateTable('h5p_libraries', LIBRARIES_TABLE_NAME); + // Add the metadata column to the table. + await db.addColumn(LIBRARIES_TABLE_NAME, 'metadatasettings', 'TEXT'); }, }; diff --git a/src/core/services/app.ts b/src/core/services/app.ts index 05b6a8181..3859c2236 100644 --- a/src/core/services/app.ts +++ b/src/core/services/app.ts @@ -151,7 +151,7 @@ export class CoreAppProvider { if (schema.tables) { await this.db.createTablesFromSchema(schema.tables); } - if (schema.migrate) { + if (schema.migrate && oldVersion > 0) { await schema.migrate(this.db, oldVersion); } diff --git a/src/core/services/database/sites.ts b/src/core/services/database/sites.ts index 4f4968ec6..857bdba4b 100644 --- a/src/core/services/database/sites.ts +++ b/src/core/services/database/sites.ts @@ -144,7 +144,7 @@ export const SITE_SCHEMA: CoreSiteSchema = { }, ], async migrate(db: SQLiteDB, oldVersion: number): Promise { - if (oldVersion && oldVersion < 2) { + if (oldVersion < 2) { await db.migrateTable('wscache', CoreSite.WS_CACHE_TABLE, (record) => ({ id: record.id, data: record.data, diff --git a/src/core/services/sites.ts b/src/core/services/sites.ts index 63c7e9014..f2be5fb5a 100644 --- a/src/core/services/sites.ts +++ b/src/core/services/sites.ts @@ -1581,7 +1581,7 @@ export class CoreSitesProvider { if (schema.tables) { await db.createTablesFromSchema(schema.tables); } - if (schema.migrate) { + if (schema.migrate && oldVersion > 0) { await schema.migrate(db, oldVersion, site.id); }