Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error with deprecated db name #1328

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/app/core/session/session-service/local-session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,35 +70,38 @@ export class LocalSession extends SessionService {

private async initializeDatabaseForCurrentUser() {
const userDBName = `${this.currentDBUser.name}-${AppConfig.settings.database.name}`;
this.initDatabase(userDBName);
if (!(await this.database.isEmpty())) {
const tmpDB = new PouchDatabase(undefined);
this.initDatabase(userDBName, tmpDB);
if (!(await tmpDB.isEmpty())) {
// Current user has own database, we are done here
this.initDatabase(userDBName);
return;
}

this.initDatabase(AppConfig.settings.database.name);
this.initDatabase(AppConfig.settings.database.name, tmpDB);
const dbFallback = window.localStorage.getItem(
LocalSession.DEPRECATED_DB_KEY
);
const dbAvailable = !dbFallback || dbFallback === this.currentDBUser.name;
if (dbAvailable && !(await this.database.isEmpty())) {
if (dbAvailable && !(await tmpDB.isEmpty())) {
// Old database is available and can be used by the current user
window.localStorage.setItem(
LocalSession.DEPRECATED_DB_KEY,
this.currentDBUser.name
);
this.initDatabase(AppConfig.settings.database.name);
return;
}

// Create a new database for the current user
this.initDatabase(userDBName);
}

private initDatabase(dbName: string) {
private initDatabase(dbName: string, db = this.database) {
if (AppConfig.settings.session_type === SessionType.mock) {
this.database.initInMemoryDB(dbName);
db.initInMemoryDB(dbName);
} else {
this.database.initIndexedDB(dbName);
db.initIndexedDB(dbName);
}
}

Expand Down