Files
windmill/frontend/src/lib/components/DBTableEditor.svelte
T
e6f1211d31 feat: Ducklake native support (#6268)
* upgrade duckdb

* basic ducklake works

* ducklake works with custom db catalogs

* fix: pwsh skip already installed modules outside of cache (#6037)

* improve query performance of user stats

* separate ducklake_catalog db

* ducklake settings

* DucklakeSettings frontend

* Ducklake ws settings saved in backend

* fetch ducklake catalog resource

* Ducklake works with configured s3 storage

* Ducklake as asset

* ducklake asset icon

* Fix duckdb array and object args not working properly (#6254)

* Fix bug with comments in duckdb

* Avoid multiple queries when doing ATTACH ducklake

* trunc sig no longer needed now that comments are trimmed

* cache DuckdbConnectionSettingsResponse

* duplicated code

* transform_attach_ducklake contributes to duckdb_connection_settings_cache

* eliminate the need for used_storages

* nit

* cleaner management of the bigquery credentials file

* DBManagerDrawer refactor to prepare for Ducklake

* get ducklake schema

* implement delete for ducklake

* load column metadata for ducklake

* Select query works for ducklake, basic db explorer works !

* duckdb count query

* Support all db ops for ducklake

* clean migrations

* SQL repl for Ducklake

* fix broken database studio

* nit

* assert function

* Ducklake in Editor Bar

* default ducklake syntax + allow extra args

* DucklakeCatalogWizard UI

* nit + remove extra $

* modal when databases do not exist

* cannot be windmill

* Ducklake works safely with instance database

* Avoid sending instance db credentials on network

* resource leak security

* remove fetch_attach_db_conn_str

* prevent instance pg password leak

* hide asset usage count when not available

* case unsensitivity duckdb

* warnings

* disable instance catalog

* use shorthand syntax when inserting with EditorBar

* Instance ducklake catalog is now safe to use

* use safer argon2 pwd

* update package json parsers

* update package json

* better msgs

* tooltips

* disable explore button until saved

* nit

* fix warnings

* better ducklake_user password management

* nit

* Sanitize passwords from errors in ducklake

* DisplayResult broken in job result

* remove superadmin requirement to check databases_exist

* duckdb_connection_settings_v2_inner

* Ducklake works on agent worker (finally)

* ci

* #[allow(dead_code)]

* fix openapi missing response

* Separate +Database button for DuckDB in EditorBar

* Fix dropdown in ducklake settings

* Attempt to fix migration race condition in CI

* update sqlx failing for some offline queries

* avoid temp password for ducklake_user

* nits

* ducklake settings nits

* update duckdb default script

* fix sql repl resetting text on refresh

* avoid pgcrypto extension

---------

Co-authored-by: HugoCasa <hugo@casademont.ch>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
2025-08-06 13:55:36 +00:00

421 lines
13 KiB
Svelte

<script lang="ts" module>
function validate(values: CreateTableValues, dbSchema?: DBSchema) {
const columnNamesErrs = values.columns.flatMap((column) => {
const isUnique = values.columns.filter((c) => c.name === column.name).length === 1
return !column.name.length || !isUnique ? [column.name] : []
})
const fkErrs = values.foreignKeys.map((fk) => ({
emptyTarget: !fk.targetTable,
nonExistingSourceColumns: fk.columns
.map((c) => c.sourceColumn)
.filter((sc) => values.columns.every((c) => c.name !== sc)),
nonExistingTargetColumns: fk.columns
.map((c) => c.targetColumn)
.filter(
(tc) =>
!tc ||
!Object.keys(
dbSchema?.schema?.[fk.targetTable?.split('.')?.[0] ?? '']?.[
fk.targetTable?.split('.')[1]
] ?? {}
).includes(tc)
)
}))
const someFkErr = fkErrs.some(
(fkErr) =>
fkErr.emptyTarget ||
fkErr.nonExistingSourceColumns.length ||
fkErr.nonExistingTargetColumns.length
)
const errs = {
name: !values.name.length,
columns: columnNamesErrs.length ? columnNamesErrs : undefined,
foreignKeys: someFkErr ? fkErrs : undefined
}
if (Object.values(errs).every((v) => !v)) return undefined
return errs
}
export type DBTableEditorProps = {
onConfirm: (values: CreateTableValues) => void | Promise<void>
previewSql?: (values: CreateTableValues) => string
dbType: DbType
dbSchema?: DBSchema
currentSchema?: string
}
</script>
<script lang="ts">
import { ArrowRight, ClipboardCopy, Info, Plus, Settings, X } from 'lucide-svelte'
import { Button } from './common'
import { Cell } from './table'
import DataTable from './table/DataTable.svelte'
import Head from './table/Head.svelte'
import {
datatypeHasLength,
dbSupportsSchemas,
type DbType
} from './apps/components/display/dbtable/utils'
import { DB_TYPES } from '$lib/consts'
import Popover from './meltComponents/Popover.svelte'
import Tooltip from './meltComponents/Tooltip.svelte'
import {
datatypeDefaultLength,
type CreateTableValues
} from './apps/components/display/dbtable/queries/createTable'
import ConfirmationModal from './common/confirmationModal/ConfirmationModal.svelte'
import { sendUserToast } from '$lib/toast'
import { copyToClipboard } from '$lib/utils'
import { getFlatTableNamesFromSchema, type DBSchema } from '$lib/stores'
import { twMerge } from 'tailwind-merge'
import DarkModeObserver from './DarkModeObserver.svelte'
import Select from './select/Select.svelte'
import { safeSelectItems } from './select/utils.svelte'
const { onConfirm, dbType, previewSql, dbSchema, currentSchema }: DBTableEditorProps = $props()
const columnTypes = DB_TYPES[dbType]
const defaultColumnType = (
{
postgresql: 'VARCHAR',
snowflake: 'varchar',
ms_sql_server: 'varchar',
bigquery: 'string',
mysql: 'varchar',
duckdb: 'string'
} satisfies Record<DbType, string>
)[dbType]
const values: CreateTableValues = $state({
name: '',
columns: [],
foreignKeys: []
})
function addColumn({ name, primaryKey }: { name: string; primaryKey?: boolean }) {
values.columns.push({
name,
datatype: defaultColumnType,
...(datatypeHasLength(defaultColumnType) && {
datatype_length: datatypeDefaultLength(defaultColumnType)
}),
...(primaryKey && { primaryKey })
})
}
addColumn({ name: 'id', primaryKey: dbType !== 'duckdb' })
const errors: ReturnType<typeof validate> = $derived(validate(values, dbSchema))
let askingForConfirmation:
| (ConfirmationModal['$$prop_def'] & { onConfirm: () => void; codeContent?: string })
| undefined = $state()
let darkMode = $state(false)
</script>
<DarkModeObserver bind:darkMode />
<div class="flex flex-col h-full">
<div class="flex-1 overflow-y-auto flex flex-col gap-6">
<label>
Name
<input
type="text"
placeholder="my_table"
class={errors?.name ? 'border !border-red-600/60' : ''}
bind:value={values.name}
/>
</label>
<div class="flex flex-col">
<!-- svelte-ignore a11y_label_has_associated_control -->
<label>Columns</label>
<DataTable>
<Head>
<tr>
<Cell head first>Name</Cell>
<Cell head>Type</Cell>
<Cell head last>Primary</Cell>
</tr>
</Head>
<tbody class="divide-y bg-surface">
{#each values.columns as column, i}
<tr>
<Cell first>
<input
type="text"
class={'h-10 ' +
(errors?.columns?.includes(column.name) ? 'border !border-red-600/60' : '')}
style="height: 2rem;"
placeholder="column_name"
bind:value={column.name}
/>
</Cell>
<Cell>
<Select
bind:value={
() => column.datatype,
(v) => {
column.datatype = v
if (datatypeHasLength(column.datatype)) {
column.datatype_length = datatypeDefaultLength(column.datatype)
} else {
column.datatype_length = undefined
}
}
}
items={safeSelectItems(columnTypes)}
class="w-48"
/>
</Cell>
<Cell last class="flex items-center mt-1.5">
<input type="checkbox" class="!w-4 !h-4" bind:checked={column.primaryKey} />
<Popover class="ml-8" contentClasses="py-3 px-5 flex flex-col gap-6">
{#snippet trigger()}
<Settings size={18} />
{/snippet}
{#snippet content()}
{#if datatypeHasLength(column.datatype)}
<label class="text-xs">
Length
<input type="number" placeholder="0" bind:value={column.datatype_length} />
</label>
{/if}
<label class="text-xs">
<span class="flex gap-1 mb-1">
Default value
<Tooltip>
<Info size={14} />
{#snippet text()}
Surround your expressions with curly brackets:
<code>
{'{NOW()}'}
</code>.
<br />
By default, it will be parsed as a literal
{/snippet}
</Tooltip>
</span>
<input type="text" placeholder="NULL" bind:value={column.defaultValue} />
</label>
{#if !column.primaryKey}
<label class="flex gap-2 items-center text-xs">
<input type="checkbox" class="!w-4 !h-4" bind:checked={column.not_null} />
Not nullable
</label>
{/if}
{/snippet}
</Popover>
<Button
color="light"
startIcon={{ icon: X }}
wrapperClasses="w-fit ml-2"
btnClasses="p-0"
on:click={() => values.columns.splice(i, 1)}
/>
</Cell>
</tr>
{/each}
<tr class="w-full">
<td colspan={99} class="p-1">
<Button
wrapperClasses="mx-auto"
startIcon={{ icon: Plus }}
color="light"
on:click={() => addColumn({ name: '' })}
>
Add
</Button>
</td>
</tr>
</tbody>
</DataTable>
</div>
<div class="flex flex-col">
<!-- svelte-ignore a11y_label_has_associated_control -->
<label>Foreign Keys</label>
<DataTable>
<Head>
<tr>
<Cell head first>Table</Cell>
<Cell head last>Columns</Cell>
</tr>
</Head>
<tbody class="divide-y bg-surface">
{#each values.foreignKeys as foreignKey, foreignKeyIndex}
{@const fkErrors = errors?.foreignKeys?.[foreignKeyIndex]}
<tr>
<Cell first class="flex">
<Select
inputClass={twMerge(
'!w-48',
fkErrors?.emptyTarget ? 'border !border-red-600/60' : ''
)}
placeholder=""
bind:value={foreignKey.targetTable}
items={getFlatTableNamesFromSchema(dbSchema).map((o) => ({
value: o,
label:
(currentSchema && o.startsWith(currentSchema)) || !dbSupportsSchemas(dbType)
? o.split('.')[1]
: o
}))}
/>
</Cell>
<Cell>
<div class="flex flex-col gap-2">
{#each foreignKey.columns as column, columnIndex}
<div class="flex">
<div class="flex items-center gap-1 w-60">
<!-- Div wrappers with absolute select are to prevent the Select content
from x-overflowing -->
<div class="grow h-[2rem] relative">
<Select
class="!absolute inset-0"
inputClass={twMerge(
fkErrors?.nonExistingSourceColumns.includes(column.sourceColumn)
? 'border !border-red-600/60'
: ''
)}
placeholder=""
bind:value={column.sourceColumn}
items={values.columns
.filter((c) => c.name.length)
.map((c) => ({ value: c.name }))}
clearable={false}
/>
</div>
<ArrowRight size={16} class="h-fit shrink-0" />
<div class="grow h-[2rem] relative">
<Select
class="!absolute inset-0"
inputClass={twMerge(
fkErrors?.nonExistingTargetColumns.includes(column.targetColumn)
? 'border !border-red-600/60'
: ''
)}
placeholder=""
bind:value={column.targetColumn}
items={Object.keys(
dbSchema?.schema?.[foreignKey.targetTable?.split('.')?.[0] ?? '']?.[
foreignKey.targetTable?.split('.')[1]
] ?? {}
).map((value) => ({ value }))}
clearable={false}
/>
</div>
</div>
<div class="ml-auto flex">
{#if columnIndex === 0}
<Popover contentClasses="py-3 px-5 w-52 flex flex-col gap-6">
{#snippet trigger()}
<Settings size={18} />
{/snippet}
{#snippet content()}
<span>
ON DELETE <select bind:value={foreignKey.onDelete}>
<option value="NO ACTION" selected>NO ACTION</option>
<option value="CASCADE" selected>CASCADE</option>
<option value="SET NULL" selected>SET NULL</option>
</select>
</span>
<span>
ON UPDATE <select bind:value={foreignKey.onUpdate}>
<option value="NO ACTION" selected>NO ACTION</option>
<option value="CASCADE" selected>CASCADE</option>
<option value="SET NULL" selected>SET NULL</option>
</select>
</span>
{/snippet}
</Popover>
{/if}
<Button
color="light"
startIcon={{ icon: X }}
wrapperClasses="w-fit ml-2"
btnClasses="p-0"
on:click={foreignKey.columns.length > 1
? () => foreignKey.columns.splice(columnIndex, 1)
: () => values.foreignKeys.splice(foreignKeyIndex, 1)}
/>
</div>
</div>
{/each}
<button
class="w-60 border-dashed dark:border-gray-600 border-2 rounded-md flex justify-center items-center py-1 gap-2 text-primary-500 font-normal"
onclick={() => foreignKey.columns.push({})}
>
<Plus class="h-fit" size={12} /> Add
</button>
</div></Cell
>
</tr>
{/each}
<tr class="w-full">
<td colspan={99} class="p-1">
<Button
wrapperClasses="mx-auto"
startIcon={{ icon: Plus }}
color="light"
on:click={() =>
values.foreignKeys.push({
columns: [{}],
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION'
})}
>
Add
</Button>
</td>
</tr>
</tbody>
</DataTable>
</div>
</div>
<Button
disabled={!!errors}
on:click={() =>
(askingForConfirmation = {
onConfirm: async () => {
try {
askingForConfirmation && (askingForConfirmation.loading = true)
await onConfirm(values)
sendUserToast(values.name + ' created!')
} catch (e) {
let msg: string | undefined = (e as Error)?.message
if (typeof msg !== 'string') msg = e ? JSON.stringify(e) : 'An error occurred'
sendUserToast(msg, true)
}
askingForConfirmation = undefined
},
title: 'Confirm running the following:',
confirmationText: 'Create ' + values.name,
open: true,
...(previewSql && { codeContent: previewSql(values) })
})}>Create table</Button
>
</div>
<ConfirmationModal
{...askingForConfirmation ?? { confirmationText: '', title: '' }}
on:canceled={() => (askingForConfirmation = undefined)}
on:confirmed={askingForConfirmation?.onConfirm ?? (() => {})}
>
{#if askingForConfirmation?.codeContent}
<div class="bg-surface-secondary border border-surface-selected rounded-md p-2 relative">
<code class="whitespace-pre-wrap">
{askingForConfirmation.codeContent}
</code>
<Button
on:click={() => copyToClipboard(askingForConfirmation?.codeContent)}
size="xs"
startIcon={{ icon: ClipboardCopy }}
color="none"
wrapperClasses="absolute z-10 top-0 right-0"
></Button>
</div>
{/if}
</ConfirmationModal>