From e366e624d1483e0765ca08e71199566ff7b55ec6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristijan=20Ribari=C4=87?= Date: Fri, 4 Oct 2024 17:41:48 +0200 Subject: [PATCH] Fix: Ensure only one workspace is marked as default This commit ensures that only one workspace can be marked as default. Previously, multiple workspaces could be marked as default, leading to unexpected behavior. The commit introduces a transaction to update all workspaces, setting `is_default` to `0` for all workspaces except the one being saved as default. This prevents multiple workspaces from being marked as default. --- src/ZenWorkspacesStorage.mjs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/ZenWorkspacesStorage.mjs b/src/ZenWorkspacesStorage.mjs index 5ae9363..82b5f7c 100644 --- a/src/ZenWorkspacesStorage.mjs +++ b/src/ZenWorkspacesStorage.mjs @@ -39,8 +39,15 @@ var ZenWorkspacesStorage = { async saveWorkspace(workspace) { await PlacesUtils.withConnectionWrapper('ZenWorkspacesStorage.saveWorkspace', async (db) => { const now = Date.now(); - await db.executeCached( - ` + + await db.executeTransaction(async function() { + // If the workspace is set as default, unset is_default for all other workspaces + if (workspace.default) { + await db.execute(`UPDATE zen_workspaces SET is_default = 0 WHERE uuid != :uuid`, { uuid: workspace.uuid }); + } + + // Then insert or replace the workspace + await db.executeCached(` INSERT OR REPLACE INTO zen_workspaces ( uuid, name, icon, is_default, container_id, created_at, updated_at ) VALUES ( @@ -48,16 +55,15 @@ var ZenWorkspacesStorage = { COALESCE((SELECT created_at FROM zen_workspaces WHERE uuid = :uuid), :now), :now ) - `, - { + `, { uuid: workspace.uuid, name: workspace.name, icon: workspace.icon || null, is_default: workspace.default ? 1 : 0, container_id: workspace.containerTabId || null, - now, - } - ); + now + }); + }); }); },