feat(db): extract db setup and don't send sync errors to config

This commit is contained in:
Evan Morikawa 2017-02-01 13:36:30 -08:00
parent a94e1a0127
commit 5dea41dc34
4 changed files with 60 additions and 65 deletions

View file

@ -8,16 +8,16 @@ import path from 'path';
import proc from 'child_process'
import {EventEmitter} from 'events';
import SystemTrayManager from './system-tray-manager';
import WindowManager from './window-manager';
import FileListCache from './file-list-cache';
import ApplicationMenu from './application-menu';
import AutoUpdateManager from './auto-update-manager';
import SystemTrayManager from './system-tray-manager';
import PerformanceMonitor from './performance-monitor'
import DefaultClientHelper from '../default-client-helper';
import NylasProtocolHandler from './nylas-protocol-handler';
import PackageMigrationManager from './package-migration-manager';
import ConfigPersistenceManager from './config-persistence-manager';
import DefaultClientHelper from '../default-client-helper';
let clipboard = null;
@ -38,8 +38,6 @@ export default class Application extends EventEmitter {
this.fileListCache = new FileListCache();
this.nylasProtocolHandler = new NylasProtocolHandler(this.resourcePath, this.safeMode);
this.temporaryMigrateConfig();
const Config = require('../config');
const config = new Config();
this.config = config;
@ -102,17 +100,6 @@ export default class Application extends EventEmitter {
return this.quitting;
}
temporaryMigrateConfig() {
const oldConfigFilePath = fs.resolve(this.configDirPath, 'config.cson');
const newConfigFilePath = path.join(this.configDirPath, 'config.json');
if (oldConfigFilePath) {
const CSON = require('season');
const userConfig = CSON.readFileSync(oldConfigFilePath);
fs.writeFileSync(newConfigFilePath, JSON.stringify(userConfig, null, 2));
fs.unlinkSync(oldConfigFilePath);
}
}
// Opens a new window based on the options provided.
handleLaunchOptions(options) {
const {specMode, pathsToOpen, urlsToOpen} = options;

34
src/database-helpers.es6 Normal file
View file

@ -0,0 +1,34 @@
import path from 'path';
import Sqlite3 from 'better-sqlite3';
export function setupDatabase(dbPath) {
return new Promise((resolve, reject) => {
const db = new Sqlite3(dbPath, {});
db.on('close', reject)
db.on('open', () => {
// https://www.sqlite.org/wal.html
// WAL provides more concurrency as readers do not block writers and a writer
// does not block readers. Reading and writing can proceed concurrently.
db.pragma(`journal_mode = WAL`);
// Note: These are properties of the connection, so they must be set regardless
// of whether the database setup queries are run.
// https://www.sqlite.org/intern-v-extern-blob.html
// A database page size of 8192 or 16384 gives the best performance for large BLOB I/O.
db.pragma(`main.page_size = 8192`);
db.pragma(`main.cache_size = 20000`);
db.pragma(`main.synchronous = NORMAL`);
resolve(db);
});
})
}
export function databasePath(configDirPath, specMode = false) {
let dbPath = path.join(configDirPath, 'edgehill.db');
if (specMode) {
dbPath = path.join(configDirPath, 'edgehill.test.db');
}
return dbPath
}

View file

@ -11,7 +11,6 @@ import DatabaseStore from './database-store'
const configAccountsKey = "nylas.accounts"
const configVersionKey = "nylas.accountsVersion"
const configTokensKey = "nylas.accountTokens"
/*
@ -136,8 +135,9 @@ class AccountStore extends NylasStore {
_save = () => {
this._version += 1
NylasEnv.config.set(configTokensKey, null)
NylasEnv.config.set(configAccountsKey, this._accounts)
const configAccounts = this._accounts.map(a => a.toJSON())
configAccounts.forEach(a => delete a.sync_error)
NylasEnv.config.set(configAccountsKey, configAccounts)
NylasEnv.config.set(configVersionKey, this._version)
this._trigger()
}

View file

@ -1,7 +1,6 @@
/* eslint global-require: 0 */
import path from 'path';
import fs from 'fs';
import Sqlite3 from 'better-sqlite3';
import childProcess from 'child_process';
import PromiseQueue from 'promise-queue';
import {remote, ipcRenderer} from 'electron';
@ -14,6 +13,7 @@ import Actions from '../actions'
import DatabaseChangeRecord from './database-change-record';
import DatabaseTransaction from './database-transaction';
import DatabaseSetupQueryBuilder from './database-setup-query-builder';
import {setupDatabase, databasePath} from '../../database-helpers'
const DatabaseVersion = "23";
const DatabasePhase = {
@ -88,11 +88,7 @@ class DatabaseStore extends NylasStore {
this.setupEmitter();
this._emitter.setMaxListeners(100);
if (NylasEnv.inSpecMode()) {
this._databasePath = path.join(NylasEnv.getConfigDirPath(), 'edgehill.test.db');
} else {
this._databasePath = path.join(NylasEnv.getConfigDirPath(), 'edgehill.db');
}
this._databasePath = databasePath(NylasEnv.getConfigDirPath(), NylasEnv.inSpecMode())
this._databaseMutationHooks = [];
@ -102,7 +98,7 @@ class DatabaseStore extends NylasStore {
setTimeout(() => this._onPhaseChange(), 0);
}
_onPhaseChange() {
async _onPhaseChange() {
if (NylasEnv.inSpecMode()) {
return;
}
@ -111,23 +107,21 @@ class DatabaseStore extends NylasStore {
const phase = app.databasePhase()
if (phase === DatabasePhase.Setup && NylasEnv.isWorkWindow()) {
this._openDatabase(() => {
this._checkDatabaseVersion({allowUnset: true}, () => {
this._runDatabaseSetup(() => {
app.setDatabasePhase(DatabasePhase.Ready);
setTimeout(() => this._runDatabaseAnalyze(), 60 * 1000);
});
await this._openDatabase()
this._checkDatabaseVersion({allowUnset: true}, () => {
this._runDatabaseSetup(() => {
app.setDatabasePhase(DatabasePhase.Ready);
setTimeout(() => this._runDatabaseAnalyze(), 60 * 1000);
});
});
} else if (phase === DatabasePhase.Ready) {
this._openDatabase(() => {
this._checkDatabaseVersion({}, () => {
this._open = true;
for (const w of this._waiting) {
w();
}
this._waiting = [];
});
await this._openDatabase()
this._checkDatabaseVersion({}, () => {
this._open = true;
for (const w of this._waiting) {
w();
}
this._waiting = [];
});
} else if (phase === DatabasePhase.Close) {
this._open = false;
@ -152,37 +146,17 @@ class DatabaseStore extends NylasStore {
}
}
_openDatabase(ready) {
if (this._db) {
ready();
return;
}
this._db = new Sqlite3(this._databasePath, {});
this._db.on('close', (err) => {
async _openDatabase() {
if (this._db) return
try {
this._db = await setupDatabase(this._databasePath)
} catch (err) {
NylasEnv.showErrorDialog({
title: `Unable to open SQLite database at ${this._databasePath}`,
message: err.toString(),
});
this._handleSetupError(err);
})
this._db.on('open', () => {
// https://www.sqlite.org/wal.html
// WAL provides more concurrency as readers do not block writers and a writer
// does not block readers. Reading and writing can proceed concurrently.
this._db.pragma(`journal_mode = WAL`);
// Note: These are properties of the connection, so they must be set regardless
// of whether the database setup queries are run.
// https://www.sqlite.org/intern-v-extern-blob.html
// A database page size of 8192 or 16384 gives the best performance for large BLOB I/O.
this._db.pragma(`main.page_size = 8192`);
this._db.pragma(`main.cache_size = 20000`);
this._db.pragma(`main.synchronous = NORMAL`);
ready();
});
}
}
_checkDatabaseVersion({allowUnset} = {}, ready) {