MOBILE-3656 db: Add addColumn method and use it in h5p
parent
ae3339ac7d
commit
03614f1a25
|
@ -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<void> {
|
||||
if (oldVersion > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Move the records from the old table.
|
||||
await db.migrateTable('addon_mod_data_entry', DATA_ENTRY_TABLE);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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<void> {
|
||||
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<void> {
|
||||
const selectAndParams = this.whereClause(conditions);
|
||||
const select = selectAndParams.sql ? 'WHERE ' + selectAndParams.sql : '';
|
||||
const params = selectAndParams.params;
|
||||
const records = await this.getAllRecords<SQLiteDBRecordValues>(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.
|
||||
|
|
|
@ -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');
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -144,7 +144,7 @@ export const SITE_SCHEMA: CoreSiteSchema = {
|
|||
},
|
||||
],
|
||||
async migrate(db: SQLiteDB, oldVersion: number): Promise<void> {
|
||||
if (oldVersion && oldVersion < 2) {
|
||||
if (oldVersion < 2) {
|
||||
await db.migrateTable('wscache', CoreSite.WS_CACHE_TABLE, (record) => ({
|
||||
id: record.id,
|
||||
data: record.data,
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue