Skip to content

Commit

Permalink
use alter scripts from prev version till new version
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Jun 16, 2021
1 parent 6dc3bb8 commit 63d1401
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export type TMiddleware = (request: WebWorkerRequest) => Promise<any>;

export type InitDbResult = {
isCreated: Boolean,
database: IDataBase,
database?: IDataBase,
oldVersion: number,
newVersion: number
}
17 changes: 0 additions & 17 deletions src/main/connection_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,21 +263,4 @@ export class ConnectionHelper {
this.queryManager.run(requestForWorker);
}
}

// protected callEvent_(event: EVENT, args: any[]) {
// const events = this.eventQueue.filter(function (ev) {
// if (ev.event === event) {
// return ev;
// }
// });
// // events.forEach(function (ev) {
// // ev.callback(...args);
// // });
// return Promise.all(
// events.map(cb => {
// const result = cb.callback.call(this, ...args);
// return result && result.then ? result : Promise.resolve(result);
// })
// );
// }
}
58 changes: 31 additions & 27 deletions src/worker/idbutil/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ export class IDBUtil {

res({
isCreated: isDbCreated,
database: userDbSchema(db),
oldVersion: oldVersion,
newVersion: dbVersion
} as InitDbResult);
Expand Down Expand Up @@ -144,32 +143,37 @@ export class IDBUtil {
if (!storeNames.contains(table.name)) {
createObjectStore(table);
}
const alterQuery = table.alter[dbVersion];
if (!alterQuery) return;
const store = transaction.objectStore(table.name);
forObj(
alterQuery.add || {}, ((_, column) => {
addColumn(store, column);
table.columns.push(column);
})
)
forObj(
alterQuery.drop || {}, ((columnName) => {
deleteColumn(store, table, columnName);
})
)
forObj(
alterQuery.modify || {}, ((columnName, column: IColumn) => {
const shouldDelete = column.multiEntry || column.keyPath || column.unique;
let targetColumn = table.columns.find(q => q.name === columnName);
const newColumn = Object.assign(targetColumn, column);
if (shouldDelete) {
deleteColumn(store, table, columnName);
addColumn(store, newColumn);
table.columns.push(newColumn);
}
})
)
for (let i = oldVersion; i <= dbVersion; i++) {
const alterQuery = table.alter[i];
if (alterQuery) {
const store = transaction.objectStore(table.name);
forObj(
alterQuery.add || {}, ((name, column) => {
column.name = name;
addColumn(store, column);
table.columns.push(column);
})
)
forObj(
alterQuery.drop || {}, ((columnName) => {
deleteColumn(store, table, columnName);
})
)
forObj(
alterQuery.modify || {}, ((columnName, column: IColumn) => {
const shouldDelete = column.multiEntry || column.keyPath || column.unique;
let targetColumn = table.columns.find(q => q.name === columnName);
const newColumn = Object.assign(targetColumn, column);
newColumn.name = columnName;
if (shouldDelete) {
deleteColumn(store, table, columnName);
addColumn(store, newColumn);
table.columns.push(newColumn);
}
})
)
}
}
});
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/worker/query_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ export class QueryManager {
this.util.initDb().then((dbInfo) => {
if (dbInfo.isCreated) {
this.db = dbMeta;
dbInfo.database = userDbSchema(this.db);
MetaHelper.set(
MetaHelper.dbSchema, dbMeta,
this.util
Expand All @@ -277,6 +278,7 @@ export class QueryManager {
this.util
).then((value: any) => {
this.db = value;
dbInfo.database = userDbSchema(this.db);
res(dbInfo);
});
}
Expand Down
11 changes: 11 additions & 0 deletions test/cases/column_option/multi_entry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,19 @@ describe('Multi Entry Test', function () {
}

db.tables[0].alter = alter;
let isUpgradeCalled = false;
con.on("upgrade", (dataBase, oldVersion, newVersion) => {
isUpgradeCalled = true;
expect(db.version).equal(dataBase.version);
expect(db.name).equal(dataBase.name);
expect(db.tables.length).equal(dataBase.tables.length);
expect(oldVersion).equal(1);
expect(newVersion).equal(2);
expect(dataBase.tables[0].columns['tags'].multiEntry).equal(true);
})
con.initDb(db).then(function (isDbCreated) {
expect(isDbCreated).to.be.an('boolean').equal(true);
expect(isUpgradeCalled).to.be.an('boolean').equal(true);
done();
}).catch(function (err) {
done(err);
Expand Down
Loading

0 comments on commit 63d1401

Please sign in to comment.