MOBILE-2842 data: Add "action" to primary key of the offline table

main
Albert Gasset 2019-01-30 11:06:27 +01:00
parent d3246e6900
commit 9602746c9a
1 changed files with 58 additions and 39 deletions

View File

@ -14,11 +14,11 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CoreLoggerProvider } from '@providers/logger'; import { CoreLoggerProvider } from '@providers/logger';
import { CoreSitesProvider } from '@providers/sites'; import { CoreSitesProvider, CoreSiteSchema } from '@providers/sites';
import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreTextUtilsProvider } from '@providers/utils/text';
import { CoreFileProvider } from '@providers/file'; import { CoreFileProvider } from '@providers/file';
import { CoreFileUploaderProvider } from '@core/fileuploader/providers/fileuploader'; import { CoreFileUploaderProvider } from '@core/fileuploader/providers/fileuploader';
import { SQLiteDBTableSchema } from '@classes/sqlitedb'; import { SQLiteDB } from '@classes/sqlitedb';
/** /**
* Service to handle Offline data. * Service to handle Offline data.
@ -29,48 +29,67 @@ export class AddonModDataOfflineProvider {
protected logger; protected logger;
// Variables for database. // Variables for database.
static DATA_ENTRY_TABLE = 'addon_mod_data_entry'; static DATA_ENTRY_TABLE = 'addon_mod_data_entry_1';
protected tablesSchema: SQLiteDBTableSchema[] = [ protected siteSchema: CoreSiteSchema = {
{ name: 'AddonModDataOfflineProvider',
name: AddonModDataOfflineProvider.DATA_ENTRY_TABLE, version: 1,
columns: [ tables: [
{ {
name: 'dataid', name: AddonModDataOfflineProvider.DATA_ENTRY_TABLE,
type: 'INTEGER' columns: [
}, {
{ name: 'dataid',
name: 'courseid', type: 'INTEGER'
type: 'INTEGER' },
}, {
{ name: 'courseid',
name: 'groupid', type: 'INTEGER'
type: 'INTEGER' },
}, {
{ name: 'groupid',
name: 'action', type: 'INTEGER'
type: 'TEXT' },
}, {
{ name: 'action',
name: 'entryid', type: 'TEXT'
type: 'INTEGER' },
}, {
{ name: 'entryid',
name: 'fields', type: 'INTEGER'
type: 'TEXT' },
}, {
{ name: 'fields',
name: 'timemodified', type: 'TEXT'
type: 'INTEGER' },
} {
], name: 'timemodified',
primaryKeys: ['dataid', 'entryid'] type: 'INTEGER'
}
],
primaryKeys: ['dataid', 'entryid', 'action']
}
],
migrate(db: SQLiteDB, oldVersion: number): Promise<any> | void {
if (oldVersion == 0) {
// Move the records from the old table.
const newTable = AddonModDataOfflineProvider.DATA_ENTRY_TABLE;
const oldTable = 'addon_mod_data_entry';
return db.tableExists(oldTable).then(() => {
return db.insertRecordsFrom(newTable, oldTable).then(() => {
return db.dropTable(oldTable);
});
}).catch(() => {
// Old table does not exist, ignore.
});
}
} }
]; };
constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider, constructor(logger: CoreLoggerProvider, private sitesProvider: CoreSitesProvider, private textUtils: CoreTextUtilsProvider,
private fileProvider: CoreFileProvider, private fileUploaderProvider: CoreFileUploaderProvider) { private fileProvider: CoreFileProvider, private fileUploaderProvider: CoreFileUploaderProvider) {
this.logger = logger.getInstance('AddonModDataOfflineProvider'); this.logger = logger.getInstance('AddonModDataOfflineProvider');
this.sitesProvider.createTablesFromSchema(this.tablesSchema); this.sitesProvider.registerSiteSchema(this.siteSchema);
} }
/** /**