From 0288b6b5c0869908398ca4c869c503dceeee2896 Mon Sep 17 00:00:00 2001 From: Nikola Ubavic <53820106+ubavic@users.noreply.github.com> Date: Wed, 13 Oct 2021 00:51:59 +0200 Subject: [PATCH 1/3] fix: machine unpair breaks the UI Purging db tables upon unpairing from the server will prevent later UI crashes caused by working with a NULL value. --- lib/pairing.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/pairing.js b/lib/pairing.js index 5932878c..e795e8b1 100644 --- a/lib/pairing.js +++ b/lib/pairing.js @@ -15,9 +15,10 @@ function pullToken (token) { function unpair (deviceId) { const sql = 'delete from devices where device_id=$1' const deleteMachinePings = 'delete from machine_pings where device_id=$1' + const deleteMachineHeartbeat = 'delete from machine_network_heartbeat where device_id=$1' // TODO new-admin: We should remove all configs related to that device. This can get tricky. - return Promise.all([db.none(sql, [deviceId]), db.none(deleteMachinePings, [deviceId])]) + return Promise.all([db.none(sql, [deviceId]), db.none(deleteMachinePings, [deviceId]), db.none(deleteMachineHeartbeat, [deviceId])]) } function pair (token, deviceId, machineModel) { From 30ac3d61be21022e4b02172936c87c25f19703d0 Mon Sep 17 00:00:00 2001 From: Nikola Ubavic <53820106+ubavic@users.noreply.github.com> Date: Wed, 13 Oct 2021 18:16:57 +0200 Subject: [PATCH 2/3] refactor: transaction instead query sequence added query for purging the machine_network_performance table --- lib/pairing.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/pairing.js b/lib/pairing.js index e795e8b1..fdfa52a8 100644 --- a/lib/pairing.js +++ b/lib/pairing.js @@ -13,12 +13,13 @@ function pullToken (token) { } function unpair (deviceId) { - const sql = 'delete from devices where device_id=$1' - const deleteMachinePings = 'delete from machine_pings where device_id=$1' - const deleteMachineHeartbeat = 'delete from machine_network_heartbeat where device_id=$1' - // TODO new-admin: We should remove all configs related to that device. This can get tricky. - return Promise.all([db.none(sql, [deviceId]), db.none(deleteMachinePings, [deviceId]), db.none(deleteMachineHeartbeat, [deviceId])]) + return db.tx(async t => { + await t.none('DELETE FROM devices WHERE device_id=$1', [deviceId]) + await t.none('DELETE FROM machine_pings WHERE device_id=$1', [deviceId]) + await t.none('DELETE FROM machine_network_heartbeat WHERE device_id=$1', [deviceId]) + await t.none('DELETE FROM machine_network_performance WHERE device_id=$1', [deviceId]) + }) } function pair (token, deviceId, machineModel) { From 5b3c71822ea05b2fc4beda114549b821d82b02d9 Mon Sep 17 00:00:00 2001 From: Nikola Ubavic <53820106+ubavic@users.noreply.github.com> Date: Wed, 27 Oct 2021 19:05:53 +0200 Subject: [PATCH 3/3] fix: use Promise.all instead await --- lib/pairing.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/pairing.js b/lib/pairing.js index fdfa52a8..56b290d1 100644 --- a/lib/pairing.js +++ b/lib/pairing.js @@ -14,11 +14,12 @@ function pullToken (token) { function unpair (deviceId) { // TODO new-admin: We should remove all configs related to that device. This can get tricky. - return db.tx(async t => { - await t.none('DELETE FROM devices WHERE device_id=$1', [deviceId]) - await t.none('DELETE FROM machine_pings WHERE device_id=$1', [deviceId]) - await t.none('DELETE FROM machine_network_heartbeat WHERE device_id=$1', [deviceId]) - await t.none('DELETE FROM machine_network_performance WHERE device_id=$1', [deviceId]) + return db.tx(t => { + const q1 = t.none('DELETE FROM devices WHERE device_id=$1', [deviceId]) + const q2 = t.none('DELETE FROM machine_pings WHERE device_id=$1', [deviceId]) + const q3 = t.none('DELETE FROM machine_network_heartbeat WHERE device_id=$1', [deviceId]) + const q4 = t.none('DELETE FROM machine_network_performance WHERE device_id=$1', [deviceId]) + return Promise.all([q1, q2, q3, q4]) }) }