Skip to content

Commit

Permalink
upgrade rules
Browse files Browse the repository at this point in the history
  • Loading branch information
paulr34 committed Jun 28, 2022
1 parent ba0ad71 commit 57d9486
Show file tree
Hide file tree
Showing 10 changed files with 207 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src-electron/db/db-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const util = require('../util/util.js')
const dbEnum = require('../../src-shared/db-enum.js')
const dbCache = require('./db-cache')
const dbMapping = require('./db-mapping.js')
import { setUpgrade } from './query-upgrade.js'

// This is a SQLITE specific thing. With SQLITE databases,
// we can't have multiple transactions. So this mechanism
Expand Down Expand Up @@ -513,6 +514,7 @@ async function loadSchema(db, schemaPath, zapVersion, sqliteFile = null) {
await performSchemaLoad(db, context.data)
await updateCurrentSchemaCrc(db, context)
await updateSetting(db, rows)
await setUpgrade(db, 1, 'UPGRADE')
}

await insertOrReplaceSetting(db, 'APP', 'VERSION', zapVersion.version)
Expand Down
7 changes: 7 additions & 0 deletions src-electron/db/db-mapping.js
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,13 @@ exports.map = {
dirty: x.DIRTY == 1,
}
},
upgrade: (x) => {
if (x == null) return undefined
return {
upgrade: x.UPGRADE,
status: x.STATUS,
}
},
user: (x) => {
if (x == null) return undefined
return {
Expand Down
23 changes: 23 additions & 0 deletions src-electron/db/query-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ async function getSessionDirtyFlag(db, sessionId) {
return dbApi.fromDbBool(row.DIRTY)
}
}
/**
* Resolves with true or false, depending whether this session is dirty.
*
* @export
* @param {*} db
* @param {*} sessionId
* @returns A promise that resolves into true or false, reflecting session dirty state.
*/
async function getSessionUpgradeFlag(db, sessionId) {
let row = await dbApi.dbGet(db, 'SELECT UPGRADE FROM UPGRADE')
if (row.UPGRADE == 0) {
return 0
} else {
return 1
}
}

async function getSessionUpgradeStatus(db, sessionId) {
let row = await dbApi.dbGet(db, 'SELECT STATUS FROM UPGRADE')
return row.STATUS
}
/**
* Resolves w/ the session tied to a session id.
*
Expand Down Expand Up @@ -495,6 +516,8 @@ async function getAllSessionKeyValues(db, sessionId) {
exports.getAllSessions = getAllSessions
exports.setSessionClean = setSessionClean
exports.getSessionDirtyFlag = getSessionDirtyFlag
exports.getSessionUpgradeFlag = getSessionUpgradeFlag
exports.getSessionUpgradeStatus = getSessionUpgradeStatus
exports.getSessionFromSessionId = getSessionFromSessionId
exports.getSessionInfoFromSessionKey = getSessionInfoFromSessionKey
exports.ensureZapSessionId = ensureZapSessionId
Expand Down
96 changes: 96 additions & 0 deletions src-electron/db/query-upgrade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
*
* Copyright (c) 2020 Silicon Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* This module provides session related queries.
*
* @module DB API: session related queries.
*/
const dbApi = require('./db-api.js')

/**
* Sets the session upgrade flag to false.
*
* @export
* @param {*} db
* @param {*} sessionId
* @returns A promise that resolves with the number of rows updated.
*/
async function setUpgrade(db, upgrade, status) {
return dbApi.dbUpdate(
db,
'INSERT OR REPLACE INTO UPGRADE ( UPGRADE, STATUS ) VALUES ( ?, ?)',
[upgrade, status]
)
}

async function updateUpgrade(db, upgrade, status) {
return dbApi.dbUpdate(
db,
'UPDATE UPGRADE SET ( UPGRADE, STATUS ) = ( ?, ?)',
[upgrade, status]
)
}

/**
* Resolves with true or false, depending whether this upgrade flag is set.
*
* @export
* @param {*} db
* @param {*} sessionId
* @returns A promise that resolves into true or false, reflecting upgrade state.
*/
async function getUpgradeFlag(db, sessionId) {
let row = await dbApi.dbGet(db, 'SELECT UPGRADE FROM UPGRADE')
if (row == null) {
return undefined
} else {
return dbApi.fromDbBool(row.UPGRADE)
}
}

/**
* Sets the upgrade status.
*
* @export
* @param {*} db
* @param {*} sessionId
* @returns A promise that resolves with the number of rows updated.
*/
async function setUpgradeStatus(db, status) {
return dbApi.dbUpdate(db, 'UPDATE UPGRADE SET STATUS = 1')
}

/**
* Returns the status of the Upgrade.
*
* @export
* @param {*} db
* @param {*} sessionId
* @returns A promise that resolves into string, reflecting upgrade status.
*/
async function getUpgradeStatus(db) {
let row = await dbApi.dbGet(db, 'SELECT STATUS FROM UPGRADE')
return row
}

// exports
exports.setUpgrade = setUpgrade
exports.updateUpgrade = updateUpgrade
exports.getUpgradeFlag = getUpgradeFlag
exports.setUpgradeStatus = setUpgradeStatus
exports.getUpgradeStatus = getUpgradeStatus
14 changes: 14 additions & 0 deletions src-electron/db/zap-schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1068,4 +1068,18 @@ CREATE TABLE IF NOT EXISTS "SETTING" (
"VALUE" text,
UNIQUE(CATEGORY, KEY)
);

/*
Upgrade table
*/

CREATE TABLE IF NOT EXISTS "UPGRADE" (
"STATUS" text,
"UPGRADE" integer default 0,
"SESSION_REF" integer,
foreign key (SESSION_REF) references SESSION(SESSION_ID) on delete cascade,
UNIQUE(
SESSION_REF
)
);
/* EO SCHEMA */
27 changes: 27 additions & 0 deletions src-electron/ide-integration/studio-rest-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const op_add = '/rest/clic/component/add/project/'
const op_remove = '/rest/clic/component/remove/project/'

let dirtyFlagStatusId: NodeJS.Timeout
let upgradeFlagStatusId: NodeJS.Timeout
let ucComponentStateReportId: NodeJS.Timeout
let studioHttpPort: number

Expand Down Expand Up @@ -265,6 +266,9 @@ function initIdeIntegration(db: dbTypes.DbType, studioPort: number) {
dirtyFlagStatusId = setInterval(() => {
sendDirtyFlagStatus(db)
}, DIRTY_FLAG_REPORT_INTERVAL_MS)
upgradeFlagStatusId = setInterval(() => {
sendSessionUpgrade(db)
}, DIRTY_FLAG_REPORT_INTERVAL_MS)

ucComponentStateReportId = setInterval(() => {
sendUcComponentStateReport(db)
Expand Down Expand Up @@ -319,6 +323,28 @@ function sendDirtyFlagStatus(db: dbTypes.DbType) {
})
})
}
async function sendSessionUpgrade(db: dbTypes.DbType) {
let sessions = await querySession.getAllSessions(db)
for (const session of sessions) {
let socket = wsServer.clientSocket(session.sessionKey)
let upgrade = await querySession.getSessionUpgradeFlag(
db,
session.sessionId
)
let status = await querySession.getSessionUpgradeStatus(
db,
session.sessionId
)
if (socket) {
if (upgrade == 1) {
wsServer.sendWebSocketMessage(socket, {
category: dbEnum.wsCategory.upgrade,
payload: status,
})
}
}
}
}

