Skip to content

feat(chen): replace a saved SQL snippet and give its name more room - #2004

Open
adriadam10 wants to merge 1 commit into
jumpserver:devfrom
adriadam10:fix/chen-replace-sql-snippets
Open

adriadam10 wants to merge 1 commit into
jumpserver:devfrom
adriadam10:fix/chen-replace-sql-snippets

Conversation

@adriadam10

Copy link
Copy Markdown
Contributor

Problem

  • A saved SQL snippet cannot be updated from Chen. Saving again under the same name fails with "The fields name, creator must make a unique set" (AdHoc has unique_together = [('name', 'creator')]), so users end up keeping several copies under different names.
  • In "Select SQL" the name column has a fixed w-36 and long names are cut off, while the text buttons ("Insert", "Delete") take another w-36 and the content preview takes the rest.

Change

  • useChenSqlSnippets.save() first looks up the user's own snippet with that exact name (GET /api/v1/ops/adhocs/?name=…&only_mine=true). If it exists it returns "exists" instead of creating; the save dialog then explains it and the confirm button becomes "Replace", which sends PATCH /api/v1/ops/adhocs/<id>/. Renaming in the dialog after the warning goes back to a normal save. Replacing a shared snippet keeps it shared.
  • Select dialog: the name column gets 45% of the width; "Insert" and "Delete" become icon buttons with tooltips and aria-label.
  • New i18n keys Chen.ReplaceSql and Chen.ReplaceSqlConfirm in every locale.
  • Unit tests for the save flow (useChenSqlSnippets.test.ts).

No core change: name is an exact filter in AdHocFilterSet, only_mine is already handled by AdHocViewSet, and IsOwnerOrAdminWritable lets the creator update the snippet.

Testing

  • oxfmt, oxlint, eslint, nuxt typecheck and vitest (the repository's pre-commit) pass.
  • On a v5.2.1 core: the exact-name lookup with only_mine=true returns only the user's snippet, and a prefix does not match.

Saving SQL under a name the user already has failed with "The fields name,
creator must make a unique set", and there was no way to update a saved
snippet, so users kept saving copies under new names.

- Before creating, look up the user's own snippet with that exact name. If
  it exists, the save dialog asks to replace it and the confirm button turns
  into "Replace", which PATCHes the snippet. A shared snippet stays shared.
- In the select dialog the name had a fixed 9rem and was cut off, while the
  text buttons took another 9rem: the name gets 45% of the width and Insert
  and Delete become icon buttons with tooltips.
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

@ibuler

ibuler commented Dec 4, 2026

Copy link
Copy Markdown
Member

Nice UX improvement! Two edge cases worth addressing before merging:

  1. findOwn() filters by module, whereas Core's AdHoc uniqueness (unique_together = [('name', 'creator')]) applies across all modules (ui/chen/composables/useChenSqlSnippets.ts#L122-L125)

    • requests.list({ module, name, only_mine: true, ... }) and normalizeChenSqlSnippets(data, module) only look up snippets for the current dialect.
    • If the user already has a saved snippet with the same name in another database dialect (e.g. postgresql while saving in mysql) or a terminal command snippet (shell / powershell / etc., which shares /api/v1/ops/adhocs/), findOwn() returns undefined and requests.create() still fails with The fields name, creator must make a unique set.
    • Look it up without filtering by module; when an existing entry belongs to a different module (especially a terminal command snippet that may also carry comment / variable definitions), please avoid silently overwriting its module via PATCH—either reject cross-module collisions with a clear message or only allow replace when existing.module === module.
  2. Stale snippetConflict can display error and replace alerts simultaneously (ui/chen/v1/GoQueryPane.vue#L192-L200)

    • saveSnippet() clears snippetError.value = "" on submit, but keeps any previous snippetConflict.value. If "A" triggers exists, the user tries "B" which fails validation or throws a request error, and then edits the input back to "A", both error and replacing alerts are shown at once. Clearing snippetConflict.value = "" when starting a new save attempt (or when name changes) keeps the dialog state clean.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants