Skip to content

Commit

Permalink
fix(main): fix await run db migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfscream authored and oceanlvr committed Jul 31, 2021
1 parent 583ca60 commit ba711a2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
46 changes: 17 additions & 29 deletions src/background.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
'use strict'

import 'reflect-metadata' // Required by TypoORM.
;('use strict')
import { app, protocol, BrowserWindow, ipcMain, shell, Menu } from 'electron'
import { createProtocol } from 'vue-cli-plugin-electron-builder/lib'
import installExtension, { VUEJS_DEVTOOLS } from 'electron-devtools-installer'
import { updateConnectionMessage } from '@/api/connection'
import { quitAndRenameLogger } from './utils/logger'
import db from './database/index'
import updateChecker from './main/updateChecker'
import getMenuTemplate from './main/getMenuTemplate'
import saveFile from './main/saveFile'
Expand All @@ -14,13 +13,14 @@ import newWindow from './main/newWindow'
import useConnection, { initOptionModel } from '@/database/useConnection'
import useServices from '@/database/useServices'

interface WindowSizeModel {
width: number
height: number
}

declare const __static: string

let theme: Theme = 'light'
let autoCheckUpdate: boolean = true
const windowSize = {
width: 1025,
height: 749,
}
const isDevelopment: boolean = process.env.NODE_ENV !== 'production'
const isMac: boolean = process.platform === 'darwin'

Expand Down Expand Up @@ -81,29 +81,18 @@ function beforeAppQuit() {
app.quit()
}

let windowSize: WindowSizeModel
let theme: Theme
let autoCheckUpdate: boolean

async function beforeWindowReady() {
await ConnectionInit({
doMigrations: true,
undoMigrations: false,
} as initOptionModel)
async function createWindow() {
// Init tables and connect to local database.
await ConnectionInit({ doMigrations: true } as initOptionModel)
const { settingService } = useServices()
await settingService.setSetting()
const setting = await settingService.getSetting()
if (setting) {
windowSize = {
height: setting.height,
width: setting.width,
}
theme = setting.currentTheme
autoCheckUpdate = setting.autoCheck
windowSize.height = setting.height
windowSize.width = setting.width
}
}

function createWindow() {
// Create the browser window.
win = new BrowserWindow({
...windowSize,
Expand Down Expand Up @@ -142,13 +131,16 @@ function createWindow() {
win = null
beforeAppQuit()
})
handleIpcMessages()
if (autoCheckUpdate) {
updateChecker()
}
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', async () => {
await beforeWindowReady()
if (isDevelopment && !process.env.IS_TEST) {
// Install Vue Devtools
try {
Expand All @@ -158,10 +150,6 @@ app.on('ready', async () => {
}
}
createWindow()
handleIpcMessages()
if (autoCheckUpdate) {
updateChecker()
}
})

// Quit when all windows are closed.
Expand Down
5 changes: 3 additions & 2 deletions src/database/useConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ const useConnection = () => {
useContainer(Container)
sqlConnection = await createConnection(ORMConfig)
if (initOption.doMigrations) {
sqlConnection.runMigrations()
await sqlConnection.runMigrations()
} else if (initOption.undoMigrations) {
sqlConnection.undoLastMigration()
await sqlConnection.undoLastMigration()
}
return sqlConnection
}
async function ConnectionDestory() {
if (sqlConnection) {
Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import 'reflect-metadata' // Required by TypoORM.
import Vue from 'vue'
import VueI18n from 'vue-i18n'
import VueClipboard from 'vue-clipboard2'
import path from 'path'
import log4js from 'log4js'

import 'element-ui/lib/theme-chalk/index.css'
import ElementLocale from 'element-ui/lib/locale'

import App from './App.vue'
import router from './router/index'
import store from './store/index'
import VueI18n from 'vue-i18n'
import VueClipboard from 'vue-clipboard2'
import Lang from './lang'
import element from './utils/element'
import VueLog4js from './plugins/logPlugin/index'
import log4js from 'log4js'
import { getOrCreateLogDir } from './utils/logger'
import logConfig from './plugins/logPlugin/logConfig.json'
import path from 'path'
import useConnection, { initOptionModel } from './database/useConnection'

const { ConnectionInit } = useConnection()

// Init typeORM connection before Vue APP start, after this DI services are available.
ConnectionInit({ doMigrations: false, undoMigrations: false } as initOptionModel).then(() => {
const LOG_DIR = getOrCreateLogDir()
Expand Down

0 comments on commit ba711a2

Please sign in to comment.