/**
* Notify front-end that current session failed to load.
Expand Down Expand Up @@ -375,5 +401,6 @@ exports.projectName = projectName
exports.integrationEnabled = integrationEnabled
exports.initIdeIntegration = initIdeIntegration
exports.deinit = deinit
exports.sendSessionUpgrade = sendSessionUpgrade
exports.sendSessionCreationErrorStatus = sendSessionCreationErrorStatus
exports.sendComponentUpdateStatus = sendComponentUpdateStatus
2 changes: 2 additions & 0 deletions src-shared/db-enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ exports.pathRelativity = {
exports.wsCategory = {
generic: 'generic',
dirtyFlag: 'dirtyFlag',
upgrade: 'upgrade',
upgradeStatus: 'upgradeStatus',
validation: 'validation',
sessionCreationError: 'sessionCreationError',
componentUpdateStatus: 'componentUpdateStatus',
Expand Down
9 changes: 8 additions & 1 deletion src-shared/rend-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ exports.renderer_api = {
dirtyFlag: {
arg: 'dirtyState',
},
upgrade: {
arg: 'upgrade',
},
fileBrowse: {
arg: 'browseObject',
},
Expand Down Expand Up @@ -118,7 +121,11 @@ exports.id = {
setDarkTheme: 'setDarkTheme',
}

exports.notifyKey = { dirtyFlag: 'dirtyFlag', fileBrowse: 'fileBrowse' }
exports.notifyKey = {
dirtyFlag: 'dirtyFlag',
fileBrowse: 'fileBrowse',
upgrade: 'upgrade',
}

exports.jsonPrefix = 'rendererApiJson:'

Expand Down
1 change: 1 addition & 0 deletions src-shared/rest-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const updateKey = {
attributeReportChange: 'reportableChange',
attributeStorage: 'storageOption',
init: 'init',
upgrade: 'upgrade',
}

exports.param = {
Expand Down
30 changes: 27 additions & 3 deletions src/boot/ws.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import Events from 'events'
import dbEnum from '../../src-shared/db-enum.js'
import restApi from '../../src-shared/rest-api.js'
import rendApi from '../../src-shared/rend-api.js'
import { Notify } from 'quasar'
import { Dialog, Notify } from 'quasar'
import * as Util from '../util/util.js'
const http = require('http-status-codes')

Expand Down Expand Up @@ -128,13 +128,37 @@ onWebSocket(dbEnum.wsCategory.sessionCreationError, (data) => {
</center>`
Notify.create({
message: html,
color: 'negative',
color: 'black',
position: 'top',
html: true,
timeout: 0,
})

console.log(`sessionCreationError: ${JSON.stringify(data)}`)
console.log(`upgrade ${JSON.stringify(data)}`)
})

//commented unnecessary logs and listeners

// onWebSocket(dbEnum.wsCategory.validation, (data) => {
// // console.log(`Validation recieved: ${data}`)
// })

onWebSocket(dbEnum.wsCategory.upgrade, (data) => {
let html = `<center>
<strong>${data}</strong>
<br>
'upgrade'
</center>`
Notify.create({
message: html,
color: 'primary',
position: 'top',
html: true,
timeout: 0,
actions: [{ icon: 'close', color: 'white' }],
})

console.log(`upgrade: ${JSON.stringify(data)}`)
})

//commented unnecessary logs
Expand Down

0 comments on commit 57d9486

Please sign in to comment.