diff --git a/frontend/e2e/DbManagerPage.ts b/frontend/e2e/DbManagerPage.ts index 19dbfbf95e..9c1a5dcb37 100644 --- a/frontend/e2e/DbManagerPage.ts +++ b/frontend/e2e/DbManagerPage.ts @@ -132,7 +132,6 @@ export async function runDbManagerAlterTableTest(page: Page, dbType: _DbType) { // Bigquery cannot rename a table with primary keys await friendCol.setPrimaryKey(true) await createdAtCol.setPrimaryKey(true) - await contentColumn.setPrimaryKey(true) } if (dbFeatures.foreignKeys) await tableEditor.deleteForeignKey() await tableEditor.alterTable() @@ -157,7 +156,7 @@ export async function runDbManagerAlterTableTest(page: Page, dbType: _DbType) { if (dbFeatures.primaryKeys && dbType !== 'bigquery') { await friendCol.checkPrimaryKeyIs(true) await createdAtCol.checkPrimaryKeyIs(true) - await contentColumn.checkPrimaryKeyIs(true) + await contentColumn.checkPrimaryKeyIs(false) } } diff --git a/frontend/src/lib/components/DBManager.svelte b/frontend/src/lib/components/DBManager.svelte index 3b0399b9fb..24db41cd85 100644 --- a/frontend/src/lib/components/DBManager.svelte +++ b/frontend/src/lib/components/DBManager.svelte @@ -34,7 +34,7 @@ dbType: DbType dbSchema: DBSchema dbSupportsSchemas: boolean - getColDefs: (tableKey: string) => Promise + colDefs: Record | undefined dbTableOpsFactory: (params: { colDefs: ColumnDef[]; tableKey: string }) => IDbTableOps dbSchemaOps: IDbSchemaOps refresh?: () => void @@ -56,8 +56,8 @@ dbSchema, dbTableOpsFactory, dbSchemaOps, - getColDefs, dbSupportsSchemas, + colDefs, refresh, initialSchemaKey, initialTableKey, @@ -207,10 +207,11 @@ if (!table) return let tableKey2 = dbSupportsSchemas && selected.schemaKey ? `${selected.schemaKey}.${table}` : table + if (!colDefs?.[tableKey2]) return return await dbSchemaOps.onFetchTableEditorDefinition({ table: table, schema: selected.schemaKey, - getColDefs: () => getColDefs(tableKey2) + colDefs: colDefs[tableKey2] }) } ) @@ -506,13 +507,9 @@ {/if} - {#if tableKey} - {#await getColDefs(tableKey) then colDefs} - {#if colDefs && colDefs?.length} - {@const dbTableOps = dbTableOpsFactory({ colDefs, tableKey })} - - {/if} - {/await} + {#if tableKey && colDefs?.[tableKey]?.length} + {@const dbTableOps = dbTableOpsFactory({ colDefs: colDefs[tableKey], tableKey })} + {/if} diff --git a/frontend/src/lib/components/DBManagerContent.svelte b/frontend/src/lib/components/DBManagerContent.svelte index caa56c1b54..a353c23ecb 100644 --- a/frontend/src/lib/components/DBManagerContent.svelte +++ b/frontend/src/lib/components/DBManagerContent.svelte @@ -2,7 +2,7 @@ import { dbSchemas, workspaceStore, type DBSchema } from '$lib/stores' import { sendUserToast, sortArray } from '$lib/utils' import { Loader2 } from 'lucide-svelte' - import { dbSupportsSchemas, type TableMetadata } from './apps/components/display/dbtable/utils' + import { dbSupportsSchemas } from './apps/components/display/dbtable/utils' import DbManager from './DBManager.svelte' import { dbSchemaOpsWithPreviewScripts, @@ -15,14 +15,11 @@ import SimpleAgTable from './SimpleAgTable.svelte' import { untrack, type Snippet } from 'svelte' import type { DbInput } from './dbTypes' - import { - getDbSchemas, - loadAllTablesMetaData, - loadTableMetaData - } from './apps/components/display/dbtable/metadata' + import { getDbSchemas, loadAllTablesMetaData } from './apps/components/display/dbtable/metadata' import type { SelectedTable } from './DBManager.svelte' import { getDbFeatures } from './apps/components/display/dbtable/dbFeatures' + import { resource } from 'runed' interface Props { input?: DbInput @@ -90,29 +87,23 @@ async function getSchema() { if (!input) return const dbSchemasPath = getDbSchemasPath(input) - await Promise.all([ - (async () => { - if ($dbSchemas[dbSchemasPath] && !refreshing) return + if ($dbSchemas[dbSchemasPath] && !refreshing) return + + $dbSchemas[dbSchemasPath] + if (input.type == 'database') { + $dbSchemas[dbSchemasPath] = await getDbSchemas( + input.resourceType, + input.resourcePath, + $workspaceStore, + (message: string) => sendUserToast(message, true) + ) + } else if (input.type == 'ducklake') { + $dbSchemas[dbSchemasPath] = await getDucklakeSchema({ + workspace: $workspaceStore!, + ducklake: input.ducklake + }) + } - $dbSchemas[dbSchemasPath] - if (input.type == 'database') { - $dbSchemas[dbSchemasPath] = await getDbSchemas( - input.resourceType, - input.resourcePath, - $workspaceStore, - (message: string) => sendUserToast(message, true) - ) - } else if (input.type == 'ducklake') { - $dbSchemas[dbSchemasPath] = await getDucklakeSchema({ - workspace: $workspaceStore!, - ducklake: input.ducklake - }) - } - })(), - (async () => { - cachedColDefs = (await loadAllTablesMetaData($workspaceStore, input)) ?? {} - })() - ]) refreshing = false } @@ -126,23 +117,13 @@ hasReplResult = !!replResultData }) - let cachedColDefs: Record = {} - let cachedLastRefreshCount = 0 - - async function getColDefs(tableKey: string): Promise { - if (cachedLastRefreshCount !== refreshCount) cachedColDefs = {} - cachedLastRefreshCount = refreshCount - - if (cachedColDefs[tableKey]) return cachedColDefs[tableKey] - if (!input) return [] - - if (input?.type == 'ducklake') throw 'Impossible that loadAllTablesMetaData fails for Ducklake' - // Query is not implemented for all dbs, need a fallback - const result = await loadTableMetaData(input, $workspaceStore, tableKey) - - if (result) cachedColDefs[tableKey] = result - return result ?? [] - } + let colDefs = resource( + () => [input, refreshCount], + async () => { + if (!input) return + return await loadAllTablesMetaData($workspaceStore, input) + } + ) // Export for parent components export function clearReplResult() { @@ -188,7 +169,7 @@ dbTableOpsWithPreviewScripts({ colDefs, diff --git a/frontend/src/lib/components/apps/components/display/dbtable/queries/relationalKeys.ts b/frontend/src/lib/components/apps/components/display/dbtable/queries/relationalKeys.ts index 5dab0e94da..174a2b5432 100644 --- a/frontend/src/lib/components/apps/components/display/dbtable/queries/relationalKeys.ts +++ b/frontend/src/lib/components/apps/components/display/dbtable/queries/relationalKeys.ts @@ -3,7 +3,6 @@ import { wrapDucklakeQuery } from '$lib/components/ducklake' import { runScriptAndPollResult } from '$lib/components/jobs/utils' import type { ScriptLang } from '$lib/gen' import type { TableEditorForeignKey } from '../tableEditor' -import type { TableMetadata } from '../utils' /** * Raw foreign key result from database queries @@ -403,12 +402,10 @@ export async function fetchTableRelationalKeys( schema: string | undefined, workspace: string, dbArg: Record, - language: ScriptLang, - getColDefs: () => Promise + language: ScriptLang ): Promise<{ foreignKeys: TableEditorForeignKey[] pk_constraint_name?: string - colDefs: TableMetadata }> { let fkPromise = async () => { try { @@ -466,11 +463,7 @@ export async function fetchTableRelationalKeys( } } - const [foreignKeys, pk_constraint_name, colDefs] = await Promise.all([ - fkPromise(), - pkPromise(), - getColDefs() - ]) + const [foreignKeys, pk_constraint_name] = await Promise.all([fkPromise(), pkPromise()]) - return { foreignKeys, pk_constraint_name, colDefs } + return { foreignKeys, pk_constraint_name } } diff --git a/frontend/src/lib/components/dbOps.ts b/frontend/src/lib/components/dbOps.ts index dac75e2a5a..2f38b78a71 100644 --- a/frontend/src/lib/components/dbOps.ts +++ b/frontend/src/lib/components/dbOps.ts @@ -133,7 +133,7 @@ export type IDbSchemaOps = { onFetchTableEditorDefinition: (params: { table: string schema?: string - getColDefs: () => Promise + colDefs: TableMetadata }) => Promise } @@ -192,16 +192,15 @@ export function dbSchemaOpsWithPreviewScripts({ requestBody: { args: { ...dbArg }, language, content: dropSchemaQuery } }) }, - onFetchTableEditorDefinition: async ({ table, schema, getColDefs }) => { - let { foreignKeys, pk_constraint_name, colDefs } = await fetchTableRelationalKeys( + onFetchTableEditorDefinition: async ({ table, schema, colDefs }) => { + let { foreignKeys, pk_constraint_name } = await fetchTableRelationalKeys( input, dbType, table, schema, workspace, dbArg, - language, - getColDefs + language ) return buildTableEditorValues({