Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix(sql-editor): guard removeFavorite against missing snippet like addFavorite (#48111)

Ayaan Gazali5db1137c56a6
diff --git a/apps/studio/state/sql-editor/sql-editor-state.test.ts b/apps/studio/state/sql-editor/sql-editor-state.test.ts
new file mode 100644
index 00000000000..ba59013f006
--- /dev/null
+++ b/apps/studio/state/sql-editor/sql-editor-state.test.ts
@@ -0,0 +1,72 @@
+import { untrustedSql } from '@supabase/pg-meta'
+import { beforeEach, describe, expect, it } from 'vitest'
+
+import { sqlEditorState } from './sql-editor-state'
+import type { SnippetWithContent } from '@/data/content/sql-folders-query'
+
+function makeSnippet(
+ id: string,
+ overrides: Omit<Partial<SnippetWithContent>, 'content'> = {}
+): SnippetWithContent {
+ return {
+ id,
+ type: 'sql',
+ name: 'My Query',
+ description: 'A description',
+ visibility: 'user',
+ project_id: 42,
+ owner_id: 7,
+ folder_id: null,
+ favorite: false,
+ status: 'saved',
+ inserted_at: '2024-01-01T00:00:00.000Z',
+ updated_at: '2024-01-01T00:00:00.000Z',
+ ...overrides,
+ content: {
+ content_id: id,
+ schema_version: '1',
+ unchecked_sql: untrustedSql('SELECT * FROM users;'),
+ },
+ }
+}
+
+describe('addFavorite / removeFavorite', () => {
+ beforeEach(() => {
+ // sqlEditorState is a module-level singleton, so reset the state these tests touch
+ for (const id of Object.keys(sqlEditorState.snippets)) {
+ delete sqlEditorState.snippets[id]
+ }
+ sqlEditorState.needsSaving.clear()
+ })
+
+ it('marks a loaded snippet as favorite and queues it for saving', () => {
+ sqlEditorState.addSnippet({ projectRef: 'ref', snippet: makeSnippet('snippet-1') })
+
+ sqlEditorState.addFavorite('snippet-1')
+
+ expect(sqlEditorState.snippets['snippet-1'].snippet.favorite).toBe(true)
+ expect(sqlEditorState.needsSaving.get('snippet-1')).toBe(true)
+ })
+
+ it('unmarks a favorited snippet and queues it for saving', () => {
+ sqlEditorState.addSnippet({
+ projectRef: 'ref',
+ snippet: makeSnippet('snippet-1', { favorite: true }),
+ })
+
+ sqlEditorState.removeFavorite('snippet-1')
+
+ expect(sqlEditorState.snippets['snippet-1'].snippet.favorite).toBe(false)
+ expect(sqlEditorState.needsSaving.get('snippet-1')).toBe(true)
+ })
+
+ it('ignores addFavorite for a snippet that is not in the store', () => {
+ expect(() => sqlEditorState.addFavorite('missing')).not.toThrow()
+ expect(sqlEditorState.needsSaving.has('missing')).toBe(false)
+ })
+
+ it('ignores removeFavorite for a snippet that is not in the store', () => {
+ expect(() => sqlEditorState.removeFavorite('missing')).not.toThrow()
+ expect(sqlEditorState.needsSaving.has('missing')).toBe(false)
+ })
+})
diff --git a/apps/studio/state/sql-editor/sql-editor-state.ts b/apps/studio/state/sql-editor/sql-editor-state.ts
index 6e1c6cdc545..c0073c6df9f 100644
--- a/apps/studio/state/sql-editor/sql-editor-state.ts
+++ b/apps/studio/state/sql-editor/sql-editor-state.ts
@@ -257,7 +257,7 @@ export const sqlEditorState = proxy({
removeFavorite: (id: string) => {
const storeSnippet = sqlEditorState.snippets[id]
- if (storeSnippet.snippet) {
+ if (storeSnippet) {
storeSnippet.snippet.favorite = false
sqlEditorState.needsSaving.set(id, true)
}