Files
windmill/frontend/src/lib/components/DBManagerDrawerButton.svelte
T
Diego Imbert 41c15fc78a feat: Database Manager (#5586)
* hack fix dnd with tick

* DBExplorer table left table selector

* kinda works

* correct table metadata

* separated columnDefs creation logic

* Removed dependency on AppDbExplorer

* (tweak) loadTableMetaData much faster

* nit for darkmode

* DBExplorerDrawerButton

* footer

* count footer

* reload

* update

* fix height

* db explorer btn in resources table

* delete row

* InsertRowDrawerButton

* insert

* refresh on insert and delete

* moved db logic to ts file

* better update ux

* moved all IO upwards

* fix: Remaining svelte 5 bugs (#5563)

* hack fix dnd with tick

* fix: infinite loading in CodeDisplay after update to svelte 5

* regen package-lock

* fix tutorial (#5562)

* fix tutorial first part

* fix tutorial

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>

* feat: Implement sending diff to ai (#5510)

* allow mentioning specific files in instructions

* remove not working highlight implementation

* make highlighted text work

* fix tooltip position

* clean code

* cleaning

* use lib for tooltip positioning

* fix logic

* draft for db in context

* use tools for db in context

* fixes

* cleaning and bug fixes

* fix

* cleaning

* fix when script is db type

* simplify logic

* put schema in context if already here

* fix imports

* fix tooltip position and make it scrollable

* remove console logs

* check if selected is in available

* fix tooltip list

* add back lost logic

* last fix

* fix type errors

* use loaded schema from dbSchemas

* fix typing, content and lang are always there

* remove from context if not available anymore

* add not loaded yet mention if schema not loaded

* add missing callback logic

* fix prompt

* fix usage of updateselectedContext function

* fix styling for white theme

* handle tab and arrows

* fix schemas not being refreshed on contexts

* also refresh displayMessages when dbschemas change

* fix duplicate available contexts

* fix logic for new scripts

* fix new lines inside text area

* implement sending diff in context

* add button in deploy options to ask ai about diff

* also visualize change when asking for diff

* better prompt

* add limit to diff size

* put diff mode toggle in editor bar

* add button to see history from editor

* adjustements

* put see diff button in dropdown

* fixes

* better styling

* highlight if diff mode

* format files

* change buttons based on diffmode

* remove diff after sending message

* fix type error

* smaller buttons

* draft

* use existing editor in diff editor

* fix number of db resources fetches

* fix apply and add buttons on diff mode

* cleaning

* undo ai gen button show

* better buttons

* styling asjustements + show diff in badge

* styling

* fix deployed code check

* cleaning and styling

* better quick actions

* dont send code when analyzing

* remove apply in chat if only code and no diff

* fix bad code refactor

---------

Co-authored-by: HugoCasa <hugo@casademont.ch>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>

* fix: prevent invalid returned ai completion object errors (#5564)

* fix(frontend): app builder - force json configuration in rich result (#5565)

* feat: make azure a standalone AI provider (#5558)

* feat: make azure a standalone AI provider

* oups

* nit

* fix: openai/azure oauth

* nit

* nits

* feat(frontend) add flow step result viewer (#5398)

* fix(cli): properly handle enabled/disabled updates of schedules

* fix benchmarks

* feat: handle sending selected lines to ai context (#5527)

* allow mentioning specific files in instructions

* remove not working highlight implementation

* make highlighted text work

* fix tooltip position

* clean code

* cleaning

* use lib for tooltip positioning

* fix logic

* draft for db in context

* use tools for db in context

* fixes

* cleaning and bug fixes

* fix

* cleaning

* fix when script is db type

* simplify logic

* put schema in context if already here

* fix imports

* fix tooltip position and make it scrollable

* remove console logs

* check if selected is in available

* fix tooltip list

* add back lost logic

* last fix

* fix type errors

* use loaded schema from dbSchemas

* fix typing, content and lang are always there

* remove from context if not available anymore

* add not loaded yet mention if schema not loaded

* add missing callback logic

* fix prompt

* fix usage of updateselectedContext function

* fix styling for white theme

* handle tab and arrows

* fix schemas not being refreshed on contexts

* also refresh displayMessages when dbschemas change

* fix duplicate available contexts

* fix logic for new scripts

* fix new lines inside text area

* implement sending diff in context

* add button in deploy options to ask ai about diff

* also visualize change when asking for diff

* better prompt

* add limit to diff size

* put diff mode toggle in editor bar

* add button to see history from editor

* adjustements

* put see diff button in dropdown

* fixes

* better styling

* handle adding code piece to context

* add code piece in context

* draft start end markers

* adapt code

* draft

* apply code pieces before sending request

* cleaning

* highlight if diff mode

* format files

* change buttons based on diffmode

* remove diff after sending message

* fix type error

* smaller buttons

* draft

* use existing editor in diff editor

* fix number of db resources fetches

* fix apply and add buttons on diff mode

* cleaning

* undo ai gen button show

* better buttons

* better prompt

* remove console log

* fix merge

* avoid duplicates

* fix merge

* fix

* fix apply logic

* remove useless if

* focus text area + close chat if no selected lines

---------

Co-authored-by: HugoCasa <hugo@casademont.ch>

* fix: flow editor svelte 5 issues (#5567)

* feat: add diff toggle to flow inline scripts (#5550)

* draft flow diff

* add missing import

* cleaning

* code cleaning

* fix for recursive renderings

* fix typo

* cleaning

---------

Co-authored-by: HugoCasa <hugo@casademont.ch>

* fix(frontend): proper each block binding + better app settings reactivity (#5568)

* fix: properly bind to array elements in Svelte each loops

This commit fixes an issue where binding directly to loop variables in Svelte's #each loops doesn't properly update the original array. Instead of binding directly to the loop variable, we now bind to the array elements using index variables.

The pattern used is: - Change: {#each arr as el} -> {#each arr as _, index} - Change: bind:value={el} -> bind:value={arr[index]}

Modified files: - frontend/src/lib/components/ArrayTypeNarrowing.svelte - frontend/src/lib/components/apps/editor/AppInputs.svelte - frontend/src/lib/components/flows/content/FlowModuleWrapper.svelte

* better app settings panel reactivity

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
Co-authored-by: HugoCasa <hugo@casademont.ch>

* fix: app editor svelte 5 fixes (#5570)

* fix: properly bind to array elements in Svelte each loops

This commit fixes an issue where binding directly to loop variables in Svelte's #each loops doesn't properly update the original array. Instead of binding directly to the loop variable, we now bind to the array elements using index variables.

The pattern used is: - Change: {#each arr as el} -> {#each arr as _, index} - Change: bind:value={el} -> bind:value={arr[index]}

Modified files: - frontend/src/lib/components/ArrayTypeNarrowing.svelte - frontend/src/lib/components/apps/editor/AppInputs.svelte - frontend/src/lib/components/flows/content/FlowModuleWrapper.svelte

* better app settings panel reactivity

* fix: app editor table svelte 5 fixes

---------

Co-authored-by: Guilhem <guilhemlemouel@gmail.com>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>

* select border (#5571)

* fix: properly bind to array elements in Svelte each loops

This commit fixes an issue where binding directly to loop variables in Svelte's #each loops doesn't properly update the original array. Instead of binding directly to the loop variable, we now bind to the array elements using index variables.

The pattern used is: - Change: {#each arr as el} -> {#each arr as _, index} - Change: bind:value={el} -> bind:value={arr[index]}

Modified files: - frontend/src/lib/components/ArrayTypeNarrowing.svelte - frontend/src/lib/components/apps/editor/AppInputs.svelte - frontend/src/lib/components/flows/content/FlowModuleWrapper.svelte

* better app settings panel reactivity

* fix: app editor table svelte 5 fixes

* fix: select border

---------

Co-authored-by: Guilhem <guilhemlemouel@gmail.com>
Co-authored-by: Ruben Fiszel <ruben@windmill.dev>

* feat: add windmill context to autocomplete (#5548)

* add windmill context to autocomplete

* fix formatting

* remove console log

* do not mention tool call for autocomplete

* apply logic to php

---------

Co-authored-by: HugoCasa <hugo@casademont.ch>

* fix tabs selected behavior change from svelte 5

* fix: fix list jobs by tag

* fix: tenant id to never be undefined on teams (#5572)

* fix: tenant id to never be undefined

* simplify azure oauth

* simplify azure oauth

* update ee ref

* sqlx prepare

* sqlx prepare

* fix: legacy script gen model selection (#5574)

* feat: add wildcards filter for worker/label/tags

* fix: Dynamic select does not work with tag //native (#5576)

closes #5490

* function takes 13 arguments but 14 arguments were supplied (#5577)

* fix(frontend): prevent deploy popover to show if deploy dropdown is open (#5542)

* prevent deploy popover to show if deploy dropdown is open

* wip

* Revert "wip"

This reverts commit 85434654af.

* Revert "prevent deploy popover to show if deploy dropdown is open"

This reverts commit edd9eda156.

* add prop to hide popup fro dropdown

* feat: button can have tooltip

* improve deploy tooltip and dropdown behavior

* rename tooltip to tooltipPopover

* nit

* add deploy button component

* use svelte 5 runes

* use new deploy button for script builder

* add delay to deploy popover when dropdown is open

* add delay to deploy popover when dropdown is open

# Conflicts:
#	frontend/src/lib/components/DeployButton.svelte
#	frontend/src/lib/components/common/button/Button.svelte

* Update frontend/src/lib/components/common/button/Button.svelte

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

* remove unsused field

* nit

---------

Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>

* chore: update hub sync script (#5579)

* fix monaco suggestion z-index (#5578)

* fix: validate saved module before passing to flow module editor (#5580)

* fix: freeze when clicking script history diff button (#5581)

* fix: validate saved module before passing to flow module editor

* fix: freeze when clicking script history diff button

* add space (#5582)

* fix: binding not working in nested array script arg (#5585)

* fix: mssql ca_cert deserializing (#5587)

* fix: improve app image picker UX (#5589)

* DBTableAction

* delete table

* fix intempestive error toasts

* fullscreen mode

* rename db explorer to db manager

* use drawer open state instead of oo open method

* create table btn

* factor away sucess text

* basic table creation form

* uniq check

* better select

* better add btn

* extra settings

* create table works in pgsql

* MySQL kinda works

* CRUD works in mysql

* fix lowercase

* allow create table with no schema

* handle default value

* sql repl pane

* execute sql queries

* db ops opt in

* SQL Repl v0

* ux

* UX

* better refresh

* better placeholder sql

* sql code clipboard copy

* handle multiple primary keys in table creation

* fix all fields being required on insert

* fix postgres enum not properly converted

* use InsertRowDrawerButton in App db studio

* insert shortcut

* fix mysql datetime parser for non tz dates

* npm run check

* Revert "fix all fields being required on insert"

This reverts commit 6bec952fb7.

* fk ui

* don't commit .env...

* tweak

* fk ui bindings

* fk dropdowns right values

* schema notation fixes

* handle on delete / update cascade etc

* better loading button without flicker

* fix infinite loop caused by getDbSchemas

* foreign key error validation

* type error

* cache col defs

* fix label hover

* fix fk select overflow

* Fix Select styling

* mssql fixes

* fix wrong typecast failing with mssql

* extract makeLoadTableMetaDataQuery

* Fetch all col defs in one go (mysql)

* loadAllTablesMetaData for postgres

* for some reason factoring transformColumnDefs broke ag infinite table

* mssql loads all coldefs at once

* snowflake preload all col defs

* filter out information schema snowflake

* default schema select

* fix original pg col def logic broken

* Fix ugly flickers

* fix updateGrid before grid ready

* better auto sizing

* smoother CRUD UI refreshes

* fix col defs qury for bigquery

* bigquery works

* nits

* do not change queries used in policies !

* fix runPreviewJobAndPollResult on WINDMILL_TOO_BIG

* select padding

* ellipsis typos

* fix mysterious ugly red flash when mounting ag grid in dark mode only

* Load on click

* Remove schema explorer mode

* repl min size

* fix flash on dark mode

* dirty fix ag grid not refreshing sometimes

* sql repl history

* fix ag theme

* close sql repl result viewer by clicking outside

* Select styling fix in dark mode

* better default query sqlrepl

* Buttons less aggressive

---------

Co-authored-by: Ruben Fiszel <ruben@windmill.dev>
Co-authored-by: centdix <40307056+centdix@users.noreply.github.com>
Co-authored-by: HugoCasa <hugo@casademont.ch>
Co-authored-by: Alexander Petric <alpetric@users.noreply.github.com>
Co-authored-by: Guilhem <guilhemlemouel@gmail.com>
Co-authored-by: Piyush मिश्र <piyushxcoder@gmail.com>
Co-authored-by: Piyush मिश्र <piyush.raj.kit@gmail.com>
Co-authored-by: ellipsis-dev[bot] <65095814+ellipsis-dev[bot]@users.noreply.github.com>
2025-05-01 10:33:42 +02:00

273 lines
7.4 KiB
Svelte

<script lang="ts">
import { dbSchemas, workspaceStore, type DBSchema } from '$lib/stores'
import Button from './common/button/Button.svelte'
import Drawer from './common/drawer/Drawer.svelte'
import DrawerContent from './common/drawer/DrawerContent.svelte'
import { sendUserToast, sortArray } from '$lib/utils'
import { ArrowLeft, Database, Expand, Loader2, Minimize, RefreshCcw } from 'lucide-svelte'
import {
dbSupportsSchemas,
getDbSchemas,
getLanguageByResourceType,
loadAllTablesMetaData,
loadTableMetaData,
type DbType,
type TableMetadata
} from './apps/components/display/dbtable/utils'
import DbManager from './DBManager.svelte'
import { Alert } from './common'
import { dbDeleteTableActionWithPreviewScript, dbTableOpsWithPreviewScripts } from './dbOps'
import { makeCreateTableQuery } from './apps/components/display/dbtable/queries/createTable'
import { runPreviewJobAndPollResult } from './jobs/utils'
import { Pane, Splitpanes } from 'svelte-splitpanes'
import SqlRepl from './SqlRepl.svelte'
import SimpleAgTable from './SimpleAgTable.svelte'
import { untrack } from 'svelte'
type Props = {
resourceType: DbType
resourcePath: string
class?: string
}
let { resourceType, resourcePath }: Props = $props()
let dbSchema: DBSchema | undefined = $derived(
resourcePath in $dbSchemas ? $dbSchemas[resourcePath] : undefined
)
let isDrawerOpen: boolean = $state(false)
let shouldDisplayError = $derived(
resourcePath && resourcePath in $dbSchemas && !$dbSchemas[resourcePath]
)
// `refreshCount` is a derived state. `refreshing` is the source of truth
let refreshCount = $state(0)
$effect(() => {
if (refreshing) untrack(() => (refreshCount += 1))
})
let refreshing = $state(false)
$effect(() => {
if (refreshing) getSchema()
})
const refresh = () => !refreshing && (refreshing = true)
let expand = $state(false)
$effect(() => {
if (!isDrawerOpen) expand = false
})
async function getSchema() {
if ($dbSchemas[resourcePath] && !refreshing) return
try {
const oldDbSchema = $dbSchemas[resourcePath]
await getDbSchemas(
resourceType,
resourcePath,
$workspaceStore,
$dbSchemas,
(message: string) => {
if (isDrawerOpen) {
sendUserToast(message, true)
}
}
)
// avoid infinite loop on error due to the way getDbSchemas is implemented
// and relying on an assignement side effect
if (oldDbSchema !== $dbSchemas[resourcePath]) $dbSchemas = $dbSchemas
} catch (e) {
console.error(e)
} finally {
refreshing = false
}
}
let windowWidth = $state(window.innerWidth)
let replPanelSize = $state(36)
const REPL_MIN_SIZE = 1.5
let replResultData: undefined | Record<string, any>[] = $state(undefined)
let cachedColDefs: Record<string, TableMetadata> = {}
let cachedLastRefreshCount = 0
async function getColDefs(tableKey: string) {
if (cachedLastRefreshCount !== refreshCount) cachedColDefs = {}
cachedLastRefreshCount = refreshCount
if (cachedColDefs[tableKey]) {
return cachedColDefs[tableKey]
}
try {
cachedColDefs =
(await loadAllTablesMetaData('$res:' + resourcePath, $workspaceStore, resourceType)) ??
cachedColDefs
return cachedColDefs[tableKey]
} catch (e) {
const result = await loadTableMetaData(
'$res:' + resourcePath,
$workspaceStore,
tableKey,
resourceType
)
if (result) cachedColDefs[tableKey] = result
return result ?? []
}
}
</script>
<svelte:window
bind:innerWidth={windowWidth}
onkeydown={(e) => {
if (e.key === 'Escape') {
if (replResultData) {
replResultData = undefined
}
}
}}
/>
{#if shouldDisplayError}
<Alert type="error" size="xs" title="Schema not available" class="mt-2">
Schema could not be loaded. Please check the permissions of the resource.
</Alert>
{:else}
<Button
size="xs"
variant="border"
spacingSize="xs2"
btnClasses="mt-1 w-24"
on:click={async () => {
if (!dbSchema || !$workspaceStore) refreshing = true
isDrawerOpen = true
}}
>
<Database size={18} /> Manager
</Button>
<Drawer bind:open={isDrawerOpen} size={expand ? `${windowWidth}px` : '1200px'} preventEscape>
<DrawerContent
title={replResultData ? 'Query Result' : 'Database Manager'}
on:close={() => {
if (replResultData) {
replResultData = undefined
} else {
isDrawerOpen = false
}
}}
CloseIcon={replResultData ? ArrowLeft : undefined}
noPadding
>
{#if dbSchema && $workspaceStore}
<Splitpanes horizontal>
<Pane class="relative">
<!-- svelte-ignore a11y_click_events_have_key_events -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
class={'absolute inset-0 z-10 p-8 ' +
(replResultData
? 'bg-surface/90'
: 'transition-colors bg-transparent pointer-events-none select-none')}
onclick={(e) => {
// Only proceed if the click is directly on this div and not on the child elements
if (e.target === e.currentTarget) {
replResultData = undefined
}
}}
>
{#if replResultData}
{#key replResultData}
<SimpleAgTable data={replResultData} class="animate-zoom-in" />
{/key}
{/if}
</div>
<DbManager
dbSupportsSchemas={dbSupportsSchemas(resourceType)}
{dbSchema}
{getColDefs}
dbTableOpsFactory={({ colDefs, tableKey }) =>
dbTableOpsWithPreviewScripts({
colDefs,
tableKey,
resourcePath,
resourceType,
workspace: $workspaceStore
})}
dbTableActionsFactory={[
dbDeleteTableActionWithPreviewScript({
resourcePath,
resourceType,
workspace: $workspaceStore
})
]}
{refresh}
dbTableEditorPropsFactory={({ selectedSchemaKey }) => ({
resourceType,
previewSql: (values) =>
makeCreateTableQuery(values, resourceType, selectedSchemaKey),
async onConfirm(values) {
await runPreviewJobAndPollResult({
workspace: $workspaceStore,
requestBody: {
args: { database: '$res:' + resourcePath },
content: makeCreateTableQuery(values, resourceType, selectedSchemaKey),
language: getLanguageByResourceType(resourceType)
}
})
refresh()
}
})}
/>
</Pane>
<Pane bind:size={replPanelSize} minSize={REPL_MIN_SIZE} class="relative">
<SqlRepl
{resourcePath}
{resourceType}
onData={(data) => {
replResultData = data
}}
placeholderTableName={sortArray(
Object.keys(
dbSchema?.schema[
'public' in dbSchema?.schema
? 'public'
: 'dbo' in dbSchema?.schema
? 'dbo'
: Object.keys(dbSchema?.schema)?.[0]
]
)
)?.[0]}
/>
</Pane>
</Splitpanes>
{:else}
<Splitpanes>
<Pane class="relative flex justify-center items-center">
<Loader2 class="animate-spin" size={32} />
</Pane>
</Splitpanes>
{/if}
<svelte:fragment slot="actions">
<Button
loading={refreshing}
on:click={() => refresh()}
startIcon={{ icon: RefreshCcw }}
size="xs"
color="light"
>
Refresh
</Button>
<Button
on:click={() => (expand = !expand)}
startIcon={{ icon: expand ? Minimize : Expand }}
size="xs"
color="light"
/>
</svelte:fragment>
</DrawerContent>
</Drawer>
{/if}