mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 07:59:03 +00:00
## Context The Table Editor doesn't really handle partitioned tables well given the following issues: - Partitioned tables are rendered as a flat list in the Table Editor side menu, so the menu could get very long and noisy - this was the primary motivation for this PR - Child partitioned tables are not indicated with a proper icon, they're treated as regular tables in the Table Editor which is incorrect ## Changes involved - Table Editor side menu now only lists parent partitioned tables, omits all child partitioned tables - Child partitioned tables can be viewed by clicking on the button tooltip here or drilling down via "View partitions" <img width="300" alt="image" src="https://github.com/user-attachments/assets/e3cfbe4b-21ff-47df-8fcd-81d0c9f68d94" /><br /> <img width="400" alt="image" src="https://github.com/user-attachments/assets/5d0747cb-fb52-4301-ad9e-c7be504f43c9" /> - Selecting that will then render a nested nav as such <img width="291" height="323" alt="image" src="https://github.com/user-attachments/assets/bff0a42b-4265-4317-8299-537d82544388" /> - Sub partitioned tables can be drilled into the same way, and the back CTA steps up one level <img width="485" height="306" alt="image" src="https://github.com/user-attachments/assets/66f6f6cb-8b7d-46c5-8ea3-4ee4fd57aa84" /> - Parent partition tables have the following actions (Previously was only copy name and schema) <img width="465" height="257" alt="image" src="https://github.com/user-attachments/assets/2aa90995-7c30-4bcf-a446-d01ab52c1ede" /> - Deleting a parent partition table will also delete the child partition table, and that's highlighted in the confirmation modal <img width="418" height="316" alt="image" src="https://github.com/user-attachments/assets/0d61984b-f85e-4c93-84d0-549d300838c9" /> - Child partition tables have the following actions (Edit + duplicate removed since they aren't supported via the UI) <img width="482" height="229" alt="image" src="https://github.com/user-attachments/assets/e028b13d-2e92-4491-b05c-7a6cb0a129d6" /> ## To test In general, just verify the viewing UX of partitioned tables to see if it all feels alright, can use the following SQL to quickly set up partitioned tables ```sql create schema if not exists tint; create table public.nest_demo (id int, hue int not null, primary key (id, hue)) partition by range (hue); create table public.nest_demo_blue partition of public.nest_demo for values from (200) to (300) partition by range (hue); create table public.nest_demo_blue_light partition of public.nest_demo_blue for values from (200) to (250); create table tint.nest_demo_blue_deep partition of public.nest_demo_blue for values from (250) to (300); create table public.nest_demo_other partition of public.nest_demo for values from (0) to (200); ```
216 lines
7.4 KiB
TypeScript
216 lines
7.4 KiB
TypeScript
import { act, renderHook } from '@testing-library/react'
|
|
import type { ReactNode } from 'react'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
import { useSqlEditorTabsCleanup, useTableEditorTabsCleanUp } from './Tabs.utils'
|
|
import { ENTITY_TYPE } from '@/data/entity-types/entity-type-constants'
|
|
import type { Entity } from '@/data/entity-types/entity-types-infinite-query'
|
|
import { sqlEditorState } from '@/state/sql-editor/sql-editor-state'
|
|
import { createTabsState, TabsStateContext, type Tab } from '@/state/tabs'
|
|
import { seedSnippet } from '@/tests/lib/sql-editor-test-utils'
|
|
|
|
const dbTab = (id: string): Tab => ({
|
|
id: `sql-${id}`,
|
|
type: 'sql',
|
|
label: id,
|
|
isPreview: false,
|
|
metadata: { sqlId: id, sqlSource: 'database' },
|
|
})
|
|
|
|
const logsTab = (id: string): Tab => ({
|
|
id: `sql-${id}`,
|
|
type: 'sql',
|
|
label: id,
|
|
isPreview: false,
|
|
metadata: { sqlId: id, sqlSource: 'logs' },
|
|
})
|
|
|
|
function renderCleanup(store: ReturnType<typeof createTabsState>) {
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<TabsStateContext.Provider value={store}>{children}</TabsStateContext.Provider>
|
|
)
|
|
return renderHook(() => useSqlEditorTabsCleanup(), { wrapper }).result.current
|
|
}
|
|
|
|
describe('useSqlEditorTabsCleanup', () => {
|
|
afterEach(() => {
|
|
for (const key of Object.keys(sqlEditorState.snippets)) delete sqlEditorState.snippets[key]
|
|
})
|
|
|
|
it('keeps the tab of a locally-created snippet that has not been persisted yet', () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(dbTab('existing'))
|
|
store.addTab(dbTab('brand-new'))
|
|
|
|
// A snippet created by typing in a new tab: it lives in the local store with
|
|
// a never-persisted status, but the server-fetched snippet list can't know
|
|
// about it yet.
|
|
seedSnippet({ id: 'brand-new', name: 'Untitled query', sql: 'select 1;' })
|
|
|
|
const cleanup = renderCleanup(store)
|
|
act(() => cleanup({ snippets: [{ id: 'existing', type: 'sql', name: 'existing' }] }))
|
|
|
|
expect(store.tabsMap['sql-brand-new']).toBeDefined()
|
|
expect(store.openTabs).toContain('sql-brand-new')
|
|
expect(store.activeTab).toEqual('sql-brand-new')
|
|
})
|
|
|
|
it('prunes tabs for deleted snippets (database and logs) while keeping live ones', () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(dbTab('db-stale'))
|
|
store.addTab(logsTab('logs-stale'))
|
|
store.addTab(logsTab('logs-live'))
|
|
|
|
const cleanup = renderCleanup(store)
|
|
|
|
// The caller passes both `sql` and `log_sql` snippets it knows to be live; only
|
|
// `logs-live` is present, so the two stale tabs are pruned and it is kept.
|
|
act(() => cleanup({ snippets: [{ id: 'logs-live', type: 'log_sql', name: 'logs-live' }] }))
|
|
|
|
expect(store.openTabs).toEqual(['sql-logs-live'])
|
|
expect(store.tabsMap['sql-db-stale']).toBeUndefined()
|
|
expect(store.tabsMap['sql-logs-stale']).toBeUndefined()
|
|
expect(store.tabsMap['sql-logs-live']).toBeDefined()
|
|
})
|
|
|
|
it('preserves logs tabs when logs snippets are not authoritative (canPruneLogsTabs false)', () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(dbTab('db-stale'))
|
|
store.addTab(logsTab('logs'))
|
|
|
|
const cleanup = renderCleanup(store)
|
|
// Logs section disabled / query errored: no logs snippets passed, and logs tabs
|
|
// must not be pruned — but stale database tabs still are.
|
|
act(() => cleanup({ snippets: [], canPruneLogsTabs: false }))
|
|
|
|
expect(store.openTabs).toEqual(['sql-logs'])
|
|
expect(store.tabsMap['sql-db-stale']).toBeUndefined()
|
|
|
|
// Recent items carry their own logs-source condition, so assert them separately.
|
|
const recentIds = store.recentItems.map((item) => item.id)
|
|
expect(recentIds).toContain('sql-logs')
|
|
expect(recentIds).not.toContain('sql-db-stale')
|
|
})
|
|
|
|
it('prunes recent items for deleted logs snippets while keeping live ones', () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(logsTab('logs-stale'))
|
|
store.addTab(logsTab('logs-live'))
|
|
|
|
const cleanup = renderCleanup(store)
|
|
act(() => cleanup({ snippets: [{ id: 'logs-live', type: 'log_sql', name: 'logs-live' }] }))
|
|
|
|
const recentIds = store.recentItems.map((item) => item.id)
|
|
expect(recentIds).toContain('sql-logs-live')
|
|
expect(recentIds).not.toContain('sql-logs-stale')
|
|
})
|
|
})
|
|
|
|
const tableTab = (id: number, name: string): Tab => ({
|
|
id: `r-${id}`,
|
|
type: ENTITY_TYPE.TABLE,
|
|
label: name,
|
|
isPreview: false,
|
|
metadata: { schema: 'public', name, tableId: id },
|
|
})
|
|
|
|
const tableEntity = (id: number, name: string): Entity => ({
|
|
id,
|
|
schema: 'public',
|
|
name,
|
|
type: ENTITY_TYPE.TABLE,
|
|
comment: null,
|
|
rls_enabled: false,
|
|
is_partition: false,
|
|
})
|
|
|
|
describe('useTableEditorTabsCleanUp', () => {
|
|
const renderTableCleanup = (store: ReturnType<typeof createTabsState>) => {
|
|
const wrapper = ({ children }: { children: ReactNode }) => (
|
|
<TabsStateContext.Provider value={store}>{children}</TabsStateContext.Provider>
|
|
)
|
|
return renderHook(() => useTableEditorTabsCleanUp(), { wrapper }).result.current
|
|
}
|
|
|
|
it('keeps tabs missing from the list when the entity still exists', async () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(tableTab(1, 'plain'))
|
|
store.addTab(tableTab(2, 'child_partition'))
|
|
store.addTab(dbTab('snippet'))
|
|
const resolveEntities = vi.fn(async () => [tableEntity(2, 'child_partition')])
|
|
|
|
const cleanup = renderTableCleanup(store)
|
|
await act(() =>
|
|
cleanup({ schemas: ['public'], entities: [tableEntity(1, 'plain')], resolveEntities })
|
|
)
|
|
|
|
expect(resolveEntities).toHaveBeenCalledWith([2])
|
|
expect(store.openTabs).toEqual(expect.arrayContaining(['r-1', 'r-2']))
|
|
})
|
|
|
|
it('removes tabs of entities that no longer exist', async () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(tableTab(1, 'plain'))
|
|
store.addTab(tableTab(2, 'dropped'))
|
|
|
|
const cleanup = renderTableCleanup(store)
|
|
await act(() =>
|
|
cleanup({
|
|
schemas: ['public'],
|
|
entities: [tableEntity(1, 'plain')],
|
|
resolveEntities: async () => [],
|
|
})
|
|
)
|
|
|
|
expect(store.openTabs).toContain('r-1')
|
|
expect(store.openTabs).not.toContain('r-2')
|
|
})
|
|
|
|
it('keeps tabs opened while the lookup is pending', async () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(tableTab(1, 'plain'))
|
|
store.addTab(tableTab(2, 'child_partition'))
|
|
|
|
let finishLookup: () => void = () => {}
|
|
const lookup = new Promise<void>((resolve) => (finishLookup = resolve))
|
|
|
|
const cleanup = renderTableCleanup(store)
|
|
const cleanupPromise = cleanup({
|
|
schemas: ['public'],
|
|
entities: [tableEntity(1, 'plain')],
|
|
resolveEntities: async () => {
|
|
await lookup
|
|
return [tableEntity(2, 'child_partition')]
|
|
},
|
|
})
|
|
|
|
await act(async () => {
|
|
store.addTab(tableTab(3, 'opened_meanwhile'))
|
|
})
|
|
finishLookup()
|
|
await act(() => cleanupPromise)
|
|
|
|
expect(store.openTabs).toEqual(expect.arrayContaining(['r-1', 'r-2', 'r-3']))
|
|
})
|
|
|
|
it('removes nothing when the lookup fails', async () => {
|
|
const store = createTabsState('default')
|
|
store.addTab(tableTab(2, 'child_partition'))
|
|
|
|
const cleanup = renderTableCleanup(store)
|
|
await act(async () => {
|
|
await expect(
|
|
cleanup({
|
|
schemas: ['public'],
|
|
entities: [],
|
|
resolveEntities: async () => {
|
|
throw new Error('network')
|
|
},
|
|
})
|
|
).rejects.toThrow('network')
|
|
})
|
|
|
|
expect(store.openTabs).toContain('r-2')
|
|
})
|
|
})
|