diff --git a/.github/workflows/auto-release.yml b/.github/workflows/auto-release.yml index 80dca6b3..ecdb0e3e 100644 --- a/.github/workflows/auto-release.yml +++ b/.github/workflows/auto-release.yml @@ -43,7 +43,8 @@ jobs: steps: - uses: actions/checkout@v5 with: - ref: nightly + # The full ref: a `nightly` tag (the rolling pre-release) exists too. + ref: refs/heads/nightly fetch-depth: 0 # A PAT, so the bump commit can be pushed to nightly. token: ${{ secrets.RELEASES_TOKEN }} @@ -82,8 +83,20 @@ jobs: git config user.email "41898282+github-actions[bot]@users.noreply.github.com" git add src-tauri/tauri.conf.json package.json CHANGELOG.md .changeset/ git commit -m "chore: version ${VERSION} (#${PR_NUMBER})" - git push origin HEAD:nightly - echo "nightly is now ${VERSION}; the next nightly build ships it." + # A PR merged while this ran moves nightly ahead of us. Runs are + # serialized, so what landed is a merge, never another bump: rebase + # onto it and push again instead of failing (its changeset is folded + # by its own run). + for attempt in 1 2 3 4 5; do + if git push origin HEAD:refs/heads/nightly; then + echo "nightly is now ${VERSION}; the next nightly build ships it." + exit 0 + fi + sleep $((attempt * 3)) + git pull --rebase origin refs/heads/nightly + done + echo "::error::Could not push the version bump after 5 attempts." + exit 1 # ── PR merged into master: promotion or hotfix ───────────────────────────── stable-release: @@ -157,12 +170,12 @@ jobs: run: | git config user.name "github-actions[bot]" git config user.email "41898282+github-actions[bot]@users.noreply.github.com" - git fetch origin nightly - git checkout -B nightly origin/nightly + git fetch origin refs/heads/nightly:refs/remotes/origin/nightly + git checkout -B nightly refs/remotes/origin/nightly if git merge-base --is-ancestor origin/master HEAD; then echo "nightly already contains master." elif git merge --no-edit origin/master -m "chore: merge master into nightly"; then - git push origin nightly + git push origin HEAD:refs/heads/nightly else git merge --abort gh pr create --base nightly --head master \ diff --git a/.github/workflows/nightly.yml b/.github/workflows/nightly.yml index 4bafc570..6f597c9c 100644 --- a/.github/workflows/nightly.yml +++ b/.github/workflows/nightly.yml @@ -43,7 +43,8 @@ jobs: steps: - uses: actions/checkout@v5 with: - ref: nightly + # The branch, not the `nightly` tag of the same name. + ref: refs/heads/nightly fetch-depth: 0 - id: check @@ -64,9 +65,27 @@ jobs: echo "Building $VERSION from $SHA (last nightly: ${LAST:-none})." fi + # ── 1b. Tests: unit tests, then every main dialog and page mounted once ──── + # A Nightly that crashes on open is worse than no Nightly. Two did, from a + # missing import and an effect loop; the component smoke tests catch both. + test: + needs: check + if: needs.check.outputs.build == 'true' + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ needs.check.outputs.sha }} + - uses: actions/setup-node@v5 + with: + node-version: 22 + cache: npm + - run: npm ci + - run: npm test + # ── 2. Build every platform ──────────────────────────────────────────────── build: - needs: check + needs: [check, test] if: needs.check.outputs.build == 'true' strategy: fail-fast: false @@ -83,7 +102,8 @@ jobs: - platform: macos-latest name: macos-aarch64 args: '--target aarch64-apple-darwin --bundles app,dmg' - - platform: macos-latest + # Intel runner: a separate pool from the busy arm64 one, and a native x64 build. + - platform: macos-15-intel name: macos-x64 args: '--target x86_64-apple-darwin --bundles app,dmg' @@ -191,6 +211,18 @@ jobs: esac ls -la out + # The .deb is an update payload too: a .deb install updates from the + # app (the updater runs pkexec dpkg -i on it), so it needs a signature. + - name: Sign the .deb + if: matrix.name == 'linux' + shell: bash + env: + TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }} + TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }} + run: | + npx tauri signer sign out/stroke-nightly_amd64.deb + test -s out/stroke-nightly_amd64.deb.sig + - uses: actions/upload-artifact@v4 with: name: nightly-${{ matrix.name }} @@ -198,11 +230,69 @@ jobs: if-no-files-found: error retention-days: 3 + # ── 2b. The Arch package ──────────────────────────────────────────────────── + # The same package the AUR builds (stroke-nightly-bin, from the .deb), + # built here so an Arch install can update from the app: it downloads + # this and runs `pkexec pacman -U` on it. Same name and version as the + # AUR's, so yay sees the result as up to date. + build-pacman: + needs: [check, build] + runs-on: ubuntu-latest + container: archlinux:latest + steps: + - name: Tools + run: pacman -Syu --noconfirm --needed base-devel nodejs npm git sudo + + - uses: actions/checkout@v5 + with: + ref: ${{ needs.check.outputs.sha }} + + - uses: actions/download-artifact@v4 + with: + name: nightly-linux + path: linux + + - name: Build the package + env: + VERSION: ${{ needs.check.outputs.version }} + COMMIT: ${{ needs.check.outputs.sha }} + run: | + set -euo pipefail + PKGVER="${VERSION//-/.}" + useradd -m builder + mkdir -p /tmp/pkg + # Named the way the PKGBUILD's source names it, so makepkg uses this + # file instead of downloading a release that isn't published yet. + cp linux/stroke-nightly_amd64.deb "/tmp/pkg/stroke-nightly-${PKGVER}.deb" + SHA=$(sha256sum "/tmp/pkg/stroke-nightly-${PKGVER}.deb" | cut -d' ' -f1) + sed -e "s/@PKGVER@/${PKGVER}/" -e "s/@SHA256@/${SHA}/" -e "s/@COMMIT@/${COMMIT}/" \ + packaging/aur/stroke-nightly-bin/PKGBUILD.in > /tmp/pkg/PKGBUILD + chown -R builder /tmp/pkg + sudo -u builder bash -c 'cd /tmp/pkg && makepkg --nodeps --noconfirm' + mkdir -p out + cp /tmp/pkg/stroke-nightly-bin-*.pkg.tar.zst out/stroke-nightly_x86_64.pkg.tar.zst + ls -la out + + - name: Sign + env: + TAURI_SIGNING_PRIVATE_KEY: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY }} + TAURI_SIGNING_PRIVATE_KEY_PASSWORD: ${{ secrets.TAURI_SIGNING_PRIVATE_KEY_PASSWORD }} + run: | + npx -y @tauri-apps/cli@2 signer sign out/stroke-nightly_x86_64.pkg.tar.zst + test -s out/stroke-nightly_x86_64.pkg.tar.zst.sig + + - uses: actions/upload-artifact@v4 + with: + name: nightly-pacman + path: out/* + if-no-files-found: error + retention-days: 3 + # ── 3. Replace the `nightly` pre-release ─────────────────────────────────── # Only after every platform built: a failed night keeps the previous # nightly up instead of publishing a partial one. publish: - needs: [check, build] + needs: [check, build, build-pacman] runs-on: ubuntu-latest steps: - uses: actions/checkout@v5 @@ -223,7 +313,9 @@ jobs: run: | set -euo pipefail BASE="https://github.com/${GITHUB_REPOSITORY}/releases/download/nightly" - VERSION="$VERSION" BASE="$BASE" node -e " + git fetch -q origin master || true + STABLE=$(git show origin/master:src-tauri/tauri.conf.json 2>/dev/null | node -e "let s='';process.stdin.on('data',d=>s+=d).on('end',()=>{try{console.log(JSON.parse(s).version)}catch{console.log('')}})") + VERSION="$VERSION" BASE="$BASE" STABLE="$STABLE" node -e " const fs = require('fs') const { VERSION, BASE } = process.env const sig = (f) => fs.readFileSync('out/' + f, 'utf8').trim() @@ -231,9 +323,23 @@ jobs: 'darwin-aarch64': { url: BASE + '/stroke-nightly_aarch64.app.tar.gz', signature: sig('stroke-nightly_aarch64.app.tar.gz.sig') }, 'darwin-x86_64': { url: BASE + '/stroke-nightly_x64.app.tar.gz', signature: sig('stroke-nightly_x64.app.tar.gz.sig') }, 'linux-x86_64': { url: BASE + '/stroke-nightly_amd64.AppImage', signature: sig('stroke-nightly_amd64.AppImage.sig') }, + // Linux packages update from the app too, each from its own entry: + // a .deb through pkexec dpkg, an Arch install through pkexec pacman. + 'linux-x86_64-deb': { url: BASE + '/stroke-nightly_amd64.deb', signature: sig('stroke-nightly_amd64.deb.sig') }, + 'linux-x86_64-pacman': { url: BASE + '/stroke-nightly_x86_64.pkg.tar.zst', signature: sig('stroke-nightly_x86_64.pkg.tar.zst.sig') }, 'windows-x86_64': { url: BASE + '/stroke-nightly_x64-setup.exe', signature: sig('stroke-nightly_x64-setup.exe.sig') }, } - const notes = 'Stroke Nightly ' + VERSION + '. See the nightly release for what changed.' + // The changelog since the last stable release, in CHANGELOG.md's own + // format: the update window parses it into New / Improved / Fixed. + let notes = 'Stroke Nightly ' + VERSION + '. See the nightly release for what changed.' + try { + const cl = fs.readFileSync('CHANGELOG.md', 'utf8') + const stable = process.env.STABLE || '' + const start = cl.indexOf('## [') + const end = stable ? cl.indexOf('## [' + stable + ']') : -1 + const body = start === -1 ? '' : cl.slice(start, end > start ? end : undefined).trim() + if (body) notes = body.slice(0, 60000) + } catch {} fs.writeFileSync('out/latest.json', JSON.stringify({ version: VERSION, notes, pub_date: new Date().toISOString(), platforms }, null, 2)) " cat out/latest.json @@ -253,7 +359,7 @@ jobs: echo "| macOS (Apple Silicon) | \`stroke-nightly_aarch64.dmg\` |" echo "| macOS (Intel) | \`stroke-nightly_x64.dmg\` |" echo "| Windows | \`stroke-nightly_x64-setup.exe\` |" - echo "| Linux | \`stroke-nightly_amd64.AppImage\` · \`stroke-nightly_amd64.deb\` |" + echo "| Linux | \`stroke-nightly_amd64.AppImage\` · \`stroke-nightly_amd64.deb\` · \`stroke-nightly_x86_64.pkg.tar.zst\` (Arch) |" echo echo "### Changes since the last nightly" if [ -n "$PREV" ]; then @@ -284,3 +390,49 @@ jobs: --prerelease \ --latest=false echo "✅ Published Stroke Nightly ${VERSION}" + + # Arch: refresh stroke-nightly-bin on the AUR with this build. Skipped + # without the AUR_SSH_PRIVATE_KEY secret (forks). + update-aur: + needs: [check, publish] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ needs.check.outputs.sha }} + + - name: Fill in the PKGBUILD + id: pkg + env: + VERSION: ${{ needs.check.outputs.version }} + COMMIT: ${{ needs.check.outputs.sha }} + AUR_KEY: ${{ secrets.AUR_SSH_PRIVATE_KEY }} + run: | + set -euo pipefail + if [ -z "$AUR_KEY" ]; then + echo "No AUR_SSH_PRIVATE_KEY secret: skipping the AUR publish." + echo "skip=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + # pacman versions can't hold "-": 2.8.8-nightly.2026... -> 2.8.8.nightly.2026... + PKGVER="${VERSION//-/.}" + curl -fsSL --retry 5 --retry-delay 5 -o stroke-nightly.deb \ + "https://github.com/${GITHUB_REPOSITORY}/releases/download/nightly/stroke-nightly_amd64.deb" + SHA=$(sha256sum stroke-nightly.deb | cut -d' ' -f1) + mkdir -p aur + sed -e "s/@PKGVER@/${PKGVER}/" -e "s/@SHA256@/${SHA}/" -e "s/@COMMIT@/${COMMIT}/" \ + packaging/aur/stroke-nightly-bin/PKGBUILD.in > aur/PKGBUILD + cat aur/PKGBUILD + echo "skip=false" >> "$GITHUB_OUTPUT" + + - name: Push to the AUR + if: steps.pkg.outputs.skip == 'false' + uses: KSXGitHub/github-actions-deploy-aur@v4.1.1 + with: + pkgname: stroke-nightly-bin + pkgbuild: aur/PKGBUILD + commit_username: broisnees + commit_email: broisnees@users.noreply.github.com + ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }} + commit_message: "Update to ${{ needs.check.outputs.version }}" + ssh_keyscan_types: rsa,ecdsa,ed25519 diff --git a/.github/workflows/promote.yml b/.github/workflows/promote.yml index 987bc50f..371d4924 100644 --- a/.github/workflows/promote.yml +++ b/.github/workflows/promote.yml @@ -19,7 +19,8 @@ jobs: steps: - uses: actions/checkout@v5 with: - ref: nightly + # The branch, not the `nightly` tag of the same name. + ref: refs/heads/nightly fetch-depth: 0 - name: Open the release PR diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 89066f64..f100c640 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -149,7 +149,8 @@ jobs: - platform: macos-latest name: macos-aarch64 args: '--target aarch64-apple-darwin' - - platform: macos-latest + # Intel runner: a separate pool from the busy arm64 one, and a native x64 build. + - platform: macos-15-intel name: macos-x64 args: '--target x86_64-apple-darwin' @@ -438,6 +439,48 @@ jobs: TAG: ${{ env.RELEASE_TAG }} run: gh release upload "$TAG" --repo "$RELEASES_REPO" latest.json --clobber +# ── 4a. Publish the AUR package (stroke-bin) ──────────────────────────────── +# `yay -S stroke-bin` / `paru -S stroke-bin`. Needs the AUR_SSH_PRIVATE_KEY +# secret (a key added to the AUR account that maintains stroke-bin); without +# it this job says so and does nothing. + update-aur: + needs: publish-release + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v5 + + - name: Fill in the PKGBUILD + id: pkg + env: + TAG: ${{ env.RELEASE_TAG }} + AUR_KEY: ${{ secrets.AUR_SSH_PRIVATE_KEY }} + run: | + if [ -z "$AUR_KEY" ]; then + echo "No AUR_SSH_PRIVATE_KEY secret: skipping the AUR publish." + echo "skip=true" >> "$GITHUB_OUTPUT" + exit 0 + fi + VERSION="${TAG#v}" + URL="https://github.com/stroke-app/stroke/releases/download/${TAG}/stroke_${VERSION}_amd64.deb" + curl -fsSL -o stroke.deb "$URL" + SHA=$(sha256sum stroke.deb | cut -d' ' -f1) + mkdir -p aur + sed -e "s/@VERSION@/${VERSION}/" -e "s/@SHA256@/${SHA}/" packaging/aur/stroke-bin/PKGBUILD.in > aur/PKGBUILD + cat aur/PKGBUILD + echo "skip=false" >> "$GITHUB_OUTPUT" + + - name: Push to the AUR + if: steps.pkg.outputs.skip == 'false' + uses: KSXGitHub/github-actions-deploy-aur@v4.1.1 + with: + pkgname: stroke-bin + pkgbuild: aur/PKGBUILD + commit_username: broisnees + commit_email: broisnees@users.noreply.github.com + ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }} + commit_message: "Update to ${{ env.RELEASE_TAG }}" + ssh_keyscan_types: rsa,ecdsa,ed25519 + # ── 4. Refresh the Scoop manifest ──────────────────────────────────────────── # `scoop bucket add stroke https://github.com/stroke-app/stroke` clones # THIS repo anonymously and reads bucket/stroke.json, so the manifest is diff --git a/CHANGELOG.md b/CHANGELOG.md index cf0e1fcd..a22b4288 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,1013 @@ All notable changes to Stroke are listed here, newest first. --- +## [3.0.0] - 2026-11-19 + +### New Features + +#### Stroke 3 +- **One account, three computers** - Sign in with GitHub or Google. Your license comes with your account and covers up to 3 computers; manage them at stroke.click/app. Paid Pro keeps working offline for 30 days. +- **Sync** - Workspaces, connections, settings, saved queries, charts and dashboards sync between your computers, end-to-end encrypted. +- **21 databases** - New since 2.3: Oracle, Elasticsearch, OpenSearch, Xata and SingleStore, alongside MongoDB, CouchDB, Cassandra, DynamoDB, Firestore and Redis. +- **Several databases at once (Labs)** - Keep more than one connection open, each with its tabs, and switch instantly. + +#### AI +- **AI chat in a tab** - AI opens beside your tables instead of taking over the window, and its chats live in the sidebar. On by default in this version; switch it off in Settings → Labs. +- **Access levels for every model** - Read only, Read & write, Modify schema or Dangerous, now enforced for the built-in models and API keys too, not only local agents. +- **Plans and step timeline** - A multi-step request shows its plan, each tool call is one row with its result, and a finished turn says what changed. + +#### Your databases +- **Local database servers** - Run Postgres, MySQL or Redis on this computer in any version, in their own window, start them with Stroke, and see the servers already running here. +- **Migrations** - Drizzle, Prisma, Wrangler (D1) and plain SQL folders: what has run, what is pending, and the SQL of each. +- **Scheduled backups** - To Stroke Cloud or your own S3, end-to-end encrypted, with a preview of a dump and its encoding before you restore. +- **Docker in one step** - ⌘K → Docker starts any of 18 databases, already connected. +- **Dashboards** - Live chart cards that refresh themselves, open in their own window, full screen. +- **Paste any connection string** - .env lines, ORM URLs and key/value strings land on the right form. + +#### Everywhere +- **Leader key** - Press ⌘; (Ctrl+; on Windows and Linux), then one key. +- **Share as a web studio (Labs)** - Share a database through a link that opens in any browser. +- **SQL notebooks** - Variables, import and export, autosave. +- **Results beside the editor** - Put SQL results below or beside the editor. +- **Quieter updates** - Updates download in the background and install as Stroke quits. Homebrew, Scoop, AUR, .deb and .rpm installs update through their package manager. + +### Changes +- A faster start: Settings, the command palette and one-off dialogs load on first use, and the data grid loads lazily. +- Windows and Linux get a 40px title bar with native window buttons, and native Wayland on Linux. +- echarts 6.1, which fixes an XSS advisory; charts look as they did. +- The Explorer tree stays in Labs (Settings → Labs). + + +## [2.20.4] - 2026-11-19 + +### Changes +- Hold the built-in AI chat to its access level; redesign Migrations (#306) + + +## [2.20.3] - 2026-11-19 + +### Changes +- Move local servers to their own window; fix dashboard window and D1 migrations (#305) + + +## [2.20.2] - 2026-11-18 + +### Changes +- Add Xata and SingleStore providers (#303) + + +## [2.20.1] - 2026-11-18 + +### Changes +- Tell Supabase users to sign in again for project creation (#302) + + +## [2.20.0] - 2026-11-18 + +### Changes +- Add a Migrations page and local database servers (#301) + + +## [2.19.9] - 2026-11-18 + +### Changes +- Calmer update check and a lighter views icon (#300) + + +## [2.19.8] - 2026-11-18 + +### Changes +- Preview a dump and pick its encoding before restoring (#297) + + +## [2.19.7] - 2026-11-18 + +### Changes +- Switch SQL results between below and beside the editor (#296) + + +## [2.19.6] - 2026-11-18 + +### Changes +- Let the DynamoDB console send the API calls it offers (#295) + + +## [2.19.5] - 2026-11-18 + +### Changes +- End-to-end encrypt scheduled backups (#294) + + +## [2.19.4] - 2026-11-18 + +### Changes +- TODO: what's next (#293) + + +## [2.19.3] - 2026-11-18 + +### Changes +- Compact scheduled backups screen (#292) + + +## [2.19.2] - 2026-11-18 + +### Changes +- Fix sync failing with "Unknown record kind: library" (#291) + + +## [2.19.1] - 2026-11-18 + +### Changes +- Scheduled backups to Stroke Cloud or your own S3 (#290) + + +## [2.19.0] - 2026-11-18 + +### Changes +- Account-only licensing for Stroke 3 (#289) + + +## [2.18.9] - 2026-11-17 + +### Changes +- Go back to fat LTO for Nightly builds (#288) + + +## [2.18.8] - 2026-11-17 + +### Changes +- AI table commands for DynamoDB and MongoDB, live backup log, fixes (#287) + + +## [2.18.7] - 2026-11-17 + +### Changes +- Terminal: DOM renderer on Linux; panel slide animation off by default (#286) + + +## [2.18.6] - 2026-11-17 + +### Changes +- Lazy data grid, native Wayland with X11 fallback, whole-table exports, faster Nightly builds (#285) + + +## [2.18.5] - 2026-11-17 + +### Changes +- Hide scrollbars behind an open dialog on Linux (#284) + + +## [2.18.4] - 2026-11-17 + +### Changes +- Share a database as a web studio (Labs) (#283) + + +## [2.18.3] - 2026-11-17 + +### Changes +- No console windows from the AI agent CLIs on Windows; reload the page if WebView2's renderer dies (#282) + + +## [2.18.2] - 2026-11-17 + +### Changes +- Update Arch and .deb installs from the app, through the system password prompt (#281) + + +## [2.18.1] - 2026-11-17 + +### Changes +- Load Settings, the command palette and one-off dialogs on first use (#280) + + +## [2.18.0] - 2026-11-17 + +### Changes +- Upgrade echarts to 6.1 for the XSS fix, keeping charts as they look on 5 (#279) + + +## [2.17.9] - 2026-11-17 + +### Changes +- Don't self-update Scoop installs on Windows; show scoop update stroke instead (#278) + + +## [2.17.8] - 2026-11-17 + +### Changes +- Don't self-update AUR, .deb and .rpm installs; show the package manager command instead (#277) + + +## [2.17.7] - 2026-11-17 + +### Changes +- Tree sidebar keyboard: visible focus when tabbing in, Tab lands on the first match, one clear button (#276) + + +## [2.17.6] - 2026-11-17 + +### Changes +- One Sign in button; calmer trial screen; sign-out goes back to sign-in; better callback page (#275) + + +## [2.17.5] - 2026-11-17 + +### Changes +- Keep the title bar's spacing fixed when zoomed on Linux (#274) + + +## [2.17.4] - 2026-11-17 + +### Changes +- Check for updates from the sign-in and trial screens; open title bar menus over the welcome tour (#273) + + +## [2.17.3] - 2026-11-17 + +### Changes +- Redesign the license key entry: a field well and a keycap Activate button (#272) + + +## [2.17.2] - 2026-11-17 + +### Changes +- Show the grabbing cursor while dragging the geometry mini map (#271) + + +## [2.17.1] - 2026-11-17 + +### Changes +- Let the AppImage run on native Wayland with STROKE_WAYLAND=1 (#270) + + +## [2.17.0] - 2026-11-17 + +### Changes +- Add the v3 plan, launch week and what comes after to the TODO (#269) + + +## [2.16.9] - 2026-11-17 + +### Changes +- Replace the TODO with the website redesign and the update emails (#268) + + +## [2.16.8] - 2026-11-17 + +### Changes +- Close the other open databases when several connections is turned off (#267) + + +## [2.16.7] - 2026-11-17 + +### Changes +- Switch databases on double-click in the tree without confirming (#266) + + +## [2.16.6] - 2026-11-16 + +### Changes +- Polish connect page, provider sign-in, tree with several databases (#265) + + +## [2.16.5] - 2026-11-16 + +### Changes +- Plan badge: a small dark keycap (#264) + + +## [2.16.4] - 2026-11-16 + +### Changes +- Labs: flat tab bar, one name per database, quiet swap; smaller plan tag (#263) + + +## [2.16.3] - 2026-11-16 + +### Changes +- Labs: fix the crash on disconnect, switch without flicker, report crashes (#262) + + +## [2.16.2] - 2026-11-15 + +### Changes +- Labs: several databases open together, one tab bar, switching in place (#260) + + +## [2.16.1] - 2026-11-15 + +### Changes +- NoSQL: a tab per collection, like table tabs (#259) + + +## [2.16.0] - 2026-11-15 + +### Changes +- SQL notebook: import/export, variables, autosave, a usable cell UI (#258) + + +## [2.15.9] - 2026-11-15 + +### Changes +- Security page: the app's type, identifiers in mono (#257) + + +## [2.15.8] - 2026-11-15 + +### Changes +- Advisor: a verdict, sharper checks, and fixes you can open (#256) + + +## [2.15.7] - 2026-11-15 + +### Changes +- Model picker: hovering a provider shows its models (#255) + + +## [2.15.6] - 2026-11-15 + +### Changes +- Labs: focus the tree on a database switch (#254) + + +## [2.15.5] - 2026-11-15 + +### Changes +- MCP current_database names the database queries actually run on (#253) + + +## [2.15.4] - 2026-11-15 + +### Changes +- Theme picker shows its shortcut (⌘⌥T) where you find it (#252) + + +## [2.15.3] - 2026-11-15 + +### Changes +- Restore into D1/libSQL settles foreign keys; libSQL/Turso gets backup and restore (#251) + + +## [2.15.2] - 2026-11-15 + +### Changes +- AI chat: your messages render markdown; the composer types it (#250) + + +## [2.15.1] - 2026-11-15 + +### Changes +- Diagrams: saved diagrams show at once; no stray half-render; the list folds away (#249) + + +## [2.15.0] - 2026-11-15 + +### Changes +- New connection: engine logos stacked on the button (#248) + + +## [2.14.9] - 2026-11-15 + +### Changes +- Vim chip as a glossy key; editor panels stay under popovers; Explain cards fit their text (#246) + + +## [2.14.8] - 2026-11-15 + +### Changes +- Sidebar catches AI-made views and tables without a manual refresh (#245) + + +## [2.14.7] - 2026-11-15 + +### Changes +- AI chat Run refreshes the sidebar; refresh buttons show they ran (#244) + + +## [2.14.6] - 2026-11-15 + +### Changes +- Connect screen: the footer lines up with the page above it (#243) + + +## [2.14.5] - 2026-11-15 + +### Changes +- npx strokedb: open a project's database in the app or the web studio (#241) + + +## [2.14.4] - 2026-11-15 + +### Changes +- Labs: several connections at once; Parquet and Excel export; searchable dropdowns (#240) + + +## [2.14.3] - 2026-11-15 + +### Changes +- Dashboard: live cards, numbers, refresh; list picker; Bro theme (#239) + + +## [2.14.2] - 2026-11-15 + +### Changes +- Panels and terminal: no jumps, no blank or stale terminal (#238) + + +## [2.14.1] - 2026-11-15 + +### Changes +- Hold ⌘Q or press it twice to quit on macOS (#237) + + +## [2.14.0] - 2026-11-15 + +### Changes +- Explain: findings, step details, tree view; never keep ANALYZE writes (#236) + + +## [2.13.9] - 2026-11-15 + +### Changes +- Focus the open tab's search on /, not the sidebar filter (#235) + + +## [2.13.8] - 2026-11-15 + +### Changes +- Show only the tick on the current item in menus (#234) + + +## [2.13.7] - 2026-11-15 + +### Changes +- Focus the grid column when I click it in the sidebar tree (#233) + + +## [2.13.6] - 2026-11-15 + +### Changes +- Add a theme: community gallery, theme files and pasted JSON (#232) + + +## [2.13.5] - 2026-11-15 + +### Changes +- Share: titles from what is shared, chart preview; simpler New view (#231) + + +## [2.13.4] - 2026-11-15 + +### Changes +- Live mode on MongoDB, the other document stores and Redis (#230) + + +## [2.13.3] - 2026-11-15 + +### Changes +- Dashboard: searchable switcher, one menu, rename in place (#229) + + +## [2.13.2] - 2026-11-15 + +### Changes +- Terminal dock and AI panel slide with a transform, not a resize (#228) + + +## [2.13.1] - 2026-11-15 + +### Changes +- D1 prompt: Time Travel bookmarks and restore, slash commands (#227) + + +## [2.13.0] - 2026-11-15 + +### Changes +- Schema snapshots: searchable connection, tidy cards, readable migration (#226) + + +## [2.12.9] - 2026-11-15 + +### Changes +- Terminal: steady font while resizing, Oracle via SQLcl thin, panel animation setting (#225) + + +## [2.12.8] - 2026-11-15 + +### Changes +- Chart pickers and FieldSelect are searchable (#224) + + +## [2.12.7] - 2026-11-15 + +### Changes +- Chat code blocks: visible Copy, solid Run, readable request bodies (#223) + + +## [2.12.6] - 2026-11-15 + +### Changes +- Saved charts: search, group filter, one menu per chart, share (#222) + + +## [2.12.5] - 2026-11-15 + +### Changes +- Sync saved queries, charts, dashboards and query folders (#221) + + +## [2.12.4] - 2026-11-15 + +### Changes +- NoSQL pages: Mod+R and agent changes reload them; no flicker on refresh (#220) + + +## [2.12.3] - 2026-11-15 + +### Changes +- Document stores: docked apply bar, no SQL review, Mod+S works (#219) + + +## [2.12.2] - 2026-11-15 + +### Changes +- NoSQL Browse: the view is a dropdown (#218) + + +## [2.12.1] - 2026-11-15 + +### Changes +- Table pager: one control, cleaner go-to-page (#217) + + +## [2.12.0] - 2026-11-15 + +### Changes +- Grid Vim: visual selection, yank, paste, screen and page motions (#216) + + +## [2.11.9] - 2026-11-15 + +### Changes +- Docker databases start in the background with a progress toast (#215) + + +## [2.11.8] - 2026-11-15 + +### Changes +- DynamoDB: name AWS connections by region; compact credentials switch (#214) + + +## [2.11.7] - 2026-11-15 + +### Changes +- Agents can write to Elasticsearch and OpenSearch at the right level (#213) + + +## [2.11.6] - 2026-11-15 + +### Changes +- Sidebar follows agent changes; cleaner dock, chips and title-bar buttons (#212) + + +## [2.11.5] - 2026-11-15 + +### Changes +- One search field with /; tighter Shared links; Settings follows changes from anywhere (#211) + + +## [2.11.4] - 2026-11-15 + +### Changes +- Live mode on every engine; all terminal suggestions; glossy plan chips (#210) + + +## [2.11.3] - 2026-11-15 + +### Changes +- Expanded rows build off screen; tighter sidebar footer with room below (#209) + + +## [2.11.2] - 2026-11-15 + +### Changes +- Agents can share: share_link MCP tool opens the share dialog (#208) + + +## [2.11.1] - 2026-11-15 + +### Changes +- Native panel slides, zoom-proof switches, share dialog settings rows (#207) + + +## [2.11.0] - 2026-11-15 + +### Changes +- Keep a trackpad coast vertical when Shift goes down mid-scroll (#206) + + +## [2.10.9] - 2026-11-15 + +### Changes +- Update notes per version; quiet auto-updates; Update track in Settings (#205) + + +## [2.10.8] - 2026-11-15 + +### Changes +- Smoke tests gate Nightly; cancel stale counts; search index; more (#204) + + +## [2.10.7] - 2026-11-15 + +### Changes +- Inset tree rows; neutral AI stop button (#203) + + +## [2.10.6] - 2026-11-15 + +### Changes +- Tidy the account menu (#202) + + +## [2.10.5] - 2026-11-15 + +### Changes +- Polish connection, search, table and keybinding screens; tree options (#201) + + +## [2.10.4] - 2026-11-15 + +### Changes +- Fix share crash, faster big-table search, Shared links tab, terminal and AI fixes (#200) + + +## [2.10.3] - 2026-11-15 + +### Changes +- Share links, AI share tool, and Try Nightly from stable (#199) + + +## [2.10.2] - 2026-11-15 + +### Changes +- Diagram and schema diff redesign, faster terminal start, / hints, fonts (#198) + + +## [2.10.1] - 2026-11-15 + +### Changes +- Smoother grid scrolling (120Hz budget) (#197) + + +## [2.10.0] - 2026-11-15 + +### Changes +- Elasticsearch and OpenSearch (#196) + + +## [2.9.9] - 2026-11-15 + +### Changes +- Windows polish: terminal, connection screen, Segoe UI (#195) + + +## [2.9.8] - 2026-11-15 + +### Changes +- Auto-fit column widths (#194) + + +## [2.9.7] - 2026-11-15 + +### Changes +- Related-rows panel keeps one height (#193) + + +## [2.9.6] - 2026-11-15 + +### Changes +- Sidebar: no "No tables" while counts load; quieter select box (#192) + + +## [2.9.5] - 2026-11-15 + +### Changes +- AUR stroke-nightly-bin on every nightly; Lines table style by default (#191) + + +## [2.9.4] - 2026-11-15 + +### Changes +- / focuses the search field in view (#190) + + +## [2.9.3] - 2026-11-15 + +### Changes +- Borderless AI composer, theme contrast fixes, SF Pro on Mac (#189) + + +## [2.9.2] - 2026-11-15 + +### Changes +- Press feedback only on compact buttons (#188) + + +## [2.9.1] - 2026-11-15 + +### Changes +- Calmer trial-ended screen (#187) + + +## [2.9.0] - 2026-11-15 + +### Changes +- Offer the update on the crash screen (#186) + + +## [2.8.9] - 2026-11-15 + +### Changes +- AUR: keep placeholders out of the PKGBUILD header comment (#185) + + +## [2.8.8] - 2026-11-15 + +### Changes +- Fix the welcome screen crash (missing Icon import) (#184) + + +## [2.8.7] - 2026-11-14 + +### Changes +- Publish stroke-bin to the AUR on stable releases (#183) + + +## [2.8.6] - 2026-11-14 + +### Changes +- Sync the last changes before Stroke closes (#182) + + +## [2.8.5] - 2026-11-14 + +### Changes +- Labs settings page; rename chats from their menus (#181) + + +## [2.8.4] - 2026-11-14 + +### Changes +- Sidebar: explain tables hidden by Hide empty / Hide system (#180) + + +## [2.8.3] - 2026-11-14 + +### Changes +- Notification corner setting, compact SQLite switch (#179) + + +## [2.8.2] - 2026-11-14 + +### Changes +- Aligned connection fields, smaller swatches, keyboard-first staged-changes bar (#178) + + +## [2.8.1] - 2026-11-14 + +### Changes +- License per account, one Update button, compact What's new, press feedback (#177) + + +## [2.8.0] - 2026-11-14 + +### Changes +- Schema snapshots on every engine, flat provider panel, Supabase create error (#176) + + +## [2.7.9] - 2026-11-14 + +### Changes +- Fixes: AI background replies, compact AI transcript, tabs, TimescaleDB, theme surfaces (#175) + + +## [2.7.8] - 2026-11-14 + +### Features +- Copy URL on the connection form: the connection as a connection string (postgresql://, mysql://, sqlserver://, oracle://, redis://, libsql:// and the rest), ready for a .env file, an ORM or another client. Passwords with special characters are encoded so they paste back intact + + +## [2.7.7] - 2026-11-14 + +### Improvements +- Updates download and install in the background by default, everywhere: no window appears mid-work. Where Stroke can install as it quits it does; elsewhere the sidebar's pill offers Restart. The next launch shows what changed +- A new update window in the style of macOS's Sparkle: the app's mark, the version on offer and yours, the release notes right there, one button, and the automatic-updates setting beside it +- What's new and the update window group every change into New, Improved and Fixed across all the versions you skipped, with counts, a filter, and the version each change arrived in +- Nightly's update feed carries the real changelog since the last stable release, so the update window shows what is coming before you install it + + +## [2.7.6] - 2026-11-14 + +### Features +- The status bar shows your connection: the database's round trip ("48 ms", amber when slow, red when very slow), "Offline" when the computer has no network, and "No response" when the database stops answering. Click it to check now + + +## [2.7.5] - 2026-11-14 + +### Bug Fixes +- Provider panels (Neon, Supabase, Prisma, PlanetScale, Turso, Nile, Upstash, Railway, PostHog) retry a request once when it never got an answer, so a slow or briefly dropped connection no longer shows as "couldn't reach the provider". Creates are never repeated after a timeout + + +## [2.7.4] - 2026-11-14 + +### Improvements +- Buttons respond to a press (a small scale on every variant, ghost buttons too), the sidebar and AI footer icons get a hover background and a press, and the welcome search bar lifts on hover +- The sidebar's table search has room between the magnifier and the text, and reads as a field at rest +- Updates work like T3 Code: checking spins the update button instead of opening a dialog, "up to date" and failures are toasts (with Retry), and a found update turns the button into an Update pill that becomes Restart once downloaded + + +## [2.7.3] - 2026-11-14 + +### Bug Fixes +- Fields side by side line up: label rows have one fixed height whether they hold just a label or a label with buttons (Cloudflare D1's Account and D1 database, the provider database picker) +- Cloudflare D1 no longer opens the only database on the account by itself; you pick it + + +## [2.7.2] - 2026-11-14 + +### Features +- What's new after this update leads with AI chat in a tab (with a button to turn it on), one-click Docker databases, the leader key, and Oracle plus paste-any-connection-string +- "What's new" in the command palette opens the release notes any time, not only right after an update + + +## [2.7.1] - 2026-11-14 + +### Features +- Leader key: press ⌘; (Ctrl+; on Windows and Linux), then one key. A panel lists what can follow, so nothing has to be remembered: 1–9 go to a tab, S new SQL query, A AI chat, T terminal, E schema diagram, C connections, K command palette, B sidebar, L logs, W close tab, R reopen closed tab, "," settings, "/" all shortcuts. The leader is rebindable in Keyboard shortcuts + + +## [2.7.0] - 2026-11-14 + +### Features +- Open more than one terminal: "New terminal tab" (Ctrl+Shift+`, the + in the terminal's bar, or the command palette) starts another session in its own tab, numbered Terminal 2, Terminal 3…; closing a tab ends only its own session + + +## [2.6.9] - 2026-11-14 + +### Improvements +- Relation tree: cards are quiet raised surfaces with a row-count bar (log scale, so sizes compare at a glance) and "used by N" instead of the colour strips and ↑↓ counters; lines are thinner and rounded; a faint dot grid pans and zooms with the drawing + + +## [2.6.8] - 2026-11-14 + +### Bug Fixes +- Opening the schema diagram, relation tree or diagrams no longer closes the sidebar; it stays as you left it (⌘B hides it) + + +## [2.6.7] - 2026-11-14 + +### Improvements +- AI mode's chat list matches the sidebar's Chats: each chat shows the database and schema it worked in over its title, and an empty list offers to start one + + +## [2.6.6] - 2026-11-14 + +### Bug Fixes +- A request sent without an API key says "No API key was sent" and offers AI settings, instead of "The provider rejected the API key" with OpenRouter's "No cookie auth credentials found" +- The AI error shows as a card in the composer's column instead of a red bar across the window + +### Improvements +- Model picker: Claude Code shows Claude's own mark, Ollama its own; moving the pointer across the provider icons no longer swaps the list under you; the search box is a proper combobox for screen readers; "Full access" is named as such and shown in amber +- AI models and Add model: one header and footer like the other dialogs; the provider step is a list with each provider's logo and one line on what it is, and picking one moves straight on +- The composer has a clear edge and a focus ring, the model chip and send button line up at one height, and the "↵ to send" hint is gone + + +## [2.6.5] - 2026-11-14 + +### Bug Fixes +- Oracle, MongoDB, CouchDB, Cassandra/Scylla and DynamoDB Local containers now show up under Docker in the connection dialog, with their credentials filled in + + +## [2.6.4] - 2026-11-14 + +### Bug Fixes +- AI chat with a local agent no longer spins forever when a tool never answers: after 30 seconds it says which step it is waiting on, and after 3 minutes it stops the agent and ends the turn with the reason +- When a license is on all the devices it covers, the message says where to free one or ask for more devices (stroke.click/app) +- Settings: one line under each section heading instead of two +- The sign-in prompt for license-key holders says what signing in gives you: connections, workspaces and settings on every device, end-to-end encrypted +- Shift + mouse wheel scrolls sideways everywhere: the related-rows panel, query results, settings and every other wide area, not only the data grid +- Shift + wheel over the related-rows panel no longer scrolls the grid behind it + +### Improvements +- Create database, Report an issue and About share one calm dialog frame: a title with one line under it, the content, and a ruled footer with a single filled action. Decorative icon tiles, boxed rows and the full-width Deactivate bar are gone; Report an issue picks its kind from a segmented control + +### Features +- Settings has an Account & Sync tab: your account, plan and license, and end-to-end encrypted sync (set up, unlock, status, Sync now) in one place + + +## [2.6.3] - 2026-11-14 + +### Changes +- fix(connect): buttons on the traffic lights' line; full-width connection screen (#156) + + +## [2.6.2] - 2026-11-14 + +### Bug Fixes +- AI chats belong to the connection, not the database: switching databases keeps your chats, and SQLite files, MongoDB, D1, DynamoDB and Firestore connections no longer share one chat list +- Connection screen: the show-connections and close buttons sit in a title row on the traffic lights' line on macOS (and in a 40px row on Windows and Linux), instead of floating at an offset +- Connection screen uses the window's width, left-aligned with the connections list, instead of a narrow centred column +- A pasted redis:// or libsql:// URL fills the Redis or LibSQL form correctly instead of being read as a Postgres URL +- Provider panel: an account with one database no longer connects to it unasked; you pick it +- A database just created on Prisma Postgres (or any provider) is connected once it takes connections, instead of failing with "Failed to connect to upstream database" the first time +- Region and organization in "New database" are searchable, like the database list +- The generated password shows in one field with its copy button inside it + +### Features +- AI chat in a tab (beta, Settings → AI chat in a tab): AI opens as a tab beside your tables instead of taking over the window, and its chats are listed in the sidebar with the database and schema each one worked in, and "Working" while one replies +- The AI can run statements in another database on the same server (a database it just created, or one you name) instead of asking you to switch connections +- ⌘K → Docker starts any of 18 databases in one step, each with its brand icon: PostgreSQL 17 and 16, pgvector, PostGIS, TimescaleDB, MySQL, MariaDB, SQL Server, Oracle Free, CockroachDB, MongoDB, Redis, Valkey, CouchDB, DynamoDB Local, Cassandra, ClickHouse and a libSQL server. It picks a free port, waits until the database takes connections, and opens it +- ⌘K → Docker lists the database containers already running, to open in one step +- Real brand marks for MongoDB, Cassandra, CouchDB, Oracle, DynamoDB and Firestore +- Paste any connection string to connect: whole .env lines (DATABASE_URL="…"), SQLAlchemy/ORM URLs (postgresql+psycopg2://), libpq key/value (host=… dbname=…), ADO.NET SQL Server strings (Server=…;Database=…;), valkey://, couchdb://, cassandra://, mysqlx://, Turso https:// and ClickHouse http:// endpoints, and TiDB Cloud, Upstash, Nile and Railway hosts + + +## [2.6.1] - 2026-11-14 + +### Bug Fixes +- Oracle connections on macOS no longer fail with "Oracle Instant Client is not installed" when Instant Client is installed +- Oracle: the status bar shows the container you are in (FREEPDB1) instead of "No database", and warns when a blank service name put you in the root container (CDB$ROOT), which holds only Oracle's own schemas +- Oracle: "Hide system tables" hides the tables Oracle maintains itself (AQ$_, LOGMNR, MVIEW$ and the rest) +- Oracle: paging, sorting and filtering a table no longer re-read the catalog each time (3.5 s to a few ms once the table is open) +- The SQL filter bar applies on Enter again; a typed-out word before the cursor made Enter insert a space instead +- The Oracle terminal no longer starts GNU Parallel's `sql` command in place of SQLcl, and it finds SQL*Plus inside the Instant Client folder and SQLcl installed from the Homebrew cask + +### Features +- Keyboard shortcuts (⌘/ or Ctrl+/) opens as a tab next to your tables and queries instead of covering the window; with no connection open it still opens as a full page + + +## [2.6.0] - 2026-11-14 + +### New Features + +#### Connections +- **Oracle Database** - Connect to Oracle 12c and later by host, port and service name or SID, or with an Easy Connect / TNS connect string (an `oracle://` or `jdbc:oracle:thin:` string pastes straight into the form). Browse schemas and tables with row counts, open tables with search, filters, sort and pagination, edit, insert and delete rows on tables with a primary key, and run SQL and PL/SQL scripts (`/` lines and all) in the editor, with Stop that interrupts the running statement. Read-only mode and the agent access levels cover Oracle too. Uses Oracle Instant Client, a free download from Oracle: when it is missing, the connection form says where to get it and lets you point at its folder. + + +## [2.5.9] - 2026-11-14 + +### New Features + +#### AI Chat +- **Plan checklist** - a multi-step request opens with a short numbered plan that ticks off as the steps finish +- **Step timeline** - each tool call is one row with a status icon, a plain label ("Create table customers", "Insert 3 rows into orders"), its duration, and the SQL and result a click away. A retry folds under the step it fixes ("Retried · succeeded") +- **Turn summary** - a finished turn says what changed ("Created database ecommerce · Created 3 tables · Inserted 12 rows into 3 tables"), with a button to copy every statement it ran + +### Bug Fixes +- AI chat on MySQL and TiDB: a statement batch sent in one tool call (`CREATE DATABASE shop; USE shop; CREATE TABLE …`) no longer fails as a whole. MySQL refused it with a syntax error and TiDB with "Can not prepare multiple statements"; the statements now run one by one on one connection, and a failure says which statement broke and which ones already ran +- MySQL and TiDB: a `USE` no longer leaks into the connection pool. The connection that ran it went back to the pool pointed at the other database, so later statements landed there at random and failed with "Table 'ecommerce.customers' doesn't exist". A lone `USE` now also says it only applies to that call +- AI chat: a failed SQL step shows the database's own error in red under the step, and the model gets the full error, a hint, and an instruction to fix and retry. A double-quoted string on MySQL (read as a column under ANSI_QUOTES) now gets a hint that says so + + +## [2.5.8] - 2026-11-14 + +### New Features + +#### Connections +- **Create a database from the provider panel** - After signing in to Neon, Supabase, Prisma Postgres, TiDB Cloud, Turso, Nile, Upstash or Cloudflare D1, "Create database" (or "New database" above the list) asks for a name and region, waits while the provider sets it up, then connects to it. Supabase gets a generated password, shown once with a copy button. PlanetScale and Railway keep a link to their dashboard, with the reason. + + +## [2.5.7] - 2026-11-14 + +### New Features +- Windows: the terminal installs psql and mysql with one click (PostgreSQL or MariaDB through winget, the server included) + +### Bug Fixes +- Windows and Linux: Ctrl+D, Ctrl+X and the terminal's other Ctrl keys stay in the terminal instead of also running app shortcuts (Ctrl+D opened Switch database) +- The built-in prompt handles Ctrl+D (delete under the cursor) and Ctrl+X (cut the line) + + +## [2.5.6] - 2026-11-14 + +### New Features +- Ctrl+Alt+T (Cmd+Alt+T) opens the theme picker +- The terminal says when a client isn't installed in a bar above the prompt, with Install or How to install + +### Bug Fixes +- Windows and Linux: a 40px title bar with Windows-style minimize, maximize and close buttons +- Zooming in no longer pushes the connection page's footer over the status bar +- A failed connection is no longer saved or synced; it is saved once it connects +- TiDB Cloud and other providers: Connect waits for a database to be picked instead of dialling 127.0.0.1 +- Switching or previewing themes repaints at once, without the lag on Windows and Linux +- The command palette's footer no longer casts a heavy shadow over the list + +### Changes +- What's new leads with one feature, lists the rest, and folds the full release notes + + ## [2.5.5] - 2026-11-14 ### New Features diff --git a/README.md b/README.md index 353b6fb3..eac64e0b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Stroke is a Rust + Svelte app for browsing, editing and querying databases. It s ## Databases -**Engines:** PostgreSQL, MySQL, MariaDB, CockroachDB, SQLite, Turso / libSQL, Cloudflare D1, ClickHouse, DuckDB, SQL Server and Redis. +**Engines:** PostgreSQL, MySQL, MariaDB, CockroachDB, SQLite, Turso / libSQL, Cloudflare D1, ClickHouse, DuckDB, SQL Server, Oracle (with Oracle Instant Client installed) and Redis. **Sign in and pick a database**, no connection string needed: @@ -60,6 +60,8 @@ scoop install stroke Or grab an installer from [Releases](https://github.com/stroke-app/stroke/releases): `.dmg` for macOS (Apple Silicon or Intel), `-setup.exe` for Windows, `.deb` (Ubuntu 22.04+, Debian 12+), `.rpm` (Fedora 41+, RHEL 10) or `.AppImage` (Arch and any distro with glibc 2.35+) for Linux. +On Wayland, Stroke runs natively. If a launch can't draw there (some GPU and driver setups), the next one falls back to X11 by itself. `STROKE_X11=1` forces X11; `STROKE_WAYLAND=1` forces Wayland and forgets the fallback. + **Stroke Nightly** is built every night from the `nightly` branch and installs as a separate app beside Stroke, with its own settings, so you can try what's coming without risking your setup: [download the nightly](https://github.com/stroke-app/stroke/releases/tag/nightly). It updates itself every night something lands. If macOS blocks the first launch, run `xattr -cr /Applications/Stroke.app`. If Windows SmartScreen warns, choose **More info → Run anyway**. diff --git a/TODO_TODAY.md b/TODO_TODAY.md index c42bfff4..5cc79570 100644 --- a/TODO_TODAY.md +++ b/TODO_TODAY.md @@ -1,66 +1,127 @@ # TODO -What is open as of 2026-11-14 (master at 2.5.5). Security items live in -`TODO_SECURITY.md`. +What is open as of 2026-11-19 (nightly at 2.20.x, stable at 2.3.0). Security +items live in `TODO_SECURITY.md`. -## Before the next stable release +## v3 -- [ ] Test the 2.5.5 Windows build: theme switching and zoom without flicker, - Ctrl+K rebound to Alt+K working in the SQL editor, the title bar with the - sidebar on the right. -- [ ] Test offline Pro on macOS: sign in as Pro, turn Wi-Fi off, Pro stays on. -- [ ] Ship it: promote `nightly` to `master` (see CONTRIBUTING.md). +Stable has been on 2.3.0 since 2026-11-02, with 273 commits on `nightly` +since. Every Stable user jumps across all of it in one update, so v3 is a +release with a freeze and a test pass, not a plain promote. In order: -## Next - -- [ ] **AI access levels for built-in models.** The Read only / Read & write - control exists for local agents only (enforced by the MCP server). Built-in - models (Stroke Free, API keys) can run writes, asking only before - destructive ones. Enforce a level in `AiChat`'s `execute_sql` with the - same classifier as `src-tauri/src/mcp/access.rs`, then show the control. -- [ ] **echarts 6** (PR #14): clears the echarts XSS advisory; the charts need +- [x] **Feature freeze.** Fixes only on `nightly` until v3 ships. +- [x] **Decide what leaves Labs.** Decided for 3.0: AI chat in a tab stays + in Labs but is on by default (turned on once for everyone); the tree + sidebar and several connections stay opt-in. + Was: Several connections at once and the tree + sidebar: graduate both to the default (they are what the site and the + email show) and test them as the default on every engine, or ship v3 + with them still opt-in. +- [ ] **Test the jump from 2.3.0.** Install 2.3.0 with real saved connections, + settings, saved queries, a theme and a Pro license, then update to the + v3 build through the updater. Connections, keychain passwords, + settings, query history, notebooks and Pro all have to survive. On + macOS, Windows and Linux: the updater is what breaks first. +- [x] **AI access levels for built-in models.** Done in 2.20.4: every + execute_sql is classified by `access.rs` and refused above the level. + Was: The Read only / Read & write + control exists for local agents only (enforced by the MCP server). + Built-in models (Stroke Free, API keys) can run writes, asking only + before destructive ones. Enforce a level in `AiChat`'s `execute_sql` + with the same classifier as `src-tauri/src/mcp/access.rs`, then show + the control. +- [x] **echarts 6** (on 6.1.0, audit clean): clears the echarts XSS advisory; the charts need testing. -- [ ] Decide on the old PRs: #105 (license wording), #91 (ship-safely gate), - #56 and #54 (older feature/tooltip work, likely superseded). -- [ ] RHEL / Rocky / Alma 9 can't run Stroke (no WebKitGTK 4.1). Documented in - the README and release notes; nothing to do unless that changes. - -## Performance pass (paused) - -The render and memory work is on `perf/render-and-memory` (last commit -2026-08-31, not merged, 14 commits ahead). Its notes are in that branch's -`TODO_TODAY.md`; several items there are obsolete since the editor moved -from Monaco to CodeMirror. Rebase onto `nightly` and re-measure before -picking it up. - -## Themes: follow-ups (2026-11-12) - -Glass (`glass-dark` / `glass-light`) and the Custom theme with opacity below -100% are macOS-only today: `set_window_glass` in `src-tauri/src/commands.rs` -is a no-op elsewhere, and every glass rule in `src/lib/themes/app-themes.css` -is scoped to `html[data-os="macos"]`. - -- [ ] **Glass on Windows (Mica / Acrylic).** Needs testing on a real Windows - build (`npm run tauri:build:win`), not just a compile. - - Create the window with `.transparent(true)` on Windows too - (`src-tauri/src/lib.rs`, the window builder). - - In `set_window_glass`, add a Windows branch: `Effect::Mica`, with - `Effect::Acrylic` as a fallback before Windows 11. - - Let the WebView2 backdrop go transparent while glass is on. - `set_windows_webview_backdrop` in `lib.rs` currently forces alpha to opaque. - - Widen the CSS scope to `html:is([data-os="macos"], [data-os="windows"])`. - - Set `data-glass` on Windows too: `syncWindowGlass` in - `src/lib/stores/settings.js` only does it for macOS. - - Risk: the Windows window is hidden until first paint specifically to avoid - a startup flicker. Re-check that it still opens cleanly. -- [ ] **Page-by-page pass in Glass and in Custom with opacity below 100%.** - The structural cases are already fixed: - - sticky headers, overlays and fixed panels (frosted rule in - `app-themes.css`) - - the data grid's erase fills and `withAlpha` - - the terminal - - the ERD and map colour probes - - What's left are one-off components that paint their own solid colour and - stand out as patches over the glass. Go through each page, screenshot the - odd ones, and fix them per component. +- [ ] **Real hardware.** Windows: theme switching and zoom without flicker, + Alt+K in the SQL editor, the title bar with the sidebar on the right, + the new fonts, and a click through every page. macOS: offline Pro (sign + in as Pro, Wi-Fi off, Pro stays on). +- [ ] **Faster startup.** Lazy-load the heavy pages: the entry is 1.3MB and + 143 chunks (1.5MB, the DataTable alone 405KB) preload at startup. New + users on Windows notice this first. +- [ ] **The license change**, settled before the release: the same wording on + the site's legal pages, in the app (license page, About) and in the + email. +- [x] **Release notes: what changed since 2.3.0.** Written once, used for the + release page, the in-app What's new, the email and the launch posts. +- [x] **Rollback plan.** If v3 breaks on day one, run + `gh release edit v2.3.0 --repo stroke-app/stroke --latest`: the Stable + updater reads `releases/latest/download/latest.json`, so nobody else + moves to v3. The updater never downgrades, so anyone already on v3 needs + a 3.0.1 fix. + Before: Keep the 2.3.0 installers easy to find, and know how + to point the Stable updater back at 2.3.0 if v3 breaks something on day + one. +- [ ] **Release candidate.** A few days of daily use on Nightly with nothing + new landing. +- [x] **Ship it.** Label the release PR `release:major` (CONTRIBUTING.md), then + Actions, *Promote nightly to stable*. + +## Next up + +- [x] **Layout control.** Dropped; the SQL results bottom/right switch + replaced it (2.19.x). Was: Three buttons in the title bar for the sidebar (⌘B), + the terminal panel (⌘J) and the docked AI panel (⌘⌥B), the open edge + filled. Built on the `layout-and-dashboard` branch; check it in the + running app on every OS before merging. +- [x] **Dashboard tab redesign.** Shipped, plus a dashboard window. Was: A 12-column grid of chart cards, each with a + status dot, refresh, an auto-refresh interval, a title and its error on + the card. Resize and rearrange cards, add one from any saved query, and + save the layout. +- [ ] **Admin metrics page** on stroke.click, for me only: sign-ups, active + trials, trial-to-Pro conversion, revenue, downloads, active devices, + app versions in use, countries, backups and sponsors, over time. +- [x] **Local database servers.** Shipped as their own window. Was: Postgres, MySQL and Redis side by side in + any version, with Start, Stop, the port and Open, per server on Docker + or as native binaries; Homebrew services listed too. + +## Website + +The site is `stroke-web` (TanStack Start on Cloudflare Workers). + +- [ ] **Redesign the whole site.** Every page, not a refresh of the home page: + home, features, pricing, download, databases, compare, docs, changelog, + blog and the legal pages. Keep the app's look (dark first, the same type + scale and accent) so the site and the app read as one product. +- [ ] **New pictures.** Fresh screenshots of the app as it is now: the tree + sidebar, several databases open at once, the data grid, the AI chat, ER + diagrams, the sign-in flows. Light and dark, the same window size. +- [ ] **A demo on the site.** A short screen recording of real use (connect, + browse, query, ask the AI) near the top of the home page, with a poster + frame so the page doesn't wait on the video. +- [ ] **Measure it.** Downloads per OS and trial-to-Pro conversion, so the + redesign has numbers to be judged by. + +## Email + +Mail goes out through Plunk (`stroke-web/src/lib/mail`). There are templates +for purchases and failed payments only; nothing sends to every user yet. + +- [ ] **Major update and What's new.** One email to every user about the big + release: what changed since 2.3.0, with the new pictures and a link to + the changelog and the download. Send it when the release is on Stable. +- [ ] **License update.** One email to license holders about the license + change. Decide the wording first; it has to match what the site's legal + pages and the app say. +- [ ] Both need a broadcast path: a template per email, the recipient list + (users, or license holders only), an unsubscribe link, and a test send to + myself before the real one. + +## Launch week + +After v3 is on Stable: the website, then the emails, then the posts, the +same week. + +- [ ] **First-run tour in the app.** Everyone the launch brings in lands on + the empty connect page. A sample database (a bundled SQLite file) that + opens in one click, and a short tour of the tree sidebar, the AI chat + and the command palette. +- [ ] **Launch posts.** Product Hunt, Show HN, r/PostgreSQL and r/SQL, the + same day as the email, with the demo recording and the new pictures. + +## After v3 + +- [ ] **Schema diff** between two databases: tables, columns, indexes and + constraints that differ, and the SQL that brings one up to the other. +- [ ] **Query parameters**: `:name` / `$1` in a query asks for values (and + remembers the last ones) before it runs. diff --git a/cli/README.md b/cli/README.md new file mode 100644 index 00000000..08ab24eb --- /dev/null +++ b/cli/README.md @@ -0,0 +1,57 @@ +# strokedb + +Open your project's database in [Stroke](https://stroke.click). + +```sh +npx strokedb +``` + +- **Stroke installed?** The app opens with a New Connection form filled in from your project. Check it and press Connect. +- **No app?** [stroke.click/studio](https://stroke.click/studio) opens in your browser with your tables, rows and a SQL console (PostgreSQL, MySQL, MariaDB and SQLite). + +## Where it finds the database + +1. An argument: `npx strokedb postgres://user:pass@localhost:5432/app` or `npx strokedb ./dev.db` +2. `DATABASE_URL` (or `POSTGRES_URL`, `MYSQL_URL`, `DB_URL`, ...) in the environment +3. The same keys in `.env`, `.env.development`, `.env.local`, `.env.development.local` +4. A `.db` / `.sqlite` file in the folder, or in `prisma/`, `data/` or `db/` + +`file:./dev.db` URLs (Prisma, Drizzle) resolve against the project folder. + +## Options + +| Flag | | +| --- | --- | +| `--web` | Use the web studio even if the app is installed | +| `--app` | Use the app; fail if it isn't installed | +| `--name ` | Name for the connection (default: the folder name) | +| `--port ` | Port for the local bridge (default: any free port) | +| `--no-open` | Print the studio link instead of opening a browser | +| `--share` | (Labs) Also print a link that opens this studio on any device. Read-only. Needs Node 22+ | +| `--write` | With `--share`: let the link edit rows and run any SQL | + +## How the web studio is kept private + +The browser can't open a database connection, so `strokedb` runs a small bridge on `127.0.0.1` and the studio page sends it queries. The bridge: + +- listens on loopback only; +- answers only `https://stroke.click`, and only requests carrying a random token that lives in the link's `#fragment` (a fragment is never sent to a server); +- rejects any other `Host`, so a DNS-rebinding page can't reach it; +- stops when you press Ctrl+C. + +Your queries go from your browser to your machine. They never pass through stroke.click. + +Safari blocks secure pages from calling `127.0.0.1`; use Chrome, Edge, Firefox or the desktop app. + +SQLite in the web studio needs Node 22.5 or newer. + +## Sharing (Labs) + +`npx strokedb --share` prints a second link that opens the studio on any device, for anyone you send it to, while the command runs. + +- It goes through a relay at stroke.click: this command holds a connection open to it, and the studio page sends its calls there. Nothing has to be reachable from the internet. +- End-to-end encrypted. Every call is AES-256-GCM with a key that exists only in the link's `#fragment`; the relay passes ciphertext along and can't read it. +- Read-only unless you add `--write`: browsing, and SQL that only reads (SELECT, WITH, EXPLAIN, SHOW). The shared side has its own read-only connection (Postgres `BEGIN READ ONLY`, MySQL `START TRANSACTION READ ONLY`, SQLite opened read-only), so a statement can't talk its way into a write. +- Stop it with Ctrl+C; the link stops working until you share again (a new run makes a new link). + +The desktop app can share an open connection the same way: turn on Settings → Labs → Share as a web studio, then pick **Share as web studio** in the command palette. diff --git a/cli/bin/strokedb.js b/cli/bin/strokedb.js new file mode 100644 index 00000000..01dca2a2 --- /dev/null +++ b/cli/bin/strokedb.js @@ -0,0 +1,10 @@ +#!/usr/bin/env node +import { main } from '../src/cli.js' + +main(process.argv.slice(2)).then( + (code) => process.exit(code ?? 0), + (e) => { + console.error(e instanceof Error ? e.message : e) + process.exit(1) + }, +) diff --git a/cli/package.json b/cli/package.json new file mode 100644 index 00000000..1b38fbb8 --- /dev/null +++ b/cli/package.json @@ -0,0 +1,40 @@ +{ + "name": "strokedb", + "version": "0.3.0", + "description": "Open your project's database in Stroke: the desktop app if it's installed, otherwise the web studio at stroke.click/studio", + "license": "SEE LICENSE IN LICENSE", + "homepage": "https://stroke.click/studio", + "repository": { + "type": "git", + "url": "https://github.com/stroke-app/stroke", + "directory": "cli" + }, + "keywords": [ + "database", + "studio", + "postgres", + "mysql", + "sqlite", + "gui", + "stroke" + ], + "type": "module", + "bin": { + "strokedb": "bin/strokedb.js" + }, + "files": [ + "bin", + "src", + "README.md" + ], + "engines": { + "node": ">=20.19" + }, + "scripts": { + "test": "node --test" + }, + "dependencies": { + "mysql2": "^3.15.3", + "pg": "^8.16.3" + } +} diff --git a/cli/src/app.js b/cli/src/app.js new file mode 100644 index 00000000..9fbbad84 --- /dev/null +++ b/cli/src/app.js @@ -0,0 +1,88 @@ +// Is the Stroke desktop app installed, with stroke:// links? Older builds have +// no link handler, so "installed" means the scheme is registered, not just that +// the app is there - opening an unhandled link fails with an OS error dialog. + +import fs from 'node:fs' +import os from 'node:os' +import path from 'node:path' +import { execFileSync, spawn } from 'node:child_process' + +/** Stable first: a link to it opens the app most people have. */ +export const SCHEMES = ['stroke', 'stroke-nightly'] + +const MAC_APPS = { stroke: 'Stroke.app', 'stroke-nightly': 'Stroke Nightly.app' } + +/** + * @param {string} cmd + * @param {string[]} args + */ +function run(cmd, args) { + try { + return execFileSync(cmd, args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'], timeout: 5000 }) + } catch { + return '' + } +} + +/** @param {string} scheme */ +function handlesScheme(scheme) { + switch (process.platform) { + case 'darwin': { + const name = MAC_APPS[/** @type {keyof typeof MAC_APPS} */ (scheme)] + for (const dir of ['/Applications', path.join(os.homedir(), 'Applications')]) { + try { + const plist = fs.readFileSync(path.join(dir, name, 'Contents', 'Info.plist'), 'utf8') + if (plist.includes(`${scheme}`)) return true + } catch { + // not there + } + } + return false + } + case 'win32': + return /URL Protocol/i.test(run('reg', ['query', `HKCU\\Software\\Classes\\${scheme}`])) || + /URL Protocol/i.test(run('reg', ['query', `HKCR\\${scheme}`])) + default: + return run('xdg-mime', ['query', 'default', `x-scheme-handler/${scheme}`]).trim() !== '' + } +} + +/** The first scheme an installed Stroke answers to, or null. */ +export function installedScheme() { + return SCHEMES.find(handlesScheme) ?? null +} + +/** + * @param {string} scheme + * @param {string} uri + * @param {string} [name] + */ +export function appLink(scheme, uri, name) { + const q = new URLSearchParams({ url: uri }) + if (name) q.set('name', name) + return `${scheme}://open?${q}` +} + +/** + * Hand a URL to the OS: the browser for https, the app for stroke://. + * @param {string} url + */ +export function openUrl(url) { + const [cmd, args] = + process.platform === 'darwin' ? ['open', [url]] + // `start` reads `&` as a command separator; rundll32 takes the URL whole. + : process.platform === 'win32' ? ['rundll32', ['url.dll,FileProtocolHandler', url]] + : ['xdg-open', [url]] + return new Promise((resolve) => { + try { + const child = spawn(cmd, args, { stdio: 'ignore', detached: true }) + child.on('error', () => resolve(false)) + child.on('spawn', () => { + child.unref() + resolve(true) + }) + } catch { + resolve(false) + } + }) +} diff --git a/cli/src/bridge.js b/cli/src/bridge.js new file mode 100644 index 00000000..8a572751 --- /dev/null +++ b/cli/src/bridge.js @@ -0,0 +1,211 @@ +// The local bridge the web studio talks to. The browser can't open a database +// socket, so the page sends JSON here and this runs it on the project's +// database. +// +// It runs whatever SQL it is sent, so it is locked down to one page, one +// session and this machine: +// - listens on 127.0.0.1 only; +// - every call carries a random token that exists only in the URL fragment +// of the studio tab this process opened (a fragment never reaches a server); +// - the Host header must be the loopback address it listens on, so a DNS +// rebinding page can't borrow the browser to reach it; +// - CORS answers only the studio's origin. + +import http from 'node:http' +import crypto from 'node:crypto' + +export const STUDIO_ORIGIN = 'https://stroke.click' +/** Bumped with the package; the page reads `features` to know what it may offer. */ +export const BRIDGE_VERSION = '0.3.0' +export const FEATURES = ['structure', 'where', 'edit', 'rowCounts'] +const MAX_BODY = 1 << 20 + +/** + * @param {{ + * engine: import('./engines.js').Engine, + * name: string, + * origins?: string[], + * port?: number, + * token?: string, + * }} o + * @returns {Promise<{ port: number, token: string, close(): Promise, server: http.Server }>} + */ +export async function startBridge({ engine, name, origins = [STUDIO_ORIGIN], port = 0, token = crypto.randomBytes(24).toString('base64url') }) { + const want = Buffer.from(token) + /** @param {string | undefined} got */ + const tokenOk = (got) => { + const b = Buffer.from(String(got ?? '').replace(/^Bearer\s+/i, '')) + return b.length === want.length && crypto.timingSafeEqual(b, want) + } + + const routes = makeRoutes({ engine, name }) + + /** @type {number} */ + let boundPort = 0 + const server = http.createServer(async (req, res) => { + const origin = req.headers.origin + const allowed = typeof origin === 'string' && origins.includes(origin) + if (allowed) { + res.setHeader('Access-Control-Allow-Origin', origin) + res.setHeader('Vary', 'Origin') + } + // DNS rebinding: a page on evil.test resolving to 127.0.0.1 sends its own Host. + const host = String(req.headers.host ?? '') + if (host !== `127.0.0.1:${boundPort}` && host !== `localhost:${boundPort}`) return send(res, 421, { error: 'Wrong host' }) + + if (req.method === 'OPTIONS') { + if (!allowed) return send(res, 403, { error: 'Origin not allowed' }) + res.setHeader('Access-Control-Allow-Methods', 'POST, OPTIONS') + res.setHeader('Access-Control-Allow-Headers', 'authorization, content-type') + // Chrome's Private Network Access: a public page asking for loopback. + if (req.headers['access-control-request-private-network']) res.setHeader('Access-Control-Allow-Private-Network', 'true') + res.setHeader('Access-Control-Max-Age', '600') + res.writeHead(204).end() + return + } + if (!allowed) return send(res, 403, { error: 'Origin not allowed' }) + if (req.method !== 'POST') return send(res, 405, { error: 'POST only' }) + if (!tokenOk(req.headers.authorization)) return send(res, 401, { error: 'Bad token' }) + const route = routes[new URL(req.url ?? '/', 'http://x').pathname] + if (!route) return send(res, 404, { error: 'Not found' }) + try { + const body = await readJson(req) + send(res, 200, await route(body)) + } catch (e) { + send(res, 400, { error: e instanceof Error ? e.message : String(e) }) + } + }) + + await new Promise((resolve, reject) => { + server.once('error', reject) + server.listen(port, '127.0.0.1', () => resolve(undefined)) + }) + boundPort = /** @type {import('node:net').AddressInfo} */ (server.address()).port + return { + port: boundPort, + token, + server, + close: () => new Promise((resolve) => { + server.closeAllConnections?.() + server.close(() => resolve()) + }), + } +} + +/** + * What the studio can call, by path. The local bridge and the share relay + * both answer from this. A shared studio that isn't allowed to write gets no + * edit routes, and its SQL console takes reading statements only (the engine + * it is given runs those read-only as well). + * @param {{ engine: import('./engines.js').Engine, name: string, shared?: boolean, write?: boolean }} o + * @returns {Record Promise>} + */ +export function makeRoutes({ engine, name, shared = false, write = true }) { + /** @type {Record Promise>} */ + const routes = { + '/info': async () => ({ + ...(await engine.info()), + name, + bridge: BRIDGE_VERSION, + features: write ? FEATURES : [...FEATURES.filter((f) => f !== 'edit'), 'readonly'], + ...(shared ? { shared: true, readonly: !write } : {}), + }), + '/tables': () => engine.tables(), + '/columns': (b) => engine.columns(str(b.schema), str(b.table)), + '/structure': (b) => engine.structure(str(b.schema), str(b.table)), + '/rows': (b) => engine.rows({ + schema: str(b.schema), + table: str(b.table), + limit: Number(b.limit) || 100, + offset: Number(b.offset) || 0, + sort: b.sort ?? null, + where: typeof b.where === 'string' ? b.where : '', + }), + '/update': (b) => engine.update({ schema: str(b.schema), table: str(b.table), key: obj(b.key), set: obj(b.set) }), + '/insert': (b) => engine.insert({ schema: str(b.schema), table: str(b.table), values: obj(b.values) }), + '/delete': (b) => { + if (!Array.isArray(b.keys) || !b.keys.length) throw new Error('Expected keys') + return engine.remove({ schema: str(b.schema), table: str(b.table), keys: b.keys.map(obj) }) + }, + '/query': async (b) => { + const t = performance.now() + const r = await engine.query(str(b.sql)) + return { ...r, durationMs: Math.round(performance.now() - t) } + }, + } + + if (!write) { + delete routes['/update'] + delete routes['/insert'] + delete routes['/delete'] + const query = routes['/query'] + routes['/query'] = async (b) => { + if (!isReadingSql(str(b.sql))) throw new Error('This shared studio is read-only: run SELECT, WITH, EXPLAIN or SHOW.') + return query(b) + } + } + return routes +} + +/** + * A statement that only reads. The engine runs it read-only anyway; this + * turns away COPY, CALL, DO, ATTACH and friends before they reach it. + * @param {string} sql + */ +export function isReadingSql(sql) { + const text = sql.replace(/^\s*(?:(?:--[^\n]*\n)|(?:\/\*[\s\S]*?\*\/)|\s)*/, '') + const word = (text.match(/^[a-z]+/i)?.[0] ?? '').toLowerCase() + if (word === 'pragma') return !text.includes('=') + return ['select', 'with', 'explain', 'show', 'values', 'table', 'describe', 'desc'].includes(word) +} + +/** @param {unknown} v @returns {Record} */ +function obj(v) { + if (!v || typeof v !== 'object' || Array.isArray(v)) throw new Error('Expected an object') + return /** @type {Record} */ (v) +} + +/** @param {unknown} v */ +function str(v) { + if (typeof v !== 'string') throw new Error('Expected a string') + return v +} + +/** @param {http.IncomingMessage} req */ +function readJson(req) { + return new Promise((resolve, reject) => { + let size = 0 + /** @type {Buffer[]} */ + const chunks = [] + req.on('data', (c) => { + size += c.length + if (size > MAX_BODY) { + reject(new Error('Request too large')) + req.destroy() + } else chunks.push(c) + }) + req.on('end', () => { + try { + resolve(chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : {}) + } catch { + reject(new Error('Body is not JSON')) + } + }) + req.on('error', reject) + }) +} + +/** @param {http.ServerResponse} res @param {number} status @param {unknown} body */ +function send(res, status, body) { + if (res.headersSent) return + res.writeHead(status, { 'content-type': 'application/json; charset=utf-8', 'cache-control': 'no-store' }) + res.end(JSON.stringify(body)) +} + +/** + * The studio page for this bridge. Everything after `#` stays in the browser. + * @param {{ studio: string, port: number, token: string }} o + */ +export function studioUrl({ studio, port, token }) { + return `${studio}#${new URLSearchParams({ bridge: `http://127.0.0.1:${port}`, token })}` +} diff --git a/cli/src/cli.js b/cli/src/cli.js new file mode 100644 index 00000000..c5c546f9 --- /dev/null +++ b/cli/src/cli.js @@ -0,0 +1,156 @@ +import path from 'node:path' +import { parseArgs } from 'node:util' +import { findConnection, engineOf, redact, URL_KEYS } from './find.js' +import { installedScheme, appLink, openUrl } from './app.js' +import { openEngine } from './engines.js' +import { startBridge, studioUrl, makeRoutes, STUDIO_ORIGIN } from './bridge.js' +import { newShare, shareUrl, startRelayHost } from './relay.js' + +const HELP = `strokedb - open your project's database in Stroke + +Usage + npx strokedb [connection] Desktop app if installed, else the web studio + npx strokedb --web [connection] Always the web studio + + connection A URL (postgres://, mysql://, ...) or a SQLite file. Left out, + it's read from ${URL_KEYS.slice(0, 3).join(', ')}, ... in the + environment or .env files, or a .db/.sqlite file in this folder. + +Options + --web Use the web studio even if the app is installed + --app Use the desktop app; fail if it isn't installed + --name Name for the connection (default: this folder's name) + --port Port for the local bridge (default: any free port) + --no-open Print the studio link instead of opening a browser + --share (Labs) Also print a link that opens this studio on any + device, through stroke.click's relay. Read-only: browse + and run SELECTs. End-to-end encrypted; the key is only in + the link. Needs Node 22+. + --write With --share: let the link edit rows and run any SQL + -h, --help Show this help + -v, --version Show the version + +The web studio talks to a bridge on 127.0.0.1 that only answers the tab this +command opens. It stops when you press Ctrl+C.` + +/** @param {string[]} argv */ +export async function main(argv) { + const { values, positionals } = parseArgs({ + args: argv, + allowPositionals: true, + options: { + web: { type: 'boolean' }, + app: { type: 'boolean' }, + name: { type: 'string' }, + port: { type: 'string' }, + 'no-open': { type: 'boolean' }, + share: { type: 'boolean' }, + write: { type: 'boolean' }, + help: { type: 'boolean', short: 'h' }, + version: { type: 'boolean', short: 'v' }, + }, + }) + if (values.help) return void console.log(HELP) + if (values.version) { + const { default: pkg } = await import('../package.json', { with: { type: 'json' } }) + return void console.log(pkg.version) + } + + const cwd = process.cwd() + const found = findConnection({ arg: positionals[0], cwd }) + if (!found) { + console.error(`No database found here.\n\nPass one (npx strokedb postgres://user:pass@host/db), set DATABASE_URL in .env,\nor run this in a folder with a .db or .sqlite file.`) + return 1 + } + const name = values.name ?? path.basename(cwd) + console.log(`Database: ${redact(found.uri)} (from ${found.source})`) + + if (values.write && !values.share) { + console.error('--write goes with --share.') + return 1 + } + // Sharing keeps this process running, so it uses the web studio here too. + if (!values.web && !values.share) { + const scheme = installedScheme() + if (scheme) { + const ok = await openUrl(appLink(scheme, found.uri, name)) + if (ok) { + console.log(`Opened in ${scheme === 'stroke' ? 'Stroke' : 'Stroke Nightly'}. Check the details and press Connect.`) + return 0 + } + console.error('Could not hand the link to the app; using the web studio instead.') + } else if (values.app) { + console.error('The Stroke app is not installed (or is older than the version with stroke:// links).\nGet it at https://stroke.click/download') + return 1 + } + } + + const engine = engineOf(found.uri) + if (!engine) { + console.error(`The web studio speaks PostgreSQL, MySQL and SQLite. For this database, use the app: https://stroke.click/download`) + return 1 + } + quietSqliteWarning() + let db + try { + db = await openEngine(engine, found.uri) + } catch (e) { + console.error(`Could not connect: ${e instanceof Error ? e.message : e}`) + return 1 + } + + const studio = process.env.STROKE_STUDIO_URL || `${STUDIO_ORIGIN}/studio` + const bridge = await startBridge({ + engine: db, + name, + origins: [new URL(studio).origin], + port: values.port ? Number(values.port) : 0, + }) + const link = studioUrl({ studio, port: bridge.port, token: bridge.token }) + const opened = !values['no-open'] ? await openUrl(link) : false + console.log(`\n${opened ? 'Opened' : 'Open'} the studio${opened ? '' : ' at'}:\n ${link}\n\nThe link is the key to this session; don't share it. Press Ctrl+C to stop.`) + + /** @type {{ close(): void } | null} */ + let relay = null + /** @type {import('./engines.js').Engine | null} */ + let sharedDb = null + if (values.share) { + try { + // Its own connection, read-only unless --write: the engine itself + // refuses writes, not just the routes. + sharedDb = values.write ? db : await openEngine(engine, found.uri, { readOnly: true }) + const share = newShare() + relay = await startRelayHost({ + routes: makeRoutes({ engine: sharedDb, name, shared: true, write: !!values.write }), + ...share, + base: new URL(studio).origin, + onStatus: (status, detail) => console.log(status === 'online' ? ' Sharing: online' : ` Sharing: ${detail ?? 'offline'}`), + }) + console.log(`\nShare link (Labs, ${values.write ? 'can edit' : 'read-only'}):\n ${shareUrl({ studio, ...share })}\n\nAnyone with this link can ${values.write ? 'read and change' : 'read'} this database while this runs. It's\nend-to-end encrypted: the key is only in the link, stroke.click sees ciphertext.`) + } catch (e) { + console.error(`Could not start sharing: ${e instanceof Error ? e.message : e}`) + } + } + + await new Promise((resolve) => { + const stop = () => resolve(undefined) + process.once('SIGINT', stop) + process.once('SIGTERM', stop) + }) + relay?.close() + if (sharedDb && sharedDb !== db) await sharedDb.close().catch(() => {}) + await bridge.close() + await db.close().catch(() => {}) + console.log('\nStopped.') + return 0 +} + +/** node:sqlite is marked experimental and says so on every run; it's fine here. */ +function quietSqliteWarning() { + const emit = process.emitWarning.bind(process) + process.emitWarning = /** @type {typeof process.emitWarning} */ ((w, ...rest) => { + const msg = typeof w === 'string' ? w : w?.message + if (/SQLite is an experimental feature/.test(msg ?? '')) return + return emit(/** @type {any} */ (w), .../** @type {any[]} */ (rest)) + }) +} diff --git a/cli/src/engines.js b/cli/src/engines.js new file mode 100644 index 00000000..e1b40cdc --- /dev/null +++ b/cli/src/engines.js @@ -0,0 +1,574 @@ +// The three engines the bridge speaks, behind one interface the web studio +// calls: tables, structure, a page of rows, edits by primary key, and a SQL +// console. + +/** + * @typedef {{ schema: string, name: string, kind: 'table' | 'view', rows: number | null }} TableRef + * @typedef {{ name: string, type: string, nullable: boolean, primaryKey: boolean, default: string | null }} Column + * @typedef {{ name: string, columns: string[], unique: boolean, primary: boolean }} Index + * @typedef {{ name: string, columns: string[], refSchema: string, refTable: string, refColumns: string[] }} ForeignKey + * @typedef {{ columns: Column[], indexes: Index[], foreignKeys: ForeignKey[] }} Structure + * @typedef {{ columns: string[], rows: unknown[][], rowCount?: number | null, command?: string }} Result + * @typedef {{ column: string, dir: 'asc' | 'desc' }} Sort + * @typedef {{ schema: string, table: string, limit: number, offset: number, sort?: Sort | null, where?: string }} RowsQuery + * @typedef {Record} Values + * @typedef {{ sql: string, params: unknown[], atMostOne?: boolean }} Step + * @typedef {{ + * engine: 'postgres' | 'mysql' | 'sqlite', + * info(): Promise<{ engine: string, database: string, host: string, version: string }>, + * tables(): Promise, + * columns(schema: string, table: string): Promise, + * structure(schema: string, table: string): Promise, + * rows(o: RowsQuery): Promise, + * update(o: { schema: string, table: string, key: Values, set: Values }): Promise<{ rowCount: number }>, + * insert(o: { schema: string, table: string, values: Values }): Promise<{ rowCount: number }>, + * remove(o: { schema: string, table: string, keys: Values[] }): Promise<{ rowCount: number }>, + * query(sql: string): Promise, + * close(): Promise, + * }} Engine + */ + +/** Exact counts above this many (estimated) rows cost too much to run per page. */ +const EXACT_COUNT_MAX = 200_000 + +/** + * JSON-safe cell: dates as ISO strings, bytes as \x-hex, bigints as strings. + * @param {unknown} v + * @returns {unknown} + */ +export function cell(v) { + if (v === null || v === undefined) return null + if (typeof v === 'bigint') return v.toString() + if (v instanceof Date) return Number.isNaN(v.getTime()) ? null : v.toISOString() + if (v instanceof Uint8Array) return `\\x${Buffer.from(v).toString('hex')}` + if (Array.isArray(v)) return v.map(cell) + if (typeof v === 'object') return JSON.parse(JSON.stringify(v, (_k, x) => (typeof x === 'bigint' ? x.toString() : x))) + return v +} + +/** + * A value from the page as a driver parameter: objects (json columns) as JSON + * text, everything else as is - the database casts text to the column's type. + * @param {unknown} v + */ +export function param(v) { + if (v === undefined) return null + if (v !== null && typeof v === 'object') return JSON.stringify(v) + return v +} + +/** @param {string} id */ +const dq = (id) => `"${String(id).replace(/"/g, '""')}"` +/** @param {string} id */ +const bq = (id) => `\`${String(id).replace(/`/g, '``')}\`` + +/** @param {number} n @param {number} max */ +const clampInt = (n, max) => Math.max(0, Math.min(max, Math.floor(Number(n) || 0))) + +/** + * SQL builders for one dialect. + * @param {{ q: (id: string) => string, ph: (i: number) => string, from: (schema: string, table: string) => string }} d + */ +export function dialect(d) { + /** @param {Values} key @param {number} start */ + const whereKey = (key, start) => { + const cols = Object.keys(key) + if (!cols.length) throw new Error('No primary key: this table can only be changed with SQL.') + return { + sql: cols.map((c, i) => `${d.q(c)} = ${d.ph(start + i)}`).join(' AND '), + params: cols.map((c) => param(key[c])), + } + } + return { + ...d, + /** @param {RowsQuery} o */ + page(o) { + const where = o.where?.trim() ? ` WHERE (${o.where})` : '' + const order = o.sort?.column ? ` ORDER BY ${d.q(o.sort.column)} ${o.sort.dir === 'desc' ? 'DESC' : 'ASC'}` : '' + return { + sql: `SELECT * FROM ${d.from(o.schema, o.table)}${where}${order} LIMIT ${clampInt(o.limit, 5000)} OFFSET ${clampInt(o.offset, 1e12)}`, + count: `SELECT count(*) AS n FROM ${d.from(o.schema, o.table)}${where}`, + } + }, + /** @param {string} schema @param {string} table @param {Values} key @param {Values} set @returns {Step} */ + update(schema, table, key, set) { + const cols = Object.keys(set) + if (!cols.length) throw new Error('Nothing to change.') + const w = whereKey(key, cols.length + 1) + return { + sql: `UPDATE ${d.from(schema, table)} SET ${cols.map((c, i) => `${d.q(c)} = ${d.ph(i + 1)}`).join(', ')} WHERE ${w.sql}`, + params: [...cols.map((c) => param(set[c])), ...w.params], + atMostOne: true, + } + }, + /** @param {string} schema @param {string} table @param {Values} values @returns {Step} */ + insert(schema, table, values) { + const cols = Object.keys(values) + if (!cols.length) return { sql: d.emptyInsert(d.from(schema, table)), params: [] } + return { + sql: `INSERT INTO ${d.from(schema, table)} (${cols.map(d.q).join(', ')}) VALUES (${cols.map((_, i) => d.ph(i + 1)).join(', ')})`, + params: cols.map((c) => param(values[c])), + } + }, + /** @param {string} schema @param {string} table @param {Values} key @returns {Step} */ + remove(schema, table, key) { + const w = whereKey(key, 1) + return { sql: `DELETE FROM ${d.from(schema, table)} WHERE ${w.sql}`, params: w.params, atMostOne: true } + }, + emptyInsert: d.emptyInsert, + } +} + +/** @param {number} n @param {Step} step */ +function checkOne(n, step) { + if (step.atMostOne && n > 1) throw new Error(`That would change ${n} rows, not one. Nothing was changed.`) +} + +/** + * @param {string} uri + * @returns {Promise} + */ +export async function openPostgres(uri, { readOnly = false } = {}) { + const { default: pg } = await import('pg') + // Dates and timestamps as text, exactly as stored: no timezone shifts. + for (const oid of [1082, 1114, 1184, 1083, 1266]) pg.types.setTypeParser(oid, (v) => v) + const pool = new pg.Pool({ connectionString: uri, max: 4, connectionTimeoutMillis: 15_000 }) + await (await pool.connect()).release() + /** + * Every query this engine runs. Read-only (a shared studio): each statement + * runs alone, extended protocol (one statement, so no `COMMIT; DELETE`), in + * its own `BEGIN READ ONLY` that is always rolled back, so even a SET it + * makes is undone. + * @param {any} cfg @param {unknown[]} [values] + */ + const pq = async (cfg, values) => { + if (!readOnly) return pool.query(cfg, values) + const c = await pool.connect() + try { + await c.query('BEGIN READ ONLY') + const q = typeof cfg === 'string' ? { text: cfg, values } : { ...cfg, values: cfg.values ?? values } + return await c.query({ ...q, queryMode: 'extended' }) + } finally { + await c.query('ROLLBACK').catch(() => {}) + c.release() + } + } + const sql = dialect({ + q: dq, + ph: (i) => `$${i}`, + from: (s, t) => `${dq(s)}.${dq(t)}`, + emptyInsert: (from) => `INSERT INTO ${from} DEFAULT VALUES`, + }) + + /** @param {string} text @param {unknown[]} [values] */ + const arr = async (text, values) => { + const r = await pq({ text, values, rowMode: 'array' }) + const last = Array.isArray(r) ? r[r.length - 1] : r + return { + columns: (last.fields ?? []).map((/** @type {{ name: string }} */ f) => f.name), + rows: (last.rows ?? []).map((/** @type {unknown[]} */ row) => row.map(cell)), + rowCount: last.rowCount ?? null, + command: last.command ?? '', + } + } + /** @param {Step[]} steps */ + const tx = async (steps) => { + if (readOnly) throw new Error(READ_ONLY) + const c = await pool.connect() + let total = 0 + try { + await c.query('BEGIN') + for (const s of steps) { + const r = await c.query(s.sql, s.params) + checkOne(r.rowCount ?? 0, s) + total += r.rowCount ?? 0 + } + await c.query('COMMIT') + return { rowCount: total } + } catch (e) { + await c.query('ROLLBACK').catch(() => {}) + throw e + } finally { + c.release() + } + } + + /** @param {string} schema @param {string} table */ + const columns = async (schema, table) => { + const r = await pq( + `select a.attname as name, format_type(a.atttypid, a.atttypmod) as type, not a.attnotnull as nullable, + coalesce(a.attnum = any(i.indkey), false) as "primaryKey", + pg_get_expr(d.adbin, d.adrelid) as default + from pg_attribute a + join pg_class c on c.oid = a.attrelid + join pg_namespace n on n.oid = c.relnamespace + left join pg_index i on i.indrelid = c.oid and i.indisprimary + left join pg_attrdef d on d.adrelid = c.oid and d.adnum = a.attnum + where n.nspname = $1 and c.relname = $2 and a.attnum > 0 and not a.attisdropped + order by a.attnum`, + [schema, table], + ) + return r.rows + } + + return { + engine: 'postgres', + async info() { + const r = await pq('select current_database() as db, version() as v') + const u = new URL(uri.replace(/^[a-z0-9+.-]+:/i, 'http:')) + return { engine: 'postgres', database: r.rows[0].db, host: u.hostname, version: String(r.rows[0].v).split(' on ')[0] } + }, + async tables() { + const r = await pq(` + select n.nspname as schema, c.relname as name, + case when c.relkind in ('v', 'm') then 'view' else 'table' end as kind, + case when c.relkind in ('r', 'p') then coalesce(nullif(c.reltuples, -1)::bigint, st.n_live_tup) end as rows + from pg_class c join pg_namespace n on n.oid = c.relnamespace + left join pg_stat_user_tables st on st.relid = c.oid + where c.relkind in ('r', 'p', 'v', 'm', 'f') + and n.nspname not in ('pg_catalog', 'information_schema') and n.nspname not like 'pg_toast%' and n.nspname not like 'pg_temp%' + and not c.relispartition + order by 1, 2`) + return r.rows.map((t) => ({ ...t, rows: t.rows == null ? null : Number(t.rows) })) + }, + columns, + async structure(schema, table) { + const idx = await pq( + `select i.relname as name, ix.indisunique as unique, ix.indisprimary as primary, + array(select a.attname from unnest(ix.indkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = t.oid and a.attnum = k.n order by k.o)::text[] as columns + from pg_index ix join pg_class t on t.oid = ix.indrelid join pg_class i on i.oid = ix.indexrelid join pg_namespace n on n.oid = t.relnamespace + where n.nspname = $1 and t.relname = $2 order by ix.indisprimary desc, i.relname`, + [schema, table], + ) + const fks = await pq( + `select con.conname as name, rn.nspname as "refSchema", rt.relname as "refTable", + array(select a.attname from unnest(con.conkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = con.conrelid and a.attnum = k.n order by k.o)::text[] as columns, + array(select a.attname from unnest(con.confkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = con.confrelid and a.attnum = k.n order by k.o)::text[] as "refColumns" + from pg_constraint con join pg_class t on t.oid = con.conrelid join pg_namespace n on n.oid = t.relnamespace + join pg_class rt on rt.oid = con.confrelid join pg_namespace rn on rn.oid = rt.relnamespace + where con.contype = 'f' and n.nspname = $1 and t.relname = $2 order by con.conname`, + [schema, table], + ) + return { columns: await columns(schema, table), indexes: idx.rows, foreignKeys: fks.rows } + }, + async rows(o) { + const q = sql.page(o) + const page = await arr(q.sql) + if (!o.where?.trim()) { + const est = await pq( + `select c.reltuples::bigint as n from pg_class c join pg_namespace n on n.oid = c.relnamespace where n.nspname = $1 and c.relname = $2`, + [o.schema, o.table], + ) + const guess = Number(est.rows[0]?.n ?? -1) + if (guess > EXACT_COUNT_MAX) return { ...page, total: guess, estimated: true } + } + const exact = await pq(q.count) + return { ...page, total: Number(exact.rows[0].n), estimated: false } + }, + update: async (o) => tx([sql.update(o.schema, o.table, o.key, o.set)]), + insert: async (o) => tx([sql.insert(o.schema, o.table, o.values)]), + remove: async (o) => tx(o.keys.map((k) => sql.remove(o.schema, o.table, k))), + query: (text) => arr(text), + close: () => pool.end(), + } +} + +/** + * @param {string} uri + * @returns {Promise} + */ +export async function openMysql(uri, { readOnly = false } = {}) { + const mysql = await import('mysql2/promise') + const pool = mysql.createPool({ uri: uri.replace(/^(mariadb|mysql2):/i, 'mysql:'), connectionLimit: 4, dateStrings: true, supportBigNumbers: true, bigNumberStrings: true, connectTimeout: 15_000 }) + await (await pool.getConnection()).release() + /** + * Every query this engine runs. Read-only (a shared studio): one statement + * per call (mysql2 keeps multipleStatements off) inside its own + * `START TRANSACTION READ ONLY`, always rolled back. + * @param {any} cfg @param {unknown[]} [values] + */ + const pq = async (cfg, values) => { + if (!readOnly) return pool.query(cfg, values) + const c = await pool.getConnection() + try { + await c.query('START TRANSACTION READ ONLY') + return await c.query(cfg, values) + } finally { + await c.query('ROLLBACK').catch(() => {}) + c.release() + } + } + const sql = dialect({ + q: bq, + ph: () => '?', + from: (s, t) => `${bq(s)}.${bq(t)}`, + emptyInsert: (from) => `INSERT INTO ${from} () VALUES ()`, + }) + + /** @param {string} text @param {unknown[]} [values] */ + const arr = async (text, values) => { + const [rows, fields] = await pq({ sql: text, values, rowsAsArray: true }) + if (!Array.isArray(rows)) { + const h = /** @type {{ affectedRows?: number }} */ (rows) + return { columns: [], rows: [], rowCount: h.affectedRows ?? null, command: text.trim().split(/\s+/)[0].toUpperCase() } + } + return { + columns: (fields ?? []).map((f) => f.name), + rows: rows.map((row) => /** @type {unknown[]} */ (row).map(cell)), + rowCount: rows.length, + command: 'SELECT', + } + } + /** @param {Step[]} steps */ + const tx = async (steps) => { + if (readOnly) throw new Error(READ_ONLY) + const c = await pool.getConnection() + let total = 0 + try { + await c.beginTransaction() + for (const s of steps) { + const [r] = /** @type {any} */ (await c.query(s.sql, s.params)) + checkOne(r.affectedRows ?? 0, s) + total += r.affectedRows ?? 0 + } + await c.commit() + return { rowCount: total } + } catch (e) { + await c.rollback().catch(() => {}) + throw e + } finally { + c.release() + } + } + /** @param {string} schema @param {string} table */ + const columns = async (schema, table) => { + const [r] = /** @type {any} */ (await pq( + `select column_name as name, column_type as type, is_nullable = 'YES' as nullable, column_key = 'PRI' as primaryKey, column_default as \`default\` + from information_schema.columns where table_schema = ? and table_name = ? order by ordinal_position`, + [schema, table], + )) + return r.map((/** @type {any} */ c) => ({ ...c, nullable: !!c.nullable, primaryKey: !!c.primaryKey, default: c.default === 'NULL' ? null : c.default })) + } + + return { + engine: 'mysql', + async info() { + const [r] = /** @type {any} */ (await pq('select database() as db, version() as v')) + const u = new URL(uri.replace(/^[a-z0-9+.-]+:/i, 'http:')) + return { engine: 'mysql', database: r[0].db ?? '', host: u.hostname, version: r[0].v } + }, + async tables() { + const [r] = /** @type {any} */ (await pq( + `select table_schema as \`schema\`, table_name as name, if(table_type = 'VIEW', 'view', 'table') as kind, table_rows as \`rows\` + from information_schema.tables + where table_schema = coalesce(database(), table_schema) + and table_schema not in ('mysql', 'information_schema', 'performance_schema', 'sys') + order by 1, 2`, + )) + return r.map((/** @type {any} */ t) => ({ ...t, rows: t.rows == null ? null : Number(t.rows) })) + }, + columns, + async structure(schema, table) { + const [idx] = /** @type {any} */ (await pq( + `select index_name as name, non_unique = 0 as \`unique\`, index_name = 'PRIMARY' as \`primary\`, group_concat(column_name order by seq_in_index separator '\\n') as cols + from information_schema.statistics where table_schema = ? and table_name = ? group by index_name, non_unique order by \`primary\` desc, name`, + [schema, table], + )) + const [fks] = /** @type {any} */ (await pq( + `select constraint_name as name, referenced_table_schema as refSchema, referenced_table_name as refTable, + group_concat(column_name order by ordinal_position separator '\\n') as cols, + group_concat(referenced_column_name order by ordinal_position separator '\\n') as refCols + from information_schema.key_column_usage + where table_schema = ? and table_name = ? and referenced_table_name is not null + group by constraint_name, referenced_table_schema, referenced_table_name order by name`, + [schema, table], + )) + return { + columns: await columns(schema, table), + indexes: idx.map((/** @type {any} */ i) => ({ name: i.name, columns: String(i.cols).split('\n'), unique: !!i.unique, primary: !!i.primary })), + foreignKeys: fks.map((/** @type {any} */ f) => ({ name: f.name, refSchema: f.refSchema, refTable: f.refTable, columns: String(f.cols).split('\n'), refColumns: String(f.refCols).split('\n') })), + } + }, + async rows(o) { + const q = sql.page(o) + const page = await arr(q.sql) + if (!o.where?.trim()) { + const [est] = /** @type {any} */ (await pq( + 'select table_rows as n from information_schema.tables where table_schema = ? and table_name = ?', + [o.schema, o.table], + )) + const guess = Number(est[0]?.n ?? -1) + if (guess > EXACT_COUNT_MAX) return { ...page, total: guess, estimated: true } + } + const [exact] = /** @type {any} */ (await pq(q.count)) + return { ...page, total: Number(exact[0].n), estimated: false } + }, + update: async (o) => tx([sql.update(o.schema, o.table, o.key, o.set)]), + insert: async (o) => tx([sql.insert(o.schema, o.table, o.values)]), + remove: async (o) => tx(o.keys.map((k) => sql.remove(o.schema, o.table, k))), + query: (text) => arr(text), + close: () => pool.end(), + } +} + +/** + * @param {string} file + * @returns {Promise} + */ +export async function openSqlite(file, { readOnly = false } = {}) { + /** @type {typeof import('node:sqlite')} */ + let sqlite + try { + sqlite = await import('node:sqlite') + } catch { + throw new Error(`SQLite needs Node 22.5 or newer (this is ${process.version}). Or open the file in the Stroke app.`) + } + // Read-only (a shared studio): opened read-only at the file level, which no + // statement can undo (PRAGMA query_only can be switched back off; this can't). + const db = readOnly ? new sqlite.DatabaseSync(file, { readOnly: true }) : new sqlite.DatabaseSync(file) + // One schema per file; the page still sends "main". + const sql = dialect({ q: dq, ph: () => '?', from: (_s, t) => dq(t), emptyInsert: (from) => `INSERT INTO ${from} DEFAULT VALUES` }) + /** @param {unknown} v */ + const bind = (v) => (typeof v === 'boolean' ? Number(v) : v) + + /** @param {string} text @param {unknown[]} [params] */ + const arr = (text, params = []) => { + const stmt = db.prepare(text) + const cols = typeof stmt.columns === 'function' ? stmt.columns().map((c) => c.name) : null + const returnsRows = cols ? cols.length > 0 : /^\s*(select|with|pragma|explain|values)\b/i.test(text) + if (!returnsRows) { + const r = stmt.run(.../** @type {any[]} */ (params.map(bind))) + return { columns: [], rows: [], rowCount: Number(r.changes), command: text.trim().split(/\s+/)[0].toUpperCase() } + } + const objs = /** @type {Record[]} */ (stmt.all(.../** @type {any[]} */ (params.map(bind)))) + const columns = cols ?? Object.keys(objs[0] ?? {}) + return { columns, rows: objs.map((o) => columns.map((c) => cell(o[c]))), rowCount: objs.length, command: 'SELECT' } + } + /** @param {Step[]} steps */ + const tx = async (steps) => { + if (readOnly) throw new Error(READ_ONLY) + let total = 0 + db.exec('BEGIN') + try { + for (const s of steps) { + const n = Number(db.prepare(s.sql).run(.../** @type {any[]} */ (s.params.map(bind))).changes) + checkOne(n, s) + total += n + } + db.exec('COMMIT') + return { rowCount: total } + } catch (e) { + try { db.exec('ROLLBACK') } catch { /* already ended */ } + throw e + } + } + /** @param {string} _schema @param {string} table */ + const columns = async (_schema, table) => { + const r = db.prepare('select name, type, "notnull" as nn, pk, dflt_value as dflt from pragma_table_info(?)').all(table) + return r.map((c) => ({ name: String(c.name), type: String(c.type || ''), nullable: !c.nn, primaryKey: Number(c.pk) > 0, default: c.dflt == null ? null : String(c.dflt) })) + } + + return { + engine: 'sqlite', + async info() { + const v = /** @type {any} */ (db.prepare('select sqlite_version() as v').get()).v + return { engine: 'sqlite', database: file.split(/[\\/]/).pop() ?? file, host: 'local file', version: `SQLite ${v}` } + }, + async tables() { + const r = db.prepare(`select name, type from sqlite_schema where type in ('table', 'view') and name not like 'sqlite_%' order by name`).all() + return r.map((t) => { + const kind = t.type === 'view' ? 'view' : 'table' + let rows = null + if (kind === 'table') { + try { rows = Number(/** @type {any} */ (db.prepare(`select count(*) as n from ${dq(String(t.name))}`).get()).n) } catch { /* virtual table */ } + } + return { schema: 'main', name: String(t.name), kind, rows } + }) + }, + columns, + async structure(schema, table) { + const list = db.prepare('select name, "unique" as u, origin from pragma_index_list(?)').all(table) + const indexes = list.map((i) => ({ + name: String(i.name), + unique: !!i.u, + primary: i.origin === 'pk', + columns: db.prepare('select name from pragma_index_info(?) order by seqno').all(String(i.name)).map((c) => String(c.name)), + })) + /** @type {Map} */ + const byId = new Map() + for (const f of db.prepare('select id, "table" as t, "from" as f, "to" as c from pragma_foreign_key_list(?) order by id, seq').all(table)) { + const id = Number(f.id) + const fk = byId.get(id) ?? { name: `fk_${id}`, refSchema: 'main', refTable: String(f.t), columns: [], refColumns: [] } + fk.columns.push(String(f.f)) + fk.refColumns.push(f.c == null ? '' : String(f.c)) + byId.set(id, fk) + } + return { columns: await columns(schema, table), indexes, foreignKeys: [...byId.values()] } + }, + async rows(o) { + const q = sql.page(o) + const page = arr(q.sql) + const total = Number(/** @type {any} */ (db.prepare(q.count).get()).n) + return { ...page, total, estimated: false } + }, + update: async (o) => tx([sql.update(o.schema, o.table, o.key, o.set)]), + insert: async (o) => tx([sql.insert(o.schema, o.table, o.values)]), + remove: async (o) => tx(o.keys.map((k) => sql.remove(o.schema, o.table, k))), + async query(text) { + // node:sqlite prepares one statement at a time; run all but the last + // with exec and return the last one's rows, like the other consoles do. + const parts = splitStatements(text) + if (parts.length > 1 && readOnly) throw new Error('Run one statement at a time in a shared studio.') + if (parts.length > 1) db.exec(parts.slice(0, -1).join(';\n')) + return arr(parts[parts.length - 1] ?? text) + }, + async close() { + db.close() + }, + } +} + +/** + * Split on `;` outside quotes and comments. Enough for a console; not a parser. + * @param {string} text + */ +export function splitStatements(text) { + /** @type {string[]} */ + const out = [] + let cur = '' + for (let i = 0; i < text.length; i++) { + const c = text[i] + if (c === "'" || c === '"' || c === '`') { + const end = text.indexOf(c, i + 1) + const j = end < 0 ? text.length : end + cur += text.slice(i, j + 1) + i = j + } else if (c === '-' && text[i + 1] === '-') { + const end = text.indexOf('\n', i) + i = end < 0 ? text.length : end - 1 + } else if (c === '/' && text[i + 1] === '*') { + const end = text.indexOf('*/', i + 2) + i = end < 0 ? text.length : end + 1 + } else if (c === ';') { + if (cur.trim()) out.push(cur.trim()) + cur = '' + } else { + cur += c + } + } + if (cur.trim()) out.push(cur.trim()) + return out +} + +/** + * @param {'postgres' | 'mysql' | 'sqlite'} engine + * @param {string} uri + * @param {{ readOnly?: boolean }} [opts] + */ +export function openEngine(engine, uri, opts = {}) { + if (engine === 'postgres') return openPostgres(uri, opts) + if (engine === 'mysql') return openMysql(uri, opts) + return openSqlite(uri, opts) +} + +const READ_ONLY = 'This shared studio is read-only.' diff --git a/cli/src/find.js b/cli/src/find.js new file mode 100644 index 00000000..e6fa266a --- /dev/null +++ b/cli/src/find.js @@ -0,0 +1,164 @@ +// Where the project keeps its database: an argument, the environment, a .env +// file, or a SQLite file sitting in the folder. + +import fs from 'node:fs' +import path from 'node:path' + +/** Checked in this order; the first one set wins. */ +export const URL_KEYS = [ + 'DATABASE_URL', + 'POSTGRES_URL', + 'POSTGRES_PRISMA_URL', + 'POSTGRES_URL_NON_POOLING', + 'DIRECT_URL', + 'PG_URL', + 'PGURL', + 'MYSQL_URL', + 'MYSQL_DATABASE_URL', + 'DB_URL', + 'SQLITE_URL', + 'DB_FILE_NAME', +] + +/** Later files override earlier ones, as in Next.js and Vite. */ +export const ENV_FILES = ['.env', '.env.development', '.env.local', '.env.development.local'] + +const DB_FILE = /\.(db|sqlite|sqlite3|db3)$/i + +/** + * A dotenv file's KEY=value pairs. Handles `export`, quotes, `#` comments and + * `${VAR}` references to keys defined earlier in the same file set. + * @param {string} text + * @param {Record} [into] + */ +export function parseEnv(text, into = {}) { + for (const raw of text.split(/\r?\n/)) { + const m = /^\s*(?:export\s+)?([A-Za-z_][A-Za-z0-9_]*)\s*=\s*(.*)$/.exec(raw) + if (!m) continue + let v = m[2].trim() + const q = v[0] + if ((q === '"' || q === "'" || q === '`') && v.lastIndexOf(q) > 0) { + v = v.slice(1, v.lastIndexOf(q)) + if (q === '"') v = v.replace(/\\n/g, '\n') + } else { + v = v.replace(/\s+#.*$/, '') + } + if (q !== "'") v = v.replace(/\$\{([A-Za-z_][A-Za-z0-9_]*)\}/g, (_, k) => into[k] ?? process.env[k] ?? '') + into[m[1]] = v + } + return into +} + +/** @param {string} dir */ +export function readEnvFiles(dir) { + /** @type {Record} */ + const env = {} + /** @type {string[]} */ + const read = [] + for (const f of ENV_FILES) { + const p = path.join(dir, f) + try { + parseEnv(fs.readFileSync(p, 'utf8'), env) + read.push(f) + } catch { + // not there + } + } + return { env, read } +} + +/** + * @typedef {{ uri: string, source: string }} Found + */ + +/** + * The connection to open, and where it came from. + * @param {{ arg?: string, cwd: string, env?: Record }} o + * @returns {Found | null} + */ +export function findConnection({ arg, cwd, env = process.env }) { + if (arg) { + if (/^[a-z][a-z0-9+.-]*:\/\//i.test(arg)) return { uri: arg, source: 'argument' } + const file = path.resolve(cwd, arg) + if (fs.existsSync(file)) return { uri: file, source: 'argument' } + throw new Error(`Not a connection URL or a file: ${arg}`) + } + for (const k of URL_KEYS) { + const v = env[k]?.trim() + if (v) return { uri: resolveFileUri(v, cwd), source: `$${k}` } + } + const { env: fileEnv, read } = readEnvFiles(cwd) + for (const k of URL_KEYS) { + const v = fileEnv[k]?.trim() + if (v) { + const from = [...read].reverse().find((f) => { + try { + return k in parseEnv(fs.readFileSync(path.join(cwd, f), 'utf8')) + } catch { + return false + } + }) + return { uri: resolveFileUri(v, cwd), source: `${k} in ${from ?? '.env'}` } + } + } + const files = listDbFiles(cwd) + if (files.length) return { uri: path.join(cwd, files[0]), source: files[0] } + return null +} + +/** + * `file:./dev.db` (Prisma, Drizzle, libSQL) and bare relative paths become + * absolute, so the app and the bridge read the same file from anywhere. + * @param {string} v + * @param {string} cwd + */ +export function resolveFileUri(v, cwd) { + const m = /^(?:file|sqlite3?):(?:\/\/)?(.+)$/i.exec(v) + if (m) return path.resolve(cwd, m[1].replace(/\?.*$/, '')) + if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(v) && DB_FILE.test(v)) return path.resolve(cwd, v) + return v +} + +/** @param {string} dir */ +function listDbFiles(dir) { + const out = [] + for (const sub of ['', 'prisma', 'data', 'db']) { + try { + for (const f of fs.readdirSync(path.join(dir, sub))) { + if (DB_FILE.test(f)) out.push(path.join(sub, f)) + } + } catch { + // not there + } + } + return out +} + +/** + * Which engine a connection string is for, from the bridge's point of view. + * @param {string} uri + * @returns {'postgres' | 'mysql' | 'sqlite' | null} + */ +export function engineOf(uri) { + const scheme = /^([a-z][a-z0-9+.-]*):\/\//i.exec(uri)?.[1]?.toLowerCase() + if (!scheme) return DB_FILE.test(uri) || fs.existsSync(uri) ? 'sqlite' : null + if (['postgres', 'postgresql', 'pg', 'cockroachdb', 'neon'].includes(scheme)) return 'postgres' + if (['mysql', 'mariadb', 'mysql2'].includes(scheme)) return 'mysql' + if (['sqlite', 'sqlite3', 'file'].includes(scheme)) return 'sqlite' + return null +} + +/** + * The URL with its password replaced, for printing. + * @param {string} uri + */ +export function redact(uri) { + // The host can't contain `@`, so the last one ends the login - a typed + // password often has a raw `@` in it. + const m = /^([a-z][a-z0-9+.-]*:\/\/)(.*)$/is.exec(uri) + if (!m) return uri + const at = m[2].lastIndexOf('@') + const colon = m[2].indexOf(':') + if (at < 0 || colon < 0 || colon > at) return uri + return `${m[1]}${m[2].slice(0, colon)}:****${m[2].slice(at)}` +} diff --git a/cli/src/relay.js b/cli/src/relay.js new file mode 100644 index 00000000..508240b5 --- /dev/null +++ b/cli/src/relay.js @@ -0,0 +1,140 @@ +// Share the studio beyond this machine (Labs). The local bridge only answers +// 127.0.0.1; a share holds one WebSocket open to a room on the relay at +// stroke.click, and a studio page on any device sends its calls there. +// +// Everything is end-to-end encrypted: AES-256-GCM with a key that exists only +// in the share link's #fragment, which no browser sends to a server. The relay +// sees the room id and ciphertext. The same scheme is in stroke-web +// (src/lib/relay/relay-crypto.ts) and the desktop app (src/lib/studio-share.js). + +import crypto from 'node:crypto' + +const subtle = crypto.webcrypto.subtle + +/** @param {Uint8Array | Buffer} b */ +export const toB64u = (b) => Buffer.from(b).toString('base64url') +/** @param {string} s */ +export const fromB64u = (s) => new Uint8Array(Buffer.from(s, 'base64url')) + +/** A fresh share: a room on the relay, the link's key, and the host's claim on the room. */ +export function newShare() { + return { + room: toB64u(crypto.randomBytes(16)), + key: toB64u(crypto.randomBytes(32)), + secret: toB64u(crypto.randomBytes(32)), + } +} + +/** The link someone else opens. Everything after # stays in their browser. */ +export function shareUrl({ studio, room, key }) { + return `${studio}#${new URLSearchParams({ relay: room, key })}` +} + +/** @param {string} key */ +export async function importKey(key) { + return subtle.importKey('raw', fromB64u(key), 'AES-GCM', false, ['encrypt', 'decrypt']) +} + +/** @param {CryptoKey} key @param {unknown} value */ +export async function seal(key, value) { + const iv = crypto.randomBytes(12) + const ct = await subtle.encrypt({ name: 'AES-GCM', iv }, key, Buffer.from(JSON.stringify(value))) + return { iv: toB64u(iv), ct: toB64u(new Uint8Array(ct)) } +} + +/** @param {CryptoKey} key @param {{ iv: string, ct: string }} sealed */ +export async function open(key, sealed) { + const pt = await subtle.decrypt({ name: 'AES-GCM', iv: fromB64u(sealed.iv) }, key, fromB64u(sealed.ct)) + return JSON.parse(Buffer.from(pt).toString('utf8')) +} + +/** + * Answer a room's calls until closed. Reconnects with backoff if the socket + * drops (laptop sleep, Wi-Fi change); the link keeps working when it's back. + * @param {{ + * routes: Record Promise>, + * room: string, key: string, secret: string, + * base?: string, + * onStatus?: (status: 'online' | 'offline', detail?: string) => void, + * }} o + */ +export async function startRelayHost({ routes, room, key, secret, base = 'https://stroke.click', onStatus = () => {} }) { + if (typeof WebSocket !== 'function') { + throw new Error(`Sharing needs Node 22 or newer (this is ${process.version}).`) + } + const cryptoKey = await importKey(key) + const url = `${base.replace(/^http/, 'ws')}/api/relay/${room}/host?${new URLSearchParams({ secret })}` + let closed = false + let delay = 1000 + /** @type {WebSocket | null} */ + let ws = null + /** @type {ReturnType | undefined} */ + let ping + + /** @param {{ id: number, iv: string, ct: string }} msg */ + const answer = async (msg) => { + let reply + try { + const { path, body } = await open(cryptoKey, msg) + const route = typeof path === 'string' ? routes[path] : undefined + reply = route + ? { status: 200, data: await route(body ?? {}) } + : { status: 404, error: 'Not available in this shared studio.' } + } catch (e) { + reply = { status: 400, error: e instanceof Error ? e.message : String(e) } + } + try { + ws?.send(JSON.stringify({ id: msg.id, ...(await seal(cryptoKey, reply)) })) + } catch { + // the socket went; the caller times out and retries + } + } + + const connect = () => { + if (closed) return + const sock = new WebSocket(url) + ws = sock + sock.addEventListener('open', () => { + delay = 1000 + onStatus('online') + clearInterval(ping) + ping = setInterval(() => sock.readyState === 1 && sock.send('{"ping":1}'), 25_000) + }) + sock.addEventListener('message', (ev) => { + let msg + try { + msg = JSON.parse(String(ev.data)) + } catch { + return + } + if (typeof msg?.id === 'number' && typeof msg.iv === 'string' && typeof msg.ct === 'string') void answer(msg) + }) + sock.addEventListener('close', (ev) => { + clearInterval(ping) + if (ws !== sock || closed) return + if (ev.code === 1008 || ev.code === 4003) { + onStatus('offline', 'The relay refused this share.') + return + } + onStatus('offline', 'Reconnecting') + setTimeout(connect, delay) + delay = Math.min(delay * 2, 30_000) + }) + sock.addEventListener('error', () => { + // 'close' follows and reconnects + }) + } + connect() + + return { + close() { + closed = true + clearInterval(ping) + try { + ws?.close(1000, 'Stopped sharing') + } catch { + // already closed + } + }, + } +} diff --git a/cli/test/cli.test.js b/cli/test/cli.test.js new file mode 100644 index 00000000..d1f6460a --- /dev/null +++ b/cli/test/cli.test.js @@ -0,0 +1,231 @@ +import { test } from 'node:test' +import assert from 'node:assert/strict' +import fs from 'node:fs' +import os from 'node:os' +import path from 'node:path' +import { parseEnv, findConnection, resolveFileUri, engineOf, redact } from '../src/find.js' +import { appLink } from '../src/app.js' +import { splitStatements, cell, openSqlite } from '../src/engines.js' +import { startBridge, studioUrl } from '../src/bridge.js' + +const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'strokedb-')) + +test('parseEnv: export, quotes, comments, references', () => { + const env = parseEnv(` +# comment +export HOST=db.local +USER="me" +PASS='p#ss \${HOST}' +DATABASE_URL=postgres://\${USER}:x@\${HOST}/app # trailing +EMPTY= +`) + assert.equal(env.HOST, 'db.local') + assert.equal(env.PASS, 'p#ss ${HOST}') + assert.equal(env.DATABASE_URL, 'postgres://me:x@db.local/app') + assert.equal(env.EMPTY, '') +}) + +test('findConnection: argument, then env, then .env files, then a .db file', () => { + const dir = tmp() + assert.equal(findConnection({ cwd: dir, env: {} }), null) + + fs.writeFileSync(path.join(dir, 'dev.sqlite'), '') + assert.equal(findConnection({ cwd: dir, env: {} })?.uri, path.join(dir, 'dev.sqlite')) + + fs.writeFileSync(path.join(dir, '.env'), 'DATABASE_URL=postgres://a@h/one\n') + fs.writeFileSync(path.join(dir, '.env.local'), 'DATABASE_URL=postgres://a@h/two\n') + const fromFile = findConnection({ cwd: dir, env: {} }) + assert.equal(fromFile?.uri, 'postgres://a@h/two') + assert.equal(fromFile?.source, 'DATABASE_URL in .env.local') + + assert.equal(findConnection({ cwd: dir, env: { POSTGRES_URL: 'postgres://env/x' } })?.uri, 'postgres://env/x') + assert.equal(findConnection({ arg: 'mysql://u@h/d', cwd: dir, env: {} })?.source, 'argument') + assert.throws(() => findConnection({ arg: 'nope.db', cwd: dir, env: {} })) +}) + +test('file: URLs and bare paths resolve against the project', () => { + assert.equal(resolveFileUri('file:./dev.db', '/p'), path.resolve('/p', 'dev.db')) + assert.equal(resolveFileUri('file:dev.db?mode=ro', '/p'), path.resolve('/p', 'dev.db')) + assert.equal(resolveFileUri('data/app.sqlite', '/p'), path.resolve('/p', 'data/app.sqlite')) + assert.equal(resolveFileUri('postgres://h/db', '/p'), 'postgres://h/db') +}) + +test('engineOf and redact', () => { + assert.equal(engineOf('postgresql://h/d'), 'postgres') + assert.equal(engineOf('mariadb://h/d'), 'mysql') + assert.equal(engineOf('/x/app.db'), 'sqlite') + assert.equal(engineOf('mongodb://h'), null) + assert.equal(redact('postgres://me:s3cr@t@host/db'), 'postgres://me:****@host/db') + assert.equal(redact('postgres://host/db'), 'postgres://host/db') +}) + +test('appLink round-trips the URL and name', () => { + const link = appLink('stroke', 'postgres://me:p&ss@h/db?sslmode=require', 'my app') + const u = new URL(link) + assert.equal(u.protocol, 'stroke:') + assert.equal(u.host, 'open') + assert.equal(u.searchParams.get('url'), 'postgres://me:p&ss@h/db?sslmode=require') + assert.equal(u.searchParams.get('name'), 'my app') +}) + +test('splitStatements respects quotes and comments', () => { + assert.deepEqual(splitStatements(`select ';'; -- a;b\nselect 2; /* ; */ select "x;y"`), [`select ';'`, 'select 2', 'select "x;y"']) +}) + +test('cell makes values JSON-safe', () => { + assert.equal(cell(10n), '10') + assert.equal(cell(new Date(0)), '1970-01-01T00:00:00.000Z') + assert.equal(cell(Buffer.from([1, 255])), '\\x01ff') + assert.deepEqual(cell({ a: 1n }), { a: '1' }) +}) + +test('sqlite engine and bridge, end to end', async () => { + const file = path.join(tmp(), 'app.db') + const db = await openSqlite(file) + await db.query(`create table users (id integer primary key, name text not null); insert into users (name) values ('ada'), ('bob')`) + assert.deepEqual(await db.tables(), [{ schema: 'main', name: 'users', kind: 'table', rows: 2 }]) + const cols = await db.columns('main', 'users') + assert.deepEqual(cols.map((c) => [c.name, c.primaryKey, c.nullable]), [['id', true, true], ['name', false, false]]) + + const origin = 'https://stroke.click' + const bridge = await startBridge({ engine: db, name: 'proj', origins: [origin], token: 'secret-token' }) + const base = `http://127.0.0.1:${bridge.port}` + /** @param {string} p @param {unknown} body @param {Record} [headers] */ + const call = (p, body, headers = {}) => + fetch(base + p, { method: 'POST', headers: { origin, authorization: 'Bearer secret-token', 'content-type': 'application/json', ...headers }, body: JSON.stringify(body) }) + try { + const rows = await (await call('/rows', { schema: 'main', table: 'users', limit: 1, offset: 1, sort: { column: 'name', dir: 'desc' } })).json() + assert.deepEqual(rows.columns, ['id', 'name']) + assert.deepEqual(rows.rows, [[1, 'ada']]) + assert.equal(rows.total, 2) + + const q = await (await call('/query', { sql: 'select count(*) as n from users' })).json() + assert.deepEqual(q.rows, [[2]]) + const bad = await call('/query', { sql: 'select * from nope' }) + assert.equal(bad.status, 400) + assert.match((await bad.json()).error, /no such table/) + + const info = await (await call('/info', {})).json() + assert.equal(info.name, 'proj') + + assert.equal((await call('/tables', {}, { authorization: 'Bearer wrong-token!' })).status, 401) + assert.equal((await call('/tables', {}, { origin: 'https://evil.test' })).status, 403) + + const pre = await fetch(base + '/tables', { method: 'OPTIONS', headers: { origin, 'access-control-request-private-network': 'true' } }) + assert.equal(pre.status, 204) + assert.equal(pre.headers.get('access-control-allow-private-network'), 'true') + assert.equal(pre.headers.get('access-control-allow-origin'), origin) + } finally { + await bridge.close() + await db.close() + } +}) + +test('bridge rejects a foreign Host (DNS rebinding)', async () => { + const http = await import('node:http') + const bridge = await startBridge({ engine: /** @type {any} */ ({}), name: 'x', token: 't' }) + try { + const status = await new Promise((resolve, reject) => { + const req = http.request({ host: '127.0.0.1', port: bridge.port, path: '/tables', method: 'POST', headers: { host: `evil.test:${bridge.port}`, origin: 'https://stroke.click' } }, (res) => { + res.resume() + resolve(res.statusCode) + }) + req.on('error', reject) + req.end('{}') + }) + assert.equal(status, 421) + } finally { + await bridge.close() + } +}) + +test('studioUrl keeps the token in the fragment', () => { + const u = new URL(studioUrl({ studio: 'https://stroke.click/studio', port: 4321, token: 'abc' })) + assert.equal(u.pathname, '/studio') + assert.equal(u.search, '') + const f = new URLSearchParams(u.hash.slice(1)) + assert.equal(f.get('bridge'), 'http://127.0.0.1:4321') + assert.equal(f.get('token'), 'abc') +}) + +test('sqlite: structure, where, and edits by primary key', async () => { + const db = await openSqlite(path.join(tmp(), 'edit.db')) + try { + await db.query(`create table teams (id integer primary key, name text); + create table users (id integer primary key, email text not null default 'x', team integer references teams(id)); + create unique index users_email on users(email); + insert into teams (name) values ('core'); + insert into users (email, team) values ('a@x.io', 1), ('b@x.io', 1), ('c@x.io', null)`) + const t = await db.tables() + assert.equal(t.find((x) => x.name === 'users')?.rows, 3) + + const s = await db.structure('main', 'users') + assert.equal(s.columns.find((c) => c.name === 'email')?.default, "'x'") + assert.ok(s.indexes.some((i) => i.name === 'users_email' && i.unique && i.columns[0] === 'email')) + assert.deepEqual(s.foreignKeys.map((f) => [f.columns[0], f.refTable, f.refColumns[0]]), [['team', 'teams', 'id']]) + + // A key that matches more than one row changes nothing. + await assert.rejects(db.update({ schema: 'main', table: 'users', key: { team: 1 }, set: { team: null } }), /2 rows/) + assert.equal((await db.query('select count(*) from users where team = 1')).rows[0][0], 2) + + const filtered = await db.rows({ schema: 'main', table: 'users', limit: 10, offset: 0, where: 'team is not null' }) + assert.equal(filtered.total, 2) + + assert.equal((await db.update({ schema: 'main', table: 'users', key: { id: 2 }, set: { email: 'bob@x.io', team: null } })).rowCount, 1) + assert.equal((await db.insert({ schema: 'main', table: 'users', values: { email: 'd@x.io' } })).rowCount, 1) + assert.equal((await db.remove({ schema: 'main', table: 'users', keys: [{ id: 1 }, { id: 3 }] })).rowCount, 2) + const left = await db.query('select id, email, team from users order by id') + assert.deepEqual(left.rows, [[2, 'bob@x.io', null], [4, 'd@x.io', null]]) + + await assert.rejects(db.update({ schema: 'main', table: 'users', key: {}, set: { email: 'z' } }), /primary key/) + } finally { + await db.close() + } +}) + +test('share: read-only routes drop edits and keep reading SQL only', async () => { + const { makeRoutes, isReadingSql } = await import('../src/bridge.js') + const engine = { info: async () => ({ engine: 'sqlite', database: 'x', host: 'local file', version: '' }), query: async (sql) => ({ columns: ['q'], rows: [[sql]] }) } + const ro = makeRoutes({ engine: /** @type {any} */ (engine), name: 'x', shared: true, write: false }) + assert.equal(ro['/update'], undefined) + assert.equal(ro['/insert'], undefined) + assert.equal(ro['/delete'], undefined) + const info = /** @type {any} */ (await ro['/info']({})) + assert.equal(info.shared, true) + assert.equal(info.readonly, true) + assert.ok(info.features.includes('readonly') && !info.features.includes('edit')) + await assert.rejects(() => ro['/query']({ sql: 'delete from t' }), /read-only/) + assert.deepEqual((/** @type {any} */ (await ro['/query']({ sql: 'select 1' }))).rows, [['select 1']]) + for (const sql of ['select 1', 'WITH a AS (select 1) select * from a', '/* x */ show tables', 'pragma table_info(t)']) assert.ok(isReadingSql(sql), sql) + for (const sql of ['delete from t', 'copy t to stdout', "attach 'x' as y", 'pragma query_only = 0', 'update t set a=1']) assert.ok(!isReadingSql(sql), sql) + const rw = makeRoutes({ engine: /** @type {any} */ (engine), name: 'x', shared: true, write: true }) + assert.equal(typeof rw['/update'], 'function') +}) + +test('share: sealed calls round-trip with the link key and fail with another', async () => { + const { newShare, importKey, seal, open, shareUrl } = await import('../src/relay.js') + const a = newShare() + assert.match(a.room, /^[A-Za-z0-9_-]{22}$/) + assert.match(a.key, /^[A-Za-z0-9_-]{43}$/) + const key = await importKey(a.key) + const sealed = await seal(key, { path: '/tables', body: {} }) + assert.deepEqual(await open(key, sealed), { path: '/tables', body: {} }) + const other = await importKey(newShare().key) + await assert.rejects(() => open(other, sealed)) + assert.equal(shareUrl({ studio: 'https://stroke.click/studio', room: a.room, key: a.key }), `https://stroke.click/studio#relay=${a.room}&key=${a.key}`) +}) + +test('sqlite: a read-only engine refuses writes at the file level', async () => { + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'strokedb-ro-')) + const file = path.join(dir, 'ro.db') + const rw = await openSqlite(file) + await rw.query('create table t(id integer primary key, a text)') + await rw.query("insert into t(a) values ('x')") + await rw.close() + const ro = await openSqlite(file, { readOnly: true }) + assert.equal((await ro.query('select count(*) as n from t')).rows[0][0], 1) + await assert.rejects(() => ro.query("insert into t(a) values ('y')")) + await assert.rejects(() => ro.query('select 1; delete from t'), /one statement/) + await assert.rejects(() => ro.update({ schema: 'main', table: 't', key: { id: 1 }, set: { a: 'z' } }), /read-only/) + await ro.close() +}) diff --git a/package-lock.json b/package-lock.json index d1ef5df1..1bea7063 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "stroke", - "version": "2.5.5", + "version": "2.19.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "stroke", - "version": "2.5.5", + "version": "2.19.0", "license": "SEE LICENSE IN LICENSE", "dependencies": { "@codemirror/autocomplete": "^6.20.3", @@ -52,7 +52,8 @@ "canvas-confetti": "^1.9.4", "chart.js": "^4.5.1", "clsx": "^2.1.1", - "echarts": "^5.6.0", + "croner": "^9.1.0", + "echarts": "^6.1.0", "echarts-wordcloud": "^2.1.0", "elkjs": "^0.11.1", "idb": "^8.0.3", @@ -77,6 +78,7 @@ "@tauri-apps/api": "^2.12.4", "@tauri-apps/cli": "^2.11.2", "cross-env": "^10.1.0", + "jsdom": "^25.0.1", "mode-watcher": "^1.1.0", "svelte": "5.55.9", "svelte-check": "^4.7.6", @@ -101,6 +103,20 @@ "url": "https://github.com/sponsors/antfu" } }, + "node_modules/@asamuzakjp/css-color": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/@asamuzakjp/css-color/-/css-color-3.2.0.tgz", + "integrity": "sha512-K1A6z8tS3XsmCMM86xoWdn7Fkdn9m6RSVtocUrJYIwZnFVkng/PvkEoWtOWmP+Scc6saYWHWZYbndEEXxl24jw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@csstools/css-calc": "^2.1.3", + "@csstools/css-color-parser": "^3.0.9", + "@csstools/css-parser-algorithms": "^3.0.4", + "@csstools/css-tokenizer": "^3.0.3", + "lru-cache": "^10.4.3" + } + }, "node_modules/@braintree/sanitize-url": { "version": "7.1.2", "resolved": "https://registry.npmjs.org/@braintree/sanitize-url/-/sanitize-url-7.1.2.tgz", @@ -263,6 +279,121 @@ "w3c-keyname": "^2.2.4" } }, + "node_modules/@csstools/color-helpers": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/@csstools/color-helpers/-/color-helpers-5.1.0.tgz", + "integrity": "sha512-S11EXWJyy0Mz5SYvRmY8nJYTFFd1LCNV+7cXyAgQtOOuzb4EsgfqDufL+9esx72/eLhsRdGZwaldu/h+E4t4BA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/csstools" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/csstools" + } + ], + "license": "MIT-0", + "engines": { + "node": ">=18" + } + }, + "node_modules/@csstools/css-calc": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/@csstools/css-calc/-/css-calc-2.1.4.tgz", + "integrity": "sha512-3N8oaj+0juUw/1H3YwmDDJXCgTB1gKU6Hc/bB502u9zR0q2vd786XJH9QfrKIEgFlZmhZiq6epXl4rHqhzsIgQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/csstools" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/csstools" + } + ], + "license": "MIT", + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "@csstools/css-parser-algorithms": "^3.0.5", + "@csstools/css-tokenizer": "^3.0.4" + } + }, + "node_modules/@csstools/css-color-parser": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/@csstools/css-color-parser/-/css-color-parser-3.1.0.tgz", + "integrity": "sha512-nbtKwh3a6xNVIp/VRuXV64yTKnb1IjTAEEh3irzS+HkKjAOYLTGNb9pmVNntZ8iVBHcWDA2Dof0QtPgFI1BaTA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/csstools" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/csstools" + } + ], + "license": "MIT", + "dependencies": { + "@csstools/color-helpers": "^5.1.0", + "@csstools/css-calc": "^2.1.4" + }, + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "@csstools/css-parser-algorithms": "^3.0.5", + "@csstools/css-tokenizer": "^3.0.4" + } + }, + "node_modules/@csstools/css-parser-algorithms": { + "version": "3.0.5", + "resolved": "https://registry.npmjs.org/@csstools/css-parser-algorithms/-/css-parser-algorithms-3.0.5.tgz", + "integrity": "sha512-DaDeUkXZKjdGhgYaHNJTV9pV7Y9B3b644jCLs9Upc3VeNGg6LWARAT6O+Q+/COo+2gg/bM5rhpMAtf70WqfBdQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/csstools" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/csstools" + } + ], + "license": "MIT", + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "@csstools/css-tokenizer": "^3.0.4" + } + }, + "node_modules/@csstools/css-tokenizer": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@csstools/css-tokenizer/-/css-tokenizer-3.0.4.tgz", + "integrity": "sha512-Vd/9EVDiu6PPJt9yAh6roZP6El1xHrdvIVGjyBsHR0RYwNHgL7FJPyIIW4fANJNG6FtyZfvlRPpFI4ZM/lubvw==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/csstools" + }, + { + "type": "opencollective", + "url": "https://opencollective.com/csstools" + } + ], + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/@dagrejs/dagre": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/@dagrejs/dagre/-/dagre-3.0.0.tgz", @@ -2197,6 +2328,16 @@ "node": ">=0.4.0" } }, + "node_modules/agent-base": { + "version": "7.1.4", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.4.tgz", + "integrity": "sha512-MnA+YT8fwfJPgBx3m60MNqakm30XOkyIoH1y6huTQvC0PwZG7ki8NacLBcrPbNoo8vEZy7Jpuk7+jMO+CUovTQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 14" + } + }, "node_modules/argparse": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz", @@ -2222,6 +2363,13 @@ "node": ">=12" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "dev": true, + "license": "MIT" + }, "node_modules/axobject-query": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/axobject-query/-/axobject-query-4.1.0.tgz", @@ -2265,6 +2413,20 @@ "svelte": "^5.33.0" } }, + "node_modules/call-bind-apply-helpers": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz", + "integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/canvas-confetti": { "version": "1.9.4", "resolved": "https://registry.npmjs.org/canvas-confetti/-/canvas-confetti-1.9.4.tgz", @@ -2352,6 +2514,19 @@ "node": ">=6" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dev": true, + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/comma-separated-tokens": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/comma-separated-tokens/-/comma-separated-tokens-2.0.3.tgz", @@ -2390,6 +2565,15 @@ "integrity": "sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA==", "license": "MIT" }, + "node_modules/croner": { + "version": "9.1.0", + "resolved": "https://registry.npmjs.org/croner/-/croner-9.1.0.tgz", + "integrity": "sha512-p9nwwR4qyT5W996vBZhdvBCnMhicY5ytZkR4D1Xj0wuTDEiMnjwR57Q3RXYY/s0EpX6Ay3vgIcfaR+ewGHsi+g==", + "license": "MIT", + "engines": { + "node": ">=18.0" + } + }, "node_modules/cross-env": { "version": "10.1.0", "resolved": "https://registry.npmjs.org/cross-env/-/cross-env-10.1.0.tgz", @@ -2423,6 +2607,27 @@ "node": ">= 8" } }, + "node_modules/cssstyle": { + "version": "4.6.0", + "resolved": "https://registry.npmjs.org/cssstyle/-/cssstyle-4.6.0.tgz", + "integrity": "sha512-2z+rWdzbbSZv6/rhtvzvqeZQHrBaqgogqt85sqFNbabZOuFbCVFb8kPeEtZjiKkbrm395irpNKiYeFeLiQnFPg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@asamuzakjp/css-color": "^3.2.0", + "rrweb-cssom": "^0.8.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/cssstyle/node_modules/rrweb-cssom": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/rrweb-cssom/-/rrweb-cssom-0.8.0.tgz", + "integrity": "sha512-guoltQEx+9aMf2gDZ0s62EcV8lsXR+0w8915TC3ITdn2YueuNjdAYh/levpU9nFaoChh9RUS5ZdQMrKfVEN9tw==", + "dev": true, + "license": "MIT" + }, "node_modules/cytoscape": { "version": "3.34.4", "resolved": "https://registry.npmjs.org/cytoscape/-/cytoscape-3.34.4.tgz", @@ -2931,12 +3136,61 @@ "lodash-es": "^4.17.21" } }, + "node_modules/data-urls": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/data-urls/-/data-urls-5.0.0.tgz", + "integrity": "sha512-ZYP5VBHshaDAiVZxjbRVcFJpc+4xGgT0bK3vzy1HLN8jTO975HEbuYzZJcHoQEY5K1a0z8YayJkyVETa08eNTg==", + "dev": true, + "license": "MIT", + "dependencies": { + "whatwg-mimetype": "^4.0.0", + "whatwg-url": "^14.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/data-urls/node_modules/whatwg-mimetype": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-4.0.0.tgz", + "integrity": "sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/dayjs": { "version": "1.11.23", "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.23.tgz", "integrity": "sha512-QDTCU0M0MxR3hQfnlDJfwekQiaanm1ubOD231u73WBckQ/fsamwRLiE2GBz6D3a/xF1NgfiDLJjXBa1hYOYTtQ==", "license": "MIT" }, + "node_modules/debug": { + "version": "4.4.3", + "resolved": "https://registry.npmjs.org/debug/-/debug-4.4.3.tgz", + "integrity": "sha512-RGwwWnwQvkVfavKVt22FGLw+xYSdzARwm0ru6DhTVA3umU5hZc28V3kO4stgYryrTlLpuvgI9GiijltAjNbcqA==", + "dev": true, + "license": "MIT", + "dependencies": { + "ms": "^2.1.3" + }, + "engines": { + "node": ">=6.0" + }, + "peerDependenciesMeta": { + "supports-color": { + "optional": true + } + } + }, + "node_modules/decimal.js": { + "version": "10.6.0", + "resolved": "https://registry.npmjs.org/decimal.js/-/decimal.js-10.6.0.tgz", + "integrity": "sha512-YpgQiITW3JXGntzdUmyUR1V812Hn8T1YVXhCu+wO3OpS4eU9l4YdD3qjyiKdV6mvV29zapkMeD390UVEf2lkUg==", + "dev": true, + "license": "MIT" + }, "node_modules/deepmerge": { "version": "4.3.1", "resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz", @@ -2956,6 +3210,16 @@ "robust-predicates": "^3.0.2" } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/dequal": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/dequal/-/dequal-2.0.3.tgz", @@ -3008,14 +3272,29 @@ "@types/trusted-types": "^2.0.7" } }, + "node_modules/dunder-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz", + "integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==", + "dev": true, + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.1", + "es-errors": "^1.3.0", + "gopd": "^1.2.0" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/echarts": { - "version": "5.6.0", - "resolved": "https://registry.npmjs.org/echarts/-/echarts-5.6.0.tgz", - "integrity": "sha512-oTbVTsXfKuEhxftHqL5xprgLoc0k7uScAwtryCgWF6hPYFLRwOUHiFmHGCBKP5NPFNkDVopOieyUqYGH8Fa3kA==", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/echarts/-/echarts-6.1.0.tgz", + "integrity": "sha512-q0yaFPggC9FUdsWH4blavRWFmxdrIodbkoKNAjJudAI6CA9gNPxHtV2RcZNEepZVlk4yvBYkOkbk6HIVpIyHZA==", "license": "Apache-2.0", "dependencies": { "tslib": "2.3.0", - "zrender": "5.6.1" + "zrender": "6.1.0" } }, "node_modules/echarts-wordcloud": { @@ -3064,6 +3343,26 @@ "url": "https://github.com/fb55/entities?sponsor=1" } }, + "node_modules/es-define-property": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz", + "integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-errors": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", + "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/es-module-lexer": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/es-module-lexer/-/es-module-lexer-2.1.0.tgz", @@ -3071,6 +3370,35 @@ "dev": true, "license": "MIT" }, + "node_modules/es-object-atoms": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.2.tgz", + "integrity": "sha512-HWcBoN6NileqtSydK2FqHbS/LoDd2pqrnQHLyJzBj4kOp/ky2MWMN694xOfkK8/SnUsW2DH7EfyVlydKCsm1Zw==", + "dev": true, + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/es-set-tostringtag": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz", + "integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==", + "dev": true, + "license": "MIT", + "dependencies": { + "es-errors": "^1.3.0", + "get-intrinsic": "^1.2.6", + "has-tostringtag": "^1.0.2", + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/es-toolkit": { "version": "1.47.0", "resolved": "https://registry.npmjs.org/es-toolkit/-/es-toolkit-1.47.0.tgz", @@ -3149,6 +3477,23 @@ } } }, + "node_modules/form-data": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.6.tgz", + "integrity": "sha512-vKatAh4SlVfgbv+YtmhiRjhEMJsYpsG1Y2rMQtR+SVSbytsSD1YGzDIcrAJmdFec88u/+VoGmxnl+80gL1tRCQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "es-set-tostringtag": "^2.1.0", + "hasown": "^2.0.4", + "mime-types": "^2.1.35" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -3163,6 +3508,68 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-intrinsic": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz", + "integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "call-bind-apply-helpers": "^1.0.2", + "es-define-property": "^1.0.1", + "es-errors": "^1.3.0", + "es-object-atoms": "^1.1.1", + "function-bind": "^1.1.2", + "get-proto": "^1.0.1", + "gopd": "^1.2.0", + "has-symbols": "^1.1.0", + "hasown": "^2.0.2", + "math-intrinsics": "^1.1.0" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/get-proto": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz", + "integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==", + "dev": true, + "license": "MIT", + "dependencies": { + "dunder-proto": "^1.0.1", + "es-object-atoms": "^1.0.0" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/gopd": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz", + "integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/graceful-fs": { "version": "4.2.11", "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", @@ -3175,6 +3582,80 @@ "integrity": "sha512-3GKBOn+m2LX9iq+JC1064cSFprJY4jL1jCXTcpnfER5HYE2l/4EfWSGzkPa/ZDBmYI0ZOEj5VHV/eKnPGkHuOg==", "license": "MIT" }, + "node_modules/happy-dom": { + "version": "15.11.7", + "resolved": "https://registry.npmjs.org/happy-dom/-/happy-dom-15.11.7.tgz", + "integrity": "sha512-KyrFvnl+J9US63TEzwoiJOQzZBJY7KgBushJA8X61DMbNsH+2ONkDuLDnCnwUiPTF42tLoEmrPyoqbenVA5zrg==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "entities": "^4.5.0", + "webidl-conversions": "^7.0.0", + "whatwg-mimetype": "^3.0.0" + }, + "engines": { + "node": ">=18.0.0" + } + }, + "node_modules/happy-dom/node_modules/entities": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz", + "integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==", + "dev": true, + "license": "BSD-2-Clause", + "optional": true, + "peer": true, + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, + "node_modules/has-symbols": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz", + "integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/has-tostringtag": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz", + "integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==", + "dev": true, + "license": "MIT", + "dependencies": { + "has-symbols": "^1.0.3" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/hasown": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.4.tgz", + "integrity": "sha512-T2UbfbBEF32wiepXIsMlTW9+dDYC6wMh/t/vYA4tuOMKqWz/n3vr1NFSxQiyP+zk2mXsoMA/i/7qV6LKut1t1A==", + "dev": true, + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/hast-util-to-html": { "version": "9.0.5", "resolved": "https://registry.npmjs.org/hast-util-to-html/-/hast-util-to-html-9.0.5.tgz", @@ -3211,6 +3692,19 @@ "url": "https://opencollective.com/unified" } }, + "node_modules/html-encoding-sniffer": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/html-encoding-sniffer/-/html-encoding-sniffer-4.0.0.tgz", + "integrity": "sha512-Y22oTqIU4uuPgEemfz7NDJz6OeKf12Lsu+QC+s3BVpda64lTiMYCyGwg5ki4vFxkMwQdeZDl2adZoqUgdFuTgQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "whatwg-encoding": "^3.1.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/html-void-elements": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/html-void-elements/-/html-void-elements-3.0.0.tgz", @@ -3221,6 +3715,34 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/http-proxy-agent": { + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-7.0.2.tgz", + "integrity": "sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig==", + "dev": true, + "license": "MIT", + "dependencies": { + "agent-base": "^7.1.0", + "debug": "^4.3.4" + }, + "engines": { + "node": ">= 14" + } + }, + "node_modules/https-proxy-agent": { + "version": "7.0.6", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz", + "integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==", + "dev": true, + "license": "MIT", + "dependencies": { + "agent-base": "^7.1.2", + "debug": "4" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/iconv-lite": { "version": "0.6.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz", @@ -3264,6 +3786,13 @@ "node": ">=12" } }, + "node_modules/is-potential-custom-element-name": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.1.tgz", + "integrity": "sha512-bCYeRA2rVibKZd+s2625gGnGF/t7DSqDs4dP7CrLA1m7jKWz6pps0LpYLJN8Q64HtmPKJ1hrN3nzPNKFEKOUiQ==", + "dev": true, + "license": "MIT" + }, "node_modules/is-reference": { "version": "3.0.3", "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-3.0.3.tgz", @@ -3289,6 +3818,57 @@ "jiti": "lib/jiti-cli.mjs" } }, + "node_modules/jsdom": { + "version": "25.0.1", + "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-25.0.1.tgz", + "integrity": "sha512-8i7LzZj7BF8uplX+ZyOlIz86V6TAsSs+np6m1kpW9u0JWi4z/1t+FzcK1aek+ybTnAC4KhBL4uXCNT0wcUIeCw==", + "dev": true, + "license": "MIT", + "dependencies": { + "cssstyle": "^4.1.0", + "data-urls": "^5.0.0", + "decimal.js": "^10.4.3", + "form-data": "^4.0.0", + "html-encoding-sniffer": "^4.0.0", + "http-proxy-agent": "^7.0.2", + "https-proxy-agent": "^7.0.5", + "is-potential-custom-element-name": "^1.0.1", + "nwsapi": "^2.2.12", + "parse5": "^7.1.2", + "rrweb-cssom": "^0.7.1", + "saxes": "^6.0.0", + "symbol-tree": "^3.2.4", + "tough-cookie": "^5.0.0", + "w3c-xmlserializer": "^5.0.0", + "webidl-conversions": "^7.0.0", + "whatwg-encoding": "^3.1.1", + "whatwg-mimetype": "^4.0.0", + "whatwg-url": "^14.0.0", + "ws": "^8.18.0", + "xml-name-validator": "^5.0.0" + }, + "engines": { + "node": ">=18" + }, + "peerDependencies": { + "canvas": "^2.11.2" + }, + "peerDependenciesMeta": { + "canvas": { + "optional": true + } + } + }, + "node_modules/jsdom/node_modules/whatwg-mimetype": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-4.0.0.tgz", + "integrity": "sha512-QaKxh0eNIi2mE9p2vEdzfagOKHCcj1pJ56EEHGQOVxp8r9/iszLUUV7v89x9O1p/T+NlTM5W7jW6+cz4Fq1YVg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, "node_modules/katex": { "version": "0.18.10", "resolved": "https://registry.npmjs.org/katex/-/katex-0.18.10.tgz", @@ -3586,6 +4166,13 @@ "integrity": "sha512-J8xewKD/Gk22OZbhpOVSwcs60zhd95ESDwezOFuA3/099925PdHJ7OFHNTGtajL3AlZkykD32HykiMo+BIBI8A==", "license": "MIT" }, + "node_modules/lru-cache": { + "version": "10.4.3", + "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-10.4.3.tgz", + "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", + "dev": true, + "license": "ISC" + }, "node_modules/lz-string": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/lz-string/-/lz-string-1.5.0.tgz", @@ -3616,6 +4203,16 @@ "node": ">= 20" } }, + "node_modules/math-intrinsics": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz", + "integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + } + }, "node_modules/mdast-util-to-hast": { "version": "13.2.1", "resolved": "https://registry.npmjs.org/mdast-util-to-hast/-/mdast-util-to-hast-13.2.1.tgz", @@ -3768,6 +4365,29 @@ ], "license": "MIT" }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dev": true, + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/mode-watcher": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/mode-watcher/-/mode-watcher-1.1.0.tgz", @@ -3851,6 +4471,13 @@ "node": ">=4" } }, + "node_modules/ms": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", + "integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==", + "dev": true, + "license": "MIT" + }, "node_modules/nanoid": { "version": "3.3.20", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.20.tgz", @@ -3891,6 +4518,13 @@ "url": "https://nearley.js.org/#give-to-nearley" } }, + "node_modules/nwsapi": { + "version": "2.2.28", + "resolved": "https://registry.npmjs.org/nwsapi/-/nwsapi-2.2.28.tgz", + "integrity": "sha512-IlVB7OS7qrOsVYlpnFIkETjMwT9jwvmocJmmM+GZU/PAB3uGi9Ezd7vcWhWBUnSc0ya4ppmQITOyP1ez9gg8cg==", + "dev": true, + "license": "MIT" + }, "node_modules/obug": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/obug/-/obug-2.1.1.tgz", @@ -3925,6 +4559,32 @@ "integrity": "sha512-61A5ThoTiDG/C8s8UMZwSorAGwMJ0ERVGj2OjoW5pAalsNOg15+iQiPzrLJ4jhZ1HJzmC2PIHT2oEiH3R5fzNA==", "license": "MIT" }, + "node_modules/parse5": { + "version": "7.3.0", + "resolved": "https://registry.npmjs.org/parse5/-/parse5-7.3.0.tgz", + "integrity": "sha512-IInvU7fabl34qmi9gY8XOVxhYyMyuH2xUNpb2q8/Y+7552KlejkRvqvD19nMoUW/uQGGbqNpA6Tufu5FL5BZgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "entities": "^6.0.0" + }, + "funding": { + "url": "https://github.com/inikulin/parse5?sponsor=1" + } + }, + "node_modules/parse5/node_modules/entities": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/entities/-/entities-6.0.1.tgz", + "integrity": "sha512-aN97NXWF6AWBTahfVOIrB/NShkzi5H7F9r1s9mD3cDj4Ko5f2qhhVoYMibXF7GlLveb/D2ioWay8lxI97Ven3g==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=0.12" + }, + "funding": { + "url": "https://github.com/fb55/entities?sponsor=1" + } + }, "node_modules/path-data-parser": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/path-data-parser/-/path-data-parser-0.1.0.tgz", @@ -4039,6 +4699,16 @@ "url": "https://github.com/sponsors/wooorm" } }, + "node_modules/punycode": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", + "integrity": "sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, "node_modules/railroad-diagrams": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/railroad-diagrams/-/railroad-diagrams-1.0.0.tgz", @@ -4156,6 +4826,13 @@ "points-on-path": "^0.2.1" } }, + "node_modules/rrweb-cssom": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/rrweb-cssom/-/rrweb-cssom-0.7.1.tgz", + "integrity": "sha512-TrEMa7JGdVm0UThDJSx7ddw5nVm3UJS9o9CCIZ72B1vSyEZoziDqBYP3XIoi/12lKrJR8rE3jeFHMok2F/Mnsg==", + "dev": true, + "license": "MIT" + }, "node_modules/runed": { "version": "0.35.1", "resolved": "https://registry.npmjs.org/runed/-/runed-0.35.1.tgz", @@ -4205,6 +4882,19 @@ "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==", "license": "MIT" }, + "node_modules/saxes": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/saxes/-/saxes-6.0.0.tgz", + "integrity": "sha512-xAg7SOnEhrm5zI3puOOKyy1OMcMlIJZYNJY7xLBwSze0UjhPLnWfj2GF2EpT0jmzaJKIWKHLsaSSajf35bcYnA==", + "dev": true, + "license": "ISC", + "dependencies": { + "xmlchars": "^2.2.0" + }, + "engines": { + "node": ">=v12.22.7" + } + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", @@ -4437,6 +5127,13 @@ "integrity": "sha512-XEzy5HCw7yESb7QajKTBJ+NA/BL0kAKlE7XhSMPZawF740X4ws/5CFtXRsVDQ+EztISC759a9+DJM9mxjz4hXg==", "license": "GPL-3.0" }, + "node_modules/symbol-tree": { + "version": "3.2.4", + "resolved": "https://registry.npmjs.org/symbol-tree/-/symbol-tree-3.2.4.tgz", + "integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==", + "dev": true, + "license": "MIT" + }, "node_modules/tabbable": { "version": "6.4.0", "resolved": "https://registry.npmjs.org/tabbable/-/tabbable-6.4.0.tgz", @@ -4533,6 +5230,52 @@ "node": ">=14.0.0" } }, + "node_modules/tldts": { + "version": "6.1.86", + "resolved": "https://registry.npmjs.org/tldts/-/tldts-6.1.86.tgz", + "integrity": "sha512-WMi/OQ2axVTf/ykqCQgXiIct+mSQDFdH2fkwhPwgEwvJ1kSzZRiinb0zF2Xb8u4+OqPChmyI6MEu4EezNJz+FQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "tldts-core": "^6.1.86" + }, + "bin": { + "tldts": "bin/cli.js" + } + }, + "node_modules/tldts-core": { + "version": "6.1.86", + "resolved": "https://registry.npmjs.org/tldts-core/-/tldts-core-6.1.86.tgz", + "integrity": "sha512-Je6p7pkk+KMzMv2XXKmAE3McmolOQFdxkKw0R8EYNr7sELW46JqnNeTX8ybPiQgvg1ymCoF8LXs5fzFaZvJPTA==", + "dev": true, + "license": "MIT" + }, + "node_modules/tough-cookie": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-5.1.2.tgz", + "integrity": "sha512-FVDYdxtnj0G6Qm/DhNPSb8Ju59ULcup3tuJxkFb5K8Bv2pUXILbf0xZWU8PX8Ov19OXljbUyveOFwRMwkXzO+A==", + "dev": true, + "license": "BSD-3-Clause", + "dependencies": { + "tldts": "^6.1.32" + }, + "engines": { + "node": ">=16" + } + }, + "node_modules/tr46": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/tr46/-/tr46-5.1.1.tgz", + "integrity": "sha512-hdF5ZgjTqgAntKkklYw0R03MG2x/bSzTtkxmIRw/sTNV8YXsCJ1tfLAX23lhxhHJlEf3CRCOCGGWw3vI3GaSPw==", + "dev": true, + "license": "MIT", + "dependencies": { + "punycode": "^2.3.1" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/trim-lines": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/trim-lines/-/trim-lines-3.0.1.tgz", @@ -5132,6 +5875,69 @@ "integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==", "license": "MIT" }, + "node_modules/w3c-xmlserializer": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-5.0.0.tgz", + "integrity": "sha512-o8qghlI8NZHU1lLPrpi2+Uq7abh4GGPpYANlalzWxyWteJOCsr/P+oPBA49TOLu5FTZO4d3F9MnWJfiMo4BkmA==", + "dev": true, + "license": "MIT", + "dependencies": { + "xml-name-validator": "^5.0.0" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/webidl-conversions": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", + "integrity": "sha512-VwddBukDzu71offAQR975unBIGqfKZpM+8ZX6ySk8nYhVoo5CYaZyzt3YBvYtRtO+aoGlqxPg/B87NGVZ/fu6g==", + "dev": true, + "license": "BSD-2-Clause", + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-encoding": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/whatwg-encoding/-/whatwg-encoding-3.1.1.tgz", + "integrity": "sha512-6qN4hJdMwfYBtE3YBTTHhoeuUrDBPZmbQaxWAqSALV/MeEnR5z1xd8UKud2RAkFoPkmB+hli1TZSnyi84xz1vQ==", + "deprecated": "Use @exodus/bytes instead for a more spec-conformant and faster implementation", + "dev": true, + "license": "MIT", + "dependencies": { + "iconv-lite": "0.6.3" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/whatwg-mimetype": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/whatwg-mimetype/-/whatwg-mimetype-3.0.0.tgz", + "integrity": "sha512-nt+N2dzIutVRxARx1nghPKGv1xHikU7HKdfafKkLNLindmPU/ch3U31NOCGGA/dmPcmb1VlofO0vnKAcsm0o/Q==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "engines": { + "node": ">=12" + } + }, + "node_modules/whatwg-url": { + "version": "14.2.0", + "resolved": "https://registry.npmjs.org/whatwg-url/-/whatwg-url-14.2.0.tgz", + "integrity": "sha512-De72GdQZzNTUBBChsXueQUnPKDkg/5A5zp7pFDuQAj5UFoENpiACU0wlCvzpAGnTkj++ihpKwKyYewn/XNUbKw==", + "dev": true, + "license": "MIT", + "dependencies": { + "tr46": "^5.1.0", + "webidl-conversions": "^7.0.0" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/which": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/which/-/which-2.0.2.tgz", @@ -5165,6 +5971,45 @@ "node": ">=8" } }, + "node_modules/ws": { + "version": "8.23.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.23.0.tgz", + "integrity": "sha512-wCkmX8QMOrKjgO8HW2WyDbTSGeqkocxgDzS9iR79rEAexty6/2qzilx3Ag8Tqf9ykop3irLKle0MtVn5qjVnoQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/xml-name-validator": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-5.0.0.tgz", + "integrity": "sha512-EvGK8EJ3DhaHfbRlETOWAS5pO9MZITeauHKJyb8wyajUfQUenkIg2MvLDTZ4T/TgIcm3HU0TFBgWWboAZ30UHg==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=18" + } + }, + "node_modules/xmlchars": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/xmlchars/-/xmlchars-2.2.0.tgz", + "integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==", + "dev": true, + "license": "MIT" + }, "node_modules/zimmerframe": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/zimmerframe/-/zimmerframe-1.1.4.tgz", @@ -5172,9 +6017,9 @@ "license": "MIT" }, "node_modules/zrender": { - "version": "5.6.1", - "resolved": "https://registry.npmjs.org/zrender/-/zrender-5.6.1.tgz", - "integrity": "sha512-OFXkDJKcrlx5su2XbzJvj/34Q3m6PvyCZkVPHGYpcCJ52ek4U/ymZyfuV1nKE23AyBJ51E/6Yr0mhZ7xGTO4ag==", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/zrender/-/zrender-6.1.0.tgz", + "integrity": "sha512-oEGMDB6pOP2S6OwRR4PdVv610zrjnA3Bh+JnSG12fYJlBKjtNAoEb5fSUoCOOINlH96I2fU38/A2UpRKs67xYQ==", "license": "BSD-3-Clause", "dependencies": { "tslib": "2.3.0" diff --git a/package.json b/package.json index 9f86d61f..4f996907 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "stroke", "private": true, - "version": "2.5.5", + "version": "3.0.0", "description": "Fast, minimal desktop database client for PostgreSQL, MySQL, SQLite, D1, and more", "license": "SEE LICENSE IN LICENSE", "homepage": "https://stroke.click", @@ -17,7 +17,7 @@ "dev": "vite", "build": "vite build", "preview": "vite preview", - "test": "vitest run", + "test": "vitest run && vitest run -c vitest.dom.config.js", "test:watch": "vitest", "check": "svelte-check --threshold error", "check:all": "svelte-check", @@ -34,7 +34,8 @@ "fixtures:reset": "bash scripts/fixtures.sh reset", "fixtures:down": "bash scripts/fixtures.sh down", "lint:design": "node scripts/lint-design.mjs", - "perf": "node scripts/perf-report.mjs" + "perf": "node scripts/perf-report.mjs", + "test:dom": "vitest run -c vitest.dom.config.js" }, "devDependencies": { "@emnapi/core": "^1.10.0", @@ -45,6 +46,7 @@ "@tauri-apps/api": "^2.12.4", "@tauri-apps/cli": "^2.11.2", "cross-env": "^10.1.0", + "jsdom": "^25.0.1", "mode-watcher": "^1.1.0", "svelte": "5.55.9", "svelte-check": "^4.7.6", @@ -96,7 +98,8 @@ "canvas-confetti": "^1.9.4", "chart.js": "^4.5.1", "clsx": "^2.1.1", - "echarts": "^5.6.0", + "croner": "^9.1.0", + "echarts": "^6.1.0", "echarts-wordcloud": "^2.1.0", "elkjs": "^0.11.1", "idb": "^8.0.3", @@ -114,6 +117,9 @@ }, "overrides": { "dompurify": "^3.4.13", - "katex": "^0.18.2" + "katex": "^0.18.2", + "echarts-wordcloud": { + "echarts": "$echarts" + } } } diff --git a/packaging/aur/stroke-bin/PKGBUILD.in b/packaging/aur/stroke-bin/PKGBUILD.in new file mode 100644 index 00000000..8d3dfad5 --- /dev/null +++ b/packaging/aur/stroke-bin/PKGBUILD.in @@ -0,0 +1,36 @@ +# Maintainer: broisnees +# Generated by .github/workflows/release.yml (update-aur) on each stable +# release, which fills in the version and the .deb checksum. +pkgname=stroke-bin +pkgver=@VERSION@ +pkgrel=1 +pkgdesc="Native database client for PostgreSQL, MySQL, SQLite, MongoDB, Redis and more, with an AI agent and an MCP server" +arch=('x86_64') +url="https://stroke.click" +license=('LicenseRef-Stroke') +depends=('webkit2gtk-4.1' 'gtk3' 'hicolor-icon-theme') +provides=('stroke') +conflicts=('stroke') +options=('!strip' '!debug') +source=("stroke-${pkgver}.deb::https://github.com/stroke-app/stroke/releases/download/v${pkgver}/stroke_${pkgver}_amd64.deb" + "LICENSE-${pkgver}::https://raw.githubusercontent.com/stroke-app/stroke/v${pkgver}/LICENSE") +sha256sums=('@SHA256@' + 'SKIP') + +package() { + bsdtar -xf data.tar.gz -C "$pkgdir" + + # The .deb names its binary and icon "app"; on Arch that would collide with + # anything else called app, so both become "stroke". + mv "$pkgdir/usr/bin/app" "$pkgdir/usr/bin/stroke" + for icon in "$pkgdir"/usr/share/icons/hicolor/*/apps/app.png; do + mv "$icon" "${icon%/app.png}/stroke.png" + done + sed -i -e 's/^Exec=app$/Exec=stroke/' \ + -e 's/^Icon=app$/Icon=stroke/' \ + -e 's/^StartupWMClass=app$/StartupWMClass=stroke/' \ + -e 's/^Categories=$/Categories=Development;Database;/' \ + "$pkgdir/usr/share/applications/Stroke.desktop" + + install -Dm644 "LICENSE-${pkgver}" "$pkgdir/usr/share/licenses/$pkgname/LICENSE" +} diff --git a/packaging/aur/stroke-nightly-bin/PKGBUILD.in b/packaging/aur/stroke-nightly-bin/PKGBUILD.in new file mode 100644 index 00000000..c426c4ca --- /dev/null +++ b/packaging/aur/stroke-nightly-bin/PKGBUILD.in @@ -0,0 +1,29 @@ +# Maintainer: broisnees +# Generated by .github/workflows/nightly.yml (update-aur) on each nightly +# build, which fills in the version, the .deb checksum and the commit. +pkgname=stroke-nightly-bin +pkgver=@PKGVER@ +pkgrel=1 +pkgdesc="Stroke Nightly: the daily build of the native database client, installed next to Stroke" +arch=('x86_64') +url="https://stroke.click" +license=('LicenseRef-Stroke') +depends=('webkit2gtk-4.1' 'gtk3' 'hicolor-icon-theme') +provides=('stroke-nightly') +conflicts=('stroke-nightly') +options=('!strip' '!debug') +# The nightly release is replaced each night under the same tag, so the file +# is saved under this build's version and pinned by its checksum. +source=("stroke-nightly-${pkgver}.deb::https://github.com/stroke-app/stroke/releases/download/nightly/stroke-nightly_amd64.deb" + "LICENSE-${pkgver}::https://raw.githubusercontent.com/stroke-app/stroke/@COMMIT@/LICENSE") +sha256sums=('@SHA256@' + 'SKIP') + +package() { + bsdtar -xf data.tar.gz -C "$pkgdir" + # The .deb already names everything stroke-nightly; only the menu category + # is empty. + sed -i 's/^Categories=$/Categories=Development;Database;/' \ + "$pkgdir/usr/share/applications/Stroke Nightly.desktop" + install -Dm644 "LICENSE-${pkgver}" "$pkgdir/usr/share/licenses/$pkgname/LICENSE" +} diff --git a/scripts/appimage-strip-host-libs.sh b/scripts/appimage-strip-host-libs.sh index 3d628388..61324518 100755 --- a/scripts/appimage-strip-host-libs.sh +++ b/scripts/appimage-strip-host-libs.sh @@ -14,6 +14,10 @@ # Tested on Arch, Fedora 41/45, Debian 12/13 and Ubuntu 22.04/24.04. # Issue: https://github.com/stroke-app/stroke/issues/112 # +# It also stops linuxdeploy's GTK hook from hard-setting GDK_BACKEND=x11, so +# Stroke can choose: native Wayland, with an automatic X11 fallback when a +# Wayland launch never draws (display_backend.rs). +# # The image is repacked with its own runtime and the same squashfs settings, # so nothing else about the AppImage changes. Needs squashfs-tools. @@ -44,20 +48,36 @@ find_excluded() { find "$root" \( -type f -o -type l \) \( "${args[@]}" \) "$@" } +changed=0 removed="$(find_excluded -print | sed "s|^$root/||" | sort)" -if [ -z "$removed" ]; then - echo "appimage: no host-only libraries bundled, nothing to do" - exit 0 +if [ -n "$removed" ]; then + find_excluded -delete + echo "appimage: removed" + printf ' %s\n' $removed + changed=1 fi -find_excluded -delete -echo "appimage: removed" -printf ' %s\n' $removed if [ -n "$(find_excluded -print)" ]; then echo "appimage: host-only libraries still present" >&2 exit 1 fi +# X11 by default, native Wayland with STROKE_WAYLAND=1. +hook="$root/apprun-hooks/linuxdeploy-plugin-gtk.sh" +if [ -f "$hook" ] && grep -qE '^export GDK_BACKEND=x11' "$hook"; then + # Stroke picks the backend itself (src-tauri/src/display_backend.rs: Wayland, + # falling back to X11 if a Wayland launch never draws), so the hook only + # passes the two overrides through. + sed -i -E 's|^export GDK_BACKEND=x11.*$|if [ "${STROKE_WAYLAND:-}" = 1 ]; then export GDK_BACKEND=wayland; elif [ "${STROKE_X11:-}" = 1 ]; then export GDK_BACKEND=x11; fi|' "$hook" + echo "appimage: GDK_BACKEND left to Stroke (STROKE_X11=1 / STROKE_WAYLAND=1 override)" + changed=1 +fi + +if [ "$changed" = 0 ]; then + echo "appimage: nothing to change" + exit 0 +fi + # Same compression and block size linuxdeploy's appimagetool uses. mksquashfs "$root" "$work/fs.squashfs" -root-owned -noappend -comp zstd -b 131072 -no-progress -quiet cat "$work/runtime" "$work/fs.squashfs" > "$work/out.AppImage" diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 231358f2..e722a9bc 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -178,6 +178,8 @@ dependencies = [ "chrono", "duckdb", "ed25519-dalek", + "encoding_rs", + "flate2", "futures", "gdk", "getrandom 0.2.17", @@ -193,6 +195,7 @@ dependencies = [ "objc2-app-kit", "objc2-quartz-core", "objc2-web-kit", + "oracle", "portable-pty", "redis", "reqwest 0.12.28", @@ -205,10 +208,12 @@ dependencies = [ "sha2 0.10.9", "sqlx", "sysinfo", + "tar", "tauri", "tauri-build", "tauri-plugin-autostart", "tauri-plugin-clipboard-manager", + "tauri-plugin-deep-link", "tauri-plugin-dialog", "tauri-plugin-log", "tauri-plugin-opener", @@ -1598,6 +1603,16 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "darling" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a01d95850c592940db9b8194bc39f4bc0e89dee5c4265e4b1807c34a9aba453c" +dependencies = [ + "darling_core 0.13.4", + "darling_macro 0.13.4", +] + [[package]] name = "darling" version = "0.23.0" @@ -1618,6 +1633,20 @@ dependencies = [ "darling_macro 0.24.1", ] +[[package]] +name = "darling_core" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "859d65a907b6852c9361e3185c862aae7fafd2887876799fa55f5f99dc40d610" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim 0.10.0", + "syn 1.0.109", +] + [[package]] name = "darling_core" version = "0.23.0" @@ -1627,7 +1656,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", + "strsim 0.11.1", "syn 2.0.117", ] @@ -1640,10 +1669,21 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", + "strsim 0.11.1", "syn 3.0.10", ] +[[package]] +name = "darling_macro" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" +dependencies = [ + "darling_core 0.13.4", + "quote", + "syn 1.0.109", +] + [[package]] name = "darling_macro" version = "0.23.0" @@ -1843,7 +1883,7 @@ dependencies = [ "libc", "option-ext", "redox_users 0.5.2", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -1892,6 +1932,15 @@ dependencies = [ "syn 2.0.117", ] +[[package]] +name = "dlv-list" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "442039f5147480ba31067cb00ada1adae6892028e40e45fc5de7b7df6dcc1b5f" +dependencies = [ + "const-random", +] + [[package]] name = "dom_query" version = "0.28.0" @@ -2110,7 +2159,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -3095,7 +3144,7 @@ dependencies = [ "js-sys", "log", "wasm-bindgen", - "windows-core 0.57.0", + "windows-core 0.62.2", ] [[package]] @@ -3972,7 +4021,7 @@ dependencies = [ "sha2 0.11.1", "socket2", "stringprep", - "strsim", + "strsim 0.11.1", "take_mut", "thiserror 2.0.18", "tokio", @@ -4023,7 +4072,7 @@ dependencies = [ "png", "serde", "thiserror 2.0.18", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -4522,6 +4571,15 @@ dependencies = [ "memchr", ] +[[package]] +name = "odpic-sys" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "920b5474a5128a9f0232df5a0ffc50aaa5b077b29b8b06ab0131985ac82793ed" +dependencies = [ + "cc", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -4562,6 +4620,42 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "oracle" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db40fe6e4df881b683691ade5ef1f7b1afd52aefa115581f7b92855524d7ec0" +dependencies = [ + "cc", + "odpic-sys", + "once_cell", + "oracle_procmacro", + "paste", + "rustversion", +] + +[[package]] +name = "oracle_procmacro" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad247f3421d57de56a0d0408d3249d4b1048a522be2013656d92f022c3d8af27" +dependencies = [ + "darling 0.13.4", + "proc-macro2", + "quote", + "syn 1.0.109", +] + +[[package]] +name = "ordered-multimap" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49203cdcae0030493bad186b28da2fa25645fa276a51b6fec8010d281e02ef79" +dependencies = [ + "dlv-list", + "hashbrown 0.14.5", +] + [[package]] name = "ordered-stream" version = "0.2.0" @@ -4579,7 +4673,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d8fae84b431384b68627d0f9b3b1245fcf9f46f6c0e3dc902e9dce64edd1967" dependencies = [ "libc", - "windows-sys 0.45.0", + "windows-sys 0.61.2", ] [[package]] @@ -4661,6 +4755,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pathdiff" version = "0.2.3" @@ -5642,6 +5742,16 @@ dependencies = [ "zeroize", ] +[[package]] +name = "rust-ini" +version = "0.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "796e8d2b6696392a43bea58116b667fb4c29727dc5abd27d6acf338bb4f688c7" +dependencies = [ + "cfg-if", + "ordered-multimap", +] + [[package]] name = "rust_decimal" version = "1.42.0" @@ -5707,7 +5817,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys 0.12.1", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -5766,7 +5876,7 @@ dependencies = [ "security-framework 3.7.0", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -6434,7 +6544,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -6717,7 +6827,7 @@ dependencies = [ "cfg-if", "libc", "psm", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -6791,6 +6901,12 @@ dependencies = [ "unicode-properties", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "strsim" version = "0.11.1" @@ -7090,7 +7206,7 @@ dependencies = [ "serde", "serde_json", "static_vcruntime", - "strsim", + "strsim 0.11.1", "tauri-utils", "tauri-winres", "walkdir", @@ -7182,6 +7298,27 @@ dependencies = [ "thiserror 2.0.18", ] +[[package]] +name = "tauri-plugin-deep-link" +version = "2.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab551d9bec27bb79d1c7fe5ed38c5cfd1cebb86efa745acc65f5853f345f6e56" +dependencies = [ + "dunce", + "plist", + "rust-ini", + "serde", + "serde_json", + "tauri", + "tauri-plugin", + "tauri-utils", + "thiserror 2.0.18", + "tracing", + "url", + "windows-registry", + "windows-result 0.4.1", +] + [[package]] name = "tauri-plugin-dialog" version = "2.7.1" @@ -7411,7 +7548,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix 1.1.4", - "windows-sys 0.59.0", + "windows-sys 0.61.2", ] [[package]] @@ -7874,7 +8011,7 @@ dependencies = [ "png", "serde", "thiserror 2.0.18", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -7970,7 +8107,7 @@ checksum = "f2f6fb2847f6742cd76af783a2a2c49e9375d0a111c7bef6f71cd9e738c72d6e" dependencies = [ "memoffset", "tempfile", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -8601,7 +8738,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.48.0", + "windows-sys 0.61.2", ] [[package]] @@ -8622,7 +8759,7 @@ dependencies = [ "objc2-foundation", "objc2-quartz-core", "raw-window-handle", - "windows-sys 0.60.2", + "windows-sys 0.61.2", "windows-version", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 254e3068..f2dd320f 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -28,10 +28,15 @@ tauri-plugin-updater = "2" tauri-plugin-dialog = "2" tauri-plugin-autostart = "2" tauri-plugin-clipboard-manager = "2" +# stroke://open?url=… links from `npx strokedb` and stroke.click/studio (launch.rs). +tauri-plugin-deep-link = "2" tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "net", "sync", "process"] } axum = { version = "0.7", features = ["json", "macros", "ws"] } tower-http = { version = "0.5", features = ["cors"] } sqlx = { version = "0.8", features = ["runtime-tokio", "tls-rustls", "postgres", "sqlite", "mysql", "json", "chrono", "uuid", "rust_decimal"] } +flate2 = "1" +tar = "0.4" +encoding_rs = "0.8" reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls-webpki-roots", "stream"] } futures = "0.3" # Local agent CLIs (Claude Code, Codex, OpenCode), vendored from deck. @@ -67,11 +72,15 @@ argon2 = "0.5" keyring = { version = "3", features = ["apple-native", "windows-native", "sync-secret-service", "crypto-rust"] } duckdb = { version = "1.3.2", features = ["bundled"] } tiberius = { version = "0.13.0", default-features = false, features = ["rustls", "tds73", "chrono"] } +# Oracle: ODPI-C is compiled from source by the crate (no Oracle files at build +# time); the Oracle Client library (Instant Client) is loaded at runtime on the +# first Oracle connect, so it is only needed by users who connect to Oracle. +oracle = "0.6.3" # Pinned as a direct dependency only to pick the TLS crypto provider at # startup (see `run`): tiberius 0.13 enables aws-lc-rs next to the ring the # other drivers use, and rustls then has no default of its own. rustls = { version = "0.23", default-features = false, features = ["ring", "std"] } -tokio-util = { version = "0.7.18", features = ["compat"] } +tokio-util = { version = "0.7.18", features = ["compat", "io"] } # The SQL terminal: the engine's own CLI (psql, mysql, sqlite3...) in a # pseudo-terminal. ConPTY on Windows, openpty everywhere else. portable-pty = "0.9" diff --git a/src-tauri/Entitlements.plist b/src-tauri/Entitlements.plist new file mode 100644 index 00000000..2f4ef70f --- /dev/null +++ b/src-tauri/Entitlements.plist @@ -0,0 +1,8 @@ + + + + + com.apple.security.cs.disable-library-validation + + + diff --git a/src-tauri/crates/agent-claude/src/lib.rs b/src-tauri/crates/agent-claude/src/lib.rs index d14fdfde..f0b4e81f 100644 --- a/src-tauri/crates/agent-claude/src/lib.rs +++ b/src-tauri/crates/agent-claude/src/lib.rs @@ -22,6 +22,32 @@ use tokio::process::{Child, ChildStdin, Command}; use tokio::sync::mpsc; /// Tokens of transcript we are willing to replay on a handoff. + +/// `CREATE_NO_WINDOW`. Stroke is a GUI process, so on Windows every console +/// program it starts (`claude --version`, the chat itself) got a console of +/// its own: a black cmd window popped up on top of the app each time, and the +/// focus jumping to it could take WebView2 down with it. Unix has nothing +/// like it, so off Windows these are no-ops. +#[cfg(windows)] +const CREATE_NO_WINDOW: u32 = 0x0800_0000; + +/// Start this console program with no console window (Windows). +pub fn quiet(cmd: &mut Command) -> &mut Command { + #[cfg(windows)] + cmd.creation_flags(CREATE_NO_WINDOW); + cmd +} + +/// The blocking-`Command` form of [`quiet`]. +pub fn quiet_std(cmd: &mut std::process::Command) -> &mut std::process::Command { + #[cfg(windows)] + { + use std::os::windows::process::CommandExt; + cmd.creation_flags(CREATE_NO_WINDOW); + } + cmd +} + const REPLAY_BUDGET: usize = 60_000; /// Bound on buffered events. Deep enough to absorb a fast burst, shallow enough @@ -191,7 +217,7 @@ impl AgentAdapter for ClaudeAdapter { } async fn probe(&self) -> Result { - let out = Command::new(&self.bin).arg("--version").output().await; + let out = quiet(Command::new(&self.bin).arg("--version")).output().await; match out { Ok(o) if o.status.success() => Ok(Availability::Ready { version: Some(String::from_utf8_lossy(&o.stdout).trim().to_string()), @@ -210,7 +236,7 @@ impl AgentAdapter for ClaudeAdapter { let mcp_config = self.write_mcp_config(&ctx).await?; let args = self.args(&ctx, mcp_config.as_ref()); - let mut child = Command::new(&self.bin) + let mut child = quiet(&mut Command::new(&self.bin)) .args(&args) .envs(ctx.env.iter().map(|(k, v)| (k, v))) .current_dir(&ctx.cwd) diff --git a/src-tauri/crates/agent-cli/src/lib.rs b/src-tauri/crates/agent-cli/src/lib.rs index 59d9a5df..4369997f 100644 --- a/src-tauri/crates/agent-cli/src/lib.rs +++ b/src-tauri/crates/agent-cli/src/lib.rs @@ -127,7 +127,7 @@ impl AgentAdapter for CliAdapter { let listing = self.refresh_models(); // `--version` is the cheapest liveness check that does not hit the // network or consume credits. - let version = Command::new(self.spec.bin) + let version = agent_claude::quiet(&mut Command::new(self.spec.bin)) .arg("--version") .stdin(Stdio::null()) .output() @@ -259,6 +259,7 @@ pub(crate) fn spawn_turn( resuming: bool, ) -> Result { let mut cmd = Command::new(spec.bin); + agent_claude::quiet(&mut cmd); cmd.args(&args) .envs(spec.env.iter().copied()) .envs(ctx.env.iter().map(|(k, v)| (k, v))) diff --git a/src-tauri/crates/agent-cli/src/models.rs b/src-tauri/crates/agent-cli/src/models.rs index ea928bba..577ad3b0 100644 --- a/src-tauri/crates/agent-cli/src/models.rs +++ b/src-tauri/crates/agent-cli/src/models.rs @@ -84,7 +84,7 @@ fn leak(listed: Vec) -> &'static [ModelSpec] { /// Run a CLI's list command and read its answer. Blocks, so it runs on a /// thread of its own. pub(crate) fn read(bin: &str, list: &ListModels) -> Option> { - let child = std::process::Command::new(bin) + let child = agent_claude::quiet_std(&mut std::process::Command::new(bin)) .args(list.args) .stdin(Stdio::null()) .stdout(Stdio::piped()) @@ -169,7 +169,7 @@ pub(crate) async fn codex_default(bin: &str) -> Option { .and_then(|text| top_level(text, "model"))?; let out = tokio::time::timeout( Duration::from_secs(5), - tokio::process::Command::new(bin) + agent_claude::quiet(&mut tokio::process::Command::new(bin)) .args(["debug", "models"]) .stdin(Stdio::null()) .stderr(Stdio::null()) diff --git a/src-tauri/crates/agent-cli/src/opencode.rs b/src-tauri/crates/agent-cli/src/opencode.rs index cbcdc74d..fb52046a 100644 --- a/src-tauri/crates/agent-cli/src/opencode.rs +++ b/src-tauri/crates/agent-cli/src/opencode.rs @@ -61,7 +61,7 @@ kill -TERM 0"#; /// opencode 2 moved the server's API under `/api` with new routes, which the /// session below does not speak; on 2 and later each turn is an `opencode run`. pub(crate) async fn serve_api_moved(bin: &str) -> bool { - let out = Command::new(bin) + let out = agent_claude::quiet(&mut Command::new(bin)) .arg("--version") .stdin(Stdio::null()) .output() diff --git a/src-tauri/src/account.rs b/src-tauri/src/account.rs index 6e85791c..a9755472 100644 --- a/src-tauri/src/account.rs +++ b/src-tauri/src/account.rs @@ -14,9 +14,11 @@ //! until `valid_until`. Every read of the cache re-verifies the signature, so //! editing the file grants nothing. //! -//! Existing customers: an activated `license.json` keeps working with or without -//! an account (see `combine`), and the device trial still applies to anyone who -//! is not signed in. +//! Stroke 3 has no license keys and no device trial: the signed-in account is +//! the only source of Pro. A license bought with the account's email makes it +//! Pro; every other account gets one trial (`X-Stroke-Trial: account-v3`, a +//! fresh one for v3), then the free plan. A `license.json` left by 2.x stays on +//! disk, untouched and unread, so going back to 2.x still works. use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; use ed25519_dalek::{Signature, Verifier, VerifyingKey}; @@ -37,8 +39,6 @@ const DEFAULT_BASE: &str = "https://stroke.click"; /// Vault entry holding the desktop session token. const TOKEN_KEY: &str = "__stroke_account_token__"; const CACHE_FILE: &str = "account.json"; -/// Present while a new install still has to sign in (or enter a key). -const ONBOARDING_FILE: &str = ".account-onboarding"; const SIGN_IN_TIMEOUT: Duration = Duration::from_secs(300); /// Base URL of the website/API. `STROKE_API_BASE` overrides it for local dev. @@ -239,12 +239,6 @@ struct CacheFile { /// The `/api/me` body, verbatim. me: String, fetched_at: u64, - /// SHA-256 of the license key already sent to `/api/license/link`. - #[serde(default)] - linked_key_hash: Option, - /// Why linking failed in a way retrying won't fix (`key_owned_elsewhere`). - #[serde(default)] - link_error: Option, /// The device that wrote this cache. A copy taken to another machine is /// ignored there, which has to sign in for itself. #[serde(default)] @@ -315,24 +309,6 @@ pub fn entitlement_status(ent: &Entitlement, email: &str, now: u64) -> LicenseSt LicenseStatus::TrialExpired } -/// Merge the account's verdict into the local (key / device-trial) one. -/// -/// - No fresh account entitlement: the local status, unchanged. -/// - Account grants Pro via a license: that wins. -/// - A valid local `license.json` is never downgraded by the account: Pro. -/// - Otherwise the account decides (its trial, or no Pro), replacing the -/// per-device trial for signed-in users. -pub fn combine(local: LicenseStatus, account: Option) -> LicenseStatus { - match account { - None => local, - Some(acc @ LicenseStatus::Valid { .. }) => acc, - Some(acc) => match local { - LicenseStatus::Valid { .. } => local, - _ => acc, - }, - } -} - /// The account's status from the cache only: no network, no keychain. Used by /// the license commands so existing Pro gates see account entitlements, also /// when stroke.click can't be reached (see `OFFLINE_GRACE_SECS`). @@ -362,41 +338,27 @@ fn local_dev_pro() -> Option { }) } -pub fn apply_account(dir: &Path, local: LicenseStatus) -> LicenseStatus { +/// The license status, from the signed-in account alone. +/// +/// - Signed out, or no usable entitlement: no Pro (the app asks to sign in). +/// - Fresh entitlement, or paid Pro on the offline grace: what it says. +/// - A trial entitlement past its window keeps counting offline until the +/// trial's own signed end, so a trial doesn't stop on a flight. +pub fn license_status(dir: &Path) -> LicenseStatus { if let Some(dev) = local_dev_pro() { return dev; } - if matches!(local, LicenseStatus::Error { .. }) { - return local; + if let Some(status) = account_license_status(dir) { + return status; } - combine(local, account_license_status(dir)) -} - -// ── New-install detection ───────────────────────────────────────────────────── - -/// Record, once per run and before anything writes the trial files, whether -/// this is a brand-new install (no license, no trial ever started). A new -/// install gets a marker that keeps the sign-in screen up across relaunches -/// until the user signs in or enters a key. Existing installs never get one. -pub fn note_install_state(dir: &Path) { - static NOTED: OnceLock<()> = OnceLock::new(); - NOTED.get_or_init(|| { - let fresh = crate::license::load_license(dir).is_none() - && crate::license::local_trial_start(dir).is_none() - && read_cache(dir).is_none(); - if fresh { - let _ = std::fs::create_dir_all(dir); - let _ = std::fs::write(dir.join(ONBOARDING_FILE), b"1"); + if let Some((me, _)) = cached_me(dir) { + let now = crate::license::trusted_now(dir); + let ent = &me.entitlement; + if ent.plan == "pro" && ent.source == "trial" && ent.trial.as_ref().is_some_and(|t| t.ends_at > now) { + return entitlement_status(ent, &me.user.email, now); } - }); -} - -fn onboarding_pending(dir: &Path) -> bool { - dir.join(ONBOARDING_FILE).exists() -} - -fn clear_onboarding(dir: &Path) { - let _ = std::fs::remove_file(dir.join(ONBOARDING_FILE)); + } + LicenseStatus::TrialExpired } // ── HTTP ────────────────────────────────────────────────────────────────────── @@ -514,18 +476,19 @@ async fn me_text(req: reqwest::RequestBuilder) -> Result { resp.text().await.map_err(|e| ApiError::Network(e.to_string())) } -pub async fn fetch_me(base: &str, token: &str) -> Result { - me_text(http().get(format!("{base}/api/me")).bearer_auth(token)).await -} +/// Asks for the v3 account trial: one fresh trial per account, separate from 2.x. +const TRIAL_SCHEME: (&str, &str) = ("X-Stroke-Trial", "account-v3"); -pub async fn link_license(base: &str, token: &str, key: &str) -> Result { - me_text( - http() - .post(format!("{base}/api/license/link")) - .bearer_auth(token) - .json(&serde_json::json!({ "key": key })), - ) - .await +/// This app's version, sent with every `/api/me` so the account's device list +/// shows what each computer runs now. Set on the first refresh. +static APP_VERSION: OnceLock = OnceLock::new(); + +pub async fn fetch_me(base: &str, token: &str) -> Result { + let mut req = http().get(format!("{base}/api/me")).bearer_auth(token).header(TRIAL_SCHEME.0, TRIAL_SCHEME.1); + if let Some(v) = APP_VERSION.get() { + req = req.header("X-Stroke-Version", v.as_str()); + } + me_text(req).await } pub async fn remote_sign_out(base: &str, token: &str) { @@ -578,11 +541,24 @@ pub fn parse_callback(target: &str, expected_state: &str) -> Option { } } +/// The page the browser shows after it hands the sign-in back to the app. +/// `box-sizing` matters: `min-height: 100vh` plus body padding overflowed by +/// the padding and put a scrollbar on a page with one sentence on it. fn callback_page(heading: &str, body: &str) -> String { + let ok = heading.starts_with("You're"); + let failed = heading.contains("didn't"); + // A check for success, a dash for cancelled, an exclamation for a failure. + let (icon, tone) = if ok { + ("", "ok") + } else if failed { + ("", "bad") + } else { + ("", "") + }; format!( - r#"Stroke - -

{heading}

{body}

"# + r#"{heading} · Stroke + +

{heading}

{body}

Stroke
"# ) } @@ -632,7 +608,7 @@ pub async fn wait_for_callback(listener: TcpListener, expected_state: &str) -> R Some(Callback::Code(code)) => { respond(&mut stream, "200 OK", &callback_page( "You're signed in", - "You can close this tab and return to Stroke.", + "Head back to Stroke. You can close this tab.", )).await; return Ok(code); } @@ -721,9 +697,13 @@ pub(crate) async fn load_token(app: &tauri::AppHandle) -> Option { .filter(|t| !t.is_empty()) } +/// A new sign-in also drops any sync key left on this device: it belonged to +/// whoever was signed in before (possibly another account), and this one +/// unlocks sync with its own passphrase. async fn store_token(app: &tauri::AppHandle, token: String) -> Result<(), String> { crate::secrets::update_async(app, move |m| { m.insert(TOKEN_KEY.to_string(), token); + m.remove(crate::sync::SYNC_KEY_VAULT); }) .await } @@ -754,21 +734,16 @@ pub struct AccountStatus { pub fetched_at: u64, /// The last refresh couldn't reach the server; showing the cache. pub offline: bool, - /// New install that hasn't signed in or entered a key yet. + /// Not signed in: the app shows the sign-in screen first. pub needs_onboarding: bool, - /// A license.json is present on this device. - pub has_license_key: bool, - pub link_error: Option, pub error: Option, pub manage_url: String, } fn build_status(dir: &Path, signed_in: bool) -> AccountStatus { - let has_license_key = crate::license::load_license(dir).is_some(); let mut s = AccountStatus { signed_in, - has_license_key, - needs_onboarding: !signed_in && !has_license_key && onboarding_pending(dir), + needs_onboarding: !signed_in, manage_url: format!("{}/app", base_url()), ..Default::default() }; @@ -781,23 +756,19 @@ fn build_status(dir: &Path, signed_in: bool) -> AccountStatus { s.entitlement = Some(me.entitlement); s.devices = me.devices; s.fetched_at = c.fetched_at; - s.link_error = c.link_error; } } s } -/// Save a verified `/api/me` reply, keeping the link bookkeeping. +/// Save a verified `/api/me` reply. fn save_me(dir: &Path, raw: String) -> Result { let me = parse_me(&raw)?; - let prev = read_cache(dir).unwrap_or_default(); write_cache( dir, &CacheFile { me: raw, fetched_at: now_secs(), - linked_key_hash: prev.linked_key_hash, - link_error: prev.link_error, device_id: Some(crate::license::device_id()), }, )?; @@ -807,36 +778,6 @@ fn save_me(dir: &Path, raw: String) -> Result { Ok(me) } -/// Link the held license key to the account, once per key. -async fn link_held_key(dir: &Path, base: &str, token: &str) { - let Some(lic) = crate::license::load_license(dir) else { return }; - let hash = crate::license::key_hash(&lic.key); - let Some(cache) = read_cache(dir) else { return }; - if cache.linked_key_hash.as_deref() == Some(hash.as_str()) { - return; - } - let result = link_license(base, token, &lic.key).await; - let mut c = read_cache(dir).unwrap_or(cache); - match result { - Ok(raw) if parse_me(&raw).is_ok() => { - c.me = raw; - c.fetched_at = now_secs(); - c.linked_key_hash = Some(hash); - c.link_error = None; - } - // Retrying won't change these: remember and stop asking. - Err(ApiError::Server(code, _)) - if matches!(code.as_str(), "key_owned_elsewhere" | "not_found" | "invalid_key" | "revoked") => - { - c.linked_key_hash = Some(hash); - c.link_error = Some(code); - } - // Offline, 5xx, a bad reply: try again on the next refresh. - _ => return, - } - let _ = write_cache(dir, &c); -} - fn data_dir(app: &tauri::AppHandle) -> Result { app.path().app_data_dir().map_err(|e| e.to_string()) } @@ -845,12 +786,10 @@ fn data_dir(app: &tauri::AppHandle) -> Result { /// failure keeps the cache (trusted until `valid_until`). async fn refresh(app: &tauri::AppHandle, dir: &Path, token: &str) -> AccountStatus { let base = base_url(); + APP_VERSION.get_or_init(|| app.package_info().version.to_string()); match fetch_me(&base, token).await { Ok(raw) => match save_me(dir, raw) { - Ok(_) => { - link_held_key(dir, &base, token).await; - build_status(dir, true) - } + Ok(_) => build_status(dir, true), Err(e) => AccountStatus { error: Some(e), ..build_status(dir, true) }, }, Err(ApiError::Unauthorized) => { @@ -984,7 +923,6 @@ async fn sign_in_loopback( .await .map_err(|e| e.user_message())?; store_token(app, tok.token.clone()).await?; - clear_onboarding(dir); Ok(refresh(app, dir, &tok.token).await) } @@ -1015,7 +953,6 @@ pub async fn account_paste_code(app: tauri::AppHandle, text: String) -> Result) -> Result { let dir = data_dir(&app)?; - note_install_state(&dir); let token = load_token(&app).await; Ok(match token { Some(t) if refresh.unwrap_or(false) => self::refresh(&app, &dir, &t).await, @@ -1056,13 +992,6 @@ pub async fn account_sign_out(app: tauri::AppHandle) -> Result Result<(), String> { - clear_onboarding(&data_dir(&app)?); - Ok(()) -} - // ── Tests ───────────────────────────────────────────────────────────────────── #[cfg(test)] @@ -1249,23 +1178,6 @@ mod tests { assert!(matches!(entitlement_status(&ent("free", "none", 9e9 as u64), "a", now), LicenseStatus::TrialExpired)); } - #[test] - fn existing_keys_are_never_downgraded() { - let key = LicenseStatus::Valid { email: "k@x".into(), plan: "pro".into(), issued_at: 1, expires_at: None }; - let device_trial = LicenseStatus::Trial { days_remaining: 5 }; - // No account: unchanged. - assert!(matches!(combine(key.clone(), None), LicenseStatus::Valid { .. })); - assert!(matches!(combine(device_trial.clone(), None), LicenseStatus::Trial { days_remaining: 5 })); - // Account says free: the key still grants Pro. - assert!(matches!(combine(key.clone(), Some(LicenseStatus::TrialExpired)), LicenseStatus::Valid { .. })); - assert!(matches!(combine(key, Some(LicenseStatus::Trial { days_remaining: 2 })), LicenseStatus::Valid { .. })); - // Account Pro lifts an expired device trial. - let acc = LicenseStatus::Valid { email: "a".into(), plan: "pro".into(), issued_at: 1, expires_at: None }; - assert!(matches!(combine(LicenseStatus::TrialExpired, Some(acc)), LicenseStatus::Valid { .. })); - // Signed in: the account's trial replaces the device trial. - assert!(matches!(combine(device_trial, Some(LicenseStatus::Trial { days_remaining: 9 })), LicenseStatus::Trial { days_remaining: 9 })); - } - #[test] fn sign_in_params_match_server_rules() { let (_, c) = pkce_pair(); @@ -1302,7 +1214,7 @@ mod tests { .await .unwrap(); assert_eq!(page.status(), 200); - assert!(page.text().await.unwrap().contains("return to Stroke")); + assert!(page.text().await.unwrap().contains("Head back to Stroke")); assert_eq!(waiter.await.unwrap(), Ok("c0de".to_string())); // Cancel from the website. @@ -1363,7 +1275,6 @@ mod tests { let base = mock_server(vec![ ("POST /api/desktop/token", 200, r#"{"token":"tok","user":{"id":"u1","email":"a@b.co","name":"A","image":null}}"#.into()), ("GET /api/me", 200, me.clone()), - ("POST /api/license/link", 409, r#"{"error":"key_owned_elsewhere","message":"Owned by another account"}"#.into()), ]) .await; @@ -1376,10 +1287,6 @@ mod tests { assert!(parse_me_with_key(&raw, &sk.verifying_key()).is_ok()); assert_eq!(fetch_me(&base, "wrong").await, Err(ApiError::Unauthorized)); - assert_eq!( - link_license(&base, "tok", "k").await, - Err(ApiError::Server("key_owned_elsewhere".into(), "Owned by another account".into())) - ); let bad = mock_server(vec![("POST /api/desktop/token", 400, r#"{"error":"expired_code","message":"x"}"#.into())]).await; let e = exchange_code(&bad, "c", "v", "d", "n", "1.0").await.unwrap_err(); diff --git a/src-tauri/src/agents.rs b/src-tauri/src/agents.rs index 368237d3..a1d8ecab 100644 --- a/src-tauri/src/agents.rs +++ b/src-tauri/src/agents.rs @@ -205,11 +205,10 @@ async fn codex_user_mcp_names() -> Vec { } async fn read_codex_user_mcp_names() -> Vec { - let out = tokio::process::Command::new("codex") - .args(["mcp", "list", "--json"]) - .stdin(std::process::Stdio::null()) - .output() - .await; + let mut cmd = tokio::process::Command::new("codex"); + cmd.args(["mcp", "list", "--json"]).stdin(std::process::Stdio::null()); + crate::proc::quiet(&mut cmd); + let out = cmd.output().await; let Ok(out) = out else { return Vec::new() }; let Ok(list) = serde_json::from_slice::>(&out.stdout) else { return Vec::new(); diff --git a/src-tauri/src/backup_crypto.rs b/src-tauri/src/backup_crypto.rs new file mode 100644 index 00000000..20dfb24c --- /dev/null +++ b/src-tauri/src/backup_crypto.rs @@ -0,0 +1,288 @@ +//! End-to-end encryption for scheduled backups. +//! +//! A backup is encrypted on the user's computer before it is uploaded, so +//! Stroke Cloud (and anyone with access to it) only ever holds ciphertext. +//! +//! - **Data key (DEK):** 32 random bytes, fresh for every backup. +//! - **Wrapped DEK:** `nonce24 || XChaCha20-Poly1305(sync_key, nonce, dek, +//! aad = "stroke-backup-dek-v1")`, 72 bytes. The sync key is the account's +//! end-to-end sync key (`sync.rs`), unlocked only by the sync passphrase. +//! - **Key id:** the first 8 bytes of SHA-256("stroke-backup-key-id" || +//! sync_key), hex. Says which sync key wrapped the DEK without revealing it. +//! - **File:** `"STRKBAK1" || key_id(8 raw) || wrapped_dek(72) || +//! nonce_prefix(19)` (107 bytes), then the plaintext in 64 KiB chunks, each +//! `XChaCha20-Poly1305(dek, prefix || counter_u32_be || last_flag, chunk, +//! aad = the 107-byte header)`. The last chunk has flag 1, so a truncated or +//! reordered file fails to open. An empty input is one empty last chunk. +//! +//! The browser decrypts the same format (stroke-web +//! `src/lib/backups/crypto.ts`); keep the two in step. + +use std::io::{Read, Write}; +use std::path::Path; + +use chacha20poly1305::aead::{Aead, KeyInit, Payload}; +use chacha20poly1305::{XChaCha20Poly1305, XNonce}; +use sha2::{Digest, Sha256}; + +pub const MAGIC: &[u8; 8] = b"STRKBAK1"; +const DEK_AAD: &[u8] = b"stroke-backup-dek-v1"; +pub const CHUNK: usize = 64 * 1024; +const TAG: usize = 16; +const WRAPPED_LEN: usize = 24 + 32 + TAG; +pub const HEADER_LEN: usize = 8 + 8 + WRAPPED_LEN + 19; + +fn random() -> [u8; N] { + let mut b = [0u8; N]; + getrandom::getrandom(&mut b).expect("the OS random source is available"); + b +} + +/// Which sync key wrapped a backup's data key, as 16 hex characters. +pub fn key_id(sync_key: &[u8; 32]) -> String { + hex::encode(&key_id_raw(sync_key)) +} + +fn key_id_raw(sync_key: &[u8; 32]) -> [u8; 8] { + let mut h = Sha256::new(); + h.update(b"stroke-backup-key-id"); + h.update(sync_key); + let d = h.finalize(); + d[..8].try_into().expect("8 of 32 bytes") +} + +pub fn wrap_dek(sync_key: &[u8; 32], dek: &[u8; 32]) -> Vec { + let nonce: [u8; 24] = random(); + let ct = XChaCha20Poly1305::new(sync_key.into()) + .encrypt(XNonce::from_slice(&nonce), Payload { msg: dek, aad: DEK_AAD }) + .expect("in-memory encryption cannot fail"); + [nonce.as_slice(), &ct].concat() +} + +pub fn unwrap_dek(sync_key: &[u8; 32], wrapped: &[u8]) -> Result<[u8; 32], String> { + if wrapped.len() != WRAPPED_LEN { + return Err("The backup's key is damaged.".into()); + } + let (nonce, ct) = wrapped.split_at(24); + let dek = XChaCha20Poly1305::new(sync_key.into()) + .decrypt(XNonce::from_slice(nonce), Payload { msg: ct, aad: DEK_AAD }) + .map_err(|_| "This backup was encrypted with another sync key. Unlock sync with the passphrase it was made under.".to_string())?; + dek.try_into().map_err(|_| "The backup's key is damaged.".to_string()) +} + +/// Re-wrap a data key under a new sync key (after a sync reset on a device +/// that still had the old key). +pub fn rewrap(old: &[u8; 32], new: &[u8; 32], wrapped: &[u8]) -> Result, String> { + Ok(wrap_dek(new, &unwrap_dek(old, wrapped)?)) +} + +fn chunk_nonce(prefix: &[u8; 19], counter: u32, last: bool) -> [u8; 24] { + let mut n = [0u8; 24]; + n[..19].copy_from_slice(prefix); + n[19..23].copy_from_slice(&counter.to_be_bytes()); + n[23] = u8::from(last); + n +} + +/// Read until `buf` is full or the input ends; returns how much was read. +fn fill(r: &mut impl Read, buf: &mut [u8]) -> std::io::Result { + let mut n = 0; + while n < buf.len() { + match r.read(&mut buf[n..])? { + 0 => break, + k => n += k, + } + } + Ok(n) +} + +/// What the server keeps beside an encrypted backup. +#[derive(Debug, Clone)] +pub struct Sealed { + pub key_id: String, + /// base64url (unpadded) of the 72-byte wrapped data key. + pub wrapped_key: String, +} + +pub fn b64(bytes: &[u8]) -> String { + use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; + URL_SAFE_NO_PAD.encode(bytes) +} + +pub fn unb64(s: &str) -> Result, String> { + use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; + URL_SAFE_NO_PAD.decode(s.trim_end_matches('=')).map_err(|e| e.to_string()) +} + +pub fn encrypt_stream(sync_key: &[u8; 32], mut input: impl Read, mut out: impl Write) -> Result { + let dek: [u8; 32] = random(); + let wrapped = wrap_dek(sync_key, &dek); + let prefix: [u8; 19] = random(); + let mut header = Vec::with_capacity(HEADER_LEN); + header.extend_from_slice(MAGIC); + header.extend_from_slice(&key_id_raw(sync_key)); + header.extend_from_slice(&wrapped); + header.extend_from_slice(&prefix); + out.write_all(&header).map_err(|e| e.to_string())?; + + let cipher = XChaCha20Poly1305::new((&dek).into()); + let mut cur = vec![0u8; CHUNK]; + let mut next = vec![0u8; CHUNK]; + let mut cur_len = fill(&mut input, &mut cur).map_err(|e| e.to_string())?; + let mut counter: u32 = 0; + loop { + // Look one chunk ahead: the last chunk is the one with nothing after it. + let next_len = if cur_len == CHUNK { fill(&mut input, &mut next).map_err(|e| e.to_string())? } else { 0 }; + let last = next_len == 0; + let nonce = chunk_nonce(&prefix, counter, last); + let ct = cipher + .encrypt(XNonce::from_slice(&nonce), Payload { msg: &cur[..cur_len], aad: &header }) + .map_err(|_| "Encryption failed.".to_string())?; + out.write_all(&ct).map_err(|e| e.to_string())?; + if last { + break; + } + counter = counter.checked_add(1).ok_or("The backup is too large to encrypt.")?; + std::mem::swap(&mut cur, &mut next); + cur_len = next_len; + } + out.flush().map_err(|e| e.to_string())?; + Ok(Sealed { key_id: key_id(sync_key), wrapped_key: b64(&wrapped) }) +} + +/// Decrypt a backup. `wrapped_override` is the server's copy of the wrapped +/// key, which may have been re-wrapped under a newer sync key since the file +/// was written. +pub fn decrypt_stream( + sync_key: &[u8; 32], + mut input: impl Read, + mut out: impl Write, + wrapped_override: Option<&[u8]>, +) -> Result<(), String> { + let mut header = [0u8; HEADER_LEN]; + if fill(&mut input, &mut header).map_err(|e| e.to_string())? != HEADER_LEN || &header[..8] != MAGIC { + return Err("This isn't an encrypted Stroke backup.".into()); + } + let wrapped = wrapped_override.unwrap_or(&header[16..16 + WRAPPED_LEN]); + let dek = unwrap_dek(sync_key, wrapped)?; + let prefix: [u8; 19] = header[16 + WRAPPED_LEN..].try_into().expect("19 bytes"); + let cipher = XChaCha20Poly1305::new((&dek).into()); + + let size = CHUNK + TAG; + let mut cur = vec![0u8; size]; + let mut next = vec![0u8; size]; + let mut cur_len = fill(&mut input, &mut cur).map_err(|e| e.to_string())?; + let mut counter: u32 = 0; + loop { + let next_len = if cur_len == size { fill(&mut input, &mut next).map_err(|e| e.to_string())? } else { 0 }; + let last = next_len == 0; + let nonce = chunk_nonce(&prefix, counter, last); + let pt = cipher + .decrypt(XNonce::from_slice(&nonce), Payload { msg: &cur[..cur_len], aad: &header }) + .map_err(|_| "The backup is damaged or incomplete.".to_string())?; + out.write_all(&pt).map_err(|e| e.to_string())?; + if last { + break; + } + counter = counter.checked_add(1).ok_or("The backup is damaged.")?; + std::mem::swap(&mut cur, &mut next); + cur_len = next_len; + } + out.flush().map_err(|e| e.to_string()) +} + +pub fn encrypt_file(sync_key: &[u8; 32], src: &Path, dst: &Path) -> Result { + let input = std::io::BufReader::new(std::fs::File::open(src).map_err(|e| e.to_string())?); + let out = std::io::BufWriter::new(std::fs::File::create(dst).map_err(|e| e.to_string())?); + encrypt_stream(sync_key, input, out) +} + +pub fn is_encrypted(head: &[u8]) -> bool { + head.len() >= 8 && &head[..8] == MAGIC +} + +#[cfg(test)] +mod tests { + use super::*; + + fn round_trip(len: usize) { + let key = [7u8; 32]; + let data: Vec = (0..len).map(|i| (i * 31 % 251) as u8).collect(); + let mut enc = Vec::new(); + let sealed = encrypt_stream(&key, data.as_slice(), &mut enc).unwrap(); + assert_eq!(sealed.key_id, key_id(&key)); + assert!(is_encrypted(&enc)); + let mut dec = Vec::new(); + decrypt_stream(&key, enc.as_slice(), &mut dec, None).unwrap(); + assert_eq!(dec, data, "len {len}"); + } + + #[test] + fn round_trips_at_chunk_edges() { + for len in [0, 1, CHUNK - 1, CHUNK, CHUNK + 1, 3 * CHUNK, 3 * CHUNK + 17] { + round_trip(len); + } + } + + #[test] + fn wrong_key_truncation_and_tampering_fail() { + let key = [1u8; 32]; + let data = vec![9u8; 2 * CHUNK + 5]; + let mut enc = Vec::new(); + encrypt_stream(&key, data.as_slice(), &mut enc).unwrap(); + assert!(decrypt_stream(&[2u8; 32], enc.as_slice(), Vec::new(), None).is_err()); + // Cut off at a chunk boundary: the new last chunk was not sealed as last. + let cut = &enc[..HEADER_LEN + 2 * (CHUNK + TAG)]; + assert!(decrypt_stream(&key, cut, Vec::new(), None).is_err()); + let mut bad = enc.clone(); + let n = bad.len(); + bad[n - 20] ^= 1; + assert!(decrypt_stream(&key, bad.as_slice(), Vec::new(), None).is_err()); + let mut bad_header = enc.clone(); + bad_header[HEADER_LEN - 1] ^= 1; + assert!(decrypt_stream(&key, bad_header.as_slice(), Vec::new(), None).is_err()); + } + + #[test] + fn rewrapped_key_opens_with_the_new_sync_key() { + let (old, new) = ([3u8; 32], [4u8; 32]); + let data = b"CREATE TABLE t (x int);".to_vec(); + let mut enc = Vec::new(); + let sealed = encrypt_stream(&old, data.as_slice(), &mut enc).unwrap(); + let re = rewrap(&old, &new, &unb64(&sealed.wrapped_key).unwrap()).unwrap(); + let mut dec = Vec::new(); + decrypt_stream(&new, enc.as_slice(), &mut dec, Some(&re)).unwrap(); + assert_eq!(dec, data); + assert!(decrypt_stream(&new, enc.as_slice(), Vec::new(), None).is_err()); + } + + /// A fixed vector the browser's decryptor is tested against. + #[test] + fn known_answer_for_the_web_decryptor() { + let key = [0x42u8; 32]; + let dek = [0x11u8; 32]; + let prefix = [0x22u8; 19]; + let wrap_nonce = [0x33u8; 24]; + let wrapped = [ + wrap_nonce.as_slice(), + &XChaCha20Poly1305::new((&key).into()) + .encrypt(XNonce::from_slice(&wrap_nonce), Payload { msg: &dek, aad: DEK_AAD }) + .unwrap(), + ] + .concat(); + let mut header = Vec::new(); + header.extend_from_slice(MAGIC); + header.extend_from_slice(&key_id_raw(&key)); + header.extend_from_slice(&wrapped); + header.extend_from_slice(&prefix); + let ct = XChaCha20Poly1305::new((&dek).into()) + .encrypt(XNonce::from_slice(&chunk_nonce(&prefix, 0, true)), Payload { msg: b"hello", aad: &header }) + .unwrap(); + let file = [header.as_slice(), &ct].concat(); + let mut dec = Vec::new(); + decrypt_stream(&key, file.as_slice(), &mut dec, None).unwrap(); + assert_eq!(dec, b"hello"); + // Printed for the web test: cargo test known_answer -- --nocapture + println!("KAT {}", b64(&file)); + } +} diff --git a/src-tauri/src/cloud_backup.rs b/src-tauri/src/cloud_backup.rs new file mode 100644 index 00000000..b61437ef --- /dev/null +++ b/src-tauri/src/cloud_backup.rs @@ -0,0 +1,507 @@ +//! Scheduled backups (Pro). The schedule and history live on stroke.click +//! (`/api/backups/*`); this side runs a job: open the saved connection, dump +//! it with the backup engine (`db::backup`), gzip it, and send it either to +//! Stroke Cloud (chunked upload through the API) or to the user's own +//! S3-compatible bucket (one signed PUT). Database credentials never leave the +//! device, and neither do the bucket's keys: they sit in the keychain vault. + +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use tauri::Manager; + +use crate::account::{base_url, http, load_token, read_error, ApiError}; +use crate::db::connection::{open_duckdb, open_mssql, open_mysql, open_pg, open_sqlite, ActiveConnection, AnyConnectionConfig}; + +/// Stroke Cloud upload part size. R2 wants at least 5 MB for every part but +/// the last, and the Worker reads each part into memory. +const PART_BYTES: usize = 16 * 1024 * 1024; +const VAULT_PREFIX: &str = "backup-s3:"; + +/// What the scheduler needs from a job, as the server lists it. +#[derive(Deserialize, Debug, Clone)] +pub struct JobSpec { + pub id: String, + pub label: String, + pub destination: String, + #[serde(default)] + pub s3_bucket: Option, + #[serde(default)] + pub s3_endpoint: Option, + #[serde(default)] + pub s3_prefix: Option, +} + +#[derive(Serialize, Debug)] +pub struct RunOutcome { + /// "success", "failed", or "skipped" (another device has this tick). + pub status: &'static str, + pub message: String, + pub bytes: u64, +} + +impl RunOutcome { + fn skipped(message: impl Into) -> Self { + Self { status: "skipped", message: message.into(), bytes: 0 } + } +} + +#[derive(Serialize, Deserialize, Default, Clone)] +struct S3Secret { + access_key_id: String, + secret_access_key: String, + #[serde(default)] + region: String, +} + +fn api_message(e: ApiError) -> String { + match e { + ApiError::Unauthorized => "Your Stroke session ended. Sign in again.".into(), + ApiError::Network(_) => "Couldn't reach stroke.click. Check your connection.".into(), + ApiError::Server(_, m) => m, + } +} + +async fn token(app: &tauri::AppHandle) -> Result { + load_token(app).await.ok_or_else(|| "Sign in to use scheduled backups.".to_string()) +} + +async fn send_json(req: reqwest::RequestBuilder) -> Result { + let resp = req.send().await.map_err(|e| ApiError::Network(e.to_string()))?; + if !resp.status().is_success() { + return Err(read_error(resp).await); + } + resp.json().await.map_err(|e| ApiError::Network(e.to_string())) +} + +// ── Job management (thin proxies; the server validates) ─────────────────────── + +#[tauri::command] +pub async fn backup_jobs_list(app: tauri::AppHandle) -> Result { + let t = token(&app).await?; + send_json(http().get(format!("{}/api/backups/jobs", base_url())).bearer_auth(t)) + .await + .map_err(api_message) +} + +#[tauri::command] +pub async fn backup_job_save(app: tauri::AppHandle, job: Value) -> Result { + let t = token(&app).await?; + send_json(http().post(format!("{}/api/backups/jobs", base_url())).bearer_auth(t).json(&job)) + .await + .map_err(api_message) +} + +#[tauri::command] +pub async fn backup_job_patch(app: tauri::AppHandle, id: String, patch: Value) -> Result { + let t = token(&app).await?; + send_json(http().patch(format!("{}/api/backups/jobs/{}", base_url(), enc(&id))).bearer_auth(t).json(&patch)) + .await + .map_err(api_message) +} + +#[tauri::command] +pub async fn backup_job_delete(app: tauri::AppHandle, id: String) -> Result { + let t = token(&app).await?; + let out = send_json(http().delete(format!("{}/api/backups/jobs/{}", base_url(), enc(&id))).bearer_auth(t)) + .await + .map_err(api_message)?; + let key = format!("{VAULT_PREFIX}{id}"); + let _ = crate::secrets::update_async(&app, move |m| { + m.remove(&key); + }) + .await; + Ok(out) +} + +/// Keep a job's S3 keys in the keychain vault. Empty keys remove them. +#[tauri::command] +pub async fn backup_s3_save( + app: tauri::AppHandle, + job_id: String, + access_key_id: String, + secret_access_key: String, + region: String, +) -> Result<(), String> { + let key = format!("{VAULT_PREFIX}{job_id}"); + let secret = S3Secret { + access_key_id: access_key_id.trim().to_string(), + secret_access_key: secret_access_key.trim().to_string(), + region: region.trim().to_string(), + }; + let json = serde_json::to_string(&secret).map_err(|e| e.to_string())?; + crate::secrets::update_async(&app, move |m| { + if secret.access_key_id.is_empty() || secret.secret_access_key.is_empty() { + m.remove(&key); + } else { + m.insert(key, json); + } + }) + .await +} + +/// Whether this device has S3 keys for the job (the keys themselves stay put). +#[tauri::command] +pub async fn backup_s3_status(app: tauri::AppHandle, job_id: String) -> Result, String> { + Ok(load_s3(&app, &job_id).await.map(|s| s.region)) +} + +async fn load_s3(app: &tauri::AppHandle, job_id: &str) -> Option { + let raw = crate::secrets::read_all_async(app).await.get(&format!("{VAULT_PREFIX}{job_id}")).cloned()?; + serde_json::from_str(&raw).ok() +} + +// ── Running a job ───────────────────────────────────────────────────────────── + +/// Run one job now: claim `slot` on the server, dump, upload, report. +#[tauri::command] +pub async fn backup_job_run( + app: tauri::AppHandle, + job: JobSpec, + config: AnyConnectionConfig, + slot: String, +) -> Result { + let t = token(&app).await?; + let base = base_url(); + + // Backups are encrypted on this computer with the account's sync key, so + // Stroke Cloud only ever holds ciphertext. Stroke Cloud requires it; an S3 + // bucket gets it whenever sync is set up here. + let sync_key = crate::sync::load_local(&app).await.map(|(k, _)| k); + if job.destination == "stroke" && sync_key.is_none() { + return Err("Backups to Stroke Cloud are end-to-end encrypted with your sync key. Set up or unlock sync (Settings → Sync) on this computer first.".into()); + } + + // S3 keys first: without them there is nothing to claim the tick for. + let s3 = if job.destination == "s3" { + match load_s3(&app, &job.id).await { + Some(s) => Some(s), + None => return Err("This device has no S3 keys for this backup. Add them in the schedule's settings.".into()), + } + } else { + None + }; + + let claim = http() + .post(format!("{base}/api/backups/runs")) + .bearer_auth(&t) + .json(&serde_json::json!({ "job_id": job.id, "slot": slot, "encrypted": sync_key.is_some() })); + let run = match send_json(claim).await { + Ok(v) => v, + Err(ApiError::Server(code, msg)) if code == "claimed" => return Ok(RunOutcome::skipped(msg)), + Err(e) => return Err(api_message(e)), + }; + let run_id = run.get("id").and_then(Value::as_str).unwrap_or_default().to_string(); + if run_id.is_empty() { + return Err("stroke.click didn't start the run.".into()); + } + + let dir = app.path().app_cache_dir().map_err(|e| e.to_string())?.join("scheduled-backups"); + let result = dump_and_upload(&dir, &job, config, s3, sync_key, &base, &t, &run_id).await; + + let body = match &result { + Ok(done) => serde_json::json!({ + "status": "success", "tables": done.tables, "rows": done.rows, + "bytes": done.bytes, "object_key": done.object_key, + "encrypted": done.sealed.is_some(), + "key_id": done.sealed.as_ref().map(|s| s.key_id.clone()), + "wrapped_key": done.sealed.as_ref().map(|s| s.wrapped_key.clone()), + }), + Err(e) => serde_json::json!({ "status": "failed", "error": e }), + }; + let finish = http().patch(format!("{base}/api/backups/runs/{}", enc(&run_id))).bearer_auth(&t).json(&body); + let reported = send_json(finish).await; + + match (result, reported) { + (Ok(done), Ok(_)) => Ok(RunOutcome { status: "success", message: format!("Backed up {} tables.", done.tables), bytes: done.bytes }), + (Ok(_), Err(e)) => Err(format!("The backup finished but couldn't be recorded: {}", api_message(e))), + (Err(e), _) => Ok(RunOutcome { status: "failed", message: e, bytes: 0 }), + } +} + +struct Done { + tables: usize, + rows: usize, + bytes: u64, + object_key: Option, + sealed: Option, +} + +async fn dump_and_upload( + dir: &Path, + job: &JobSpec, + config: AnyConnectionConfig, + s3: Option, + sync_key: Option<[u8; 32]>, + base: &str, + token: &str, + run_id: &str, +) -> Result { + std::fs::create_dir_all(dir).map_err(|e| e.to_string())?; + let sql_path = dir.join(format!("{run_id}.sql")); + let gz_path = dir.join(format!("{run_id}.sql.gz")); + let enc_path = dir.join(format!("{run_id}.sql.gz.stroke")); + let cleanup = |paths: &[&PathBuf]| { + for p in paths { + let _ = std::fs::remove_file(p); + } + }; + + let result = async { + let conn = open(config).await?; + crate::db::backup::reset_cancel(); + let rep = crate::db::backup::Reporter::silent(); + let opts = crate::db::backup::ExportOptions::default(); + let export = crate::db::backup::run_export(&rep, conn.clone(), None, None, &opts, Some(sql_path.to_string_lossy().into_owned())).await; + crate::db::connection::close_in_background(conn); + let export = export?; + if export.cancelled { + return Err("The backup was cancelled.".to_string()); + } + let src = sql_path.clone(); + let dst = gz_path.clone(); + let mut bytes = tokio::task::spawn_blocking(move || gzip(&src, &dst)).await.map_err(|e| e.to_string())??; + let _ = std::fs::remove_file(&sql_path); + + // Encrypt on this computer; what leaves it is ciphertext. + let (file, sealed) = match sync_key { + Some(key) => { + let (src, dst) = (gz_path.clone(), enc_path.clone()); + let sealed = tokio::task::spawn_blocking(move || crate::backup_crypto::encrypt_file(&key, &src, &dst)) + .await + .map_err(|e| e.to_string())??; + let _ = std::fs::remove_file(&gz_path); + bytes = std::fs::metadata(&enc_path).map_err(|e| e.to_string())?.len(); + (enc_path.clone(), Some(sealed)) + } + None => (gz_path.clone(), None), + }; + + let object_key = match s3 { + Some(s) => Some(upload_s3(&file, job, &s, bytes, sealed.is_some()).await?), + None => { + upload_stroke(&file, base, token, run_id).await?; + None + } + }; + Ok(Done { tables: export.table_count, rows: export.row_count, bytes, object_key, sealed }) + } + .await; + cleanup(&[&sql_path, &gz_path, &enc_path]); + result +} + +/// A standalone connection for the dump, apart from whatever the page has open. +async fn open(config: AnyConnectionConfig) -> Result { + Ok(match config { + AnyConnectionConfig::Postgres(c) => ActiveConnection::Postgres(open_pg(&c).await?), + AnyConnectionConfig::Sqlite(c) => ActiveConnection::Sqlite(open_sqlite(&c).await?), + AnyConnectionConfig::Mysql(c) => ActiveConnection::Mysql(open_mysql(&c).await?), + AnyConnectionConfig::D1(c) => ActiveConnection::D1(c), + AnyConnectionConfig::Libsql(c) => ActiveConnection::LibSql(c), + AnyConnectionConfig::Duckdb(c) => ActiveConnection::Duckdb(open_duckdb(&c).await?), + AnyConnectionConfig::Mssql(c) => ActiveConnection::Mssql(open_mssql(&c).await?), + _ => return Err("Scheduled backups support PostgreSQL, MySQL, SQLite, D1, Turso/libSQL, DuckDB and SQL Server.".into()), + }) +} + +fn gzip(src: &Path, dst: &Path) -> Result { + let mut input = std::fs::File::open(src).map_err(|e| e.to_string())?; + let out = std::fs::File::create(dst).map_err(|e| e.to_string())?; + let mut enc = flate2::write::GzEncoder::new(std::io::BufWriter::new(out), flate2::Compression::default()); + std::io::copy(&mut input, &mut enc).map_err(|e| e.to_string())?; + enc.finish().map_err(|e| e.to_string())?.flush().map_err(|e| e.to_string())?; + Ok(std::fs::metadata(dst).map_err(|e| e.to_string())?.len()) +} + +/// Stroke Cloud: the file in PART_BYTES parts, each retried a few times. +async fn upload_stroke(path: &Path, base: &str, token: &str, run_id: &str) -> Result<(), String> { + let mut file = std::fs::File::open(path).map_err(|e| e.to_string())?; + let mut n = 1u32; + loop { + let mut buf = vec![0u8; PART_BYTES]; + let mut filled = 0; + while filled < PART_BYTES { + let read = file.read(&mut buf[filled..]).map_err(|e| e.to_string())?; + if read == 0 { + break; + } + filled += read; + } + if filled == 0 { + break; + } + buf.truncate(filled); + let url = format!("{base}/api/backups/runs/{}/parts/{n}", enc(run_id)); + let mut attempt = 0; + loop { + attempt += 1; + let sent = http() + .put(&url) + .bearer_auth(token) + .header("content-type", "application/octet-stream") + .timeout(Duration::from_secs(300)) + .body(buf.clone()) + .send() + .await; + match sent { + Ok(r) if r.status().is_success() => break, + Ok(r) if !crate::account::is_outage(r.status()) || attempt >= 3 => { + return Err(format!("Uploading to Stroke Cloud failed: {}", api_message(read_error(r).await))); + } + Err(e) if attempt >= 3 => return Err(format!("Uploading to Stroke Cloud failed: {e}")), + _ => tokio::time::sleep(Duration::from_secs(2 * attempt)).await, + } + } + if filled < PART_BYTES { + break; + } + n += 1; + } + Ok(()) +} + +/// Object keys use only these characters, so the path needs no escaping. +fn key_safe(s: &str) -> String { + let out: String = s + .chars() + .map(|c| if c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | '_' | '/') { c } else { '-' }) + .collect(); + out.trim_matches('/').to_string() +} + +/// The user's bucket: one PUT, the payload streamed from disk and left +/// unsigned (`UNSIGNED-PAYLOAD`, accepted by S3, R2, MinIO and the rest). +async fn upload_s3(path: &Path, job: &JobSpec, s: &S3Secret, bytes: u64, encrypted: bool) -> Result { + let bucket = job.s3_bucket.as_deref().filter(|b| !b.is_empty()).ok_or("The schedule has no S3 bucket.")?; + let region = if s.region.is_empty() { "us-east-1" } else { s.region.as_str() }; + let stamp = chrono::Utc::now().format("%Y-%m-%dT%H-%M-%SZ"); + let prefix = job.s3_prefix.as_deref().map(key_safe).filter(|p| !p.is_empty()); + let ext = if encrypted { "sql.gz.stroke" } else { "sql.gz" }; + let name = format!("{}-{stamp}.{ext}", key_safe(&job.label).replace('/', "-")); + let key = match prefix { + Some(p) => format!("{p}/{name}"), + None => name, + }; + + // A custom endpoint (R2, MinIO, B2, Wasabi) is addressed path-style; AWS + // itself virtual-host style. + let (scheme_host, host, path_part) = match job.s3_endpoint.as_deref().map(str::trim).filter(|e| !e.is_empty()) { + Some(ep) => { + let ep = ep.trim_end_matches('/'); + let with_scheme = if ep.contains("://") { ep.to_string() } else { format!("https://{ep}") }; + let host = with_scheme.split("://").nth(1).unwrap_or_default().split('/').next().unwrap_or_default().to_string(); + (with_scheme, host, format!("/{bucket}/{key}")) + } + None => { + let host = format!("{bucket}.s3.{region}.amazonaws.com"); + (format!("https://{host}"), host, format!("/{key}")) + } + }; + + let amz_date = chrono::Utc::now().format("%Y%m%dT%H%M%SZ").to_string(); + let headers = vec![ + ("host".to_string(), host), + ("x-amz-content-sha256".to_string(), "UNSIGNED-PAYLOAD".to_string()), + ("x-amz-date".to_string(), amz_date.clone()), + ]; + let creds = crate::db::dynamodb::Creds { + access_key_id: s.access_key_id.clone(), + secret_access_key: s.secret_access_key.clone(), + session_token: None, + }; + let auth = crate::db::dynamodb::sign("PUT", &path_part, "", &headers, b"", &creds, region, "s3", &amz_date); + + let file = tokio::fs::File::open(path).await.map_err(|e| e.to_string())?; + let body = reqwest::Body::wrap_stream(tokio_util::io::ReaderStream::new(file)); + let resp = http() + .put(format!("{scheme_host}{path_part}")) + .header("authorization", auth) + .header("x-amz-content-sha256", "UNSIGNED-PAYLOAD") + .header("x-amz-date", amz_date) + .header("content-type", if encrypted { "application/octet-stream" } else { "application/gzip" }) + .header("content-length", bytes.to_string()) + .timeout(Duration::from_secs(3600)) + .body(body) + .send() + .await + .map_err(|e| format!("Couldn't reach the S3 endpoint: {e}"))?; + if !resp.status().is_success() { + let status = resp.status(); + let text = resp.text().await.unwrap_or_default(); + let code = text.split("").nth(1).and_then(|s| s.split("").next()).unwrap_or(""); + let msg = text.split("").nth(1).and_then(|s| s.split("").next()).unwrap_or(""); + return Err(format!("S3 refused the upload ({status}{}{}).", if code.is_empty() { "" } else { ", " }, [code, msg].iter().filter(|s| !s.is_empty()).copied().collect::>().join(": "))); + } + Ok(key) +} + +/// After a sync reset on this device: re-wrap every backup key the old sync +/// key wrapped, so those backups open with the new one. +pub(crate) async fn rewrap_backup_keys(token: &str, old: &[u8; 32], new: &[u8; 32]) -> Result<(), String> { + #[derive(Deserialize)] + struct Key { + id: String, + key_id: String, + wrapped_key: String, + } + #[derive(Deserialize)] + struct Keys { + keys: Vec, + } + let base = base_url(); + let list: Keys = serde_json::from_value( + send_json(http().get(format!("{base}/api/backups/keys")).bearer_auth(token)).await.map_err(api_message)?, + ) + .map_err(|e| e.to_string())?; + let old_id = crate::backup_crypto::key_id(old); + let new_id = crate::backup_crypto::key_id(new); + let mut updates = Vec::new(); + for k in list.keys.into_iter().filter(|k| k.key_id == old_id) { + let wrapped = crate::backup_crypto::unb64(&k.wrapped_key)?; + let re = crate::backup_crypto::rewrap(old, new, &wrapped)?; + updates.push(serde_json::json!({ "id": k.id, "key_id": new_id, "wrapped_key": crate::backup_crypto::b64(&re) })); + } + if updates.is_empty() { + return Ok(()); + } + send_json(http().post(format!("{base}/api/backups/keys")).bearer_auth(token).json(&serde_json::json!({ "keys": updates }))) + .await + .map(|_| ()) + .map_err(api_message) +} + +/// Ids are UUIDs from the app, but escape anything else defensively. +fn enc(s: &str) -> String { + s.chars().filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_').collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn object_keys_need_no_escaping() { + assert_eq!(key_safe("prod db (main)"), "prod-db--main-"); + assert_eq!(key_safe("/backups/stroke/"), "backups/stroke"); + assert_eq!(enc("a1-b2_c3/../x"), "a1-b2_c3x"); + } + + #[test] + fn gzip_round_trip() { + let dir = std::env::temp_dir().join(format!("stroke-gz-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let src = dir.join("a.sql"); + let dst = dir.join("a.sql.gz"); + std::fs::write(&src, "CREATE TABLE t (x int);\n".repeat(1000)).unwrap(); + let n = gzip(&src, &dst).unwrap(); + assert!(n > 0 && n < 24_000); + let mut out = String::new(); + flate2::read::GzDecoder::new(std::fs::File::open(&dst).unwrap()).read_to_string(&mut out).unwrap(); + assert_eq!(out.len(), 24_000); + let _ = std::fs::remove_dir_all(dir); + } +} diff --git a/src-tauri/src/cloudflare.rs b/src-tauri/src/cloudflare.rs index eae0316f..4a49d67e 100644 --- a/src-tauri/src/cloudflare.rs +++ b/src-tauri/src/cloudflare.rs @@ -324,12 +324,16 @@ pub async fn cloudflare_start_oauth(app: tauri::AppHandle) -> Result r.map_err(|_| "Authorization timed out - please try again.".to_string())??, + _ = cancelled => return Err("cancelled".to_string()), + }; let token = exchange_code(&code, &verifier, &redirect_uri).await?; @@ -594,3 +598,96 @@ pub async fn cloudflare_list_d1_databases( }) .collect()) } + +// ── Create a D1 database ────────────────────────────────────────────────────── +// +// `POST /accounts/{id}/d1/database { name, primary_location_hint? }`, covered by +// the `d1:write` scope the sign-in already requests. A D1 database is usable as +// soon as the call returns. The location hint is optional: without one, +// Cloudflare places the database near the request. + +/// Location hints Cloudflare accepts for `primary_location_hint`. +const D1_LOCATION_HINTS: &[&str] = &["wnam", "enam", "weur", "eeur", "apac", "oc"]; + +fn d1_create_body(name: &str, location_hint: Option<&str>) -> Result { + let name = name.trim(); + if name.is_empty() { + return Err("Enter a name for the database.".into()); + } + let mut body = serde_json::json!({ "name": name }); + if let Some(hint) = location_hint.map(str::trim).filter(|h| !h.is_empty()) { + if !D1_LOCATION_HINTS.contains(&hint) { + return Err(format!("Unknown D1 location: {hint}")); + } + body["primary_location_hint"] = serde_json::Value::String(hint.to_string()); + } + Ok(body) +} + +fn parse_d1_created(body: &serde_json::Value) -> Result { + let d = &body["result"]; + Ok(CfD1Database { + uuid: d["uuid"].as_str().ok_or("Cloudflare created the database but returned no id")?.to_string(), + name: d["name"].as_str().unwrap_or("Unnamed").to_string(), + created_at: d["created_at"].as_str().map(|s| s.to_string()), + num_tables: d["num_tables"].as_u64(), + }) +} + +/// Create a D1 database in an account. +#[tauri::command] +pub async fn cloudflare_create_d1_database( + api_token: String, + account_id: String, + name: String, + location_hint: Option, +) -> Result { + let body = d1_create_body(&name, location_hint.as_deref())?; + let url = format!( + "https://api.cloudflare.com/client/v4/accounts/{}/d1/database", + urlencoding::encode(account_id.trim()) + ); + let resp = http() + .post(&url) + .header("Authorization", format!("Bearer {}", api_token.trim())) + .json(&body) + .send() + .await + .map_err(|e| describe_request_error("Creating a D1 database", &e))?; + let status = resp.status().as_u16(); + let body: serde_json::Value = resp.json().await.unwrap_or_default(); + if status == 401 || status == 403 { + return Err("This Cloudflare sign-in can't create D1 databases in this account. Sign out, sign in again, and allow D1 access.".to_string()); + } + if !(200..300).contains(&status) { + let msg = body["errors"] + .as_array() + .and_then(|e| e.first()) + .and_then(|e| e["message"].as_str()) + .unwrap_or("Unknown error"); + return Err(format!("Cloudflare error {status}: {msg}")); + } + parse_d1_created(&body) +} + +#[cfg(test)] +mod d1_create_tests { + use super::*; + use serde_json::json; + + #[test] + fn create_body_trims_the_name_and_checks_the_hint() { + assert_eq!(d1_create_body(" app ", None).unwrap(), json!({ "name": "app" })); + assert_eq!(d1_create_body("app", Some("weur")).unwrap(), json!({ "name": "app", "primary_location_hint": "weur" })); + assert_eq!(d1_create_body("app", Some("")).unwrap(), json!({ "name": "app" })); + assert!(d1_create_body("app", Some("mars")).is_err()); + assert!(d1_create_body(" ", None).is_err()); + } + + #[test] + fn created_database_reads_the_result() { + let d = parse_d1_created(&json!({ "success": true, "result": { "uuid": "u-1", "name": "app", "created_at": "2026-01-01" } })).unwrap(); + assert_eq!((d.uuid.as_str(), d.name.as_str()), ("u-1", "app")); + assert!(parse_d1_created(&json!({ "result": {} })).is_err()); + } +} diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index b4c91d63..4783573e 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,3 +1,4 @@ +use crate::db::connection::Db; use std::sync::OnceLock; use tauri::Emitter; @@ -275,6 +276,52 @@ pub async fn read_file(path: String) -> Result { /// building a WebView2 window there deadlocks on Windows (tauri-apps/wry#583): /// the window never appeared, and every other window froze with it, so the /// app could not even be closed. +/// A dashboard on its own: a window with nothing but that dashboard's cards, +/// for a second screen. One window per dashboard; asking again focuses it. +/// It runs its queries on the connection the app has open, like any window. +#[tauri::command] +pub async fn open_dashboard_window(app: tauri::AppHandle, dashboard_id: String, connection_id: String, title: String) -> Result<(), String> { + let safe = |s: &str| s.chars().all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'); + if dashboard_id.is_empty() || !safe(&dashboard_id) || !safe(&connection_id) { + return Err("That isn't a dashboard".into()); + } + let label = format!("main-dashboard-{}", dashboard_id.chars().filter(|c| c.is_ascii_alphanumeric()).take(40).collect::()); + if let Some(w) = app.webview_windows().get(label.as_str()) { + let _ = w.unminimize(); + let _ = w.set_focus(); + return Ok(()); + } + let url = format!("/?view=dashboard&dash={dashboard_id}&conn={connection_id}"); + let mut builder = tauri::WebviewWindowBuilder::new(&app, &label, tauri::WebviewUrl::App(url.into())) + .title(if title.trim().is_empty() { "Dashboard".to_string() } else { title }) + .inner_size(1440.0, 900.0) + .min_inner_size(640.0, 420.0) + .resizable(true) + .visible(false) + .background_color(tauri::window::Color(8, 8, 8, 255)) + .devtools(cfg!(debug_assertions)) + .zoom_hotkeys_enabled(false) + .on_navigation(crate::navigation_allowed); + #[cfg(target_os = "macos")] + { + builder = builder + .title_bar_style(tauri::TitleBarStyle::Overlay) + .hidden_title(true) + .traffic_light_position(crate::mac_chrome::traffic_light_position()); + } + #[cfg(not(target_os = "macos"))] + { + builder = builder.decorations(false); + } + let window = builder.build().map_err(|e| e.to_string())?; + let surface = crate::surface_for_theme(window.theme().unwrap_or(tauri::Theme::Dark)); + let _ = window.set_background_color(Some(surface)); + crate::set_webview_backdrop(&window, surface); + crate::guard_webview(&window); + crate::arm_reveal_failsafe(&window); + Ok(()) +} + #[tauri::command] pub async fn open_new_window(app: tauri::AppHandle) -> Result<(), String> { // Labels must be unique and stable-ish; the counter restarts with the app, @@ -330,6 +377,8 @@ pub async fn open_new_window(app: tauri::AppHandle) -> Result<(), String> { /// capability file only grants core window permissions to `main`. #[tauri::command] pub fn reveal_window(window: tauri::WebviewWindow) { + // The page drew a finished screen, so this display backend works here. + crate::display_backend::drawn(); if !window.is_visible().unwrap_or(false) { let _ = window.show(); let _ = window.set_focus(); @@ -359,18 +408,18 @@ pub fn toggle_devtools(window: tauri::WebviewWindow) { } use crate::db::{ - connect, connect_clickhouse, connect_d1, connect_duckdb, connect_libsql, connect_mssql, connect_mysql, connect_redis, connect_sqlite, disconnect, + connect, connect_clickhouse, connect_d1, connect_duckdb, connect_libsql, connect_mssql, connect_mysql, connect_oracle, connect_redis, connect_sqlite, disconnect, delete_table_row, delete_table_rows, execute_ddl, execute_sql, execute_sql_multi, get_table_rows, count_table_rows, insert_table_row, list_schemas, list_tables, list_indexes_for, list_enums, list_functions, list_triggers, list_sequences, ping_connection, table_row_counts, truncate_table, drop_table, get_table_column_structure, get_schema_column_structure, get_incoming_foreign_keys, get_table_ddl as db_get_table_ddl, - test_clickhouse_connection, test_connection, test_d1_connection, test_duckdb_connection, test_libsql_connection, test_mssql_connection, test_mysql_connection, test_redis_connection, test_sqlite_connection, - update_table_cell, fetch_cell_value, ConnectionConfig, D1Config, DbState, EnumInfo, FunctionInfo, ExplainResult, IndexInfo, LibSqlConfig, + test_clickhouse_connection, test_connection, test_d1_connection, test_duckdb_connection, test_libsql_connection, test_mssql_connection, test_mysql_connection, test_oracle_connection, test_redis_connection, test_sqlite_connection, + update_table_cell, fetch_cell_value, ConnectionConfig, D1Config, EnumInfo, FunctionInfo, ExplainResult, IndexInfo, LibSqlConfig, SqlResult, SqliteConfig, TableInfo, TableRowCount, TableRows, TriggerInfo, SequenceInfo, ColumnStructureRow, TableColumnStructure, IncomingForeignKey, InsertRowResult, TunnelState, apply_table_changes, ApplyChangesResult, RowUpdate, explain_pg, explain_mysql, explain_sqlite, explain_from_text_lines, explain_from_sqlite_plan, }; -use crate::db::connection::{require_conn, ClickhouseConfig, DuckdbConfig, MssqlConfig, MysqlConfig, RedisConfig}; +use crate::db::connection::{require_conn, ClickhouseConfig, DuckdbConfig, MssqlConfig, MysqlConfig, OracleConfig, RedisConfig}; use crate::db::ActiveConnection; use serde_json::Value; use std::collections::HashMap; @@ -385,7 +434,7 @@ pub async fn test_postgres_connection(config: ConnectionConfig) -> Result<(), St #[tauri::command] pub async fn connect_postgres( - state: State<'_, DbState>, + state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: ConnectionConfig, ) -> Result<(), String> { @@ -401,7 +450,7 @@ pub async fn test_sqlite(config: SqliteConfig) -> Result<(), String> { #[tauri::command] pub async fn connect_sqlite_db( - state: State<'_, DbState>, + state: Db<'_>, config: SqliteConfig, ) -> Result<(), String> { connect_sqlite(state, config).await @@ -416,7 +465,7 @@ pub async fn test_mysql(config: MysqlConfig) -> Result<(), String> { #[tauri::command] pub async fn connect_mysql_db( - state: State<'_, DbState>, + state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: MysqlConfig, ) -> Result<(), String> { @@ -432,7 +481,7 @@ pub async fn test_d1(config: D1Config) -> Result<(), String> { #[tauri::command] pub async fn connect_d1_db( - state: State<'_, DbState>, + state: Db<'_>, config: D1Config, ) -> Result<(), String> { connect_d1(state, config).await @@ -446,7 +495,7 @@ pub async fn test_libsql(config: LibSqlConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_libsql_db(state: State<'_, DbState>, config: LibSqlConfig) -> Result<(), String> { +pub async fn connect_libsql_db(state: Db<'_>, config: LibSqlConfig) -> Result<(), String> { connect_libsql(state, config).await } @@ -459,7 +508,7 @@ pub async fn test_posthog(config: crate::db::connection::PosthogConfig) -> Resul #[tauri::command] pub async fn connect_posthog_db( - state: State<'_, DbState>, + state: Db<'_>, config: crate::db::connection::PosthogConfig, ) -> Result<(), String> { crate::db::connection::connect_posthog(state, config).await @@ -473,7 +522,7 @@ pub async fn test_clickhouse(config: ClickhouseConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_clickhouse_db(state: State<'_, DbState>, config: ClickhouseConfig) -> Result<(), String> { +pub async fn connect_clickhouse_db(state: Db<'_>, config: ClickhouseConfig) -> Result<(), String> { connect_clickhouse(state, config).await } @@ -485,7 +534,7 @@ pub async fn test_redis(config: RedisConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_redis_db(state: State<'_, DbState>, config: RedisConfig) -> Result<(), String> { +pub async fn connect_redis_db(state: Db<'_>, config: RedisConfig) -> Result<(), String> { connect_redis(state, config).await } @@ -493,7 +542,7 @@ pub async fn connect_redis_db(state: State<'_, DbState>, config: RedisConfig) -> /// SCAN page (next cursor + keys); the frontend loops until the cursor is "0". #[tauri::command] pub async fn redis_scan( - state: State<'_, DbState>, + state: Db<'_>, cursor: String, pattern: Option, count: u32, @@ -504,7 +553,7 @@ pub async fn redis_scan( } } -fn require_redis(state: &State<'_, DbState>) -> Result { +fn require_redis(state: &Db<'_>) -> Result { match require_conn(state)? { ActiveConnection::Redis(cfg) => Ok(cfg), _ => Err("Not connected to a Redis database".into()), @@ -514,7 +563,7 @@ fn require_redis(state: &State<'_, DbState>) -> Result { /// One page of the Redis keys table (keys with type, TTL, size and preview). #[tauri::command] pub async fn redis_key_page( - state: State<'_, DbState>, + state: Db<'_>, cursor: String, pattern: Option, key_type: Option, @@ -527,7 +576,7 @@ pub async fn redis_key_page( /// Fresh keys-table rows for specific keys. #[tauri::command] pub async fn redis_key_rows( - state: State<'_, DbState>, + state: Db<'_>, keys: Vec, ) -> Result, String> { let cfg = require_redis(&state)?; @@ -537,7 +586,7 @@ pub async fn redis_key_rows( /// A key's value, paged for collections. #[tauri::command] pub async fn redis_key_value( - state: State<'_, DbState>, + state: Db<'_>, key: crate::db::redis::KeyRef, cursor: Option, count: u32, @@ -548,7 +597,7 @@ pub async fn redis_key_value( /// One Redis command as structured JSON, for the insight views. #[tauri::command] -pub async fn redis_command(state: State<'_, DbState>, args: Vec) -> Result { +pub async fn redis_command(state: Db<'_>, args: Vec) -> Result { let cfg = require_redis(&state)?; crate::db::redis::command_json(&cfg, args).await } @@ -557,7 +606,7 @@ pub async fn redis_command(state: State<'_, DbState>, args: Vec) -> Resu #[tauri::command] pub async fn redis_subscribe( app: tauri::AppHandle, - state: State<'_, DbState>, + state: Db<'_>, id: String, channels: Vec, patterns: Vec, @@ -580,7 +629,7 @@ pub fn redis_unsubscribe(id: String) { /// Apply staged Redis writes in one MULTI/EXEC transaction. #[tauri::command] pub async fn redis_apply( - state: State<'_, DbState>, + state: Db<'_>, ops: Vec, ) -> Result, String> { let cfg = require_redis(&state)?; @@ -596,7 +645,7 @@ pub async fn test_dynamodb(config: crate::db::dynamodb::DynamoConfig) -> Result< #[tauri::command] pub async fn connect_dynamodb_db( - state: State<'_, DbState>, + state: Db<'_>, config: crate::db::dynamodb::DynamoConfig, ) -> Result<(), String> { crate::db::connection::connect_dynamodb(state, config).await @@ -611,7 +660,7 @@ pub fn dynamodb_profiles() -> Vec { /// One DynamoDB API call (`op` from an allowlist) on the active connection. #[tauri::command] pub async fn dynamodb_request( - state: State<'_, DbState>, + state: Db<'_>, op: String, body: serde_json::Value, ) -> Result { @@ -630,14 +679,14 @@ pub async fn test_couchdb(config: crate::db::couchdb::CouchConfig) -> Result<(), } #[tauri::command] -pub async fn connect_couchdb_db(state: State<'_, DbState>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { +pub async fn connect_couchdb_db(state: Db<'_>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { crate::db::connection::connect_couchdb(state, config).await } /// One CouchDB HTTP call (path allowlisted) on the active connection. #[tauri::command] pub async fn couchdb_request( - state: State<'_, DbState>, + state: Db<'_>, method: String, path: String, body: Option, @@ -649,6 +698,50 @@ pub async fn couchdb_request( crate::db::couchdb::request(&cfg, &method, &path, body.as_ref()).await } +// ── Elasticsearch / OpenSearch ──────────────────────────────────────────────── + +/// Connection test; answers which engine and version the server is. +#[tauri::command] +pub async fn test_elastic(config: crate::db::elastic::EsConfig) -> Result { + crate::db::elastic::ping(&config).await +} + +#[tauri::command] +pub async fn connect_elastic_db(state: Db<'_>, config: crate::db::elastic::EsConfig) -> Result<(), String> { + crate::db::connection::connect_elastic(state, config).await +} + +/// One REST call (path allowlisted) on the active connection. +#[tauri::command] +pub async fn elastic_request( + state: Db<'_>, + method: String, + path: String, + body: Option, +) -> Result { + let cfg = match require_conn(&state)? { + ActiveConnection::Doc(crate::db::docdb::DocConn::Elastic(cfg)) => cfg, + _ => return Err("Not connected to Elasticsearch or OpenSearch".into()), + }; + crate::db::elastic::request(&cfg, &method, &path, body.as_ref()).await +} + +/// Import a file into an index (the preview reuses `mongo_import_preview`). +#[tauri::command] +pub async fn elastic_import( + state: Db<'_>, + index: String, + path: String, + format: Option, + infer: Option, +) -> Result { + let cfg = match require_conn(&state)? { + ActiveConnection::Doc(crate::db::docdb::DocConn::Elastic(cfg)) => cfg, + _ => return Err("Not connected to Elasticsearch or OpenSearch".into()), + }; + crate::db::elastic::import_file(&cfg, &index, &path, format.as_deref().unwrap_or(""), infer.unwrap_or(true)).await +} + // ── MongoDB ─────────────────────────────────────────────────────────────────── #[tauri::command] @@ -657,11 +750,11 @@ pub async fn test_mongo(config: crate::db::mongo::MongoConfig) -> Result<(), Str } #[tauri::command] -pub async fn connect_mongo_db(state: State<'_, DbState>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { +pub async fn connect_mongo_db(state: Db<'_>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { crate::db::connection::connect_mongo(state, config).await } -fn require_mongo(state: &State<'_, DbState>) -> Result { +fn require_mongo(state: &Db<'_>) -> Result { match require_conn(state)? { ActiveConnection::Doc(crate::db::docdb::DocConn::Mongo(m)) => Ok(m), _ => Err("Not connected to MongoDB".into()), @@ -671,7 +764,7 @@ fn require_mongo(state: &State<'_, DbState>) -> Result, schema: String) -> Result<(), String> { +pub async fn doc_use_schema(state: Db<'_>, schema: String) -> Result<(), String> { match require_conn(&state)? { ActiveConnection::Doc(d) => crate::db::docdb::use_schema(&d, &schema).await, _ => Ok(()), @@ -679,20 +772,20 @@ pub async fn doc_use_schema(state: State<'_, DbState>, schema: String) -> Result } #[tauri::command] -pub async fn mongo_collections(state: State<'_, DbState>) -> Result, String> { +pub async fn mongo_collections(state: Db<'_>) -> Result, String> { let m = require_mongo(&state)?; crate::db::mongo::list_collections(&m).await } #[tauri::command] -pub async fn mongo_describe(state: State<'_, DbState>, collection: String) -> Result { +pub async fn mongo_describe(state: Db<'_>, collection: String) -> Result { let m = require_mongo(&state)?; crate::db::mongo::describe(&m, &collection).await } #[tauri::command] pub async fn mongo_find( - state: State<'_, DbState>, + state: Db<'_>, collection: String, filter: String, sort: Option, @@ -714,14 +807,14 @@ pub async fn mongo_find( } #[tauri::command] -pub async fn mongo_count(state: State<'_, DbState>, collection: String, filter: String) -> Result { +pub async fn mongo_count(state: Db<'_>, collection: String, filter: String) -> Result { let m = require_mongo(&state)?; crate::db::mongo::count(&m, &collection, &filter).await } #[tauri::command] pub async fn mongo_save( - state: State<'_, DbState>, + state: Db<'_>, collection: String, doc: serde_json::Value, prev_id: Option, @@ -731,14 +824,14 @@ pub async fn mongo_save( } #[tauri::command] -pub async fn mongo_delete(state: State<'_, DbState>, collection: String, id: serde_json::Value) -> Result<(), String> { +pub async fn mongo_delete(state: Db<'_>, collection: String, id: serde_json::Value) -> Result<(), String> { let m = require_mongo(&state)?; crate::db::mongo::delete(&m, &collection, &id).await } #[tauri::command] pub async fn mongo_explain( - state: State<'_, DbState>, + state: Db<'_>, collection: String, filter: String, sort: Option, @@ -749,20 +842,20 @@ pub async fn mongo_explain( } #[tauri::command] -pub async fn mongo_schema(state: State<'_, DbState>, collection: String, filter: String, size: Option) -> Result { +pub async fn mongo_schema(state: Db<'_>, collection: String, filter: String, size: Option) -> Result { let m = require_mongo(&state)?; crate::db::mongo::schema_sample(&m, &collection, &filter, size.unwrap_or(1000)).await } #[tauri::command] -pub async fn mongo_indexes(state: State<'_, DbState>, collection: String) -> Result, String> { +pub async fn mongo_indexes(state: Db<'_>, collection: String) -> Result, String> { let m = require_mongo(&state)?; crate::db::mongo::index_details(&m, &collection).await } #[tauri::command] pub async fn mongo_create_index( - state: State<'_, DbState>, + state: Db<'_>, collection: String, keys: String, options: Option, @@ -772,7 +865,7 @@ pub async fn mongo_create_index( } #[tauri::command] -pub async fn mongo_drop_index(state: State<'_, DbState>, collection: String, name: String) -> Result<(), String> { +pub async fn mongo_drop_index(state: Db<'_>, collection: String, name: String) -> Result<(), String> { let m = require_mongo(&state)?; crate::db::mongo::drop_index(&m, &collection, &name).await } @@ -780,7 +873,7 @@ pub async fn mongo_drop_index(state: State<'_, DbState>, collection: String, nam #[tauri::command] #[allow(clippy::too_many_arguments)] pub async fn mongo_update_field( - state: State<'_, DbState>, + state: Db<'_>, collection: String, id: serde_json::Value, path: String, @@ -793,7 +886,7 @@ pub async fn mongo_update_field( } #[tauri::command] -pub async fn mongo_aggregate(state: State<'_, DbState>, collection: String, pipeline: String, limit: Option) -> Result, String> { +pub async fn mongo_aggregate(state: Db<'_>, collection: String, pipeline: String, limit: Option) -> Result, String> { let m = require_mongo(&state)?; crate::db::mongo::aggregate_preview(&m, &collection, &pipeline, limit.unwrap_or(10)).await } @@ -808,7 +901,7 @@ pub async fn mongo_import_preview(path: String, format: Option, infer: O #[tauri::command] pub async fn mongo_import( - state: State<'_, DbState>, + state: Db<'_>, collection: String, path: String, format: Option, @@ -826,14 +919,14 @@ pub async fn test_firestore(config: crate::db::firestore::FirestoreConfig) -> Re } #[tauri::command] -pub async fn connect_firestore_db(state: State<'_, DbState>, config: crate::db::firestore::FirestoreConfig) -> Result<(), String> { +pub async fn connect_firestore_db(state: Db<'_>, config: crate::db::firestore::FirestoreConfig) -> Result<(), String> { crate::db::connection::connect_firestore(state, config).await } /// One Firestore REST call (document paths of this database only). #[tauri::command] pub async fn firestore_request( - state: State<'_, DbState>, + state: Db<'_>, method: String, path: String, body: Option, @@ -853,11 +946,11 @@ pub async fn test_cassandra(config: crate::db::cassandra::CassandraConfig) -> Re } #[tauri::command] -pub async fn connect_cassandra_db(state: State<'_, DbState>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { +pub async fn connect_cassandra_db(state: Db<'_>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { crate::db::connection::connect_cassandra(state, config).await } -fn require_cassandra(state: &State<'_, DbState>) -> Result { +fn require_cassandra(state: &Db<'_>) -> Result { match require_conn(state)? { ActiveConnection::Doc(crate::db::docdb::DocConn::Cassandra(s)) => Ok(s), _ => Err("Not connected to Cassandra".into()), @@ -865,20 +958,20 @@ fn require_cassandra(state: &State<'_, DbState>) -> Result) -> Result, String> { +pub async fn cassandra_tables(state: Db<'_>) -> Result, String> { let s = require_cassandra(&state)?; crate::db::cassandra::list_tables(&s).await } #[tauri::command] -pub async fn cassandra_describe(state: State<'_, DbState>, table: String) -> Result { +pub async fn cassandra_describe(state: Db<'_>, table: String) -> Result { let s = require_cassandra(&state)?; crate::db::cassandra::describe(&s, &table).await } #[tauri::command] pub async fn cassandra_page( - state: State<'_, DbState>, + state: Db<'_>, table: String, filter: String, cursor: Option, @@ -890,7 +983,7 @@ pub async fn cassandra_page( #[tauri::command] pub async fn cassandra_save( - state: State<'_, DbState>, + state: Db<'_>, table: String, row: serde_json::Value, prev: Option, @@ -900,7 +993,7 @@ pub async fn cassandra_save( } #[tauri::command] -pub async fn cassandra_delete(state: State<'_, DbState>, table: String, row: serde_json::Value) -> Result<(), String> { +pub async fn cassandra_delete(state: Db<'_>, table: String, row: serde_json::Value) -> Result<(), String> { let s = require_cassandra(&state)?; crate::db::cassandra::delete(&s, &table, &row).await } @@ -913,7 +1006,7 @@ pub async fn test_duckdb(config: DuckdbConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_duckdb_db(state: State<'_, DbState>, config: DuckdbConfig) -> Result<(), String> { +pub async fn connect_duckdb_db(state: Db<'_>, config: DuckdbConfig) -> Result<(), String> { connect_duckdb(state, config).await } @@ -925,10 +1018,31 @@ pub async fn test_mssql(config: MssqlConfig) -> Result<(), String> { } #[tauri::command] -pub async fn connect_mssql_db(state: State<'_, DbState>, config: MssqlConfig) -> Result<(), String> { +pub async fn connect_mssql_db(state: Db<'_>, config: MssqlConfig) -> Result<(), String> { connect_mssql(state, config).await } +// ── Oracle ────────────────────────────────────────────────────────────────────── + +#[tauri::command] +pub async fn test_oracle(config: OracleConfig) -> Result<(), String> { + test_oracle_connection(config).await +} + +#[tauri::command] +pub async fn connect_oracle_db(state: Db<'_>, config: OracleConfig) -> Result<(), String> { + connect_oracle(state, config).await +} + +/// The Oracle container the active connection is in (FREEPDB1, CDB$ROOT). +#[tauri::command] +pub async fn oracle_container_name(state: Db<'_>) -> Result { + match crate::db::connection::require_conn(&state)? { + ActiveConnection::Oracle(h) => crate::db::oracle::container_name(&h).await, + _ => Err("Not connected to Oracle".into()), + } +} + // ── Live mode ───────────────────────────────────────────────────────────────── use crate::db::live::{self, LiveState}; @@ -937,7 +1051,7 @@ use crate::db::live::{self, LiveState}; #[tauri::command] pub async fn live_start( app: tauri::AppHandle, - state: State<'_, DbState>, + state: Db<'_>, live_state: State<'_, LiveState>, schema: String, table: String, @@ -957,7 +1071,7 @@ pub async fn live_stop(live_state: State<'_, LiveState>) -> Result<(), String> { #[tauri::command] pub async fn disconnect_postgres( - state: State<'_, DbState>, + state: Db<'_>, tunnel_state: State<'_, TunnelState>, live_state: State<'_, LiveState>, ) -> Result<(), String> { @@ -1027,11 +1141,12 @@ fn mssql_plan_lines(xml: &str) -> Vec { #[tauri::command] pub async fn pg_explain_sql( - state: State<'_, DbState>, + state: Db<'_>, sql: String, + analyze: Option, ) -> Result { match require_conn(&state)? { - ActiveConnection::Postgres(pool) => explain_pg(&pool, &sql).await, + ActiveConnection::Postgres(pool) => explain_pg(&pool, &sql, analyze.unwrap_or(true)).await, ActiveConnection::Mysql(pool) => explain_mysql(&pool, &sql).await, ActiveConnection::Sqlite(pool) => explain_sqlite(&pool, &sql).await, ActiveConnection::Duckdb(h) => { @@ -1102,6 +1217,10 @@ pub async fn pg_explain_sql( } Ok(explain_from_text_lines(mssql_plan_lines(&xml), "mssql")) } + ActiveConnection::Oracle(h) => { + let lines = crate::db::oracle::explain_lines(&h, &sql).await?; + Ok(explain_from_text_lines(lines, "oracle")) + } _ => Err("EXPLAIN isn't supported for this engine yet".into()), } } @@ -1109,13 +1228,13 @@ pub async fn pg_explain_sql( // ── DB-agnostic query commands ──────────────────────────────────────────────── #[tauri::command] -pub async fn pg_list_schemas(state: State<'_, DbState>) -> Result, String> { +pub async fn pg_list_schemas(state: Db<'_>) -> Result, String> { list_schemas(state).await } #[tauri::command] pub async fn pg_list_tables( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { list_tables(state, schema).await @@ -1125,7 +1244,7 @@ pub async fn pg_list_tables( /// unknown (-1) row count. See `db::schema::table_row_counts`. #[tauri::command] pub async fn pg_table_row_counts( - state: State<'_, DbState>, + state: Db<'_>, schema: String, tables: Vec, ) -> Result, String> { @@ -1134,7 +1253,7 @@ pub async fn pg_table_row_counts( #[tauri::command] pub async fn pg_list_indexes( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: Option, ) -> Result, String> { @@ -1143,7 +1262,7 @@ pub async fn pg_list_indexes( #[tauri::command] pub async fn pg_get_table_column_structure( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result, String> { @@ -1154,7 +1273,7 @@ pub async fn pg_get_table_column_structure( /// table. Used by the ER diagram, which needs the whole schema up front. #[tauri::command] pub async fn pg_get_schema_column_structure( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { get_schema_column_structure(state, schema).await @@ -1162,7 +1281,7 @@ pub async fn pg_get_schema_column_structure( #[tauri::command] pub async fn pg_get_incoming_foreign_keys( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result, String> { @@ -1171,7 +1290,7 @@ pub async fn pg_get_incoming_foreign_keys( #[tauri::command] pub async fn pg_list_enums( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { list_enums(state, schema).await @@ -1179,7 +1298,7 @@ pub async fn pg_list_enums( #[tauri::command] pub async fn pg_list_triggers( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { list_triggers(state, schema).await @@ -1187,7 +1306,7 @@ pub async fn pg_list_triggers( #[tauri::command] pub async fn pg_list_sequences( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { list_sequences(state, schema).await @@ -1195,7 +1314,7 @@ pub async fn pg_list_sequences( #[tauri::command] pub async fn pg_list_functions( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { list_functions(state, schema).await @@ -1205,7 +1324,7 @@ pub async fn pg_list_functions( /// and which of those kinds the engine has (the sidebar's Objects tab). #[tauri::command] pub async fn list_db_objects( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result { crate::db::objects::list_db_objects(state, schema).await @@ -1213,7 +1332,7 @@ pub async fn list_db_objects( #[tauri::command] pub async fn get_object_definition( - state: State<'_, DbState>, + state: Db<'_>, kind: String, schema: String, name: String, @@ -1226,7 +1345,7 @@ pub async fn get_object_definition( #[tauri::command] #[allow(clippy::too_many_arguments)] pub async fn drop_db_object( - state: State<'_, DbState>, + state: Db<'_>, kind: String, schema: String, name: String, @@ -1251,20 +1370,20 @@ pub async fn drop_db_object( /// Table and view comments for one schema (the sidebar's comments setting). #[tauri::command] pub async fn list_object_comments( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { crate::db::objects::list_object_comments(state, schema).await } #[tauri::command] -pub async fn ping_db_connection(state: State<'_, DbState>) -> Result<(), String> { +pub async fn ping_db_connection(state: Db<'_>) -> Result<(), String> { ping_connection(state).await } #[tauri::command] pub async fn pg_truncate_table( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result<(), String> { @@ -1273,7 +1392,7 @@ pub async fn pg_truncate_table( #[tauri::command] pub async fn pg_drop_table( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, cascade: bool, @@ -1286,7 +1405,7 @@ pub async fn pg_drop_table( /// Copy a database on the current server under a new name, structure and rows. #[tauri::command] pub async fn pg_clone_database( - state: State<'_, DbState>, + state: Db<'_>, source: String, target: String, ) -> Result { @@ -1296,7 +1415,7 @@ pub async fn pg_clone_database( #[tauri::command] pub async fn get_table_ddl( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result { @@ -1314,7 +1433,7 @@ pub async fn get_table_ddl_on_connection( #[tauri::command] pub async fn pg_get_table_rows( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, limit: i64, @@ -1370,7 +1489,7 @@ pub async fn pg_get_table_rows( /// total streams in without blocking the initial paint. #[tauri::command] pub async fn pg_count_table_rows( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, search: Option, @@ -1381,6 +1500,10 @@ pub async fn pg_count_table_rows( // Optional - defaults to false. True counts a large table instead of // taking the planner's estimate (the "Exact row count" setting). exact: Option, + // Optional. A newer count under the same key cancels this one on the + // server (the grid passes one per tab), so typing a search doesn't leave + // a full-table COUNT(*) running for every keystroke. + supersede: Option, ) -> Result { count_table_rows( state, @@ -1391,13 +1514,34 @@ pub async fn pg_count_table_rows( search_case_sensitive.unwrap_or(false), filters, exact.unwrap_or(false), + supersede, ) .await } +/// Which text columns a search index would cover, and which have one. +#[tauri::command] +pub async fn pg_search_index_status( + state: Db<'_>, + schema: String, + table: String, +) -> Result { + crate::db::search_index_status(state, schema, table).await +} + +/// Create trigram indexes so the grid's search is an index scan. +#[tauri::command] +pub async fn pg_create_search_index( + state: Db<'_>, + schema: String, + table: String, +) -> Result, String> { + crate::db::create_search_index(state, schema, table).await +} + #[tauri::command] pub async fn pg_get_column_stats( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, column: String, @@ -1409,7 +1553,7 @@ pub async fn pg_get_column_stats( #[tauri::command] pub async fn geo_overview( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result { crate::db::geo_overview(state).await } @@ -1417,7 +1561,7 @@ pub async fn geo_overview( #[tauri::command] #[allow(clippy::too_many_arguments)] pub async fn geo_features( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, column: String, @@ -1442,28 +1586,28 @@ pub async fn geo_features( #[tauri::command] pub async fn instance_version( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result { crate::db::instance_version(state).await } #[tauri::command] pub async fn instance_activity( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result { crate::db::instance_activity(state).await } #[tauri::command] pub async fn instance_state( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result { crate::db::instance_state(state).await } #[tauri::command] pub async fn instance_config( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result, String> { crate::db::instance_config(state).await } @@ -1472,7 +1616,7 @@ pub async fn instance_config( /// `value = None` resets it to the server default. #[tauri::command] pub async fn instance_set_config( - state: State<'_, DbState>, + state: Db<'_>, name: String, value: Option, ) -> Result { @@ -1483,7 +1627,7 @@ pub async fn instance_set_config( /// only - it never reads user rows and never writes. #[tauri::command] pub async fn advisor_scan( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result { let conn = crate::db::connection::require_conn(&state)?; crate::db::advisor::scan(&conn).await @@ -1491,7 +1635,7 @@ pub async fn advisor_scan( #[tauri::command] pub async fn instance_replication( - state: State<'_, DbState>, + state: Db<'_>, ) -> Result { crate::db::instance_replication(state).await } @@ -1500,16 +1644,45 @@ pub async fn instance_replication( /// it keeps the single-slot behaviour older callers relied on. #[tauri::command] pub async fn pg_execute_sql( - state: State<'_, DbState>, + state: Db<'_>, sql: String, query_id: Option, ) -> Result { execute_sql(state, sql, query_id).await } +/// A shared studio's SQL (Labs: Share in a connection's menu). Read-only +/// shares go through the MCP server's own path: the access classifier first +/// (one reading statement), then the engine's read-only transaction +/// (Postgres, MySQL) or a transaction that is always rolled back (SQLite). +/// Returns the MCP tool's JSON: { columns, rows, row_count, truncated }. +#[tauri::command] +pub async fn share_execute_sql(state: Db<'_>, sql: String, read_only: bool) -> Result { + use crate::mcp::access; + let conn = require_conn(&state)?; + if read_only { + let dialect = access::Dialect::for_driver(conn.driver()); + if access::script_level_in(dialect, &sql) > access::READ || access::statement_count(dialect, &sql) > 1 { + return Err("This shared studio is read-only: run one SELECT, WITH, EXPLAIN or SHOW at a time.".into()); + } + } + crate::mcp::tools::call_tool(&conn, "execute_sql", &serde_json::json!({ "sql": sql, "max_rows": 1000 }), read_only).await +} + +/// The access level a script needs on the open connection (0 read, 1 read & +/// write, 2 modify schema, 3 dangerous), by the classifier the MCP server +/// enforces. The built-in AI chat checks it before running a statement, so +/// its access setting holds the same way a local agent's does. +#[tauri::command] +pub async fn sql_access_level(state: Db<'_>, sql: String) -> Result { + use crate::mcp::access; + let dialect = require_conn(&state).map(|c| access::Dialect::for_driver(c.driver())).unwrap_or(access::Dialect::Sql); + Ok(access::script_level_in(dialect, &sql)) +} + #[tauri::command] pub async fn pg_execute_sql_multi( - state: State<'_, DbState>, + state: Db<'_>, sql: String, query_id: Option, ) -> Result, String> { @@ -1526,7 +1699,7 @@ pub async fn pg_execute_sql_multi( /// Stream results). The window reads them with `result_window`. #[tauri::command] pub async fn pg_execute_sql_stream( - state: State<'_, DbState>, + state: Db<'_>, store: State<'_, crate::db::result_store::ResultStore>, sql: String, query_id: Option, @@ -1625,13 +1798,13 @@ pub async fn list_tables_on_connection( /// Run a DDL statement outside a transaction (required for CREATE/DROP DATABASE etc.). #[tauri::command] -pub async fn pg_execute_ddl(state: State<'_, DbState>, sql: String) -> Result<(), String> { +pub async fn pg_execute_ddl(state: Db<'_>, sql: String) -> Result<(), String> { execute_ddl(state, sql).await } #[tauri::command] pub async fn pg_update_table_cell( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_key: HashMap, @@ -1644,7 +1817,7 @@ pub async fn pg_update_table_cell( /// Load one cell in full - the value a browse page deliberately did not fetch. #[tauri::command] pub async fn pg_fetch_cell_value( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_key: HashMap, @@ -1656,7 +1829,7 @@ pub async fn pg_fetch_cell_value( #[tauri::command] pub async fn pg_delete_table_row( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_key: HashMap, @@ -1666,7 +1839,7 @@ pub async fn pg_delete_table_row( #[tauri::command] pub async fn pg_delete_table_rows( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_keys: Vec>, @@ -1677,7 +1850,7 @@ pub async fn pg_delete_table_rows( /// Staged grid changes as one transaction (Postgres, MySQL, SQLite) - see `apply_table_changes`. #[tauri::command] pub async fn pg_apply_table_changes( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, updates: Vec, @@ -1689,7 +1862,7 @@ pub async fn pg_apply_table_changes( #[tauri::command] pub async fn pg_insert_table_row( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, values: HashMap, @@ -1703,7 +1876,7 @@ pub async fn pg_insert_table_row( /// cancels every query in flight (the old whole-app "stop" behaviour). #[tauri::command] pub async fn cancel_query( - state: State<'_, DbState>, + state: Db<'_>, query_id: Option, ) -> Result<(), String> { if let Ok(mut map) = state.cancels.lock() { @@ -1740,170 +1913,19 @@ pub fn connections_store_write(app: tauri::AppHandle, json: String) -> Result<() // ── License ─────────────────────────────────────────────────────────────────── -// `async` so the first call (device-fingerprint subprocess + trial-file I/O) -// runs off the main thread instead of blocking the UI during startup. +// `async` so the first call (device fingerprint, cache read) runs off the main +// thread instead of blocking the UI during startup. Pro comes from the +// signed-in account only (see account::license_status). #[tauri::command(async)] pub fn check_license_status(app: tauri::AppHandle) -> crate::license::LicenseStatus { match app.path().app_data_dir() { - Ok(dir) => { - // Before check_status starts a trial, so a new install is told apart. - crate::account::note_install_state(&dir); - crate::account::apply_account(&dir, crate::license::check_status(&dir)) - } + Ok(dir) => crate::account::license_status(&dir), Err(e) => crate::license::LicenseStatus::Error { message: e.to_string(), }, } } -#[tauri::command] -pub async fn activate_license( - app: tauri::AppHandle, - key: String, -) -> Result { - // 1. Verify signature locally (works offline, catches forged keys) - let parsed = crate::license::verify_key(&key)?; - let device_id = crate::license::device_id(); - - // 2. Register with server. Blocks on seat_limit_exceeded / revoked. - // On network failure api_activate returns Ok(None) - we allow offline activation. - match crate::license::api_activate(&key, &device_id, &app.package_info().version.to_string()).await { - Ok(None) => {} // offline - proceed with local-only activation - Ok(Some(_)) => {} // server accepted - Err(e) => { - // Convert server error codes to user-friendly messages - let msg = match e.as_str() { - "seat_limit_exceeded" => "Seat limit reached. Deactivate another device first.", - "revoked" => "This license key has been revoked.", - "expired" => "This license key has expired.", - "not_found" => "License key not found.", - _ => return Err(e), - }; - return Err(msg.to_string()); - } - } - - // 3. Save locally - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_secs()) - .unwrap_or(0); - let dir = app.path().app_data_dir().map_err(|e| e.to_string())?; - let lic = crate::license::LicenseFile { - version: 1, - key, - email: parsed.email.clone(), - plan: parsed.plan.clone(), - issued_at: parsed.issued_at, - expires_at: parsed.expires_at, - device_id, - activated_at: now, - last_check_at: now, - }; - crate::license::save_license(&dir, &lic)?; - Ok(serde_json::json!({ - "email": parsed.email, - "plan": parsed.plan, - "issued_at": parsed.issued_at, - "expires_at": parsed.expires_at, - })) -} - -#[tauri::command] -pub async fn deactivate_license(app: tauri::AppHandle) -> Result<(), String> { - let dir = app.path().app_data_dir().map_err(|e| e.to_string())?; - // Best-effort server deactivation (release the seat) - if let Some(lic) = crate::license::load_license(&dir) { - crate::license::api_deactivate(&lic.key, &lic.device_id).await; - } - crate::license::delete_license(&dir) -} - -/// Validates the license against the server on every launch. -/// - valid: true → update last_check_at, return Valid -/// - revoked / expired → delete local license, return Trial/TrialExpired -/// - device_not_activated → re-activate this device, keep the license -/// - any other error or network failure → keep the license (local signature -/// is the grace; the next launch checks again) -/// - no license → local trial check only (no network call) -#[tauri::command] -pub async fn run_license_check( - app: tauri::AppHandle, -) -> crate::license::LicenseStatus { - let dir = match app.path().app_data_dir() { - Ok(d) => d, - Err(e) => return crate::license::LicenseStatus::Error { message: e.to_string() }, - }; - crate::account::note_install_state(&dir); - - if let Some(mut lic) = crate::license::load_license(&dir) { - let now = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|d| d.as_secs()) - .unwrap_or(0); - - use crate::license::CheckOutcome; - let version = app.package_info().version.to_string(); - let mut ended = false; - match crate::license::api_check(&lic.key, &lic.device_id, &version).await { - Some(CheckOutcome::Valid) => { - lic.last_check_at = now; - let _ = crate::license::save_license(&dir, &lic); - } - Some(CheckOutcome::Ended) => ended = true, - Some(CheckOutcome::NotActivated) => { - // Activated offline, so the server never saw this device (or its - // seat was released). Register it now rather than dropping Pro. - match crate::license::api_activate(&lic.key, &lic.device_id, &version).await { - Ok(Some(_)) => { - lic.last_check_at = now; - let _ = crate::license::save_license(&dir, &lic); - } - Err(e) if e == "revoked" || e == "expired" => ended = true, - // Offline, seat limit, or anything else: keep the key and - // try again on the next check. - _ => {} - } - } - // `not_found`, unknown errors, a bad deploy, or offline: keep the key; - // the local signature is the grace. - Some(CheckOutcome::Unknown) | None => {} - } - if ended { - let _ = crate::license::delete_license(&dir); - } - } - - // No valid paid license → reconcile the trial clock with the server so a - // reinstall/disk-wipe can't grant a fresh trial (server keeps the earliest - // start per device). Silent no-op when offline. - if crate::license::load_license(&dir).is_none() { - crate::license::reconcile_trial(&dir).await; - } - - // A signed-in account's cached, signature-checked entitlement can grant Pro; - // it never takes Pro away from a valid key (see account::combine). - crate::account::apply_account(&dir, crate::license::check_status(&dir)) -} - -// ── License debug helpers (debug builds only) ───────────────────────────────── - -/// Backdate the trial file so the UI shows it as expired (or N days elapsed). -/// Only compiled in debug mode - stripped from release builds entirely. -#[cfg(debug_assertions)] -#[tauri::command] -pub fn debug_set_trial_days_ago(app: tauri::AppHandle, days_ago: u64) -> Result<(), String> { - let dir = app.path().app_data_dir().map_err(|e| e.to_string())?; - crate::license::debug_set_trial_days_ago(&dir, days_ago) -} - -#[cfg(debug_assertions)] -#[tauri::command] -pub fn debug_reset_trial(app: tauri::AppHandle) -> Result<(), String> { - let dir = app.path().app_data_dir().map_err(|e| e.to_string())?; - crate::license::debug_reset_trial(&dir) -} - // ── Sample database ──────────────────────────────────────────────────────────── /// Ensures the bundled sample SQLite database exists in the app data directory. @@ -2148,3 +2170,100 @@ pub fn set_window_glass(window: tauri::WebviewWindow, enabled: bool, dark: bool) let _ = (window, enabled, dark); Ok(()) } + +/// D1 Time Travel: the bookmark for now, or for the moment nearest `timestamp`. +#[tauri::command] +pub async fn d1_bookmark(state: Db<'_>, timestamp: Option) -> Result { + match require_conn(&state)? { + ActiveConnection::D1(cfg) => crate::db::d1::bookmark(&cfg, timestamp.as_deref()).await, + _ => Err("Time Travel is a Cloudflare D1 feature.".into()), + } +} + +/// D1 Time Travel: rewind the database to a bookmark; returns the previous one. +#[tauri::command] +pub async fn d1_restore(state: Db<'_>, bookmark: String) -> Result { + match require_conn(&state)? { + ActiveConnection::D1(cfg) => crate::db::d1::restore(&cfg, &bookmark).await, + _ => Err("Time Travel is a Cloudflare D1 feature.".into()), + } +} + + +/// Write a result as a Parquet file, through an in-memory DuckDB (bundled, so +/// nothing is needed on the machine). `types` per column: BIGINT, DOUBLE, +/// BOOLEAN or VARCHAR, inferred by the page from the values. +#[tauri::command] +pub async fn export_parquet( + path: String, + columns: Vec, + types: Vec, + rows: Vec>, +) -> Result<(), String> { + tokio::task::spawn_blocking(move || write_parquet(&path, &columns, &types, &rows)) + .await + .map_err(|e| e.to_string())? +} + +fn write_parquet( + path: &str, + columns: &[String], + types: &[String], + rows: &[Vec], +) -> Result<(), String> { + use ::duckdb::types::Value as V; + if columns.is_empty() { + return Err("Nothing to export: the result has no columns.".into()); + } + let ty = |i: usize| match types.get(i).map(|t| t.to_ascii_uppercase()) { + Some(t) if t == "BIGINT" || t == "DOUBLE" || t == "BOOLEAN" => t, + _ => "VARCHAR".to_string(), + }; + let ident = |s: &str| format!("\"{}\"", s.replace('"', "\"\"")); + let conn = ::duckdb::Connection::open_in_memory().map_err(|e| e.to_string())?; + // Parquet is a DuckDB extension. The bundled build doesn't compile it in + // (it doubles the C++ build); DuckDB fetches it once and keeps it in its + // own cache, so later exports work offline. + conn.execute_batch("INSTALL parquet; LOAD parquet;").map_err(|e| { + format!("Parquet needs DuckDB's parquet extension, downloaded once from extensions.duckdb.org: {e}") + })?; + let cols = columns + .iter() + .enumerate() + .map(|(i, c)| format!("{} {}", ident(c), ty(i))) + .collect::>() + .join(", "); + conn.execute_batch(&format!("CREATE TABLE t ({cols})")) + .map_err(|e| e.to_string())?; + { + let mut app = conn.appender("t").map_err(|e| e.to_string())?; + for row in rows { + let vals: Vec = (0..columns.len()) + .map(|i| { + let v = row.get(i).unwrap_or(&serde_json::Value::Null); + match (ty(i).as_str(), v) { + (_, serde_json::Value::Null) => V::Null, + ("BIGINT", serde_json::Value::Number(n)) => { + n.as_i64().map(V::BigInt).unwrap_or(V::Null) + } + ("DOUBLE", serde_json::Value::Number(n)) => { + n.as_f64().map(V::Double).unwrap_or(V::Null) + } + ("BOOLEAN", serde_json::Value::Bool(b)) => V::Boolean(*b), + (_, serde_json::Value::String(s)) => V::Text(s.clone()), + (_, other) => V::Text(other.to_string()), + } + }) + .collect(); + app.append_row(::duckdb::appender_params_from_iter(vals)) + .map_err(|e| e.to_string())?; + } + app.flush().map_err(|e| e.to_string())?; + } + conn.execute_batch(&format!( + "COPY t TO '{}' (FORMAT PARQUET, COMPRESSION ZSTD)", + path.replace('\'', "''") + )) + .map_err(|e| format!("Couldn't write the Parquet file: {e}"))?; + Ok(()) +} diff --git a/src-tauri/src/crash.rs b/src-tauri/src/crash.rs new file mode 100644 index 00000000..f3347c9d --- /dev/null +++ b/src-tauri/src/crash.rs @@ -0,0 +1,112 @@ +//! Crash reports. Release builds abort on a panic (Cargo.toml), so the window +//! is simply gone and nothing on screen says why. The panic hook writes what it +//! knows to a file before the abort; the next launch finds it and the page +//! offers to report it (`take_crash_report`, ReportIssueDialog). + +use std::path::PathBuf; +use std::sync::OnceLock; + +const FILE: &str = "last-crash.json"; + +/// The app's data directory, once `setup()` knows it. A panic before that +/// writes to the temp directory instead; both are checked on the next launch. +static DIR: OnceLock = OnceLock::new(); + +pub fn set_dir(dir: PathBuf) { + let _ = std::fs::create_dir_all(&dir); + let _ = DIR.set(dir); +} + +fn fallback_path() -> PathBuf { + std::env::temp_dir().join(format!("stroke-{FILE}")) +} + +fn report_path() -> PathBuf { + DIR.get().map(|d| d.join(FILE)).unwrap_or_else(fallback_path) +} + +#[derive(serde::Serialize, serde::Deserialize, Debug, Clone, PartialEq)] +pub struct CrashReport { + pub message: String, + pub location: String, + pub thread: String, + pub version: String, + pub os: String, + /// Seconds since the Unix epoch. + pub at: u64, + pub backtrace: String, +} + +fn payload_text(info: &std::panic::PanicHookInfo<'_>) -> String { + let p = info.payload(); + p.downcast_ref::<&str>() + .map(|s| s.to_string()) + .or_else(|| p.downcast_ref::().cloned()) + .unwrap_or_else(|| "unknown panic".to_string()) +} + +/// Install the hook. Call first thing in `run()`. The previous hook still runs +/// (stderr), and after it the process aborts as before, so the operating +/// system's own crash reporter sees the crash too. +pub fn install() { + let previous = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |info| { + let report = CrashReport { + message: payload_text(info), + location: info.location().map(|l| format!("{}:{}", l.file(), l.line())).unwrap_or_default(), + thread: std::thread::current().name().unwrap_or("unnamed").to_string(), + version: env!("CARGO_PKG_VERSION").to_string(), + os: format!("{} {}", std::env::consts::OS, std::env::consts::ARCH), + at: std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|d| d.as_secs()) + .unwrap_or(0), + backtrace: std::backtrace::Backtrace::force_capture().to_string(), + }; + log::error!("panic at {}: {}", report.location, report.message); + if let Ok(json) = serde_json::to_vec_pretty(&report) { + let _ = std::fs::write(report_path(), json); + } + previous(info); + })); +} + +/// The report the last run left behind, if it crashed. Reading it deletes it, +/// so it is offered once. +#[tauri::command] +pub fn take_crash_report() -> Option { + let mut found = None; + for path in [report_path(), fallback_path()] { + if let Ok(bytes) = std::fs::read(&path) { + let _ = std::fs::remove_file(&path); + if found.is_none() { + found = serde_json::from_slice::(&bytes).ok(); + } + } + } + found +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_report_round_trips_and_is_taken_once() { + let dir = std::env::temp_dir().join(format!("stroke-crash-test-{}", std::process::id())); + set_dir(dir.clone()); + let r = CrashReport { + message: "boom".into(), + location: "src/x.rs:1".into(), + thread: "main".into(), + version: "1.0.0".into(), + os: "linux x86_64".into(), + at: 1, + backtrace: String::new(), + }; + std::fs::write(report_path(), serde_json::to_vec(&r).unwrap()).unwrap(); + assert_eq!(take_crash_report(), Some(r)); + assert_eq!(take_crash_report(), None); + let _ = std::fs::remove_dir_all(dir); + } +} diff --git a/src-tauri/src/db/admin.rs b/src-tauri/src/db/admin.rs index 8b5b67b3..5dc88b74 100644 --- a/src-tauri/src/db/admin.rs +++ b/src-tauri/src/db/admin.rs @@ -23,6 +23,7 @@ pub(crate) enum Dialect { Duckdb, Mssql, Clickhouse, + Oracle, } impl Dialect { @@ -34,6 +35,7 @@ impl Dialect { ActiveConnection::Sqlite(_) | ActiveConnection::D1(_) | ActiveConnection::LibSql(_) => Some(Self::Sqlite), ActiveConnection::Duckdb(_) => Some(Self::Duckdb), ActiveConnection::Mssql(_) => Some(Self::Mssql), + ActiveConnection::Oracle(_) => Some(Self::Oracle), ActiveConnection::Clickhouse(_) => Some(Self::Clickhouse), ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => None, } @@ -90,7 +92,7 @@ fn qualified(d: Dialect, schema: &str, name: &str) -> String { fn quote(d: Dialect, ident: &str) -> String { match d { - Dialect::Postgres | Dialect::Sqlite | Dialect::Duckdb => quote_double(ident), + Dialect::Postgres | Dialect::Sqlite | Dialect::Duckdb | Dialect::Oracle => quote_double(ident), Dialect::Mysql | Dialect::Clickhouse => quote_backtick(ident), Dialect::Mssql => quote_bracket(ident), } @@ -135,6 +137,7 @@ pub(crate) async fn run_statement(conn: &ActiveConnection, sql: &str) -> Result< ActiveConnection::LibSql(cfg) => super::libsql::query(cfg, sql, vec![]).await.map(|_| ()), ActiveConnection::Duckdb(h) => super::duckdb::execute_sql(h, sql).await.map(|_| ()), ActiveConnection::Mssql(h) => super::mssql::execute_sql(h, sql).await.map(|_| ()), + ActiveConnection::Oracle(h) => super::oracle::execute_sql(h, sql).await.map(|_| ()), ActiveConnection::Clickhouse(cfg) => super::clickhouse::query(cfg, sql).await.map(|_| ()), ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Err("This connection does not run DDL statements".into()), } diff --git a/src-tauri/src/db/advisor.rs b/src-tauri/src/db/advisor.rs index 9ec18138..2d3cb710 100644 --- a/src-tauri/src/db/advisor.rs +++ b/src-tauri/src/db/advisor.rs @@ -92,12 +92,21 @@ fn bytes_pretty(bytes: f64) -> String { /// error would mean every ordinary deployment opens this tab to a wall of red - /// which teaches people to ignore the tab. async fn check_rls_disabled(pool: &PgPool) -> Result, String> { + // Is there a role that untrusted clients use (Supabase's anon/authenticated, + // PostgREST's web_anon)? Without one, Postgres is reached through an app + // server and RLS is simply not the access model: say so quietly (info) + // instead of opening every ordinary database on a wall of warnings. let sql = format!( - "SELECT n.nspname, c.relname + "WITH api AS (SELECT rolname FROM pg_roles WHERE rolname IN ('anon','authenticated','web_anon','authenticator')) + SELECT n.nspname, c.relname, + EXISTS (SELECT 1 FROM api) AS exposed, + EXISTS (SELECT 1 FROM api WHERE api.rolname IN ('anon','web_anon') + AND has_table_privilege(api.rolname, c.oid, 'SELECT')) AS anon_reads FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind = 'r' AND NOT c.relrowsecurity + AND NOT EXISTS (SELECT 1 FROM pg_policy p WHERE p.polrelid = c.oid) AND n.nspname NOT IN ({SYSTEM_SCHEMAS}) AND n.nspname NOT LIKE 'pg\\_temp%' ORDER BY 1, 2" @@ -108,29 +117,235 @@ async fn check_rls_disabled(pool: &PgPool) -> Result, String .map(|r| { let schema: String = r.get(0); let table: String = r.get(1); + let exposed: bool = r.get(2); + let anon_reads: bool = r.get(3); + let (severity, description, detail) = if anon_reads { + ("error", "The anonymous API role can SELECT this table and nothing filters the rows: anyone with the public key reads all of it.", Some("anon can SELECT · no RLS".to_string())) + } else if exposed { + ("warning", "This database has API roles that clients use directly (PostgREST, Supabase). Without RLS, any of them granted access to the table reads every row.", None) + } else { + ("info", "No API roles here, so access goes through an application server and RLS is not how it is enforced. Turn it on before exposing the database to clients directly.", None) + }; AdvisorFinding { check_id: "rls_disabled".into(), category: "security".into(), - severity: "warning".into(), + severity: severity.into(), title: "Table without row-level security".into(), entity: format!("{schema}.{table}"), entity_kind: "table".into(), - description: "Any role that can reach this table reads every row. That is what you want behind a trusted application server, and a hole if clients connect to Postgres directly.".into(), - detail: None, + description: description.into(), + detail, remediation: Some(format!( "ALTER TABLE \"{schema}\".\"{table}\" ENABLE ROW LEVEL SECURITY;\n-- then add at least one policy, or the table denies everything:\n-- CREATE POLICY \"{table}_select\" ON \"{schema}\".\"{table}\" FOR SELECT USING (true);" )), - metric: 0.0, + metric: if anon_reads { 2.0 } else if exposed { 1.0 } else { 0.0 }, } }) .collect()) } -/// Foreign keys whose columns aren't the leading columns of any index. -/// -/// Postgres indexes the referenced side automatically and the referencing side not -/// at all, so every join and every `ON DELETE` check on this constraint is a -/// sequential scan of the child table. +/// Policies written for a table whose RLS is off: they look like protection and +/// enforce nothing. +async fn check_policies_ignored(pool: &PgPool) -> Result, String> { + let sql = format!( + "SELECT n.nspname, c.relname, count(p.*)::int8 + FROM pg_class c + JOIN pg_namespace n ON n.oid = c.relnamespace + JOIN pg_policy p ON p.polrelid = c.oid + WHERE c.relkind IN ('r','p') AND NOT c.relrowsecurity + AND n.nspname NOT IN ({SYSTEM_SCHEMAS}) + GROUP BY 1, 2 ORDER BY 1, 2" + ); + let rows = sqlx::query(&sql).fetch_all(pool).await.map_err(|e| e.to_string())?; + Ok(rows.iter().map(|r| { + let schema: String = r.get(0); + let table: String = r.get(1); + let n: i64 = r.get(2); + AdvisorFinding { + check_id: "policies_ignored".into(), + category: "security".into(), + severity: "error".into(), + title: "Policies that are not enforced".into(), + entity: format!("{schema}.{table}"), + entity_kind: "table".into(), + description: "The table has row-level policies, but RLS is off on it, so Postgres ignores every one of them and each role sees all rows.".into(), + detail: Some(format!("{n} polic{} defined · RLS off", if n == 1 { "y" } else { "ies" })), + remediation: Some(format!("ALTER TABLE \"{schema}\".\"{table}\" ENABLE ROW LEVEL SECURITY;")), + metric: n as f64, + } + }).collect()) +} + +/// RLS switched on with no policy: only the owner (and superusers) see rows. +async fn check_rls_no_policy(pool: &PgPool) -> Result, String> { + let sql = format!( + "SELECT n.nspname, c.relname + FROM pg_class c + JOIN pg_namespace n ON n.oid = c.relnamespace + WHERE c.relkind IN ('r','p') AND c.relrowsecurity + AND NOT EXISTS (SELECT 1 FROM pg_policy p WHERE p.polrelid = c.oid) + AND n.nspname NOT IN ({SYSTEM_SCHEMAS}) + ORDER BY 1, 2" + ); + let rows = sqlx::query(&sql).fetch_all(pool).await.map_err(|e| e.to_string())?; + Ok(rows.iter().map(|r| { + let schema: String = r.get(0); + let table: String = r.get(1); + AdvisorFinding { + check_id: "rls_no_policy".into(), + category: "security".into(), + severity: "warning".into(), + title: "RLS on, but no policy".into(), + entity: format!("{schema}.{table}"), + entity_kind: "table".into(), + description: "Row-level security is enabled and no policy grants anything, so every role except the owner and superusers sees an empty table. Usually a policy that was meant to follow.".into(), + detail: None, + remediation: Some(format!("CREATE POLICY \"{table}_select\" ON \"{schema}\".\"{table}\" FOR SELECT USING (true);")), + metric: 0.0, + } + }).collect()) +} + +/// SECURITY DEFINER functions run as their owner; without a pinned search_path +/// a caller can put their own objects first on it and run code as that owner. +async fn check_definer_search_path(pool: &PgPool) -> Result, String> { + let sql = format!( + "SELECT n.nspname, p.proname, pg_get_function_identity_arguments(p.oid) + FROM pg_proc p + JOIN pg_namespace n ON n.oid = p.pronamespace + WHERE p.prosecdef + AND n.nspname NOT IN ({SYSTEM_SCHEMAS}) + AND NOT EXISTS (SELECT 1 FROM unnest(coalesce(p.proconfig, '{{}}'::text[])) c WHERE c LIKE 'search\\_path=%') + ORDER BY 1, 2" + ); + let rows = sqlx::query(&sql).fetch_all(pool).await.map_err(|e| e.to_string())?; + Ok(rows.iter().map(|r| { + let schema: String = r.get(0); + let name: String = r.get(1); + let args: String = r.get(2); + AdvisorFinding { + check_id: "definer_search_path".into(), + category: "security".into(), + severity: "warning".into(), + title: "SECURITY DEFINER without a search_path".into(), + entity: format!("{schema}.{name}({args})"), + entity_kind: "function".into(), + description: "The function runs with its owner's rights and resolves names through the caller's search_path, so a caller who can create objects can shadow a table or function it uses and run code as the owner.".into(), + detail: None, + remediation: Some(format!("ALTER FUNCTION \"{schema}\".\"{name}\"({args}) SET search_path = pg_catalog, \"{schema}\";")), + metric: 0.0, + } + }).collect()) +} + +/// Two indexes with the same definition: one is pure cost. +async fn check_duplicate_index(pool: &PgPool) -> Result, String> { + let sql = format!( + "SELECT n.nspname, t.relname, i2.relname, i1.relname, pg_relation_size(x2.indexrelid)::float8, + x2.indisprimary OR x2.indisunique OR EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conindid = x2.indexrelid) + FROM pg_index x1 + JOIN pg_index x2 ON x1.indrelid = x2.indrelid AND x1.indexrelid < x2.indexrelid + AND x1.indkey = x2.indkey AND x1.indclass = x2.indclass + AND coalesce(pg_get_expr(x1.indexprs, x1.indrelid), '') = coalesce(pg_get_expr(x2.indexprs, x2.indrelid), '') + AND coalesce(pg_get_expr(x1.indpred, x1.indrelid), '') = coalesce(pg_get_expr(x2.indpred, x2.indrelid), '') + JOIN pg_class i1 ON i1.oid = x1.indexrelid + JOIN pg_class i2 ON i2.oid = x2.indexrelid + JOIN pg_class t ON t.oid = x1.indrelid + JOIN pg_namespace n ON n.oid = t.relnamespace + WHERE n.nspname NOT IN ({SYSTEM_SCHEMAS}) + ORDER BY 5 DESC" + ); + let rows = sqlx::query(&sql).fetch_all(pool).await.map_err(|e| e.to_string())?; + Ok(rows.iter().map(|r| { + let schema: String = r.get(0); + let table: String = r.get(1); + let dup: String = r.get(2); + let keep: String = r.get(3); + let bytes: f64 = r.get(4); + let backs_constraint: bool = r.get(5); + AdvisorFinding { + check_id: "duplicate_index".into(), + category: "performance".into(), + severity: if bytes > 64.0 * 1024.0 * 1024.0 { "warning" } else { "info" }.into(), + title: "Duplicate index".into(), + entity: format!("{schema}.{dup}"), + entity_kind: "index".into(), + description: "Another index on the same table has the same columns, operator classes, expressions and predicate. Every write maintains both and the planner only needs one.".into(), + detail: Some(format!("{} on {schema}.{table} · same as {keep}", bytes_pretty(bytes))), + remediation: Some(if backs_constraint { + format!("-- \"{dup}\" backs a constraint; drop \"{keep}\" instead:\nDROP INDEX CONCURRENTLY \"{schema}\".\"{keep}\";") + } else { + format!("DROP INDEX CONCURRENTLY \"{schema}\".\"{dup}\";") + }), + metric: bytes, + } + }).collect()) +} + +/// Sequences close to the end of their type: inserts start failing at the max. +async fn check_sequence_exhaustion(pool: &PgPool) -> Result, String> { + let sql = format!( + "SELECT schemaname, sequencename, data_type::text, last_value::float8, max_value::float8 + FROM pg_sequences + WHERE last_value IS NOT NULL AND max_value > 0 + AND schemaname NOT IN ({SYSTEM_SCHEMAS}) + AND last_value::float8 / max_value::float8 > 0.5 + ORDER BY last_value::float8 / max_value::float8 DESC" + ); + let rows = sqlx::query(&sql).fetch_all(pool).await.map_err(|e| e.to_string())?; + Ok(rows.iter().map(|r| { + let schema: String = r.get(0); + let seq: String = r.get(1); + let ty: String = r.get(2); + let last: f64 = r.get(3); + let max: f64 = r.get(4); + let ratio = last / max; + AdvisorFinding { + check_id: "sequence_exhaustion".into(), + category: "schema".into(), + severity: if ratio > 0.85 { "error" } else { "warning" }.into(), + title: "Sequence running out".into(), + entity: format!("{schema}.{seq}"), + entity_kind: "sequence".into(), + description: "The sequence has used most of its range. When it reaches the maximum, every insert that needs a new id fails.".into(), + detail: Some(format!("{:.0}% of {ty} used · {last:.0} of {max:.0}", ratio * 100.0)), + remediation: Some(format!("ALTER SEQUENCE \"{schema}\".\"{seq}\" AS bigint;\n-- and widen the column it feeds to bigint (ALTER TABLE ... ALTER COLUMN ... TYPE bigint)")), + metric: ratio, + } + }).collect()) +} + +/// Tables the planner is guessing about: never analyzed, or changed a lot since. +async fn check_stale_stats(pool: &PgPool) -> Result, String> { + let sql = "SELECT s.schemaname, s.relname, s.n_live_tup::int8, s.n_mod_since_analyze::int8, + (s.last_analyze IS NULL AND s.last_autoanalyze IS NULL) + FROM pg_stat_user_tables s + WHERE s.n_live_tup > 1000 + AND ((s.last_analyze IS NULL AND s.last_autoanalyze IS NULL) + OR s.n_mod_since_analyze > GREATEST(10000, s.n_live_tup / 5)) + ORDER BY s.n_mod_since_analyze DESC"; + let rows = sqlx::query(sql).fetch_all(pool).await.map_err(|e| e.to_string())?; + Ok(rows.iter().map(|r| { + let schema: String = r.get(0); + let table: String = r.get(1); + let live: i64 = r.get(2); + let changed: i64 = r.get(3); + let never: bool = r.get(4); + AdvisorFinding { + check_id: "stale_stats".into(), + category: "performance".into(), + severity: if never { "warning" } else { "info" }.into(), + title: "Planner statistics out of date".into(), + entity: format!("{schema}.{table}"), + entity_kind: "table".into(), + description: "The planner chooses joins and indexes from these statistics. A table never analyzed, or changed a lot since, gets plans built on guesses.".into(), + detail: Some(if never { format!("never analyzed · ~{live} rows") } else { format!("{changed} rows changed since the last ANALYZE · ~{live} rows") }), + remediation: Some(format!("ANALYZE \"{schema}\".\"{table}\";")), + metric: changed as f64, + } + }).collect()) +} + async fn check_unindexed_fk(pool: &PgPool) -> Result, String> { let sql = format!( "SELECT n.nspname, c.relname, con.conname, pg_get_constraintdef(con.oid), @@ -193,6 +408,15 @@ async fn check_unused_index(pool: &PgPool) -> Result, String WHERE s.idx_scan = 0 AND NOT i.indisunique AND NOT i.indisprimary + -- Tiny indexes are noise: dropping them saves nothing. + AND pg_relation_size(s.indexrelid) >= 1048576 + -- The index a foreign key's checks use: dropping it would turn + -- every parent DELETE into a scan of this table. + AND NOT EXISTS ( + SELECT 1 FROM pg_constraint con + WHERE con.contype = 'f' AND con.conrelid = i.indrelid + AND (i.indkey::int2[])[1:cardinality(con.conkey::int2[])] @> con.conkey::int2[] + ) ORDER BY 4 DESC"; let rows = sqlx::query(sql).fetch_all(pool).await.map_err(|e| e.to_string())?; Ok(rows @@ -429,7 +653,13 @@ pub async fn scan(conn: &super::connection::ActiveConnection) -> Result bool { BACKUP_CANCELLED.load(Ordering::SeqCst) } /// Request cancellation of the running backup/restore. The loops stop at the @@ -102,7 +103,7 @@ pub struct ImportResult { } /// What `backup_inspect_file` learns from the first few KB of a dump. -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Default, Serialize)] #[serde(rename_all = "camelCase")] pub struct BackupFileInfo { pub size: u64, @@ -112,6 +113,74 @@ pub struct BackupFileInfo { pub stroke: bool, /// A cancelled Stroke export: restoring it would load a fraction of the data. pub incomplete: bool, + /// An end-to-end encrypted scheduled backup: opened with the sync key on restore. + #[serde(default)] + pub encrypted: bool, + /// The encoding the file looks like it's in, as a WHATWG label ("utf-8", + /// "utf-16le", "windows-1252"). The restore decodes with it unless the + /// user picks another. + #[serde(default)] + pub encoding: String, + /// The first [`PREVIEW_CHARS`] characters, decoded, for the import preview. + #[serde(default)] + pub preview: String, + /// Whether the preview stops before the end of the file. + #[serde(default)] + pub preview_truncated: bool, +} + +/// How much of a dump the restore screen shows before it runs. +pub const PREVIEW_CHARS: usize = 100_000; + +/// The encoding of a dump: a byte-order mark wins, then valid UTF-8, then +/// Windows-1252 (what most "not UTF-8" dumps from older tools are). +fn sniff_encoding(head: &[u8], complete: bool) -> &'static encoding_rs::Encoding { + if let Some((enc, _)) = encoding_rs::Encoding::for_bom(head) { + return enc; + } + match std::str::from_utf8(head) { + Ok(_) => encoding_rs::UTF_8, + // A multi-byte character cut by the end of the sample is still UTF-8. + Err(e) if !complete && e.error_len().is_none() => encoding_rs::UTF_8, + Err(_) => encoding_rs::WINDOWS_1252, + } +} + +/// Decode a whole dump. `label` is what the user picked; None or "auto" sniffs. +fn decode_script(bytes: Vec, label: Option<&str>, rep: &Reporter) -> Result { + let enc = match label.filter(|l| !l.is_empty() && *l != "auto") { + Some(l) => encoding_rs::Encoding::for_label(l.as_bytes()) + .ok_or_else(|| format!("Unknown encoding: {l}"))?, + None => sniff_encoding(&bytes, true), + }; + if enc == encoding_rs::UTF_8 { + // The fast path: no copy when the file is valid UTF-8. + let bytes = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF][..]).map(<[u8]>::to_vec).unwrap_or(bytes); + return match String::from_utf8(bytes) { + Ok(s) => Ok(s), + Err(e) => { + rep.log("warn", "The file is not valid UTF-8; invalid bytes were replaced. Pick another encoding if the text looks wrong."); + Ok(String::from_utf8_lossy(e.as_bytes()).into_owned()) + } + }; + } + rep.log("info", &format!("Reading the file as {}", enc.name())); + let (text, _, bad) = enc.decode(&bytes); + if bad { + rep.log("warn", &format!("Some bytes are not valid {}; they were replaced", enc.name())); + } + Ok(text.into_owned()) +} + +/// The preview text of a dump's first bytes, and whether there is more. +fn preview_of(head: &[u8], complete: bool, enc: &'static encoding_rs::Encoding) -> (String, bool) { + let (text, _, _) = enc.decode(head); + let text = text.trim_start_matches('\u{feff}'); + let cut = truncate_chars(text, PREVIEW_CHARS); + let more = !complete || cut.len() < text.len(); + // A sample cut mid-character ends in a replacement char: drop it. + let cut = if !complete { cut.trim_end_matches('\u{fffd}') } else { cut }; + (cut.to_string(), more) } fn default_true() -> bool { true } @@ -212,7 +281,7 @@ impl Reporter { fn restore(app: &AppHandle) -> Self { Self { app: Some(app.clone()), log_event: "restore-log", progress_event: "restore-progress" } } - #[cfg(test)] + /// No events: scheduled backups run in the background, away from the page's log. pub(crate) fn silent() -> Self { Self { app: None, log_event: "", progress_event: "" } } @@ -446,7 +515,7 @@ fn partial_path(path: &Path) -> PathBuf { #[tauri::command] pub async fn backup_export( app: AppHandle, - state: State<'_, DbState>, + state: Db<'_>, schema: Option, tables: Option>, options: Option, @@ -459,7 +528,7 @@ pub async fn backup_export( run_export(&rep, conn, schema, tables, &opts, path).await } -async fn run_export( +pub(crate) async fn run_export( rep: &Reporter, conn: ActiveConnection, schema: Option, @@ -566,14 +635,15 @@ async fn export_into( ActiveConnection::Sqlite(pool) => export_sqlite(rep, &pool, tables.as_deref(), opts, out).await, ActiveConnection::Postgres(pool) => export_postgres(rep, &pool, schema.as_deref(), tables.as_deref(), opts, out).await, ActiveConnection::Mysql(pool) => export_mysql(rep, &pool, schema.as_deref(), tables.as_deref(), opts, out).await, - ActiveConnection::D1(cfg) => export_d1(rep, &cfg, tables.as_deref(), opts, out).await, - ActiveConnection::LibSql(_) => Err("Backup export is not supported for LibSQL/Turso connections".to_string()), + ActiveConnection::D1(cfg) => export_d1(rep, Remote::D1(&cfg), tables.as_deref(), opts, out).await, + ActiveConnection::LibSql(cfg) => export_d1(rep, Remote::LibSql(&cfg), tables.as_deref(), opts, out).await, ActiveConnection::Clickhouse(_) => Err("Backup export is not supported for ClickHouse connections".to_string()), ActiveConnection::Posthog(_) => Err("Backup is not supported for PostHog".to_string()), ActiveConnection::Redis(_) => Err("Backup is not supported on Redis".to_string()), ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Err("Backup is not supported on DynamoDB".to_string()), ActiveConnection::Duckdb(h) => export_duckdb(rep, &h, out).await, ActiveConnection::Mssql(h) => export_mssql(rep, &h, out).await, + ActiveConnection::Oracle(_) => Err("Backup export is not supported for Oracle yet".to_string()), } } @@ -582,55 +652,98 @@ async fn export_into( #[tauri::command] pub async fn backup_import( app: AppHandle, - state: State<'_, DbState>, + state: Db<'_>, sql: Option, path: Option, + encoding: Option, ) -> Result { reset_cancel(); let rep = Reporter::restore(&app); let script = match (sql, path) { (Some(s), _) => s, - (None, Some(p)) => read_script(&rep, &p).await?, + (None, Some(p)) => read_script(&app, &rep, &p, encoding.as_deref()).await?, (None, None) => return Err("Nothing to restore: no file or SQL given".into()), }; match require_conn(&state)? { ActiveConnection::Sqlite(pool) => import_sqlite(&rep, &pool, &script).await, ActiveConnection::Postgres(pool) => import_postgres(&rep, &pool, &script).await, ActiveConnection::Mysql(pool) => import_mysql(&rep, &pool, &script).await, - ActiveConnection::D1(cfg) => import_d1(&rep, &cfg, &script).await, - ActiveConnection::LibSql(_) => Err("Backup import is not supported for LibSQL/Turso connections".to_string()), + ActiveConnection::D1(cfg) => import_d1(&rep, Remote::D1(&cfg), &script).await, + ActiveConnection::LibSql(cfg) => import_d1(&rep, Remote::LibSql(&cfg), &script).await, ActiveConnection::Clickhouse(_) => Err("Backup import is not supported for ClickHouse connections".to_string()), ActiveConnection::Posthog(_) => Err("Backup is not supported for PostHog".to_string()), ActiveConnection::Redis(_) => Err("Backup is not supported on Redis".to_string()), ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Err("Backup is not supported on DynamoDB".to_string()), ActiveConnection::Duckdb(h) => import_duckdb(&rep, &h, &script).await, ActiveConnection::Mssql(h) => import_mssql(&rep, &h, &script).await, + ActiveConnection::Oracle(_) => Err("Backup import is not supported for Oracle yet".to_string()), } } -async fn read_script(rep: &Reporter, path: &str) -> Result { - let bytes = tokio::fs::read(path).await.map_err(|e| format!("Could not read {path}: {e}"))?; - match String::from_utf8(bytes) { - Ok(s) => Ok(s), - Err(e) => { - rep.log("warn", "The file is not valid UTF-8; invalid bytes were replaced"); - Ok(String::from_utf8_lossy(e.as_bytes()).into_owned()) - } +async fn read_script(app: &AppHandle, rep: &Reporter, path: &str, encoding: Option<&str>) -> Result { + let mut bytes = tokio::fs::read(path).await.map_err(|e| format!("Could not read {path}: {e}"))?; + // A scheduled backup: encrypted with the account's sync key, then gzip. + if crate::backup_crypto::is_encrypted(&bytes) { + let (key, _) = crate::sync::load_local(app) + .await + .ok_or("This backup is end-to-end encrypted. Unlock sync (Settings → Sync) with your sync passphrase to restore it.")?; + rep.log("info", "Decrypting the backup on this computer"); + let mut plain = Vec::with_capacity(bytes.len()); + crate::backup_crypto::decrypt_stream(&key, bytes.as_slice(), &mut plain, None)?; + bytes = plain; + } + if bytes.starts_with(&[0x1f, 0x8b]) { + let mut out = Vec::with_capacity(bytes.len() * 4); + flate2::read::GzDecoder::new(bytes.as_slice()) + .read_to_end(&mut out) + .map_err(|e| format!("Could not unzip {path}: {e}"))?; + bytes = out; } + decode_script(bytes, encoding, rep) } /// Size and origin of a dump file, read from its first few KB, so the restore /// screen can name the engine and warn about a cancelled (incomplete) export. #[tauri::command] -pub async fn backup_inspect_file(path: String) -> Result { - let meta = tokio::fs::metadata(&path).await.map_err(|e| format!("Could not open {path}: {e}"))?; - let mut head = vec![0u8; 4096]; - let n = { - let mut f = File::open(&path).map_err(|e| format!("Could not open {path}: {e}"))?; - f.read(&mut head).map_err(|e| format!("Could not read {path}: {e}"))? +pub async fn backup_inspect_file(path: String, encoding: Option) -> Result { + tokio::task::spawn_blocking(move || inspect_file(&path, encoding.as_deref())) + .await + .map_err(|e| e.to_string())? +} + +fn inspect_file(path: &str, encoding: Option<&str>) -> Result { + let size = std::fs::metadata(path).map_err(|e| format!("Could not open {path}: {e}"))?.len(); + let open = || File::open(path).map_err(|e| format!("Could not open {path}: {e}")); + // Up to four bytes a character, so this always covers PREVIEW_CHARS. + let limit = (PREVIEW_CHARS * 4) as u64; + let mut head = Vec::new(); + open()?.take(limit).read_to_end(&mut head).map_err(|e| format!("Could not read {path}: {e}"))?; + if crate::backup_crypto::is_encrypted(&head) { + return Ok(BackupFileInfo { size, stroke: true, encrypted: true, ..Default::default() }); + } + let mut complete = head.len() as u64 == size; + if head.starts_with(&[0x1f, 0x8b]) { + // Gzip: look at the start of what it holds. + let mut inner = Vec::new(); + let mut dec = flate2::read::GzDecoder::new(open()?).take(limit + 1); + let _ = dec.read_to_end(&mut inner); + complete = inner.len() as u64 <= limit; + inner.truncate(limit as usize); + head = inner; + } + let picked = match encoding.filter(|l| !l.is_empty() && *l != "auto") { + Some(l) => Some(encoding_rs::Encoding::for_label(l.as_bytes()).ok_or_else(|| format!("Unknown encoding: {l}"))?), + None => None, }; - head.truncate(n); - Ok(inspect_head(meta.len(), &String::from_utf8_lossy(&head))) + Ok(inspect_bytes(size, &head, complete, picked)) +} + +/// `encoding` is the detected one; the preview uses `picked` when given. +fn inspect_bytes(size: u64, head: &[u8], complete: bool, picked: Option<&'static encoding_rs::Encoding>) -> BackupFileInfo { + let enc = sniff_encoding(head, complete); + let (preview, preview_truncated) = preview_of(head, complete, picked.unwrap_or(enc)); + let info = inspect_head(size, &preview); + BackupFileInfo { encoding: enc.name().to_ascii_lowercase(), preview, preview_truncated, ..info } } fn inspect_head(size: u64, head: &str) -> BackupFileInfo { @@ -638,6 +751,7 @@ fn inspect_head(size: u64, head: &str) -> BackupFileInfo { let stroke = head.contains("-- Stroke Backup"); let engine = head .lines() + .take(40) .find_map(|l| l.strip_prefix("-- Engine").map(|r| r.trim_start_matches([' ', ':']).trim().to_string())) .filter(|s| !s.is_empty()) .or_else(|| { @@ -646,7 +760,7 @@ fn inspect_head(size: u64, head: &str) -> BackupFileInfo { else if head.starts_with("PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;") { Some("SQLite".into()) } else { None } }); - BackupFileInfo { size, engine, stroke, incomplete } + BackupFileInfo { size, engine, stroke, incomplete, ..Default::default() } } // ── Shared helpers ──────────────────────────────────────────────────────────── @@ -1183,7 +1297,7 @@ async fn import_sqlite(rep: &Reporter, pool: &sqlx::SqlitePool, sql: &str) -> Re let mut run = Runner::new(rep, sql.len()); let mut sp = Splitter::new(sql, Escapes::Standard); while let Some(stmt) = sp.next() { - let Stmt::Sql(s) = stmt else { run.total += 1; run.errors.push("COPY blocks are Postgres-only".into()); continue }; + let Stmt::Sql(s) = stmt else { run.total += 1; run.fail("COPY blocks are Postgres-only".into()); continue }; run.total += 1; let low = head_lower(&s, 24); if low.starts_with("pragma foreign_keys") || is_tx_control(&s) { run.ok += 1; continue; } @@ -1224,6 +1338,22 @@ struct Runner<'a> { batch_bytes: usize, } +/// Failures streamed to the live log before it only counts them, so a script +/// where every statement fails can't flood the page. +const LOGGED_FAILURES: usize = 200; + +impl Runner<'_> { + /// Record a failed statement and stream it to the restore log. + fn fail(&mut self, message: String) { + if self.errors.len() < LOGGED_FAILURES { + self.meter.rep.log("error", message.clone()); + } else if self.errors.len() == LOGGED_FAILURES { + self.meter.rep.log("warn", "More statements failed; the rest are counted, not listed"); + } + self.errors.push(message); + } +} + impl<'a> Runner<'a> { fn new(rep: &'a Reporter, len: usize) -> Self { Self { meter: Meter::new(rep, len), total: 0, ok: 0, errors: Vec::new(), batch: Vec::new(), batch_bytes: 0 } @@ -1258,7 +1388,7 @@ macro_rules! runner_exec { Ok(_) => self.ok += 1, Err(e) => { (&mut *conn).execute("ROLLBACK TO SAVEPOINT stroke_sp; RELEASE SAVEPOINT stroke_sp").await.ok(); - self.errors.push(stmt_error(e, stmt)); + self.fail(stmt_error(e, stmt)); } } } @@ -1937,7 +2067,7 @@ async fn import_postgres(rep: &Reporter, pool: &sqlx::PgPool, sql: &str) -> Resu run.flush_pg(&mut *conn).await; match pg_copy_in(&mut conn, ©_sql, data).await { Ok(()) => run.ok += 1, - Err(e) => run.errors.push(stmt_error(e, ©_sql)), + Err(e) => run.fail(stmt_error(e, ©_sql)), } } } @@ -2346,7 +2476,7 @@ async fn import_mysql(rep: &Reporter, pool: &sqlx::MySqlPool, sql: &str) -> Resu // it; a failing statement is atomic on its own. match (&mut *conn).execute(s.as_str()).await { Ok(_) => run.ok += 1, - Err(e) => run.errors.push(stmt_error(e, &s)), + Err(e) => run.fail(stmt_error(e, &s)), } } run.meter.tick(sp.position(), run.ok, run.errors.len(), false); @@ -2372,21 +2502,64 @@ fn is_d1_internal_table(name: &str) -> bool { name.starts_with("_cf_") || name.starts_with("sqlite_") } +/// SQLite served over HTTP: Cloudflare D1 and libSQL/Turso. Both take one +/// statement per request, keep no client transaction between requests and +/// always enforce foreign keys, so they share the export and the restore. +#[derive(Clone, Copy)] +enum Remote<'a> { + D1(&'a super::connection::D1Config), + LibSql(&'a super::connection::LibSqlConfig), +} + +impl Remote<'_> { + async fn query(&self, sql: &str) -> Result { + match self { + Remote::D1(c) => super::d1::query(c, sql, vec![]).await, + Remote::LibSql(c) => super::libsql::query(c, sql, vec![]).await, + } + } + fn label(&self) -> &'static str { + match self { + Remote::D1(_) => "Cloudflare D1 (SQLite)", + Remote::LibSql(_) => "libSQL / Turso (SQLite)", + } + } +} + +/// Tables in an order that loads parents before the rows that point at them: +/// a table follows every table its foreign keys reference. Cycles (and +/// self-references) keep their given order; the restore's retry pass settles +/// those. `deps[i]` lists the tables `tables[i]` references. +fn fk_order(tables: &[String], deps: &[Vec]) -> Vec { + let mut placed: Vec = Vec::with_capacity(tables.len()); + let mut left: Vec = (0..tables.len()).collect(); + while !left.is_empty() { + let ready: Vec = left + .iter() + .copied() + .filter(|&i| deps[i].iter().all(|d| d == &tables[i] || placed.contains(d) || !tables.contains(d))) + .collect(); + // A cycle: take the next one as it came, so the order stays stable. + let take = if ready.is_empty() { vec![left[0]] } else { ready }; + for i in &take { placed.push(tables[*i].clone()); } + left.retain(|i| !take.contains(i)); + } + placed +} + async fn export_d1( rep: &Reporter, - cfg: &super::connection::D1Config, + cfg: Remote<'_>, filter: Option<&[String]>, opts: &ExportOptions, out: &mut Out, ) -> Result { - rep.log("info", "Starting Cloudflare D1 export…"); - out.push_str(&backup_header("Cloudflare D1 (SQLite)", None)); + rep.log("info", format!("Starting {} export…", cfg.label())); + out.push_str(&backup_header(cfg.label(), None)); out.push_str("PRAGMA foreign_keys=OFF;\nBEGIN TRANSACTION;\n\n"); - let ddl_result = super::d1::query( - cfg, + let ddl_result = cfg.query( "SELECT type, name, tbl_name, sql FROM sqlite_master WHERE sql IS NOT NULL AND name NOT LIKE 'sqlite_%' ORDER BY CASE type WHEN 'table' THEN 0 WHEN 'index' THEN 1 WHEN 'view' THEN 2 ELSE 3 END, name", - vec![], ).await?; let type_idx = ddl_result.columns.iter().position(|c| c.name == "type").unwrap_or(0); @@ -2418,6 +2591,21 @@ async fn export_d1( } } + // Rows go out parents first: these engines enforce foreign keys on every + // statement, so a dump in name order (movie_cast before movies) could not + // be loaded back. + if opts.include_data && tables.len() > 1 { + let mut deps = Vec::with_capacity(tables.len()); + for t in &tables { + let sql = format!("SELECT DISTINCT \"table\" FROM pragma_foreign_key_list({})", ql(t)); + let refs = cfg.query(&sql).await.map(|r| { + r.rows.iter().filter_map(|row| row.first().and_then(|v| v.as_str()).map(str::to_string)).collect() + }).unwrap_or_default(); + deps.push(refs); + } + tables = fk_order(&tables, &deps); + } + rep.plan(tables.iter().map(|t| PlanEntry { schema: None, name: t.clone(), est_rows: None }).collect()); let mut stats = Stats { tables: tables.len(), ..Stats::default() }; @@ -2425,7 +2613,7 @@ async fn export_d1( if is_cancelled() { break; } if !opts.include_data { rep.table(None, table, "done", 0, None); continue; } rep.table(None, table, "running", 0, None); - let data_result = match super::d1::query(cfg, &format!("SELECT * FROM {}", qi(table)), vec![]).await { + let data_result = match cfg.query(&format!("SELECT * FROM {}", qi(table))).await { Ok(r) => r, Err(e) => { // A single unreadable/prohibited table shouldn't abort the whole backup. @@ -2489,23 +2677,53 @@ fn json_to_sql_val(v: &serde_json::Value) -> String { // ── D1 import ──────────────────────────────────────────────────────────────── -async fn import_d1(rep: &Reporter, cfg: &super::connection::D1Config, sql: &str) -> Result { +/// A foreign key failed: the parent row may simply not be loaded yet. +fn is_fk_error(e: &str) -> bool { + let l = e.to_ascii_lowercase(); + l.contains("foreign key constraint") || l.contains("constraint_foreignkey") +} + +async fn import_d1(rep: &Reporter, cfg: Remote<'_>, sql: &str) -> Result { let started = Instant::now(); rep.log("info", "Starting restore…"); let mut run = Runner::new(rep, sql.len()); let mut sp = Splitter::new(sql, Escapes::Standard); + // Rows a foreign key turned away, tried again once the rest are in. + let mut waiting: Vec<(String, String)> = Vec::new(); while let Some(stmt) = sp.next() { let Stmt::Sql(s) = stmt else { continue }; run.total += 1; - // D1 has no client transactions and manages FK enforcement itself. + // No client transactions over HTTP, and no turning foreign keys off: + // D1 and libSQL enforce them on every statement. if head_lower(&s, 24).starts_with("pragma foreign_keys") || is_tx_control(&s) { run.ok += 1; continue; } - match super::d1::query(cfg, &s, vec![]).await { + match cfg.query(&s).await { Ok(_) => run.ok += 1, - Err(e) => run.errors.push(stmt_error(e, &s)), + Err(e) if is_fk_error(&e) => waiting.push((s, e)), + Err(e) => run.fail(stmt_error(e, &s)), } run.meter.tick(sp.position(), run.ok, run.errors.len(), false); if is_cancelled() { run.total += sp.count(); break; } } + // A dump in name order (or one with references in a cycle) loads children + // before parents. Retry what a foreign key refused while a pass still lands + // something; only rows whose parent is really missing end as errors. + let mut pass = 0; + while !waiting.is_empty() && !is_cancelled() { + pass += 1; + rep.log("info", format!("Retrying {} statement(s) that waited on a parent row (pass {pass})…", waiting.len())); + let before = waiting.len(); + let mut still = Vec::new(); + for (s, _) in waiting { + match cfg.query(&s).await { + Ok(_) => run.ok += 1, + Err(e) if is_fk_error(&e) => still.push((s, e)), + Err(e) => run.fail(stmt_error(e, &s)), + } + } + waiting = still; + if waiting.len() == before { break; } + } + for (s, e) in waiting { run.fail(stmt_error(e, &s)); } run.meter.tick(sql.len(), run.ok, run.errors.len(), true); Ok(finish_import(rep, run.total, run.ok, run.errors, started)) } @@ -2614,7 +2832,7 @@ async fn import_duckdb( if head_lower(&s, 24).starts_with("pragma foreign_keys") { run.ok += 1; continue; } match super::duckdb::execute_sql(handle, &s).await { Ok(_) => run.ok += 1, - Err(e) => run.errors.push(stmt_error(e, &s)), + Err(e) => run.fail(stmt_error(e, &s)), } run.meter.tick(sp.position(), run.ok, run.errors.len(), false); if is_cancelled() { run.total += sp.count(); break; } @@ -2698,7 +2916,7 @@ async fn import_mssql( run.total += 1; match super::mssql::execute_sql(handle, &s).await { Ok(_) => run.ok += 1, - Err(e) => run.errors.push(stmt_error(e, &s)), + Err(e) => run.fail(stmt_error(e, &s)), } run.meter.tick(sp.position(), run.ok, run.errors.len(), false); if is_cancelled() { run.total += sp.count(); break; } @@ -2780,6 +2998,46 @@ mod tests { assert_eq!(s, vec!["SELECT 1".to_string(), "SELECT 2".to_string()]); } + #[test] + fn dump_encodings_are_sniffed_and_decoded() { + let rep = Reporter::silent(); + // Valid UTF-8, with and without a BOM. + assert_eq!(sniff_encoding("SELECT 'é';".as_bytes(), true), encoding_rs::UTF_8); + let bom = [&[0xEF, 0xBB, 0xBF][..], b"SELECT 1;"].concat(); + assert_eq!(decode_script(bom, None, &rep).unwrap(), "SELECT 1;"); + // Latin-1 bytes are not UTF-8: read as Windows-1252. + let latin = b"INSERT INTO t VALUES ('caf\xe9');".to_vec(); + assert_eq!(sniff_encoding(&latin, true), encoding_rs::WINDOWS_1252); + assert_eq!(decode_script(latin.clone(), None, &rep).unwrap(), "INSERT INTO t VALUES ('café');"); + // A picked encoding wins over the sniff. + assert!(decode_script(latin, Some("utf-8"), &rep).unwrap().contains('\u{fffd}')); + // UTF-16 with a BOM. + let mut u16 = vec![0xFF, 0xFE]; + for c in "SELECT 2;".encode_utf16() { u16.extend_from_slice(&c.to_le_bytes()); } + assert_eq!(decode_script(u16, None, &rep).unwrap(), "SELECT 2;"); + // A sample cut inside a UTF-8 character is still UTF-8, and the cut is dropped. + let cut = &"ab é".as_bytes()[..4]; + assert_eq!(sniff_encoding(cut, false), encoding_rs::UTF_8); + assert_eq!(preview_of(cut, false, encoding_rs::UTF_8), ("ab ".to_string(), true)); + assert!(decode_script(b"x".to_vec(), Some("klingon"), &rep).is_err()); + } + + #[test] + fn inspect_previews_the_start_of_a_dump() { + let i = inspect_bytes(9, b"-- MySQL dump\nSELECT 1;", true, None); + assert_eq!(i.engine.as_deref(), Some("MySQL")); + assert_eq!(i.encoding, "utf-8"); + assert_eq!(i.preview, "-- MySQL dump\nSELECT 1;"); + assert!(!i.preview_truncated); + let long = "x".repeat(PREVIEW_CHARS + 5); + let i = inspect_bytes(long.len() as u64, long.as_bytes(), true, None); + assert_eq!(i.preview.len(), PREVIEW_CHARS); + assert!(i.preview_truncated); + // A picked encoding changes the preview, not what was detected. + let i = inspect_bytes(4, b"caf\xe9", true, Some(encoding_rs::UTF_8)); + assert_eq!((i.encoding.as_str(), i.preview.as_str()), ("windows-1252", "caf\u{fffd}")); + } + #[test] fn truncate_chars_is_utf8_safe() { // Would panic with byte slicing when the boundary lands mid-character. @@ -2952,6 +3210,24 @@ mod tests { p } + #[test] + fn fk_order_loads_parents_first() { + let t = |v: &[&str]| v.iter().map(|s| s.to_string()).collect::>(); + let tables = t(&["genres", "movie_cast", "movie_genres", "movies", "people"]); + let deps = vec![t(&[]), t(&["movies", "people"]), t(&["movies", "genres"]), t(&["people"]), t(&[])]; + let order = fk_order(&tables, &deps); + let pos = |n: &str| order.iter().position(|x| x == n).unwrap(); + assert!(pos("people") < pos("movies")); + assert!(pos("movies") < pos("movie_cast")); + assert!(pos("genres") < pos("movie_genres")); + assert_eq!(order.len(), 5); + // A cycle and a self-reference still place every table once. + let cyc = fk_order(&t(&["a", "b", "c"]), &[t(&["b"]), t(&["a"]), t(&["c"])]); + assert_eq!(cyc.len(), 3); + assert!(is_fk_error("FOREIGN KEY constraint failed: SQLITE_CONSTRAINT (extended: SQLITE_CONSTRAINT_FOREIGNKEY)")); + assert!(!is_fk_error("UNIQUE constraint failed")); + } + #[tokio::test] async fn sqlite_round_trip_keeps_rows_foreign_keys_and_triggers() { let src_path = scratch("src.db"); diff --git a/src-tauri/src/db/clickhouse.rs b/src-tauri/src/db/clickhouse.rs index 3efb27e8..537581ab 100644 --- a/src-tauri/src/db/clickhouse.rs +++ b/src-tauri/src/db/clickhouse.rs @@ -228,7 +228,7 @@ pub async fn list_tables(config: &ClickhouseConfig, schema: &str) -> Result>; +// ── Oracle ────────────────────────────────────────────────────────────────────── + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct OracleConfig { + pub name: String, + #[serde(default)] + pub host: String, + #[serde(default = "default_oracle_port")] + pub port: u16, + /// Service name (Easy Connect `//host:port/service`). Used unless `sid` is set. + #[serde(default)] + pub service_name: String, + /// Legacy SID; connects through a full descriptor when set. + #[serde(default)] + pub sid: String, + /// Easy Connect string, TNS descriptor or tnsnames.ora alias. Overrides + /// host, port, service name and SID when set. + #[serde(default)] + pub connect_string: String, + pub user: String, + #[serde(default)] + pub password: String, + /// Folder holding Oracle Instant Client, when it isn't somewhere the app + /// finds by itself. + #[serde(default)] + pub client_lib_dir: String, +} + +fn default_oracle_port() -> u16 { + 1521 +} + +/// One synchronous Oracle session; see `db::oracle`. +pub type OracleHandle = Arc; + // ── Any-connection config for cross-connection queries ──────────────────────── /// Accepts the full saved-connection JSON from the frontend and routes to the @@ -305,10 +341,14 @@ pub enum AnyConnectionConfig { Dynamodb(crate::db::dynamodb::DynamoConfig), #[serde(rename = "couchdb")] Couchdb(crate::db::couchdb::CouchConfig), + #[serde(rename = "elasticsearch", alias = "opensearch")] + Elasticsearch(crate::db::elastic::EsConfig), #[serde(rename = "duckdb")] Duckdb(DuckdbConfig), #[serde(rename = "mssql")] Mssql(MssqlConfig), + #[serde(rename = "oracle")] + Oracle(OracleConfig), #[serde(rename = "posthog")] Posthog(PosthogConfig), /// Only the terminal (mongosh) uses this path; queries go through db::mongo. @@ -332,6 +372,7 @@ pub enum ActiveConnection { Doc(crate::db::docdb::DocConn), Duckdb(DuckdbHandle), Mssql(MssqlHandle), + Oracle(OracleHandle), Posthog(PosthogConfig), } @@ -349,6 +390,7 @@ impl ActiveConnection { Self::Doc(d) => d.driver(), Self::Duckdb(_) => "duckdb", Self::Mssql(_) => "mssql", + Self::Oracle(_) => "oracle", Self::Posthog(_) => "posthog", } } @@ -358,6 +400,13 @@ impl ActiveConnection { pub struct DbState { pub conn: Arc>>, + /// Labs, several connections at once: pools set aside by a switch, by the + /// connection key the page chose, oldest first. Switching back resumes one + /// without connecting again. Capped (see `parked`). + pub parked: Arc>>, + /// The page's key for the open (front) pool, when it has said. A call routed + /// to another key is served from `parked`; see `require_conn`. + pub active_key: Arc>>, /// Cancel handles for the queries currently in flight, keyed by the id the /// caller sent. Keyed rather than a single slot because the UI runs several /// queries at once (one per editor tab) - with one slot, the second run @@ -370,6 +419,8 @@ impl Default for DbState { fn default() -> Self { Self { conn: Arc::new(Mutex::new(None)), + parked: Arc::new(Mutex::new(Vec::new())), + active_key: Arc::new(Mutex::new(None)), cancels: Arc::new(Mutex::new(std::collections::HashMap::new())), } } @@ -377,21 +428,80 @@ impl Default for DbState { /// Register a cancel handle for `id`, replacing (and dropping) any handle already /// stored under it. -pub fn register_cancel(state: &State<'_, DbState>, id: &str, tx: oneshot::Sender<()>) { +pub fn register_cancel(state: &Db<'_>, id: &str, tx: oneshot::Sender<()>) { if let Ok(mut map) = state.cancels.lock() { map.insert(id.to_string(), tx); } } /// Drop the cancel handle for `id` (query finished, cancelling it is meaningless). -pub fn unregister_cancel(state: &State<'_, DbState>, id: &str) { +pub fn unregister_cancel(state: &Db<'_>, id: &str) { if let Ok(mut map) = state.cancels.lock() { map.remove(id); } } -/// Returns a clone of the active connection, or an error if disconnected. -pub fn require_conn(state: &State<'_, DbState>) -> Result { +/// `State` plus the database the call is for. +/// +/// Labs, several connections at once: tabs from several databases are open +/// together, and each call the page makes names its database in a `__conn` +/// argument (src/lib/api.js). The key is read from the request itself, so a +/// call sent just before a switch still reaches the database it was made for, +/// whichever pool is in front by the time it runs. Without one, the call goes +/// to the front pool, as before. +#[derive(Clone)] +pub struct Db<'r> { + state: State<'r, DbState>, + /// The `__conn` key the page sent, if any. + pub route: Option, +} + +impl<'r> Db<'r> { + /// For code that has the managed state but no request (MCP, background work): + /// it always means the front pool. + pub fn front(state: State<'r, DbState>) -> Self { + Self { state, route: None } + } + pub fn inner(&self) -> &'r DbState { + self.state.inner() + } +} + +impl std::ops::Deref for Db<'_> { + type Target = DbState; + fn deref(&self) -> &DbState { + self.state.inner() + } +} + +impl<'r, 'de: 'r, R: tauri::Runtime> tauri::ipc::CommandArg<'de, R> for Db<'r> { + fn from_command(command: tauri::ipc::CommandItem<'de, R>) -> Result { + let route = match command.message.payload() { + tauri::ipc::InvokeBody::Json(v) => v.get("__conn").and_then(|k| k.as_str()).filter(|k| !k.is_empty()).map(str::to_string), + _ => None, + }; + let state = as tauri::ipc::CommandArg<'de, R>>::from_command(command)?; + Ok(Self { state, route }) + } +} + +/// Returns a clone of the connection the call is for, or an error if there is +/// none. A routed call (see `Db`) gets its own database's pool: the front one +/// when that is its key, else the one kept aside for it. A key whose pool was +/// closed (pushed out by the cap, or its connection edited) is an error rather +/// than a quiet run on whatever database happens to be in front. +pub fn require_conn(state: &Db<'_>) -> Result { + if let Some(key) = state.route.as_deref() { + let front = state.active_key.lock().map_err(|e| e.to_string())?.clone(); + if front.as_deref() != Some(key) { + if let Some((_, c)) = state.parked.lock().map_err(|e| e.to_string())?.iter().find(|(k, _)| k == key) { + return Ok(c.clone()); + } + if front.is_some() { + return Err("That database's connection was closed. Click its tab to open it again.".to_string()); + } + } + } state .conn .lock() @@ -401,7 +511,7 @@ pub fn require_conn(state: &State<'_, DbState>) -> Result) -> Result { +pub fn require_pool(state: &Db<'_>) -> Result { match require_conn(state)? { ActiveConnection::Postgres(pool) => Ok(pool), other => Err(format!( @@ -433,21 +543,33 @@ pub fn update_d1_token(token: &str) { } } -fn set_conn(state: &State<'_, DbState>, conn: Option) -> Result<(), String> { +fn set_conn(state: &Db<'_>, conn: Option) -> Result<(), String> { *state.conn.lock().map_err(|e| e.to_string())? = conn; + // A fresh pool: the page names its key once it knows it (set_active_conn_key). + *state.active_key.lock().map_err(|e| e.to_string())? = None; Ok(()) } -async fn close_existing(state: &State<'_, DbState>) { +async fn close_existing(state: &Db<'_>) { let old = state.conn.lock().ok().and_then(|mut g| g.take()); let Some(conn) = old else { return }; + close_in_background(conn); +} + +/// Drain a pool off the request path (see close_existing). +pub fn close_in_background(conn: ActiveConnection) { // Drain the old pool in the background. Pool::close() waits for every // connection to be handed back - and against a dead peer (sleep/wake, // network change: exactly the state a reconnect follows) that stalls for // seconds. Nothing downstream needs the drain to finish - the state slot // is already empty - so never make connect/switch wait on it. The timeout // caps the drain; after that the OS cleans up the sockets. - tokio::spawn(async move { + // + // tauri's runtime, not tokio::spawn: this is also called from sync + // commands (park_connection, resume_connection, close_parked), which run + // outside any Tokio context. tokio::spawn panics there, and with + // panic = "abort" that closed the whole app on a Labs switch or disconnect. + tauri::async_runtime::spawn(async move { let timeout = std::time::Duration::from_secs(3); match conn { ActiveConnection::Postgres(p) => { @@ -1227,7 +1349,7 @@ pub async fn test_connection(config: PgConfig) -> Result<(), String> { } pub async fn connect( - state: State<'_, DbState>, + state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: PgConfig, ) -> Result<(), String> { @@ -1305,7 +1427,7 @@ pub async fn test_sqlite_connection(config: SqliteConfig) -> Result<(), String> Ok(()) } -pub async fn connect_sqlite(state: State<'_, DbState>, config: SqliteConfig) -> Result<(), String> { +pub async fn connect_sqlite(state: Db<'_>, config: SqliteConfig) -> Result<(), String> { let pool = open_sqlite(&config).await?; close_existing(&state).await; set_conn(&state, Some(ActiveConnection::Sqlite(pool))) @@ -1406,7 +1528,7 @@ pub async fn test_mysql_connection(config: MysqlConfig) -> Result<(), String> { } pub async fn connect_mysql( - state: State<'_, DbState>, + state: Db<'_>, tunnel_state: State<'_, TunnelState>, config: MysqlConfig, ) -> Result<(), String> { @@ -1440,7 +1562,7 @@ pub async fn test_d1_connection(config: D1Config) -> Result<(), String> { Ok(()) } -pub async fn connect_d1(state: State<'_, DbState>, config: D1Config) -> Result<(), String> { +pub async fn connect_d1(state: Db<'_>, config: D1Config) -> Result<(), String> { // Validate credentials before storing test_d1_connection(config.clone()).await?; close_existing(&state).await; @@ -1454,7 +1576,7 @@ pub async fn test_libsql_connection(config: LibSqlConfig) -> Result<(), String> Ok(()) } -pub async fn connect_libsql(state: State<'_, DbState>, config: LibSqlConfig) -> Result<(), String> { +pub async fn connect_libsql(state: Db<'_>, config: LibSqlConfig) -> Result<(), String> { test_libsql_connection(config.clone()).await?; close_existing(&state).await; set_conn(&state, Some(ActiveConnection::LibSql(config))) @@ -1471,7 +1593,7 @@ pub async fn test_clickhouse_connection(config: ClickhouseConfig) -> Result<(), .await } -pub async fn connect_clickhouse(state: State<'_, DbState>, config: ClickhouseConfig) -> Result<(), String> { +pub async fn connect_clickhouse(state: Db<'_>, config: ClickhouseConfig) -> Result<(), String> { // Validate credentials/reachability before storing. test_clickhouse_connection(config.clone()).await?; close_existing(&state).await; @@ -1484,7 +1606,7 @@ pub async fn test_posthog_connection(config: PosthogConfig) -> Result<(), String crate::db::posthog::query(&config, "SELECT 1").await.map(|_| ()) } -pub async fn connect_posthog(state: State<'_, DbState>, config: PosthogConfig) -> Result<(), String> { +pub async fn connect_posthog(state: Db<'_>, config: PosthogConfig) -> Result<(), String> { test_posthog_connection(config.clone()).await?; close_existing(&state).await; set_conn(&state, Some(ActiveConnection::Posthog(config))) @@ -1492,33 +1614,38 @@ pub async fn connect_posthog(state: State<'_, DbState>, config: PosthogConfig) - // ── DynamoDB connect / test ─────────────────────────────────────────────────── -pub async fn connect_dynamodb(state: State<'_, DbState>, config: crate::db::dynamodb::DynamoConfig) -> Result<(), String> { +pub async fn connect_dynamodb(state: Db<'_>, config: crate::db::dynamodb::DynamoConfig) -> Result<(), String> { crate::db::dynamodb::ping(&config).await?; set_conn(&state, Some(ActiveConnection::Dynamodb(config))) } // ── Document engines connect ────────────────────────────────────────────────── -pub async fn connect_cassandra(state: State<'_, DbState>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { +pub async fn connect_cassandra(state: Db<'_>, config: crate::db::cassandra::CassandraConfig) -> Result<(), String> { let session = crate::db::cassandra::connect(&config).await?; set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Cassandra(session)))) } -pub async fn connect_mongo(state: State<'_, DbState>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { +pub async fn connect_mongo(state: Db<'_>, config: crate::db::mongo::MongoConfig) -> Result<(), String> { let handle = crate::db::mongo::connect(&config).await?; set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Mongo(handle)))) } -pub async fn connect_firestore(state: State<'_, DbState>, config: crate::db::firestore::FirestoreConfig) -> Result<(), String> { +pub async fn connect_firestore(state: Db<'_>, config: crate::db::firestore::FirestoreConfig) -> Result<(), String> { crate::db::firestore::ping(&config).await?; set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Firestore(config)))) } -pub async fn connect_couchdb(state: State<'_, DbState>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { +pub async fn connect_couchdb(state: Db<'_>, config: crate::db::couchdb::CouchConfig) -> Result<(), String> { crate::db::couchdb::ping(&config).await?; set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Couch(config)))) } +pub async fn connect_elastic(state: Db<'_>, config: crate::db::elastic::EsConfig) -> Result<(), String> { + let config = crate::db::elastic::connect(config).await?; + set_conn(&state, Some(ActiveConnection::Doc(crate::db::docdb::DocConn::Elastic(config)))) +} + // ── Redis connect / test ────────────────────────────────────────────────────── pub async fn test_redis_connection(config: RedisConfig) -> Result<(), String> { @@ -1526,7 +1653,7 @@ pub async fn test_redis_connection(config: RedisConfig) -> Result<(), String> { connect_racing_probe(&host, port, None, crate::db::redis::ping(&config)).await } -pub async fn connect_redis(state: State<'_, DbState>, config: RedisConfig) -> Result<(), String> { +pub async fn connect_redis(state: Db<'_>, config: RedisConfig) -> Result<(), String> { // Validate credentials/reachability before storing. test_redis_connection(config.clone()).await?; close_existing(&state).await; @@ -1562,7 +1689,7 @@ pub async fn test_duckdb_connection(config: DuckdbConfig) -> Result<(), String> crate::db::duckdb::ping(&handle).await } -pub async fn connect_duckdb(state: State<'_, DbState>, config: DuckdbConfig) -> Result<(), String> { +pub async fn connect_duckdb(state: Db<'_>, config: DuckdbConfig) -> Result<(), String> { let handle = open_duckdb(&config).await?; crate::db::duckdb::ping(&handle).await?; close_existing(&state).await; @@ -1582,16 +1709,40 @@ pub async fn test_mssql_connection(config: MssqlConfig) -> Result<(), String> { crate::db::mssql::ping(&handle).await } -pub async fn connect_mssql(state: State<'_, DbState>, config: MssqlConfig) -> Result<(), String> { +pub async fn connect_mssql(state: Db<'_>, config: MssqlConfig) -> Result<(), String> { let handle = open_mssql(&config).await?; close_existing(&state).await; set_conn(&state, Some(ActiveConnection::Mssql(handle))) } +// ── Oracle connect / test ─────────────────────────────────────────────────────── + +pub(crate) async fn open_oracle(config: &OracleConfig) -> Result { + crate::db::oracle::prepare_client(&config.client_lib_dir).await?; + match crate::db::oracle::probe_target(config) { + Some((host, port)) => connect_racing_probe(&host, port, None, crate::db::oracle::connect(config)).await, + // A tnsnames.ora alias: nothing to probe, so only the deadline applies. + None => tokio::time::timeout(CONNECT_DEADLINE, crate::db::oracle::connect(config)) + .await + .map_err(|_| format!("Oracle connection timed out after {}s", CONNECT_DEADLINE.as_secs()))?, + } +} + +pub async fn test_oracle_connection(config: OracleConfig) -> Result<(), String> { + let handle = open_oracle(&config).await?; + crate::db::oracle::ping(&handle).await +} + +pub async fn connect_oracle(state: Db<'_>, config: OracleConfig) -> Result<(), String> { + let handle = open_oracle(&config).await?; + close_existing(&state).await; + set_conn(&state, Some(ActiveConnection::Oracle(handle))) +} + // ── Disconnect ──────────────────────────────────────────────────────────────── pub async fn disconnect( - state: State<'_, DbState>, + state: Db<'_>, tunnel_state: State<'_, TunnelState>, ) -> Result<(), String> { // close_existing already sets state to None atomically via take() before diff --git a/src-tauri/src/db/d1.rs b/src-tauri/src/db/d1.rs index fdcee60b..51a24c11 100644 --- a/src-tauri/src/db/d1.rs +++ b/src-tauri/src/db/d1.rs @@ -177,3 +177,63 @@ pub async fn query(config: &D1Config, sql: &str, params: Vec) -> Result Result { + let url = format!( + "https://api.cloudflare.com/client/v4/accounts/{}/d1/database/{}/time_travel/{path_and_query}", + config.account_id, config.database_id + ); + let send = |token: String| { + let url = url.clone(); + let method = method.clone(); + async move { + client() + .request(method, &url) + .header("Authorization", format!("Bearer {token}")) + .send() + .await + .map_err(|e| crate::cloudflare::describe_request_error("D1 Time Travel request", &e)) + } + }; + let mut res = send(config.api_token.clone()).await?; + if res.status() == reqwest::StatusCode::UNAUTHORIZED { + if let Some(fresh) = crate::cloudflare::refreshed_token().await { + super::connection::update_d1_token(&fresh); + res = send(fresh).await?; + } + } + let status = res.status(); + let body: Value = res.json().await.unwrap_or(Value::Null); + if !status.is_success() || body["success"] == Value::Bool(false) { + let msg = body["errors"][0]["message"].as_str().map(str::to_string).unwrap_or_else(|| format!("HTTP {status}")); + return Err(format!("D1 Time Travel: {msg}")); + } + Ok(body["result"].clone()) +} + +/// The bookmark for now, or for the moment nearest `timestamp` (RFC 3339). +pub async fn bookmark(config: &D1Config, timestamp: Option<&str>) -> Result { + let q = match timestamp.map(str::trim).filter(|t| !t.is_empty()) { + Some(t) => format!("bookmark?timestamp={}", urlencoding::encode(t)), + None => "bookmark".to_string(), + }; + let r = time_travel(config, reqwest::Method::GET, &q).await?; + r["bookmark"].as_str().map(str::to_string).ok_or_else(|| "D1 Time Travel returned no bookmark".to_string()) +} + +/// Rewind the database to `bookmark`. Returns the bookmark of the state it +/// replaced, to undo the restore with. +pub async fn restore(config: &D1Config, bookmark: &str) -> Result { + let b = bookmark.trim(); + if b.is_empty() { + return Err("Give the bookmark to restore to.".into()); + } + let r = time_travel(config, reqwest::Method::POST, &format!("restore?bookmark={}", urlencoding::encode(b))).await?; + Ok(r["previous_bookmark"].as_str().unwrap_or_default().to_string()) +} + diff --git a/src-tauri/src/db/docdb.rs b/src-tauri/src/db/docdb.rs index f9bbe5fa..04dff9a3 100644 --- a/src-tauri/src/db/docdb.rs +++ b/src-tauri/src/db/docdb.rs @@ -1,5 +1,6 @@ //! Document and wide-column engines that share one connection slot -//! (`ActiveConnection::Doc`): CouchDB, Cassandra, Firestore. Each keeps its +//! (`ActiveConnection::Doc`): CouchDB, Cassandra, Firestore, MongoDB, +//! Elasticsearch/OpenSearch. Each keeps its //! own module; this file only routes the calls every engine answers. use super::query::SqlResult; @@ -11,6 +12,7 @@ pub enum DocConn { Cassandra(super::cassandra::CassandraHandle), Firestore(super::firestore::FirestoreConfig), Mongo(super::mongo::MongoHandle), + Elastic(super::elastic::EsConfig), } impl DocConn { @@ -20,6 +22,8 @@ impl DocConn { Self::Cassandra(_) => "cassandra", Self::Firestore(_) => "firestore", Self::Mongo(_) => "mongodb", + Self::Elastic(c) if c.is_opensearch() => "opensearch", + Self::Elastic(_) => "elasticsearch", } } } @@ -31,6 +35,17 @@ pub async fn query(conn: &DocConn, stmt: &str) -> Result { DocConn::Cassandra(s) => super::cassandra::query(s, stmt).await, DocConn::Firestore(c) => super::firestore::query(c, stmt).await, DocConn::Mongo(m) => super::mongo::query(m, stmt).await, + DocConn::Elastic(c) => super::elastic::query(c, stmt).await, + } +} + +/// A statement from an agent: like `query`, but Elasticsearch and OpenSearch +/// may write when the MCP gate allowed it (the other consoles have their own +/// write paths or only read). +pub async fn query_as(conn: &DocConn, stmt: &str, allow_write: bool) -> Result { + match conn { + DocConn::Elastic(c) => super::elastic::query_as(c, stmt, allow_write).await, + _ => query(conn, stmt).await, } } @@ -41,10 +56,11 @@ pub async fn list_tables(conn: &DocConn) -> Result, String> { DocConn::Cassandra(s) => super::cassandra::list_tables(s).await?, DocConn::Firestore(c) => super::firestore::list_collections(c).await?, DocConn::Mongo(m) => super::mongo::list_collections(m).await?, + DocConn::Elastic(c) => super::elastic::list_indices(c).await?.into_iter().map(|i| i.name).collect(), }; Ok(names .into_iter() - .map(|name| TableInfo { name, row_count: 0, kind: "table".into(), rls_enabled: None, created_at: None, create_order: None }) + .map(|name| TableInfo { name, row_count: 0, kind: "table".into(), rls_enabled: None, created_at: None, create_order: None, system: None }) .collect()) } @@ -58,12 +74,12 @@ pub async fn list_schemas(conn: &DocConn) -> Result, String> { match conn { DocConn::Mongo(m) => super::mongo::list_databases(m).await, DocConn::Cassandra(s) => super::cassandra::list_keyspaces(s).await, - DocConn::Couch(_) | DocConn::Firestore(_) => Ok(vec![FLAT_SCHEMA.to_string()]), + DocConn::Couch(_) | DocConn::Firestore(_) | DocConn::Elastic(_) => Ok(vec![FLAT_SCHEMA.to_string()]), } } fn info(name: String, row_count: i64) -> TableInfo { - TableInfo { name, row_count, kind: "table".into(), rls_enabled: None, created_at: None, create_order: None } + TableInfo { name, row_count, kind: "table".into(), rls_enabled: None, created_at: None, create_order: None, system: None } } /// Tables in one schema, with counts where they're cheap (-1 = unknown). @@ -100,6 +116,7 @@ pub async fn list_tables_in(conn: &DocConn, schema: &str) -> Result Ok(super::firestore::list_collections(c).await?.into_iter().map(|n| info(n, -1)).collect()), + DocConn::Elastic(c) => Ok(super::elastic::list_indices(c).await?.into_iter().map(|i| info(i.name, i.docs)).collect()), } } @@ -121,7 +138,7 @@ pub async fn use_schema(conn: &DocConn, schema: &str) -> Result<(), String> { } s.use_keyspace(schema, true).await.map_err(|e| format!("Keyspace \"{schema}\": {e}")) } - DocConn::Couch(_) | DocConn::Firestore(_) => Ok(()), + DocConn::Couch(_) | DocConn::Firestore(_) | DocConn::Elastic(_) => Ok(()), } } @@ -136,6 +153,8 @@ pub async fn columns_in(conn: &DocConn, schema: &str, table: &str) -> Result super::elastic::index_columns(c, table).await, DocConn::Mongo(_) | DocConn::Couch(_) | DocConn::Firestore(_) => Ok(vec![]), } } @@ -145,7 +164,7 @@ pub async fn columns_in(conn: &DocConn, schema: &str, table: &str) -> Result Result, String> { match conn { DocConn::Cassandra(s) => Ok(super::cassandra::columns_by_table(super::cassandra::keyspace_columns(s, schema).await?)), - DocConn::Mongo(_) | DocConn::Couch(_) | DocConn::Firestore(_) => Ok(vec![]), + DocConn::Mongo(_) | DocConn::Couch(_) | DocConn::Firestore(_) | DocConn::Elastic(_) => Ok(vec![]), } } diff --git a/src-tauri/src/db/duckdb.rs b/src-tauri/src/db/duckdb.rs index b1353957..0f49440a 100644 --- a/src-tauri/src/db/duckdb.rs +++ b/src-tauri/src/db/duckdb.rs @@ -265,7 +265,7 @@ pub async fn list_tables(handle: &DuckdbHandle) -> Result, String // views → 0; base tables get -1 as a "needs count" sentinel, filled below. let row_count = if kind == "view" { 0 } else { -1 }; let create_order = row.get(2).and_then(|v| v.as_i64()); - tables.push(TableInfo { name, kind: kind.to_string(), row_count, rls_enabled: None, created_at: None, create_order }); + tables.push(TableInfo { name, kind: kind.to_string(), row_count, rls_enabled: None, created_at: None, create_order, system: None }); } // Batch all COUNT(*)s into one UNION ALL statement per chunk instead of a diff --git a/src-tauri/src/db/dynamodb.rs b/src-tauri/src/db/dynamodb.rs index 0cf7f38e..d1c1b3d8 100644 --- a/src-tauri/src/db/dynamodb.rs +++ b/src-tauri/src/db/dynamodb.rs @@ -138,10 +138,10 @@ async fn export_credentials(profile: &str) -> Result { if let Some(c) = cached(profile) { return Ok(c); } - let fut = tokio::process::Command::new("aws") - .args(["configure", "export-credentials", "--format", "process", "--profile", profile]) - .kill_on_drop(true) - .output(); + let mut cmd = tokio::process::Command::new("aws"); + cmd.args(["configure", "export-credentials", "--format", "process", "--profile", profile]).kill_on_drop(true); + crate::proc::quiet(&mut cmd); + let fut = cmd.output(); let out = tokio::time::timeout(Duration::from_secs(20), fut) .await .map_err(|_| format!("`aws configure export-credentials` timed out for profile \"{profile}\""))? @@ -236,7 +236,9 @@ fn sha_hex(data: &[u8]) -> String { } /// The `Authorization` header for one request. `headers` must already hold -/// every header to sign (host and x-amz-date included), names lowercase. +/// every header to sign (host and x-amz-date included), names lowercase. An +/// `x-amz-content-sha256` header (S3's `UNSIGNED-PAYLOAD`, say) is used as the +/// payload hash instead of hashing `body`. #[allow(clippy::too_many_arguments)] pub fn sign( method: &str, @@ -256,7 +258,12 @@ pub fn sign( hs.sort(); let canonical_headers: String = hs.iter().map(|(k, v)| format!("{k}:{v}\n")).collect(); let signed: String = hs.iter().map(|(k, _)| k.as_str()).collect::>().join(";"); - let canonical = format!("{method}\n{path}\n{query}\n{canonical_headers}\n{signed}\n{}", sha_hex(body)); + let payload_hash = hs + .iter() + .find(|(k, _)| k == "x-amz-content-sha256") + .map(|(_, v)| v.clone()) + .unwrap_or_else(|| sha_hex(body)); + let canonical = format!("{method}\n{path}\n{query}\n{canonical_headers}\n{signed}\n{payload_hash}"); let date = &amz_date[..8]; let scope = format!("{date}/{region}/{service}/aws4_request"); let to_sign = format!("AWS4-HMAC-SHA256\n{amz_date}\n{scope}\n{}", sha_hex(canonical.as_bytes())); @@ -270,23 +277,14 @@ pub fn sign( // ── Requests ───────────────────────────────────────────────────────────────── -/// Operations the app may send. Anything else is refused, so the command -/// can't be used to reach other DynamoDB APIs (tables are not created or -/// dropped from here). -const ALLOWED_OPS: &[&str] = &[ - "ListTables", - "DescribeTable", - "DescribeTimeToLive", - "Scan", - "Query", - "GetItem", - "PutItem", - "UpdateItem", - "DeleteItem", - "BatchWriteItem", - "TransactWriteItems", - "ExecuteStatement", -]; +/// Operations the app may send: the API calls the console offers (see +/// [`parse_api_call`]) and PartiQL. Anything else is refused, so the command +/// can't reach other AWS APIs. Who may run which is decided before this, by +/// the access level (`mcp::access`) and the read-only switch. +fn op_allowed(op: &str) -> bool { + op == "ExecuteStatement" + || [API_READS, API_WRITES, API_SCHEMA, API_DANGEROUS].iter().any(|l| l.contains(&op)) +} fn client() -> &'static reqwest::Client { static C: OnceLock = OnceLock::new(); @@ -329,7 +327,7 @@ fn error_message(status: reqwest::StatusCode, body: &str) -> String { /// One DynamoDB API call. pub async fn request(cfg: &DynamoConfig, op: &str, body: &serde_json::Value) -> Result { - if !ALLOWED_OPS.contains(&op) { + if !op_allowed(op) { return Err(format!("DynamoDB operation \"{op}\" is not allowed")); } let region = resolve_region(cfg); @@ -420,7 +418,104 @@ fn number(n: &str) -> serde_json::Value { /// Rows past this many stop the NextToken loop; the message says so. const PARTIQL_ROW_CAP: usize = 5_000; -/// Run one PartiQL statement (`SELECT`, `INSERT`, `UPDATE`, `DELETE`). +/// DynamoDB API operations the console (and the AI) can call as JSON, for +/// what PartiQL can't do: tables, TTL, batch writes. Grouped by what they need. +pub const API_READS: &[&str] = &[ + "DescribeTable", "ListTables", "GetItem", "Query", "Scan", "BatchGetItem", "DescribeTimeToLive", +]; +pub const API_WRITES: &[&str] = &["PutItem", "UpdateItem", "DeleteItem", "BatchWriteItem", "TransactWriteItems"]; +pub const API_SCHEMA: &[&str] = &["CreateTable", "UpdateTable", "UpdateTimeToLive"]; +pub const API_DANGEROUS: &[&str] = &["DeleteTable"]; + +/// An API call, written either as `{"operation": "CreateTable", "input": {...}}` +/// or Dev Tools style as `CreateTable {...}`. None when it's PartiQL. +pub fn parse_api_call(stmt: &str) -> Option> { + let s = stmt.trim().trim_end_matches(';').trim(); + let known = |op: &str| [API_READS, API_WRITES, API_SCHEMA, API_DANGEROUS].iter().any(|l| l.contains(&op)); + if s.starts_with('{') { + let v: serde_json::Value = match serde_json::from_str(s) { + Ok(v) => v, + Err(e) => return Some(Err(format!("The API call isn't valid JSON: {e}"))), + }; + let Some(op) = v.get("operation").and_then(|o| o.as_str()) else { + return Some(Err("An API call names its \"operation\", like {\"operation\": \"CreateTable\", \"input\": {...}}".into())); + }; + if !known(op) { + return Some(Err(format!("{op} isn't available here"))); + } + return Some(Ok((op.to_string(), v.get("input").cloned().unwrap_or_else(|| serde_json::json!({}))))); + } + let word: String = s.chars().take_while(|c| c.is_ascii_alphabetic()).collect(); + if word.is_empty() || !known(&word) { + return None; + } + let body = s[word.len()..].trim(); + let input = if body.is_empty() { + serde_json::json!({}) + } else { + match serde_json::from_str(body) { + Ok(v) => v, + Err(e) => return Some(Err(format!("The {word} input isn't valid JSON: {e}"))), + } + }; + Some(Ok((word, input))) +} + +/// Run an API call and show its answer: items as rows, anything else as one row. +async fn api_call( + cfg: &DynamoConfig, + op: &str, + input: &serde_json::Value, + statement: &str, + t0: std::time::Instant, +) -> Result { + use super::query::{ColumnInfo, SqlResult}; + let out = request(cfg, op, input).await?; + let items: Vec> = if let Some(arr) = out.get("Items").and_then(|v| v.as_array()) { + arr.iter().filter_map(|i| i.as_object().cloned()).collect() + } else if let Some(item) = out.get("Item").and_then(|v| v.as_object()) { + vec![item.clone()] + } else { + Vec::new() + }; + if items.is_empty() && !out.get("Items").is_some() { + let message = Some(format!("{op} done")); + let rows = if out.as_object().is_some_and(|o| !o.is_empty()) { vec![vec![out]] } else { Vec::new() }; + return Ok(SqlResult { + columns: vec![ColumnInfo::new("result".to_string(), "json")], + row_count: Some(rows.len() as i64), + rows, + message, + query_ms: t0.elapsed().as_millis() as u64, + sql: statement.to_string(), + }); + } + let mut cols: Vec = Vec::new(); + let mut seen = std::collections::HashSet::new(); + for it in &items { + for k in it.keys() { + if seen.insert(k.clone()) { + cols.push(k.clone()); + } + } + } + let rows: Vec> = items + .iter() + .map(|it| cols.iter().map(|c| it.get(c).map(unmarshall).unwrap_or(serde_json::Value::Null)).collect()) + .collect(); + let message = out.get("LastEvaluatedKey").map(|_| "More items exist: pass LastEvaluatedKey as ExclusiveStartKey for the next page".to_string()); + Ok(SqlResult { + columns: cols.iter().map(|c| ColumnInfo::new(c.clone(), "json")).collect(), + row_count: Some(rows.len() as i64), + rows, + message, + query_ms: t0.elapsed().as_millis() as u64, + sql: statement.to_string(), + }) +} + +/// Run one statement: PartiQL (`SELECT`, `INSERT`, `UPDATE`, `DELETE`), or an +/// API call for what PartiQL can't do (see [`parse_api_call`]). pub async fn query(cfg: &DynamoConfig, statement: &str) -> Result { use super::query::{ColumnInfo, SqlResult}; let t0 = std::time::Instant::now(); @@ -428,6 +523,10 @@ pub async fn query(cfg: &DynamoConfig, statement: &str) -> Result> = Vec::new(); let mut token: Option = None; let mut capped = false; @@ -513,6 +612,7 @@ pub async fn list_tables(cfg: &DynamoConfig) -> Result, + #[serde(default)] + pub password: Option, + /// An API key (the base64 `id:key` form Elastic shows once). Used instead + /// of the user and password when set. + #[serde(default)] + pub api_key: Option, + /// `elasticsearch` or `opensearch`. Saved connections pick it; the server's + /// own answer to `GET /` corrects it on connect. + #[serde(default)] + pub flavor: Option, + /// Accept a self-signed certificate (a local OpenSearch's demo TLS). + #[serde(default)] + pub insecure: bool, +} + +impl EsConfig { + pub fn is_opensearch(&self) -> bool { + self.flavor.as_deref() == Some("opensearch") + } + fn label(&self) -> &'static str { + if self.is_opensearch() { "OpenSearch" } else { "Elasticsearch" } + } +} + +fn client(insecure: bool) -> &'static reqwest::Client { + static SAFE: OnceLock = OnceLock::new(); + static LAX: OnceLock = OnceLock::new(); + let cell = if insecure { &LAX } else { &SAFE }; + cell.get_or_init(|| { + reqwest::Client::builder() + .timeout(Duration::from_secs(60)) + .connect_timeout(Duration::from_secs(10)) + .danger_accept_invalid_certs(insecure) + .build() + .unwrap_or_default() + }) +} + +fn base(cfg: &EsConfig) -> Result { + let raw = cfg.url.trim().trim_end_matches('/'); + let raw = if raw.contains("://") { raw.to_string() } else { format!("http://{raw}") }; + reqwest::Url::parse(&format!("{raw}/")).map_err(|e| format!("Invalid {} URL \"{raw}\": {e}", cfg.label())) +} + +/// Endpoints (`_name` path segments) that only read. The console may call +/// these with POST, since search bodies go in a POST. +const READ_ENDPOINTS: &[&str] = &[ + "_search", "_count", "_msearch", "_mget", "_explain", "_field_caps", "_validate", "_analyze", "_sql", "_mapping", + "_mappings", "_settings", "_stats", "_alias", "_aliases", "_cat", "_cluster", "_nodes", "_doc", "_source", + "_termvectors", "_resolve", "_data_stream", "_plugins", "_opendistro", "_pit", "_search_shards", +]; + +/// Read endpoints that take a POST body. `POST /{index}/_mapping` and +/// `POST /_aliases` change things, so they're GET only. +const POST_READ_ENDPOINTS: &[&str] = &[ + "_search", "_count", "_msearch", "_mget", "_explain", "_field_caps", "_validate", "_analyze", "_sql", "_plugins", + "_opendistro", "_pit", "_search_shards", "_termvectors", +]; + +/// Endpoints the browser writes documents through. +const DOC_WRITE_ENDPOINTS: &[&str] = &["_doc", "_create", "_update", "_refresh", "_bulk"]; + +/// `_plugins` / `_opendistro` paths allowed: SQL and PPL only. +fn plugin_ok(p: &str) -> bool { + let rest = p.split_once('/').map(|(_, r)| r).unwrap_or(""); + rest.starts_with("_sql") || rest.starts_with("_ppl") +} + +/// The request's `_endpoint` segments, path only. +fn endpoints(p: &str) -> Vec<&str> { + p.split('?').next().unwrap_or("").split('/').filter(|s| s.starts_with('_')).collect() +} + +/// Requests the app may make. Cluster settings, security, snapshots, index +/// deletion and `_delete_by_query` are refused; documents can be written. +pub fn check_path(method: &str, path: &str) -> Result<(), String> { + let p = path.trim_start_matches('/'); + if p.contains("..") { + return Err("Invalid path".into()); + } + if !matches!(method, "GET" | "HEAD" | "POST" | "PUT" | "DELETE") { + return Err(format!("HTTP method {method} is not allowed")); + } + let path_only = p.split('?').next().unwrap_or("").trim_end_matches('/'); + if path_only.is_empty() { + return if method == "GET" || method == "HEAD" { Ok(()) } else { Err("Not allowed on the server root".into()) }; + } + let eps = endpoints(p); + if eps.is_empty() { + // `/{index}` alone: reading its definition is fine; creating or + // deleting an index is not available from Stroke. + return if method == "GET" || method == "HEAD" { + Ok(()) + } else { + Err("Creating or deleting indices is not available from Stroke".into()) + }; + } + for e in &eps { + if matches!(*e, "_plugins" | "_opendistro") && !plugin_ok(path_only.trim_start_matches(|c| c != '_')) { + return Err(format!("/{path_only} is not available from Stroke")); + } + } + let first = eps[0]; + let read = READ_ENDPOINTS.contains(&first); + let write = DOC_WRITE_ENDPOINTS.contains(&first); + match method { + "GET" | "HEAD" if read || write => Ok(()), + // Search bodies travel in a POST; `_doc`/`_create`/`_update`/`_bulk`/ + // `_refresh` write documents. + "POST" if POST_READ_ENDPOINTS.contains(&first) => Ok(()), + "POST" | "PUT" if write => Ok(()), + "DELETE" if first == "_doc" && path_only.split('/').count() == 3 => Ok(()), + "DELETE" if first == "_pit" => Ok(()), + _ => Err(format!("{method} /{path_only} is not available from Stroke")), + } +} + +/// What an agent's request needs from the MCP access gate: reads are READ, +/// writing documents is WRITE, creating an index or adding to its mapping is +/// MODIFY, anything else (deleting an index, cluster settings) is DANGEROUS, +/// which `agent_may` refuses whatever the level. +pub fn request_level(method: &str, path: &str) -> u8 { + use crate::mcp::access::{DANGEROUS, MODIFY, READ, WRITE}; + if is_read(method, path) { + READ + } else if check_path(method, path).is_ok() { + WRITE + } else if creates_schema(method, path) { + MODIFY + } else { + DANGEROUS + } +} + +/// `PUT /{index}` (with optional settings and mappings) or `PUT|POST +/// /{index}/_mapping`: the schema changes an agent at MODIFY may make. +fn creates_schema(method: &str, path: &str) -> bool { + let p = path.trim_start_matches('/'); + let path_only = p.split('?').next().unwrap_or("").trim_end_matches('/'); + if path_only.is_empty() || path_only.starts_with('_') || p.contains("..") { + return false; + } + let eps = endpoints(p); + (method == "PUT" && eps.is_empty() && !path_only.contains('/')) + || (matches!(method, "PUT" | "POST") && eps.first() == Some(&"_mapping")) +} + +/// Requests an agent may send once the gate has passed it: documents, plus +/// creating an index or its mapping. Index deletion stays unavailable. +fn agent_may(method: &str, path: &str) -> bool { + check_path(method, path).is_ok() || creates_schema(method, path) +} + +/// Whether a request only reads (the console and MCP accept only these). +pub fn is_read(method: &str, path: &str) -> bool { + if check_path(method, path).is_err() { + return false; + } + let eps = endpoints(path.trim_start_matches('/')); + match method { + "GET" | "HEAD" => true, + "POST" => eps.first().is_some_and(|e| POST_READ_ENDPOINTS.contains(e)), + _ => false, + } +} + +fn error_text(cfg: &EsConfig, status: reqwest::StatusCode, v: &Value) -> String { + let err = &v["error"]; + // `{"error": {"root_cause": [...], "type": ..., "reason": ...}}`, or a string. + let reason = err["root_cause"][0]["reason"] + .as_str() + .or_else(|| err["reason"].as_str()) + .or_else(|| err.as_str()) + .or_else(|| v["message"].as_str()); + let kind = err["root_cause"][0]["type"].as_str().or_else(|| err["type"].as_str()); + match (status.as_u16(), kind, reason) { + (401, _, _) => format!("{} refused the login: check the user and password, or the API key", cfg.label()), + (403, _, Some(r)) => format!("Not allowed: {r}"), + (404, Some("index_not_found_exception"), Some(r)) => r.to_string(), + (409, _, _) => "Version conflict: the document changed since it was loaded. Reload and try again.".into(), + (_, Some(k), Some(r)) => format!("{k}: {r}"), + (_, None, Some(r)) => r.to_string(), + _ => format!("{} returned HTTP {status}", cfg.label()), + } +} + +/// One REST call; `path` is relative to the server, query string included. +pub async fn request(cfg: &EsConfig, method: &str, path: &str, body: Option<&Value>) -> Result { + check_path(method, path)?; + send(cfg, method, path, body.map(Body::Json)).await +} + +enum Body<'a> { + Json(&'a Value), + /// `_bulk` / `_msearch`: newline-delimited JSON. + Ndjson(String), +} + +async fn send(cfg: &EsConfig, method: &str, path: &str, body: Option>) -> Result { + let url = base(cfg)?.join(path.trim_start_matches('/')).map_err(|e| format!("Invalid path \"{path}\": {e}"))?; + let m = reqwest::Method::from_bytes(method.as_bytes()).map_err(|e| e.to_string())?; + let mut req = client(cfg.insecure).request(m, url); + if let Some(k) = cfg.api_key.as_deref().map(str::trim).filter(|k| !k.is_empty()) { + req = req.header("Authorization", format!("ApiKey {k}")); + } else if let Some(u) = cfg.user.as_deref().filter(|u| !u.is_empty()) { + req = req.basic_auth(u, cfg.password.as_deref()); + } + match body { + Some(Body::Json(b)) => req = req.json(b), + Some(Body::Ndjson(s)) => req = req.header("Content-Type", "application/x-ndjson").body(s), + None => {} + } + let resp = req.send().await.map_err(|e| { + if e.is_connect() { + let mut msg = format!("Could not reach {} at {}", cfg.label(), base(cfg).map(|u| u.to_string()).unwrap_or_default()); + // A local OpenSearch serves HTTPS with a demo certificate. + if format!("{e:?}").contains("certificate") { + msg.push_str(": the server's certificate isn't trusted. Turn on \"Accept self-signed certificate\" for a local server."); + } else { + msg.push_str(&format!(": {e}")); + } + msg + } else if e.is_timeout() { + // A red cluster (no shard can take the write, often a full disk) + // holds requests until they time out. + format!("{} didn't answer within 60 seconds. Check the cluster's health (GET _cluster/health): a red cluster holds writes and searches.", cfg.label()) + } else { + e.to_string() + } + })?; + let status = resp.status(); + let text = resp.text().await.map_err(|e| e.to_string())?; + let v: Value = serde_json::from_str(&text).unwrap_or(Value::String(text)); + if !status.is_success() { + return Err(error_text(cfg, status, &v)); + } + Ok(v) +} + +/// What `GET /` says about the server. +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct ServerInfo { + pub flavor: String, + pub version: String, + pub cluster: String, +} + +pub fn server_info(v: &Value) -> ServerInfo { + let flavor = if v["version"]["distribution"].as_str() == Some("opensearch") { "opensearch" } else { "elasticsearch" }; + ServerInfo { + flavor: flavor.into(), + version: v["version"]["number"].as_str().unwrap_or("").into(), + cluster: v["cluster_name"].as_str().unwrap_or("").into(), + } +} + +/// Connection test: the server answers, the login works, and which engine it is. +pub async fn ping(cfg: &EsConfig) -> Result { + let v = request(cfg, "GET", "", None).await?; + if !v.is_object() || v.get("version").is_none() { + return Err(format!("{} didn't answer at {}: is this the HTTP port (usually 9200)?", cfg.label(), cfg.url)); + } + Ok(server_info(&v)) +} + +/// Connect: ping, then keep the flavor the server reported. +pub async fn connect(mut cfg: EsConfig) -> Result { + let info = ping(&cfg).await?; + cfg.flavor = Some(info.flavor); + Ok(cfg) +} + +// ── Catalog ────────────────────────────────────────────────────────────────── + +/// One index from `_cat/indices`. +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct IndexRow { + pub name: String, + pub docs: i64, + pub size: String, + pub health: String, +} + +pub fn index_rows(v: &Value) -> Vec { + let mut out: Vec = v + .as_array() + .into_iter() + .flatten() + .filter_map(|r| { + let name = r["index"].as_str()?.to_string(); + // Dot-prefixed indices are the server's own (security, Kibana, + // task results) and data stream backing indices. + if name.starts_with('.') { + return None; + } + let docs = r["docs.count"].as_str().and_then(|s| s.parse().ok()).or_else(|| r["docs.count"].as_i64()).unwrap_or(-1); + Some(IndexRow { + name, + docs, + size: r["store.size"].as_str().unwrap_or("").into(), + health: r["health"].as_str().unwrap_or("").into(), + }) + }) + .collect(); + out.sort_by(|a, b| a.name.cmp(&b.name)); + out +} + +pub async fn list_indices(cfg: &EsConfig) -> Result, String> { + let v = request(cfg, "GET", "_cat/indices?format=json&h=index,docs.count,store.size,health&s=index", None).await?; + Ok(index_rows(&v)) +} + +/// Field paths and types from an index mapping, nested objects flattened to +/// dotted paths (`user.name`), multi-fields (`title.keyword`) included. +pub fn mapping_fields(mapping: &Value) -> Vec<(String, String)> { + fn walk(props: &Value, prefix: &str, out: &mut Vec<(String, String)>) { + let Some(map) = props.as_object() else { return }; + for (name, def) in map { + let path = if prefix.is_empty() { name.clone() } else { format!("{prefix}.{name}") }; + let ty = def["type"].as_str().unwrap_or(if def.get("properties").is_some() { "object" } else { "" }); + if !ty.is_empty() { + out.push((path.clone(), ty.to_string())); + } + if let Some(p) = def.get("properties") { + walk(p, &path, out); + } + if let Some(f) = def.get("fields") { + walk(f, &path, out); + } + } + } + let mut out = vec![]; + // `{index: {mappings: {properties: ...}}}`; one entry per concrete index + // when the name is an alias or pattern. The first one's fields are used. + if let Some((_, idx)) = mapping.as_object().and_then(|m| m.iter().next()) { + walk(&idx["mappings"]["properties"], "", &mut out); + } + out +} + +pub async fn index_columns(cfg: &EsConfig, index: &str) -> Result, String> { + let v = request(cfg, "GET", &format!("{}/_mapping", enc(index)), None).await?; + let mut rows = vec![super::schema::ColumnStructureRow { + ordinal_position: 1, + name: "_id".into(), + data_type: "keyword".into(), + is_nullable: false, + column_default: None, + foreign_key: None, + fk_constraint_name: None, + comment: None, + is_primary_key: true, + }]; + for (i, (name, ty)) in mapping_fields(&v).into_iter().enumerate() { + rows.push(super::schema::ColumnStructureRow { + ordinal_position: i as i32 + 2, + name, + data_type: ty, + is_nullable: true, + column_default: None, + foreign_key: None, + fk_constraint_name: None, + comment: None, + is_primary_key: false, + }); + } + Ok(rows) +} + +/// Percent-encode one path segment (ids may hold `/`, `?`, `#`). Commas and +/// `*` stay, since index lists and patterns use them. +pub fn enc(s: &str) -> String { + urlencoding::encode(s).replace("%2C", ",").replace("%2A", "*") +} + +// ── Console ────────────────────────────────────────────────────────────────── + +#[derive(Debug, PartialEq)] +pub enum Statement { + /// `METHOD path` plus an optional JSON body (Kibana Dev Tools form). + Raw { method: String, path: String, body: Option }, + Search { index: String, body: Value }, + Get { index: String, id: String }, + Count { index: String, body: Value }, + Sql(String), +} + +const USAGE: &str = "Use GET index/_search with a JSON body, index.search({...}), index.get(\"id\"), index.count({...}) or SQL"; + +pub fn parse(stmt: &str) -> Result { + let s = stmt.trim().trim_end_matches(';').trim(); + if s.is_empty() { + return Err(USAGE.into()); + } + let first_word = s.split_whitespace().next().unwrap_or("").to_ascii_uppercase(); + if matches!(first_word.as_str(), "SELECT" | "SHOW" | "DESCRIBE" | "DESC" | "WITH") { + return Ok(Statement::Sql(s.to_string())); + } + if matches!(first_word.as_str(), "GET" | "POST" | "PUT" | "DELETE" | "HEAD") { + let (line, rest) = s.split_once('\n').unwrap_or((s, "")); + let mut parts = line.split_whitespace(); + let method = parts.next().unwrap_or("").to_ascii_uppercase(); + let path = parts.next().unwrap_or("").trim_start_matches('/').to_string(); + let rest = rest.trim(); + let body = if rest.is_empty() { + None + } else { + Some(serde_json::from_str(rest).map_err(|e| format!("The request body isn't valid JSON: {e}"))?) + }; + return Ok(Statement::Raw { method, path, body }); + } + let open = s.find('(').ok_or(USAGE)?; + if !s.ends_with(')') { + return Err(USAGE.into()); + } + let (index, method) = s[..open].rsplit_once('.').ok_or(USAGE)?; + let index = index.trim().trim_matches('"').to_string(); + if index.is_empty() { + return Err(USAGE.into()); + } + let args_src = s[open + 1..s.len() - 1].trim(); + let args: Vec = if args_src.is_empty() { + vec![] + } else { + serde_json::from_str(&format!("[{args_src}]")).map_err(|e| format!("Arguments aren't valid JSON: {e}"))? + }; + let body = || -> Result { + let b = args.first().cloned().unwrap_or_else(|| json!({})); + if b.is_object() { Ok(b) } else { Err(format!("{}() takes a request body object, e.g. {{\"query\": {{\"match_all\": {{}}}}}}", method.trim())) } + }; + match method.trim() { + "search" | "find" => Ok(Statement::Search { index, body: body()? }), + "count" => Ok(Statement::Count { index, body: body()? }), + "get" => match args.first() { + Some(Value::String(id)) => Ok(Statement::Get { index, id: id.clone() }), + _ => Err("get() takes a document id string".into()), + }, + m => Err(format!("Unknown method \"{m}\". {USAGE}")), + } +} + +/// Default page of hits when the body doesn't say. +const DEFAULT_SIZE: u64 = 100; + +/// A search hit as a grid row: `_id` and `_index`, then the source fields. +fn hit_doc(h: &Value, many_indices: bool) -> Value { + let mut doc = serde_json::Map::new(); + doc.insert("_id".into(), h["_id"].clone()); + if many_indices { + doc.insert("_index".into(), h["_index"].clone()); + } + if let Some(src) = h["_source"].as_object() { + for (k, v) in src { + doc.insert(k.clone(), v.clone()); + } + } + if let Some(f) = h["fields"].as_object() { + for (k, v) in f { + // `fields` values are arrays; one value reads better unwrapped. + let v = match v.as_array() { + Some(a) if a.len() == 1 => a[0].clone(), + _ => v.clone(), + }; + doc.entry(k.clone()).or_insert(v); + } + } + Value::Object(doc) +} + +/// Aggregation buckets as rows: one row per bucket of each top-level +/// bucket aggregation, single values (avg, sum, cardinality) as one row. +fn agg_rows(aggs: &Value) -> Vec { + let mut rows = vec![]; + for (name, a) in aggs.as_object().into_iter().flatten() { + if let Some(buckets) = a["buckets"].as_array() { + for b in buckets { + let mut row = serde_json::Map::new(); + row.insert("aggregation".into(), json!(name)); + row.insert("key".into(), b.get("key_as_string").cloned().unwrap_or_else(|| b["key"].clone())); + row.insert("doc_count".into(), b["doc_count"].clone()); + // Sub-aggregations with a single value become their own columns. + for (k, v) in b.as_object().into_iter().flatten() { + if let Some(x) = v.get("value") { + row.insert(k.clone(), x.clone()); + } + } + rows.push(Value::Object(row)); + } + } else if let Some(x) = a.get("value") { + rows.push(json!({ "aggregation": name, "value": x })); + } else { + rows.push(json!({ "aggregation": name, "result": a })); + } + } + rows +} + +/// A `_search` response as grid documents and a summary line. +pub fn search_docs(out: &Value) -> (Vec, Option) { + let hits = out["hits"]["hits"].as_array().cloned().unwrap_or_default(); + let total = out["hits"]["total"]["value"].as_u64().or_else(|| out["hits"]["total"].as_u64()); + let exact = out["hits"]["total"]["relation"].as_str() != Some("gte"); + let many = { + let first = hits.first().and_then(|h| h["_index"].as_str()); + hits.iter().any(|h| h["_index"].as_str() != first) + }; + if hits.is_empty() { + if let Some(aggs) = out.get("aggregations") { + let rows = agg_rows(aggs); + let msg = total.map(|t| format!("{t}{} matching documents", if exact { "" } else { "+" })); + return (rows, msg); + } + } + let docs: Vec = hits.iter().map(|h| hit_doc(h, many)).collect(); + let msg = match total { + Some(t) if t as usize > docs.len() => Some(format!("{} of {t}{} hits", docs.len(), if exact { "" } else { "+" })), + _ => None, + }; + (docs, msg) +} + +/// A SQL response as a result grid. Elasticsearch answers `{columns, rows}`; +/// OpenSearch's SQL plugin `{schema, datarows}`. +pub fn sql_result(v: &Value, stmt: &str, t0: Instant) -> super::query::SqlResult { + use super::query::{ColumnInfo, SqlResult}; + let cols = v["columns"].as_array().or_else(|| v["schema"].as_array()).cloned().unwrap_or_default(); + let rows: Vec> = v["rows"] + .as_array() + .or_else(|| v["datarows"].as_array()) + .into_iter() + .flatten() + .map(|r| r.as_array().cloned().unwrap_or_default()) + .collect(); + let message = v["cursor"].as_str().filter(|c| !c.is_empty()).map(|_| format!("Showing the first {} rows", rows.len())); + SqlResult { + columns: cols + .iter() + .map(|c| ColumnInfo::new(c["name"].as_str().or_else(|| c["alias"].as_str()).unwrap_or("").to_string(), c["type"].as_str().unwrap_or(""))) + .collect(), + row_count: Some(rows.len() as i64), + rows, + message, + query_ms: t0.elapsed().as_millis() as u64, + sql: stmt.to_string(), + } +} + +/// Any JSON answer as grid documents: arrays as rows (`_cat` with +/// `format=json`), a search as hits, anything else as one row. +fn raw_docs(v: &Value) -> (Vec, Option) { + if v.get("hits").is_some() { + return search_docs(v); + } + if v.get("_source").is_some() && v.get("_id").is_some() { + return (vec![hit_doc(v, false)], None); + } + match v { + Value::Array(a) => (a.clone(), None), + Value::Object(_) => (vec![v.clone()], None), + other => (vec![json!({ "response": other })], None), + } +} + +/// Run one console statement I typed: reads, document writes, and creating +/// an index or its mapping. A read-only connection is stopped by the page +/// before it gets here. +pub async fn query(cfg: &EsConfig, stmt: &str) -> Result { + query_as(cfg, stmt, true).await +} + +/// Run one statement for an agent. With `allow_write` (the MCP gate already +/// checked the session's level) documents can be written and an index or its +/// mapping created; without it this is the read-only console. +pub async fn query_as(cfg: &EsConfig, stmt: &str, allow_write: bool) -> Result { + let t0 = Instant::now(); + let (docs, message) = match parse(stmt)? { + Statement::Sql(sql) => { + let (path, body) = if cfg.is_opensearch() { + ("_plugins/_sql?format=jdbc", json!({ "query": sql })) + } else { + ("_sql?format=json", json!({ "query": sql, "fetch_size": 1000 })) + }; + let v = request(cfg, "POST", path, Some(&body)).await.map_err(|e| { + if e.contains("no handler") || e.contains("HTTP 404") || e.contains("HTTP 400") && e.contains("_plugins") { + format!("{e}. SQL needs the SQL plugin (OpenSearch) or a license that includes SQL (Elasticsearch).") + } else { + e + } + })?; + return Ok(sql_result(&v, stmt, t0)); + } + Statement::Raw { method, path, body } => { + if !is_read(&method, &path) { + if allow_write && agent_may(&method, &path) { + let v = send(cfg, &method, &path, body.as_ref().map(Body::Json)).await?; + return Ok(super::couchdb::docs_result(vec![v], None, stmt, t0)); + } + return Err(match check_path(&method, &path) { + Err(why) => why, + Ok(()) if !allow_write => format!( + "Agents only read on this connection. {method} /{path} changes data; allow writes for the agent, or run it in the editor." + ), + Ok(()) => format!("{method} /{path} isn't available from Stroke."), + }); + } + let v = request(cfg, &method, &path, body.as_ref()).await?; + raw_docs(&v) + } + Statement::Search { index, mut body } => { + if body.get("size").is_none() { + body["size"] = json!(DEFAULT_SIZE); + } + let v = request(cfg, "POST", &format!("{}/_search", enc(&index)), Some(&body)).await?; + search_docs(&v) + } + Statement::Count { index, body } => { + let v = request(cfg, "POST", &format!("{}/_count", enc(&index)), Some(&body)).await?; + (vec![json!({ "count": v["count"] })], None) + } + Statement::Get { index, id } => { + let v = request(cfg, "GET", &format!("{}/_doc/{}", enc(&index), urlencoding::encode(&id)), None).await?; + (vec![hit_doc(&v, false)], None) + } + }; + Ok(super::couchdb::docs_result(docs, message, stmt, t0)) +} + +/// Import a JSON, JSON Lines or CSV file into an index, 500 documents per +/// `_bulk` request. A document's `_id` field becomes its id. +pub async fn import_file(cfg: &EsConfig, index: &str, path: &str, format: &str, infer: bool) -> Result { + let (name, text) = super::mongo::read_import(path)?; + let (_, docs) = super::mongo::parse_import(&name, &text, format, infer)?; + drop(text); + let mut res = super::mongo::ImportResult { inserted: 0, failed: 0, errors: vec![] }; + for (b, chunk) in docs.chunks(500).enumerate() { + let mut nd = String::new(); + for d in chunk { + let id = match d.get("_id") { + Some(Value::String(s)) => Some(s.clone()), + Some(Value::Number(n)) => Some(n.to_string()), + _ => None, + }; + let mut src = d.clone(); + if let Some(o) = src.as_object_mut() { + o.remove("_id"); + } + let action = match id { + Some(id) => json!({ "index": { "_index": index, "_id": id } }), + None => json!({ "index": { "_index": index } }), + }; + nd.push_str(&action.to_string()); + nd.push('\n'); + nd.push_str(&src.to_string()); + nd.push('\n'); + } + let v = send(cfg, "POST", "_bulk?refresh=wait_for", Some(Body::Ndjson(nd))) + .await + .map_err(|e| format!("Import stopped after {} documents: {e}", res.inserted))?; + for (i, item) in v["items"].as_array().into_iter().flatten().enumerate() { + let r = &item["index"]; + if r.get("error").is_some() { + res.failed += 1; + if res.errors.len() < 5 { + let reason = r["error"]["reason"].as_str().unwrap_or("failed"); + res.errors.push(format!("Document {}: {reason}", b * 500 + i + 1)); + } + } else { + res.inserted += 1; + } + } + } + Ok(res) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn path_allowlist() { + assert!(check_path("GET", "").is_ok()); + assert!(check_path("GET", "_cat/indices?format=json").is_ok()); + assert!(check_path("POST", "logs/_search").is_ok()); + assert!(check_path("POST", "logs-*,metrics/_search?size=5").is_ok()); + assert!(check_path("POST", "_sql?format=json").is_ok()); + assert!(check_path("POST", "_plugins/_sql").is_ok()); + assert!(check_path("GET", "logs/_mapping").is_ok()); + assert!(check_path("PUT", "logs/_doc/a%2Fb?refresh=wait_for").is_ok()); + assert!(check_path("POST", "logs/_doc").is_ok()); + assert!(check_path("POST", "logs/_update/1").is_ok()); + assert!(check_path("DELETE", "logs/_doc/1").is_ok()); + assert!(check_path("POST", "_bulk").is_ok()); + + assert!(check_path("DELETE", "logs").is_err()); + assert!(check_path("PUT", "logs").is_err()); + assert!(check_path("POST", "logs/_delete_by_query").is_err()); + assert!(check_path("POST", "logs/_mapping").is_err()); + assert!(check_path("POST", "_aliases").is_err()); + assert!(check_path("PUT", "_cluster/settings").is_err()); + assert!(check_path("POST", "_snapshot/repo").is_err()); + assert!(check_path("PUT", "_security/user/x").is_err()); + assert!(check_path("GET", "_plugins/_security/api/internalusers").is_err()); + assert!(check_path("DELETE", "logs/_doc").is_err()); + assert!(check_path("PATCH", "logs/_doc/1").is_err()); + assert!(check_path("GET", "logs/../_security").is_err()); + } + + #[test] + fn reads_and_writes() { + assert!(is_read("GET", "logs/_doc/1")); + assert!(is_read("POST", "logs/_search")); + assert!(is_read("POST", "_plugins/_sql")); + assert!(!is_read("POST", "logs/_doc")); + assert!(!is_read("PUT", "logs/_doc/1")); + assert!(!is_read("POST", "_bulk")); + assert!(!is_read("DELETE", "logs/_doc/1")); + } + + #[test] + fn parses_console_statements() { + assert_eq!( + parse("GET logs/_search\n{\"query\": {\"match_all\": {}}}").unwrap(), + Statement::Raw { method: "GET".into(), path: "logs/_search".into(), body: Some(json!({"query": {"match_all": {}}})) } + ); + assert_eq!( + parse("get /_cat/indices?v").unwrap(), + Statement::Raw { method: "GET".into(), path: "_cat/indices?v".into(), body: None } + ); + assert_eq!( + parse(r#"logs.search({"query": {"term": {"level": "error"}}})"#).unwrap(), + Statement::Search { index: "logs".into(), body: json!({"query": {"term": {"level": "error"}}}) } + ); + assert_eq!(parse("logs-2026.11.search()").unwrap(), Statement::Search { index: "logs-2026.11".into(), body: json!({}) }); + assert_eq!(parse(r#"logs.get("a/b")"#).unwrap(), Statement::Get { index: "logs".into(), id: "a/b".into() }); + assert_eq!(parse("logs.count()").unwrap(), Statement::Count { index: "logs".into(), body: json!({}) }); + assert_eq!(parse("select * from logs limit 5;").unwrap(), Statement::Sql("select * from logs limit 5".into())); + assert!(parse("GET logs/_search\n{bad").unwrap_err().contains("valid JSON")); + assert!(parse("logs.drop()").unwrap_err().contains("Unknown method")); + assert!(parse("logs.search([1])").unwrap_err().contains("body object")); + } + + #[test] + fn search_hits_become_rows() { + let out = json!({"hits": {"total": {"value": 42, "relation": "eq"}, "hits": [ + {"_index": "logs", "_id": "1", "_source": {"level": "error", "msg": "boom"}}, + {"_index": "logs", "_id": "2", "_source": {"level": "info"}, "fields": {"host": ["a"]}} + ]}}); + let (docs, msg) = search_docs(&out); + assert_eq!(docs[0], json!({"_id": "1", "level": "error", "msg": "boom"})); + assert_eq!(docs[1], json!({"_id": "2", "level": "info", "host": "a"})); + assert_eq!(msg.as_deref(), Some("2 of 42 hits")); + let r = super::super::couchdb::docs_result(docs, msg, "q", Instant::now()); + assert_eq!(r.columns[0].name, "_id"); + } + + #[test] + fn aggregations_become_rows() { + let out = json!({"hits": {"total": {"value": 10000, "relation": "gte"}, "hits": []}, "aggregations": { + "by_level": {"buckets": [{"key": "error", "doc_count": 3, "avg_ms": {"value": 12.5}}, {"key": "info", "doc_count": 9, "avg_ms": {"value": 2.0}}]}, + "total_ms": {"value": 99.0} + }}); + let (rows, msg) = search_docs(&out); + assert_eq!(rows.len(), 3); + assert_eq!(rows[0], json!({"aggregation": "by_level", "key": "error", "doc_count": 3, "avg_ms": 12.5})); + assert_eq!(rows[2], json!({"aggregation": "total_ms", "value": 99.0})); + assert_eq!(msg.as_deref(), Some("10000+ matching documents")); + } + + #[test] + fn sql_answers_from_both_engines() { + let es = json!({"columns": [{"name": "level", "type": "keyword"}, {"name": "n", "type": "long"}], "rows": [["error", 3]]}); + let r = sql_result(&es, "q", Instant::now()); + assert_eq!(r.columns[1].name, "n"); + assert_eq!(r.rows, vec![vec![json!("error"), json!(3)]]); + let os = json!({"schema": [{"name": "level", "type": "keyword"}], "datarows": [["info"]], "total": 1, "size": 1, "status": 200}); + let r = sql_result(&os, "q", Instant::now()); + assert_eq!(r.columns[0].name, "level"); + assert_eq!(r.rows.len(), 1); + } + + #[test] + fn mapping_flattens_nested_and_multi_fields() { + let m = json!({"logs": {"mappings": {"properties": { + "msg": {"type": "text", "fields": {"keyword": {"type": "keyword"}}}, + "user": {"properties": {"name": {"type": "keyword"}, "age": {"type": "integer"}}}, + "@timestamp": {"type": "date"} + }}}}); + let f = mapping_fields(&m); + assert!(f.contains(&("msg".into(), "text".into()))); + assert!(f.contains(&("msg.keyword".into(), "keyword".into()))); + assert!(f.contains(&("user".into(), "object".into()))); + assert!(f.contains(&("user.age".into(), "integer".into()))); + assert!(f.contains(&("@timestamp".into(), "date".into()))); + } + + #[test] + fn indices_skip_system_ones() { + let v = json!([ + {"index": ".kibana_1", "docs.count": "5", "store.size": "1kb", "health": "green"}, + {"index": "logs", "docs.count": "1200", "store.size": "2mb", "health": "yellow"} + ]); + let rows = index_rows(&v); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].name, "logs"); + assert_eq!(rows[0].docs, 1200); + } + + #[test] + fn detects_the_engine() { + let os = json!({"cluster_name": "c", "version": {"distribution": "opensearch", "number": "2.17.0"}}); + assert_eq!(server_info(&os).flavor, "opensearch"); + let es = json!({"cluster_name": "c", "version": {"number": "8.15.0", "build_flavor": "default"}}); + assert_eq!(server_info(&es).flavor, "elasticsearch"); + assert_eq!(server_info(&es).version, "8.15.0"); + } + + #[test] + fn encodes_ids_but_keeps_patterns() { + assert_eq!(enc("logs-*,metrics"), "logs-*,metrics"); + assert_eq!(urlencoding::encode("a/b"), "a%2Fb"); + } + + /// Against a local server: `cargo test --lib es_live -- --ignored`, with + /// ES_URL (default http://localhost:9200). + #[tokio::test] + #[ignore] + async fn es_live() { + let url = std::env::var("ES_URL").unwrap_or_else(|_| "http://localhost:9200".into()); + let cfg = EsConfig { name: "t".into(), url, user: None, password: None, api_key: None, flavor: None, insecure: true }; + let cfg = connect(cfg).await.unwrap(); + request(&cfg, "PUT", "stroke_it/_doc/1?refresh=wait_for", Some(&json!({"level": "error", "ms": 12}))).await.unwrap(); + request(&cfg, "PUT", "stroke_it/_doc/2?refresh=wait_for", Some(&json!({"level": "info", "ms": 3}))).await.unwrap(); + let names: Vec = list_indices(&cfg).await.unwrap().into_iter().map(|i| i.name).collect(); + assert!(names.contains(&"stroke_it".to_string()), "{names:?}"); + let r = query(&cfg, r#"stroke_it.search({"query": {"term": {"level": "error"}}})"#).await.unwrap(); + assert_eq!(r.rows.len(), 1); + let r = query(&cfg, "GET stroke_it/_search\n{\"size\": 0, \"aggs\": {\"l\": {\"terms\": {\"field\": \"level.keyword\"}}}}").await.unwrap(); + assert_eq!(r.rows.len(), 2); + let r = query(&cfg, "SELECT level, ms FROM stroke_it ORDER BY ms").await.unwrap(); + assert_eq!(r.rows.len(), 2, "{:?}", r.rows); + let cols = index_columns(&cfg, "stroke_it").await.unwrap(); + assert!(cols.iter().any(|c| c.name == "level.keyword")); + assert!(query(&cfg, "DELETE stroke_it/_doc/1").await.unwrap_err().contains("only reads")); + request(&cfg, "DELETE", "stroke_it/_doc/1", None).await.unwrap(); + request(&cfg, "DELETE", "stroke_it/_doc/2", None).await.unwrap(); + } +} diff --git a/src-tauri/src/db/explain.rs b/src-tauri/src/db/explain.rs index c71d0042..fccd69ce 100644 --- a/src-tauri/src/db/explain.rs +++ b/src-tauri/src/db/explain.rs @@ -108,12 +108,28 @@ fn strip_sql(sql: &str) -> &str { s } -pub async fn explain_pg(pool: &sqlx::PgPool, sql: &str) -> Result { +/// Postgres plan as JSON. With `analyze` the statement really runs (that is +/// what gives actual rows and times), so it runs inside a transaction that is +/// always rolled back: explaining a DELETE never deletes anything. Without it +/// only the planner's estimates come back and nothing runs. +pub async fn explain_pg(pool: &sqlx::PgPool, sql: &str, analyze: bool) -> Result { let stripped = strip_sql(sql); - let q = format!("EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) {stripped}"); + let q = if analyze { + format!("EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT JSON) {stripped}") + } else { + format!("EXPLAIN (VERBOSE, FORMAT JSON) {stripped}") + }; // PG returns [{Plan: {...}, "Planning Time": x, "Execution Time": y}] - let arr = match sqlx::query_as::<_, (serde_json::Value,)>(&q).fetch_one(pool).await { + let fetched = async { + let mut tx = pool.begin().await.map_err(|e| e.to_string())?; + let out = sqlx::query_as::<_, (serde_json::Value,)>(&q).fetch_one(&mut *tx).await; + // Roll back whatever ANALYZE did, success or not. + let _ = tx.rollback().await; + out.map_err(|e| e.to_string()) + } + .await; + let arr = match fetched { Ok(row) => row.0, // CockroachDB speaks the PG wire protocol but rejects PG's EXPLAIN option // syntax (`at or near "analyze"`). Fall back to its indented text plan; if diff --git a/src-tauri/src/db/firestore.rs b/src-tauri/src/db/firestore.rs index e4ed5370..334244ae 100644 --- a/src-tauri/src/db/firestore.rs +++ b/src-tauri/src/db/firestore.rs @@ -159,7 +159,10 @@ async fn gcloud_token() -> Result { if let Some(t) = cached_token("gcloud") { return Ok(t); } - let fut = tokio::process::Command::new("gcloud").args(["auth", "print-access-token"]).kill_on_drop(true).output(); + let mut cmd = tokio::process::Command::new("gcloud"); + cmd.args(["auth", "print-access-token"]).kill_on_drop(true); + crate::proc::quiet(&mut cmd); + let fut = cmd.output(); let out = tokio::time::timeout(Duration::from_secs(20), fut) .await .map_err(|_| "gcloud did not answer in time".to_string())? diff --git a/src-tauri/src/db/geo.rs b/src-tauri/src/db/geo.rs index f8993e8a..8e20b718 100644 --- a/src-tauri/src/db/geo.rs +++ b/src-tauri/src/db/geo.rs @@ -16,12 +16,12 @@ //! so the GiST index still answers it. Reprojection to WGS84 happens only on //! the rows that survive. -use super::connection::{require_conn, ActiveConnection, DbState}; +use crate::db::connection::Db; +use super::connection::{require_conn, ActiveConnection}; use super::query::{build_where, RowFilter}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; use sqlx::{PgPool, Row}; -use tauri::State; /// Hard ceiling on features returned in one fetch, whatever the caller asks for. /// Above this the canvas is drawing more points than the viewport has pixels and @@ -91,7 +91,7 @@ pub struct GeoFeatures { /// PostGIS puts its own tables in these schemas; they are plumbing, not data. const HIDDEN_SCHEMAS: &str = "('information_schema', 'pg_catalog', 'topology', 'tiger', 'tiger_data')"; -fn pg_pool(state: &State<'_, DbState>) -> Result, String> { +fn pg_pool(state: &Db<'_>) -> Result, String> { match require_conn(state)? { ActiveConnection::Postgres(pool) => Ok(Some(pool)), _ => Ok(None), @@ -122,7 +122,7 @@ fn unavailable() -> GeoOverview { /// Is PostGIS installed, and what can be mapped? Safe to call on any connection: /// a non-Postgres engine, or a Postgres without the extension, reports /// `available: false` rather than failing. -pub async fn geo_overview(state: State<'_, DbState>) -> Result { +pub async fn geo_overview(state: Db<'_>) -> Result { let Some(pool) = pg_pool(&state)? else { return Ok(unavailable()) }; let version: Option = @@ -372,7 +372,7 @@ async fn layer_extent( /// holds more than `limit` rows. #[allow(clippy::too_many_arguments)] pub async fn geo_features( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, column: String, diff --git a/src-tauri/src/db/import.rs b/src-tauri/src/db/import.rs index 7a868ca0..cb9978f4 100644 --- a/src-tauri/src/db/import.rs +++ b/src-tauri/src/db/import.rs @@ -10,14 +10,15 @@ keep going, the batch is replayed one row at a time so the report can name the rows that were actually bad instead of blaming all 500. */ +use crate::db::connection::Db; use std::collections::HashMap; use std::sync::atomic::{AtomicBool, Ordering}; use serde::{Deserialize, Serialize}; use serde_json::Value; -use tauri::{AppHandle, Emitter, State}; +use tauri::{AppHandle, Emitter}; -use super::connection::{require_conn, ActiveConnection, DbState}; +use super::connection::{require_conn, ActiveConnection}; use super::query::{bind_typed_value, is_bytea_type, pg_insert_meta, validate_typed_value}; use super::schema::validate_ident; @@ -123,7 +124,7 @@ fn emit_log(app: &AppHandle, level: &'static str, message: impl Into) { #[tauri::command] pub async fn import_rows( app: AppHandle, - state: State<'_, DbState>, + state: Db<'_>, schema: Option, table: String, columns: Vec, @@ -169,6 +170,7 @@ pub async fn import_rows( } ActiveConnection::Duckdb(_) => Err("Importing rows is not supported for DuckDB yet".into()), ActiveConnection::Mssql(_) => Err("Importing rows is not supported for SQL Server yet".into()), + ActiveConnection::Oracle(_) => Err("Importing rows is not supported for Oracle yet".into()), ActiveConnection::D1(_) => Err("Importing rows is not supported for Cloudflare D1 yet".into()), ActiveConnection::LibSql(_) => Err("Importing rows is not supported for LibSQL/Turso yet".into()), ActiveConnection::Clickhouse(_) => { diff --git a/src-tauri/src/db/insights.rs b/src-tauri/src/db/insights.rs index eb93e26e..2a189807 100644 --- a/src-tauri/src/db/insights.rs +++ b/src-tauri/src/db/insights.rs @@ -13,13 +13,13 @@ //! than failing the whole command. The dashboard must never hard-fail because, //! e.g., `pg_stat_replication` requires a privileged role. -use super::connection::{require_conn, ActiveConnection, ClickhouseConfig, DbState}; +use crate::db::connection::Db; +use super::connection::{require_conn, ActiveConnection, ClickhouseConfig}; use super::query::SqlResult; use serde::Serialize; use serde_json::{Map, Value}; use sqlx::{Column, MySqlPool, PgPool, Row, SqlitePool}; use std::collections::HashMap; -use tauri::State; /// Returned when Instance Insights is requested on an unsupported engine. const UNSUPPORTED: &str = @@ -265,7 +265,7 @@ pub struct InstanceVersion { } /// Cheap engine + version lookup so the dashboard header can render immediately. -pub async fn instance_version(state: State<'_, DbState>) -> Result { +pub async fn instance_version(state: Db<'_>) -> Result { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { let version = sqlx::query("SHOW server_version") @@ -369,7 +369,7 @@ fn usage_pct(total: i64, max: i64) -> f64 { } } -pub async fn instance_activity(state: State<'_, DbState>) -> Result { +pub async fn instance_activity(state: Db<'_>) -> Result { match require_conn(&state)? { ActiveConnection::Postgres(pool) => Ok(pg_activity(&pool).await), ActiveConnection::Mysql(pool) => Ok(mysql_activity(&pool).await), @@ -566,7 +566,7 @@ pub struct InstanceState { pub prepared_transactions: Vec, } -pub async fn instance_state(state: State<'_, DbState>) -> Result { +pub async fn instance_state(state: Db<'_>) -> Result { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { let sessions = pg_query_objects( @@ -692,7 +692,7 @@ pub struct ConfigSetting { pub editable: bool, } -pub async fn instance_config(state: State<'_, DbState>) -> Result, String> { +pub async fn instance_config(state: Db<'_>) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { let rows = sqlx::query( @@ -863,7 +863,7 @@ fn sql_literal(value: &str) -> String { /// hold `pg_write_all_settings`) - the server's own error is passed straight /// through when it is not. pub async fn instance_set_config( - state: State<'_, DbState>, + state: Db<'_>, name: String, value: Option, ) -> Result { @@ -983,7 +983,7 @@ pub struct InstanceReplication { pub slots: Vec, } -pub async fn instance_replication(state: State<'_, DbState>) -> Result { +pub async fn instance_replication(state: Db<'_>) -> Result { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { let stats = pg_query_objects( diff --git a/src-tauri/src/db/migrations.rs b/src-tauri/src/db/migrations.rs new file mode 100644 index 00000000..54334e7e --- /dev/null +++ b/src-tauri/src/db/migrations.rs @@ -0,0 +1,339 @@ +// The migrations a Prisma or Drizzle project has on disk, for the Migrations +// page. What ran comes from the database (`_prisma_migrations`, +// `__drizzle_migrations`), read by the page with plain SELECTs; this side only +// reads the project folder, so the page can show what's still pending and spot +// a migration whose file changed after it ran. +// +// Prisma: `/migrations//migration.sql`, its checksum the SHA-256 of +// the file (what Prisma stores in `_prisma_migrations.checksum`). +// Drizzle: `/meta/_journal.json` lists the migrations in order ({ idx, +// tag, when }); `/.sql` is the SQL and its SHA-256 is the `hash` +// Drizzle stores in `__drizzle_migrations`. + +use serde::Serialize; +use sha2::{Digest, Sha256}; +use std::path::{Path, PathBuf}; + +#[derive(Debug, Clone, Serialize, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct MigrationFile { + /// Prisma: the folder name. Drizzle: the journal's `tag`. + pub name: String, + pub sql: String, + /// SHA-256 of the SQL, hex. + pub checksum: String, + /// Drizzle's journal `when` (ms); None for Prisma. + pub when: Option, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct MigrationProject { + /// prisma | drizzle + pub tool: String, + /// The folder the migrations were read from. + pub dir: String, + pub files: Vec, + /// Every `.sql` file in the folder by file name, journal or not: what + /// `wrangler d1 migrations apply` runs and records in `d1_migrations`. + pub sql_files: Vec, + /// The D1 database a `wrangler.toml` / `wrangler.json(c)` next to the + /// migrations names, for the apply command. + pub wrangler_db: Option, +} + +/// The first `database_name = "x"` (TOML) or `"database_name": "x"` (JSON). +fn database_name(text: &str) -> Option { + for (i, _) in text.match_indices("database_name") { + let rest = text[i + "database_name".len()..].trim_start_matches('"').trim_start(); + let Some(rest) = rest.strip_prefix([':', '=']) else { continue }; + let rest = rest.trim_start(); + let q = rest.chars().next().filter(|c| *c == '"' || *c == '\'')?; + let v: String = rest[1..].chars().take_while(|c| *c != q).collect(); + if !v.is_empty() { + return Some(v); + } + } + None +} + +/// `database_name` from the first wrangler config in `from` or a folder above +/// it, up to `root`. +fn wrangler_db(root: &Path, from: &Path) -> Option { + let mut dir = Some(from); + while let Some(d) = dir { + for f in ["wrangler.toml", "wrangler.jsonc", "wrangler.json"] { + if let Ok(text) = std::fs::read_to_string(d.join(f)) { + if let Some(name) = database_name(&text) { + return Some(name); + } + } + } + if d == root { + break; + } + dir = d.parent(); + } + None +} + +const MAX_SQL: usize = 2 * 1024 * 1024; + +fn sha256_hex(bytes: &[u8]) -> String { + hex::encode(Sha256::digest(bytes)) +} + +fn read_sql(path: &Path) -> Option<(String, String)> { + let bytes = std::fs::read(path).ok()?; + let checksum = sha256_hex(&bytes); + let mut sql = String::from_utf8_lossy(&bytes).into_owned(); + if sql.len() > MAX_SQL { + let mut cut = MAX_SQL; + while !sql.is_char_boundary(cut) { + cut -= 1; + } + sql.truncate(cut); + sql.push_str("\n-- (cut: the file is larger than 2 MB)"); + } + Some((sql, checksum)) +} + +/// `migrations/` folders that hold Prisma migrations (a `migration_lock.toml`, +/// or subfolders with a `migration.sql`). +fn is_prisma_dir(dir: &Path) -> bool { + if dir.join("migration_lock.toml").is_file() { + return true; + } + std::fs::read_dir(dir) + .map(|rd| rd.flatten().any(|e| e.path().join("migration.sql").is_file())) + .unwrap_or(false) +} + +/// Drizzle output folders: they hold `meta/_journal.json`. +fn is_drizzle_dir(dir: &Path) -> bool { + dir.join("meta").join("_journal.json").is_file() +} + +/// A plain `migrations/` folder of `.sql` files, as Wrangler (D1) keeps them. +fn is_sql_dir(dir: &Path) -> bool { + dir.file_name().is_some_and(|n| n == "migrations") + && std::fs::read_dir(dir) + .map(|rd| rd.flatten().any(|e| e.path().extension().is_some_and(|x| x == "sql"))) + .unwrap_or(false) +} + +/// Every `.sql` file directly in `dir`, by name; `name` keeps the `.sql` +/// (that's what `d1_migrations.name` holds). +pub fn read_sql_dir(dir: &Path) -> Vec { + let Ok(rd) = std::fs::read_dir(dir) else { return Vec::new() }; + let mut names: Vec = rd + .flatten() + .filter(|e| e.path().is_file() && e.path().extension().is_some_and(|x| x == "sql")) + .filter_map(|e| e.file_name().to_str().map(String::from)) + .collect(); + names.sort(); + names + .into_iter() + .filter_map(|name| { + let (sql, checksum) = read_sql(&dir.join(&name))?; + Some(MigrationFile { name, sql, checksum, when: None }) + }) + .collect() +} + +/// Folders worth looking into for a project root, a few levels down, skipping +/// the ones that are never migrations. +fn walk(root: &Path, depth: usize, out: &mut Vec) { + out.push(root.to_path_buf()); + if depth == 0 { + return; + } + let Ok(rd) = std::fs::read_dir(root) else { return }; + let mut dirs: Vec = rd + .flatten() + .filter(|e| e.file_type().is_ok_and(|t| t.is_dir())) + .map(|e| e.path()) + .filter(|p| { + let n = p.file_name().and_then(|n| n.to_str()).unwrap_or(""); + !n.starts_with('.') && !matches!(n, "node_modules" | "target" | "dist" | "build" | ".next" | "coverage" | "vendor") + }) + .collect(); + dirs.sort(); + for d in dirs { + walk(&d, depth - 1, out); + } +} + +/// Find the migrations of `tool` (or either) in a project folder: the folder +/// itself, or one up to four levels below it. +pub fn find(root: &Path, tool: Option<&str>) -> Option<(String, PathBuf)> { + let mut dirs = Vec::new(); + walk(root, 4, &mut dirs); + let want_prisma = tool.is_none_or(|t| t == "prisma"); + let want_drizzle = tool.is_none_or(|t| t == "drizzle"); + // Prefer the shallowest match: a monorepo's app folder over a fixture deep down. + dirs.sort_by_key(|d| d.components().count()); + for d in &dirs { + if want_drizzle && is_drizzle_dir(d) { + return Some(("drizzle".into(), d.clone())); + } + if want_prisma && d.file_name().is_some_and(|n| n == "migrations") && is_prisma_dir(d) { + return Some(("prisma".into(), d.clone())); + } + } + // No journal anywhere: a Wrangler migrations folder reads like Drizzle's + // SQL files, applied by name. + if want_drizzle { + if let Some(d) = dirs.iter().find(|d| is_sql_dir(d) && !is_prisma_dir(d)) { + return Some(("drizzle".into(), d.clone())); + } + } + None +} + +pub fn read_prisma(dir: &Path) -> Vec { + let Ok(rd) = std::fs::read_dir(dir) else { return Vec::new() }; + let mut names: Vec = rd + .flatten() + .filter(|e| e.path().join("migration.sql").is_file()) + .filter_map(|e| e.file_name().to_str().map(String::from)) + .collect(); + // Prisma applies them in folder-name order (timestamp prefixes). + names.sort(); + names + .into_iter() + .filter_map(|name| { + let (sql, checksum) = read_sql(&dir.join(&name).join("migration.sql"))?; + Some(MigrationFile { name, sql, checksum, when: None }) + }) + .collect() +} + +pub fn read_drizzle(dir: &Path) -> Result, String> { + if !is_drizzle_dir(dir) { + // A Wrangler folder: its files are the migrations, in name order. + return Ok(read_sql_dir(dir).into_iter().map(|f| MigrationFile { name: f.name.trim_end_matches(".sql").to_string(), ..f }).collect()); + } + let journal = std::fs::read_to_string(dir.join("meta").join("_journal.json")) + .map_err(|e| format!("Could not read meta/_journal.json: {e}"))?; + let v: serde_json::Value = serde_json::from_str(&journal).map_err(|e| format!("meta/_journal.json isn't valid JSON: {e}"))?; + let mut entries: Vec<(i64, String, Option)> = v + .get("entries") + .and_then(|e| e.as_array()) + .map(|a| { + a.iter() + .filter_map(|e| { + let tag = e.get("tag")?.as_str()?.to_string(); + let idx = e.get("idx").and_then(|i| i.as_i64()).unwrap_or(0); + Some((idx, tag, e.get("when").and_then(|w| w.as_i64()))) + }) + .collect() + }) + .unwrap_or_default(); + entries.sort_by_key(|e| e.0); + Ok(entries + .into_iter() + .map(|(_, tag, when)| { + let (sql, checksum) = read_sql(&dir.join(format!("{tag}.sql"))).unwrap_or_else(|| ("-- The file for this migration is missing".into(), String::new())); + MigrationFile { name: tag, sql, checksum, when } + }) + .collect()) +} + +/// The migrations in a project folder. `tool` narrows the search when a +/// project has both. +#[tauri::command] +pub async fn migrations_project(dir: String, tool: Option) -> Result { + tokio::task::spawn_blocking(move || { + let root = PathBuf::from(&dir); + if !root.is_dir() { + return Err(format!("{dir} isn't a folder")); + } + let (tool, found) = find(&root, tool.as_deref()).ok_or_else(|| { + "No Prisma or Drizzle migrations in this folder. Pick the project folder (the one with prisma/ or drizzle.config).".to_string() + })?; + let files = if tool == "prisma" { read_prisma(&found) } else { read_drizzle(&found)? }; + let sql_files = if tool == "drizzle" { read_sql_dir(&found) } else { Vec::new() }; + let wrangler_db = wrangler_db(&root, &found); + Ok(MigrationProject { tool, dir: found.to_string_lossy().into_owned(), files, sql_files, wrangler_db }) + }) + .await + .map_err(|e| e.to_string())? +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tmp(name: &str) -> PathBuf { + let d = std::env::temp_dir().join(format!("stroke-mig-{name}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&d); + std::fs::create_dir_all(&d).unwrap(); + d + } + + #[test] + fn prisma_migrations_are_found_and_read_in_order() { + let root = tmp("prisma"); + let m = root.join("app").join("prisma").join("migrations"); + for (n, sql) in [("20240102_add_posts", "CREATE TABLE posts();"), ("20240101_init", "CREATE TABLE users();")] { + std::fs::create_dir_all(m.join(n)).unwrap(); + std::fs::write(m.join(n).join("migration.sql"), sql).unwrap(); + } + std::fs::write(m.join("migration_lock.toml"), "provider = \"postgresql\"").unwrap(); + let (tool, dir) = find(&root, None).unwrap(); + assert_eq!((tool.as_str(), dir.clone()), ("prisma", m.clone())); + let files = read_prisma(&dir); + assert_eq!(files.iter().map(|f| f.name.as_str()).collect::>(), ["20240101_init", "20240102_add_posts"]); + assert_eq!(files[0].checksum, sha256_hex(b"CREATE TABLE users();")); + let _ = std::fs::remove_dir_all(&root); + } + + #[test] + fn drizzle_journal_drives_the_order_and_hashes() { + let root = tmp("drizzle"); + let out = root.join("drizzle"); + std::fs::create_dir_all(out.join("meta")).unwrap(); + std::fs::write( + out.join("meta").join("_journal.json"), + r#"{"entries":[{"idx":1,"tag":"0001_posts","when":1700000002000},{"idx":0,"tag":"0000_init","when":1700000001000},{"idx":2,"tag":"0002_gone","when":1700000003000}]}"#, + ) + .unwrap(); + std::fs::write(out.join("0000_init.sql"), "CREATE TABLE users();").unwrap(); + std::fs::write(out.join("0001_posts.sql"), "CREATE TABLE posts();").unwrap(); + // node_modules is never searched. + std::fs::create_dir_all(root.join("node_modules").join("x").join("meta")).unwrap(); + let (tool, dir) = find(&root, None).unwrap(); + assert_eq!((tool.as_str(), dir.clone()), ("drizzle", out.clone())); + let files = read_drizzle(&dir).unwrap(); + assert_eq!(files.iter().map(|f| f.name.as_str()).collect::>(), ["0000_init", "0001_posts", "0002_gone"]); + assert_eq!(files[1].when, Some(1700000002000)); + assert_eq!(files[0].checksum, sha256_hex(b"CREATE TABLE users();")); + assert!(files[2].checksum.is_empty()); + assert!(find(&root, Some("prisma")).is_none()); + // Every .sql file, journal or not, for D1's d1_migrations. + std::fs::write(out.join("0001b_hand_written.sql"), "SELECT 1;").unwrap(); + std::fs::write(root.join("wrangler.jsonc"), "{\n \"d1_databases\": [{ \"database_name\": \"shop-db\", \"binding\": \"DB\" }]\n}").unwrap(); + assert_eq!(wrangler_db(&root, &dir).as_deref(), Some("shop-db")); + std::fs::remove_file(root.join("wrangler.jsonc")).unwrap(); + std::fs::write(root.join("wrangler.toml"), "[[d1_databases]]\nbinding = \"DB\"\ndatabase_name = \"shop-db\"\n").unwrap(); + assert_eq!(wrangler_db(&root, &dir).as_deref(), Some("shop-db")); + let all = read_sql_dir(&dir); + assert_eq!(all.iter().map(|f| f.name.as_str()).collect::>(), ["0000_init.sql", "0001_posts.sql", "0001b_hand_written.sql"]); + let _ = std::fs::remove_dir_all(&root); + } + + #[test] + fn a_wrangler_migrations_folder_reads_as_drizzle() { + let root = tmp("wrangler"); + let m = root.join("migrations"); + std::fs::create_dir_all(&m).unwrap(); + std::fs::write(m.join("0002_b.sql"), "SELECT 2;").unwrap(); + std::fs::write(m.join("0001_a.sql"), "SELECT 1;").unwrap(); + let (tool, dir) = find(&root, None).unwrap(); + assert_eq!((tool.as_str(), dir.clone()), ("drizzle", m.clone())); + let files = read_drizzle(&dir).unwrap(); + assert_eq!(files.iter().map(|f| f.name.as_str()).collect::>(), ["0001_a", "0002_b"]); + let _ = std::fs::remove_dir_all(&root); + } +} diff --git a/src-tauri/src/db/mod.rs b/src-tauri/src/db/mod.rs index 5c98b906..b3a800fc 100644 --- a/src-tauri/src/db/mod.rs +++ b/src-tauri/src/db/mod.rs @@ -1,11 +1,14 @@ pub mod admin; pub mod backup; +pub mod migrations; pub mod clickhouse; +pub mod parked; pub mod posthog; pub mod connection; pub mod d1; pub mod duckdb; pub mod mssql; +pub mod oracle; pub mod explain; pub mod advisor; pub mod insights; @@ -16,6 +19,7 @@ pub mod objects; pub mod redis; pub mod dynamodb; pub mod couchdb; +pub mod elastic; pub mod cassandra; pub mod firestore; pub mod mongo; @@ -37,8 +41,8 @@ pub mod terminal; pub mod tx; pub use connection::{ - connect, connect_clickhouse, connect_d1, connect_duckdb, connect_libsql, connect_mssql, connect_mysql, connect_redis, connect_sqlite, disconnect, - test_clickhouse_connection, test_connection, test_d1_connection, test_duckdb_connection, test_libsql_connection, test_mssql_connection, test_mysql_connection, test_redis_connection, test_sqlite_connection, + connect, connect_clickhouse, connect_d1, connect_duckdb, connect_libsql, connect_mssql, connect_mysql, connect_oracle, connect_redis, connect_sqlite, disconnect, + test_clickhouse_connection, test_connection, test_d1_connection, test_duckdb_connection, test_libsql_connection, test_mssql_connection, test_mysql_connection, test_oracle_connection, test_redis_connection, test_sqlite_connection, ActiveConnection, AnyConnectionConfig, ConnectionConfig, D1Config, DbState, LibSqlConfig, SqliteConfig, }; pub use explain::{explain_pg, explain_mysql, explain_sqlite, explain_from_text_lines, explain_from_sqlite_plan, ExplainResult}; @@ -51,7 +55,7 @@ pub use geo::{geo_features, geo_overview, GeoBbox, GeoFeatures, GeoOverview}; pub use ssh_tunnel::TunnelState; pub use query::{ delete_table_row, delete_table_rows, execute_ddl, execute_sql, execute_sql_multi, execute_sql_on_conn, execute_sql_script, StatementOutcome, - fetch_cell_value, CellValueResult, get_column_stats, get_table_rows, count_table_rows, insert_table_row, update_table_cell, ping_connection, + fetch_cell_value, CellValueResult, get_column_stats, get_table_rows, count_table_rows, search_index_status, create_search_index, SearchIndexStatus, insert_table_row, update_table_cell, ping_connection, ColumnStats, InsertRowResult, KeysetCursor, apply_table_changes, ApplyChangesResult, RowUpdate, RowChunk, RowFilter, RowSink, SortSpec, SqlResult, TableRows, }; pub use schema::{ diff --git a/src-tauri/src/db/mongo.rs b/src-tauri/src/db/mongo.rs index 911406f6..c6588f0f 100644 --- a/src-tauri/src/db/mongo.rs +++ b/src-tauri/src/db/mongo.rs @@ -1262,7 +1262,7 @@ pub struct ImportPreview { const IMPORT_MAX_BYTES: u64 = 256 * 1024 * 1024; -fn read_import(path: &str) -> Result<(String, String), String> { +pub(crate) fn read_import(path: &str) -> Result<(String, String), String> { let p = std::path::Path::new(path); let len = std::fs::metadata(p).map_err(|e| format!("Couldn't read the file: {e}"))?.len(); if len > IMPORT_MAX_BYTES { @@ -1329,6 +1329,8 @@ pub enum Stmt { Use(String), ShowDbs, ShowCollections, + /// `db.createCollection("name")` + CreateCollection(String), /// `db..(...)[.sort(...)][.limit(...)][.skip(...)]` Coll { coll: String, calls: Vec }, } @@ -1369,6 +1371,7 @@ fn calls(mut s: &str) -> Result, String> { const METHODS: &[&str] = &[ "find", "findOne", "aggregate", "countDocuments", "estimatedDocumentCount", "distinct", "insertOne", "insertMany", "updateOne", "updateMany", "replaceOne", "deleteOne", "deleteMany", "getIndexes", "explain", + "createIndex", "dropIndex", "drop", ]; pub fn parse(stmt: &str) -> Result { @@ -1383,6 +1386,17 @@ pub fn parse(stmt: &str) -> Result { _ => {} } let rest = s.strip_prefix("db.").ok_or(usage)?; + // db.createCollection("name") + if let Some(r) = rest.strip_prefix("createCollection(") { + let r = r.trim(); + let inner = r.strip_suffix(')').ok_or("createCollection() takes a quoted name")?.trim(); + let name = inner + .strip_prefix('"').and_then(|v| v.strip_suffix('"')) + .or_else(|| inner.strip_prefix('\'').and_then(|v| v.strip_suffix('\''))) + .filter(|n| !n.is_empty()) + .ok_or("createCollection() takes a quoted name")?; + return Ok(Stmt::CreateCollection(name.to_string())); + } // db.getCollection("name").method(...) if let Some(r) = rest.strip_prefix("getCollection(") { let r = r.trim_start(); @@ -1430,6 +1444,10 @@ pub async fn query(m: &MongoConn, stmt: &str) -> Result { + m.client.database(&db_name).create_collection(&name).await.map_err(err)?; + (vec![], Some(format!("Created collection {name}"))) + } Stmt::ShowCollections => { let mut names = m.client.database(&db_name).list_collection_names().await.map_err(err)?; names.sort(); @@ -1578,6 +1596,32 @@ pub async fn query(m: &MongoConn, stmt: &str) -> Result { + let keys = arg_doc(&args, 0, "index key")?; + if keys.is_empty() { + return Err("createIndex() takes the keys, like {email: 1}".into()); + } + let opts = args.get(1).filter(|v| v.is_object()); + let mut options = mongodb::options::IndexOptions::default(); + if let Some(o) = opts { + options.name = o.get("name").and_then(|v| v.as_str()).map(str::to_string); + options.unique = o.get("unique").and_then(|v| v.as_bool()); + options.sparse = o.get("sparse").and_then(|v| v.as_bool()); + options.expire_after = o.get("expireAfterSeconds").and_then(|v| v.as_u64()).map(std::time::Duration::from_secs); + } + let model = mongodb::IndexModel::builder().keys(keys).options(options).build(); + let r = c.create_index(model).await.map_err(err)?; + (vec![json!({ "name": r.index_name })], Some(format!("Created index {}", r.index_name))) + } + "dropIndex" => { + let name = args.first().and_then(|v| v.as_str()).ok_or("dropIndex() takes the index name")?; + c.drop_index(name).await.map_err(err)?; + (vec![], Some(format!("Dropped index {name}"))) + } + "drop" => { + c.drop().await.map_err(err)?; + (vec![], Some(format!("Dropped collection {coll}"))) + } other => return Err(format!("{other}() isn't supported here")), } } diff --git a/src-tauri/src/db/mssql.rs b/src-tauri/src/db/mssql.rs index 8972e40c..0dfc9e5e 100644 --- a/src-tauri/src/db/mssql.rs +++ b/src-tauri/src/db/mssql.rs @@ -370,7 +370,7 @@ pub async fn list_tables(handle: &MssqlHandle, schema: &str) -> Result(1).ok().flatten().unwrap_or("table").to_string(); let row_count = r.try_get::(2).ok().flatten().unwrap_or(-1); let created_at = r.try_get::<&str, _>(3).ok().flatten().map(str::to_string); - Some(TableInfo { name, kind, row_count, rls_enabled: None, created_at, create_order: None }) + Some(TableInfo { name, kind, row_count, rls_enabled: None, created_at, create_order: None, system: None }) }) .collect()) } diff --git a/src-tauri/src/db/mysql.rs b/src-tauri/src/db/mysql.rs index cf7bfe3a..ad26cc14 100644 --- a/src-tauri/src/db/mysql.rs +++ b/src-tauri/src/db/mysql.rs @@ -247,7 +247,38 @@ struct WhereClause { binds: Vec, } +/// The category Postgres would give a MySQL DATA_TYPE, for the search's type +/// pruning (query::column_could_contain): N numeric, D date/time, S the rest. +fn search_category(data_type: &str) -> char { + match data_type.to_ascii_lowercase().as_str() { + "tinyint" | "smallint" | "mediumint" | "int" | "integer" | "bigint" | "decimal" | "numeric" | "float" | "double" | "real" | "bit" | "year" => 'N', + "date" | "datetime" | "timestamp" | "time" => 'D', + _ => 'S', + } +} + +/// The columns a plain search should read: those whose type can print the term. +/// A word never appears in an INT or a DATETIME, and casting every one of them +/// to text on every row was most of a search's time on a large table. +fn searchable_columns(columns: &[(String, String)], term: &str) -> Vec { + columns + .iter() + .filter(|(name, ty)| { + let col = super::query::SearchColumn { name: name.clone(), category: search_category(ty), type_name: ty.to_ascii_lowercase() }; + super::query::column_could_contain(&col, term) + }) + .map(|(n, _)| n.clone()) + .collect() +} + +#[cfg(test)] fn build_where(columns: &[String], search: Option<&str>, search_is_regex: bool, search_case_sensitive: bool, filters: &[RowFilter]) -> Result { + build_where_searching(columns, None, search, search_is_regex, search_case_sensitive, filters) +} + +/// build_where with the columns a plain search reads (see searchable_columns); +/// None searches every column, as before. +fn build_where_searching(columns: &[String], search_columns: Option<&[String]>, search: Option<&str>, search_is_regex: bool, search_case_sensitive: bool, filters: &[RowFilter]) -> Result { // (conjunct - None for first condition, Some("AND"/"OR") for subsequent) let mut cond_parts: Vec<(Option<&'static str>, String)> = Vec::new(); let mut binds: Vec = Vec::new(); @@ -258,7 +289,11 @@ fn build_where(columns: &[String], search: Option<&str>, search_is_regex: bool, // REGEXP operator; the pattern arrives pre-built from the frontend. let cast = if search_case_sensitive { "BINARY" } else { "CHAR" }; let pattern = if search_is_regex { term.to_string() } else { format!("%{}%", escape_like(term)) }; - let parts: Vec = columns + let searched: &[String] = match (search_is_regex, search_columns) { + (false, Some(cols)) => cols, + _ => columns, + }; + let parts: Vec = searched .iter() .map(|c| { let qc = bt(c); @@ -268,7 +303,10 @@ fn build_where(columns: &[String], search: Option<&str>, search_is_regex: bool, .collect(); if !parts.is_empty() { cond_parts.push((None, format!("({})", parts.join(" OR ")))); - for _ in columns { binds.push(pattern.clone()); } + for _ in searched { binds.push(pattern.clone()); } + } else if search_columns.is_some() && !search_is_regex && !columns.is_empty() { + // No column can hold the term: nothing matches, and nothing is scanned. + cond_parts.push((None, "FALSE".to_string())); } } @@ -429,8 +467,12 @@ pub async fn get_table_rows( // BIGINT - decoding the wrong signedness is a hard type-mismatch in sqlx. // CAST(... AS SIGNED) normalizes both to i64, which comfortably holds any // real row count. - let build = |table_columns: &[String]| -> Result<(String, String, Vec), String> { - let where_clause = build_where(table_columns, search.as_deref(), search_is_regex, search_case_sensitive, &filters)?; + let build = |table_columns: &[String], typed: Option<&[(String, String)]>| -> Result<(String, String, Vec), String> { + let search_cols = match (typed, search.as_deref().map(str::trim).filter(|t| !t.is_empty())) { + (Some(t), Some(term)) if !search_is_regex => Some(searchable_columns(t, term)), + _ => None, + }; + let where_clause = build_where_searching(table_columns, search_cols.as_deref(), search.as_deref(), search_is_regex, search_case_sensitive, &filters)?; let order_by = if let Some(col) = sort_col.as_deref() { // Validate the sort column against the fetched columns so an unknown // name never reaches the query (mirrors the Postgres ensure_column check). @@ -475,11 +517,15 @@ pub async fn get_table_rows( // column list, so it has to land first. let meta_rows = meta_q.fetch_all(pool).await.map_err(|e| format!("Failed to load columns: {e}"))?; let names: Vec = meta_rows.iter().filter_map(|r| my_text(r, 0)).collect(); - let (count_sql, data_sql, binds) = build(&names)?; + let typed: Vec<(String, String)> = meta_rows + .iter() + .filter_map(|r| Some((my_text(r, 0)?, my_text(r, 1).unwrap_or_default()))) + .collect(); + let (count_sql, data_sql, binds) = build(&names, Some(&typed))?; let (page, meta) = tokio::join!(run_page(count_sql, data_sql, binds), pk_fk); (meta_rows, page, meta) } else { - let (count_sql, data_sql, binds) = build(&[])?; + let (count_sql, data_sql, binds) = build(&[], None)?; let (meta_rows, page, meta) = tokio::join!(meta_q.fetch_all(pool), run_page(count_sql, data_sql, binds), pk_fk); (meta_rows.map_err(|e| format!("Failed to load columns: {e}"))?, page, meta) }; @@ -631,6 +677,117 @@ pub async fn execute_sql( } } + // A script (`CREATE DATABASE x; USE x; CREATE TABLE …`) cannot go through + // the prepared protocol: MySQL answers 1064 and TiDB 8115 "Can not prepare + // multiple statements", so the whole call failed. Run its statements one + // by one on this one connection instead, which also keeps a `USE` in the + // script in effect for the statements after it. + let stmts = super::query::split_sql_statements(sql); + let switches_db = stmts.iter().any(|s| super::sql_util::statement_head(s) == "use"); + let result = if stmts.len() > 1 { + run_script_on(&mut *conn, &stmts, started).await + } else { + run_one_on(&mut *conn, sql).await.map(|mut out| { + if switches_db { + // Each call takes a connection from the pool, so a lone USE is + // gone by the next call. Say so, rather than let the next CREATE + // TABLE land in the connection's own database without a word. + out.message = Some(USE_DOES_NOT_PERSIST.to_string()); + } + out.query_ms = query_ms(); + out + }) + }; + if switches_db { + restore_default_database(pool, &mut conn).await; + } + result +} + +/// Put a connection that ran `USE` back on the pool's own database. Left as it +/// was, the connection went back to the pool still pointed at the other +/// database, and whichever later call happened to draw it ran there: a +/// `CREATE TABLE orders … REFERENCES customers` failed with "Table +/// 'ecommerce.customers' doesn't exist" while the call before it, on another +/// connection, had made `customers` in the connection's database. When there +/// is no default to go back to, the connection is closed instead of reused. +async fn restore_default_database(pool: &MySqlPool, conn: &mut sqlx::pool::PoolConnection) { + let opts = pool.connect_options(); + let restored = match opts.get_database().filter(|d| !d.is_empty()) { + Some(db) => sqlx::Executor::execute(&mut **conn, format!("USE {}", bt(db)).as_str()).await.is_ok(), + None => false, + }; + if !restored { + conn.close_on_drop(); + } +} + +/// What a lone `USE db` reports: it switched one pooled connection, not the session. +pub(crate) const USE_DOES_NOT_PERSIST: &str = "USE only applies to this call: every call runs on a pooled connection. \ + Qualify names (`db`.`table`), put USE in the same call as the statements that need it, \ + or switch database from Stroke's database picker."; + +/// The statements of a script, in order, on one connection. Stops at the +/// first failure and says which statement it was and that the ones before it +/// already ran (MySQL commits DDL implicitly, so there is nothing to roll back). +/// The last statement that returned rows is the result; otherwise the summed +/// affected-row count. +async fn run_script_on( + conn: &mut sqlx::MySqlConnection, + stmts: &[String], + started: Instant, +) -> Result { + let n = stmts.len(); + let mut affected: i64 = 0; + let mut last_rows: Option = None; + for (i, stmt) in stmts.iter().enumerate() { + match run_one_on(conn, stmt).await { + Ok(r) if !r.columns.is_empty() => last_rows = Some(r), + Ok(r) => affected += r.row_count.unwrap_or(0), + Err(e) => { + let done = if i == 0 { + "Nothing ran.".to_string() + } else if i == 1 { + "Statement 1 already ran.".to_string() + } else { + format!("Statements 1-{i} already ran.") + }; + let e = e.trim_start_matches("Statement failed: ").trim_start_matches("Query failed: "); + return Err(format!("Statement {} of {n} failed: {e}\nFailed statement: {}\n{done}", i + 1, first_line(stmt))); + } + } + } + let query_ms = started.elapsed().as_millis() as u64; + let joined = stmts.join(";\n"); + if let Some(mut r) = last_rows { + r.query_ms = query_ms; + r.sql = joined; + return Ok(r); + } + Ok(SqlResult { + columns: vec![], + rows: vec![], + row_count: Some(affected), + message: Some(format!("{n} statements ran, {affected} row(s) affected")), + query_ms, + sql: joined, + }) +} + +/// A statement's first line, cut to 120 characters, for an error message. +fn first_line(sql: &str) -> String { + let line = sql.trim().lines().next().unwrap_or(""); + match line.char_indices().nth(120) { + Some((at, _)) => format!("{}…", &line[..at]), + None => line.to_string(), + } +} + +/// One statement on `conn`: prepared first, then the text protocol for the +/// statements the server refuses to prepare. +async fn run_one_on(conn: &mut sqlx::MySqlConnection, sql: &str) -> Result { + let started = Instant::now(); + let query_ms = || started.elapsed().as_millis() as u64; let head = super::sql_util::statement_head(sql); let is_select = matches!(head.as_str(), "select" | "show" | "explain" | "describe" | "desc" | "with" | "call"); @@ -676,11 +833,12 @@ pub async fn execute_sql( /// MySQL refuses to prepare some statements: CREATE / DROP PROCEDURE, /// FUNCTION, TRIGGER and EVENT, SHOW CREATE TRIGGER and a few more (error /// 1295). `sqlx::query` always prepares, so those are run again as a bare -/// `&str`, which sqlx sends over the text protocol. +/// `&str`, which sqlx sends over the text protocol. TiDB refuses a script +/// with 8115 ("Can not prepare multiple statements"). fn unsupported_in_prepared(e: &sqlx::Error) -> bool { e.as_database_error() .and_then(|d| d.try_downcast_ref::()) - .is_some_and(|m| m.number() == 1295) + .is_some_and(|m| matches!(m.number(), 1295 | 8115)) } /// Rows of a result as JSON, converted as they stream in so the driver rows @@ -1123,3 +1281,130 @@ mod mysql_live { sqlx::query("DROP TABLE stroke_empty_probe").execute(&pool).await.unwrap(); } } + +/// The AI chat's `execute_sql` path (`open_mysql` pool with its after-connect +/// session setup, then `execute_sql`), run with the statements a model sends +/// for "create the ecommerce db and seed". Point it at MySQL or TiDB: +/// `STROKE_AI_MYSQL=127.0.0.1:4000:root::test cargo test --lib ai_tool_live -- --ignored --nocapture` +/// (host:port:user:password:database). +#[cfg(test)] +mod ai_tool_live { + use super::*; + use crate::db::connection::{open_mysql, MysqlConfig}; + + fn config() -> Option { + let spec = std::env::var("STROKE_AI_MYSQL").ok()?; + let p: Vec<&str> = spec.splitn(5, ':').collect(); + Some(MysqlConfig { + name: "ai".into(), + host: p[0].into(), + port: p[1].parse().unwrap(), + user: p[2].into(), + password: p[3].into(), + database: p[4].into(), + ssl: false, + ssl_mode: None, + ssl_root_cert: None, + ssh: None, + timezone: None, + }) + } + + #[tokio::test(flavor = "multi_thread")] + #[ignore] + async fn ai_tool_live_ecommerce_script() { + let Some(cfg) = config() else { return }; + let pool = open_mysql(&cfg).await.expect("connect"); + let tables = ["order_items", "orders", "products", "customers", "a1", "a2"]; + for t in tables { + let _ = execute_sql(&pool, &format!("DROP TABLE IF EXISTS `{t}`"), None).await; + } + let _ = execute_sql(&pool, "DROP DATABASE IF EXISTS ecommerce", None).await; + let steps = [ + "CREATE TABLE `customers` (\n `id` INT AUTO_INCREMENT PRIMARY KEY,\n `name` VARCHAR(100) NOT NULL,\n `email` VARCHAR(255) NOT NULL UNIQUE,\n `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n)", + "CREATE DATABASE ecommerce", + "CREATE TABLE `products` (`id` INT AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(200) NOT NULL, `price` DECIMAL(10,2) NOT NULL, `stock` INT NOT NULL DEFAULT 0)", + "CREATE TABLE `orders` (`id` INT AUTO_INCREMENT PRIMARY KEY, `customer_id` INT NOT NULL, `status` VARCHAR(20) NOT NULL DEFAULT 'pending', `ordered_at` DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (`customer_id`) REFERENCES `customers`(`id`))", + "CREATE TABLE `order_items` (`id` INT AUTO_INCREMENT PRIMARY KEY, `order_id` INT NOT NULL, `product_id` INT NOT NULL, `quantity` INT NOT NULL, `unit_price` DECIMAL(10,2) NOT NULL, FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`), FOREIGN KEY (`product_id`) REFERENCES `products`(`id`))", + "INSERT INTO `customers` (`name`, `email`) VALUES ('Alice Smith', 'alice@example.com'), ('Bob Jones', 'bob@example.com')", + "INSERT INTO `products` (`name`, `price`, `stock`) VALUES ('Laptop', 999.99, 10), ('Mouse', 19.99, 200)", + "INSERT INTO `orders` (`customer_id`, `status`) VALUES (1, 'shipped'), (2, 'pending')", + "INSERT INTO `order_items` (`order_id`, `product_id`, `quantity`, `unit_price`) VALUES (1, 1, 1, 999.99), (1, 2, 2, 19.99), (2, 2, 1, 19.99)", + // A script in one call: refused by the prepared protocol before. + "CREATE TABLE a1 (id INT PRIMARY KEY); CREATE TABLE a2 (id INT PRIMARY KEY);", + "CREATE TABLE ecommerce.t1 (id INT PRIMARY KEY)", + // USE inside the script holds for the rest of it. + "USE ecommerce; CREATE TABLE t2 (id INT PRIMARY KEY); INSERT INTO t2 VALUES (1), (2)", + ]; + let mut failed = Vec::new(); + for sql in steps { + match execute_sql(&pool, sql, None).await { + Ok(r) => println!("OK {:?} :: {}", r.message, sql.lines().next().unwrap()), + Err(e) => { + println!("FAIL {e} :: {}", sql.lines().next().unwrap()); + failed.push((sql, e)); + } + } + } + let t2 = execute_sql(&pool, "SELECT COUNT(*) FROM ecommerce.t2", None).await; + // A lone USE says it does not carry over to the next call. + let used = execute_sql(&pool, "USE ecommerce", None).await.unwrap(); + // A failing script says which statement failed and what already ran. + let partial = execute_sql(&pool, "INSERT INTO a1 VALUES (1); INSERT INTO nope VALUES (1); INSERT INTO a1 VALUES (2)", None).await; + // ANSI_QUOTES is on: a double-quoted value is a column name. + let quoted = execute_sql(&pool, "INSERT INTO customers (name, email) VALUES (\"Carol\", \"c@x.io\")", None).await; + for t in tables { + let _ = execute_sql(&pool, &format!("DROP TABLE IF EXISTS `{t}`"), None).await; + } + let _ = execute_sql(&pool, "DROP DATABASE IF EXISTS ecommerce", None).await; + assert!(failed.is_empty(), "{failed:#?}"); + assert_eq!(t2.unwrap().rows[0][0], json!(2)); + assert_eq!(used.message.as_deref(), Some(USE_DOES_NOT_PERSIST)); + let partial = partial.unwrap_err(); + println!("{partial}"); + assert!(partial.starts_with("Statement 2 of 3 failed") && partial.contains("Statement 1 already ran"), "{partial}"); + let quoted = quoted.unwrap_err(); + println!("{quoted}"); + assert!(quoted.contains("Unknown column"), "{quoted}"); + } +} + +#[cfg(test)] +mod search_pruning_tests { + use super::*; + + fn typed() -> Vec<(String, String)> { + vec![ + ("id".into(), "int".into()), + ("name".into(), "varchar".into()), + ("price".into(), "decimal".into()), + ("created_at".into(), "datetime".into()), + ("meta".into(), "json".into()), + ] + } + + #[test] + fn a_word_reads_only_text_columns() { + assert_eq!(searchable_columns(&typed(), "Product"), vec!["name".to_string(), "meta".to_string()]); + } + + #[test] + fn digits_read_numbers_and_dates_too() { + assert_eq!(searchable_columns(&typed(), "2026"), vec!["id", "name", "price", "created_at", "meta"]); + } + + #[test] + fn nothing_searchable_is_false_not_unfiltered() { + let names: Vec = vec!["id".into(), "created_at".into()]; + let w = build_where_searching(&names, Some(&[]), Some("abc"), false, false, &[]).unwrap(); + assert!(w.sql.contains("FALSE"), "{}", w.sql); + assert!(w.binds.is_empty()); + } + + #[test] + fn untyped_search_is_unchanged() { + let names: Vec = vec!["id".into(), "name".into()]; + let a = build_where(&names, Some("x"), false, false, &[]).unwrap(); + assert_eq!(a.binds.len(), 2); + } +} diff --git a/src-tauri/src/db/objects.rs b/src-tauri/src/db/objects.rs index 1fa4d226..4f3d7cc4 100644 --- a/src-tauri/src/db/objects.rs +++ b/src-tauri/src/db/objects.rs @@ -11,12 +11,12 @@ //! Postgres routine's argument list for DROP is read back from the catalog //! rather than taken from the caller. +use crate::db::connection::Db; use serde::Serialize; use serde_json::Value; use sqlx::{MySqlPool, PgPool, Row, SqlitePool}; -use tauri::State; -use super::connection::{require_conn, ActiveConnection, D1Config, DbState, LibSqlConfig}; +use super::connection::{require_conn, ActiveConnection, D1Config, LibSqlConfig}; use super::mysql::my_text_named; use super::query::SqlResult; use super::sql_util::{esc_backslash_quote, esc_single_quote, quote_backtick, quote_bracket, quote_double}; @@ -72,7 +72,7 @@ pub fn kinds_for(conn: &ActiveConnection) -> &'static [&'static str] { ActiveConnection::Duckdb(_) => &["view", "function", "sequence"], // Materialized views come back from list_tables as views here. ActiveConnection::Clickhouse(_) => &["view", "function"], - ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => &[], + ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) | ActiveConnection::Oracle(_) => &[], } } @@ -232,7 +232,7 @@ fn trigger_row(name: String, table: String, detail: String, comment: Option, schema: String) -> Result { +pub async fn list_db_objects(state: Db<'_>, schema: String) -> Result { let conn = require_conn(&state)?; let kinds: Vec = kinds_for(&conn).iter().map(|k| k.to_string()).collect(); let cascade = matches!(conn, ActiveConnection::Postgres(_)); @@ -245,7 +245,7 @@ pub async fn list_db_objects(state: State<'_, DbState>, schema: String) -> Resul ActiveConnection::LibSql(cfg) => sqlite_triggers_from(libsql_query(cfg, SQLITE_TRIGGERS, vec![]).await?), ActiveConnection::Duckdb(h) => list_duckdb(h, &schema).await?, ActiveConnection::Clickhouse(cfg) => list_clickhouse(cfg).await?, - ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => vec![], + ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) | ActiveConnection::Oracle(_) => vec![], }; Ok(ObjectListing { kinds, cascade, objects }) } @@ -654,7 +654,7 @@ async fn list_clickhouse(cfg: &super::connection::ClickhouseConfig) -> Result, + state: Db<'_>, kind: String, schema: String, name: String, @@ -689,7 +689,7 @@ pub async fn get_object_definition( } ActiveConnection::Duckdb(h) => def_duckdb(h, &kind, &schema, &name).await?, ActiveConnection::Clickhouse(cfg) => def_clickhouse(cfg, &kind, &schema, &name).await?, - ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => return Err(unsupported(&kind)), + ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) | ActiveConnection::Oracle(_) => return Err(unsupported(&kind)), }; Ok(format!("{}\n", def.trim_end())) } @@ -1041,7 +1041,7 @@ async fn def_clickhouse(cfg: &super::connection::ClickhouseConfig, kind: &str, d /// Drop one object. Returns the statement that ran, for the log. #[allow(clippy::too_many_arguments)] pub async fn drop_object( - state: State<'_, DbState>, + state: Db<'_>, kind: String, schema: String, name: String, @@ -1147,7 +1147,7 @@ pub async fn drop_object( super::clickhouse::query(cfg, &sql).await.map_err(fail)?; Ok(sql) } - ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Err(unsupported(&kind)), + ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) | ActiveConnection::Oracle(_) => Err(unsupported(&kind)), } } @@ -1155,7 +1155,7 @@ pub async fn drop_object( /// Table and view comments for one schema, in one query. Engines that keep /// none (SQLite and its relatives) return an empty list. -pub async fn list_object_comments(state: State<'_, DbState>, schema: String) -> Result, String> { +pub async fn list_object_comments(state: Db<'_>, schema: String) -> Result, String> { let pairs = |r: SqlResult| -> Vec { r.rows .iter() diff --git a/src-tauri/src/db/oracle.rs b/src-tauri/src/db/oracle.rs new file mode 100644 index 00000000..c9c3872f --- /dev/null +++ b/src-tauri/src/db/oracle.rs @@ -0,0 +1,2321 @@ +//! Oracle Database, through the `oracle` crate (rust-oracle). +//! +//! The crate compiles ODPI-C from source, so building Stroke needs no Oracle +//! files. At runtime ODPI-C loads the Oracle Client library (Instant Client) +//! the first time an Oracle connection opens; when it is missing the user gets +//! `INSTANT_CLIENT_HELP` instead of a crash, and every other engine is +//! unaffected. +//! +//! The driver is synchronous. Every entry point hops onto a blocking thread and +//! holds the session mutex only inside it, never across an `.await`. + +use super::connection::{OracleConfig, OracleHandle}; +use super::query::{ColumnInfo, ForeignKeyInfo, RowFilter, SqlResult, TableRows}; +use super::schema::{ColumnStructureRow, IncomingForeignKey, IndexInfo, TableColumnStructure, TableInfo}; +use oracle::sql_type::{OracleType, Timestamp, ToSql}; +use oracle::{Connection, Connector, InitParams, SqlValue}; +use serde_json::Value; +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::sync::Mutex; +use std::time::Instant; + +/// One Oracle session. `lock` serializes statements; `conn` is reached +/// without it only to interrupt the running call (`OCIBreak` is safe from +/// another thread while the session is busy). The driver's autocommit stays +/// off: `run_commit` commits after each write, so a read-only run and a +/// multi-row delete can hold one transaction open. +pub struct OracleSession { + conn: Connection, + lock: Mutex<()>, + /// What the grid needs to know about a table besides its rows, by + /// (schema, table). The ALL_* dictionary views behind it are the slow part + /// of a page fetch (seconds for a user who sees every schema), and none of + /// it changes when you page, sort or filter. + meta: Mutex)>>, +} + +struct TableMeta { + structure: Vec, + is_table: bool, + foreign_keys: Vec, +} + +/// A table changed by another client shows up after at most this long. +const META_TTL: std::time::Duration = std::time::Duration::from_secs(60); + +impl OracleSession { + fn cached_meta(&self, key: &(String, String)) -> Option> { + let map = self.meta.lock().ok()?; + map.get(key).filter(|(at, _)| at.elapsed() < META_TTL).map(|(_, m)| m.clone()) + } + + fn store_meta(&self, key: (String, String), meta: std::sync::Arc) { + if let Ok(mut map) = self.meta.lock() { + map.insert(key, (Instant::now(), meta)); + } + } + + /// Forget every table's catalog facts, after a statement that may have + /// changed them. + fn forget_meta(&self) { + if let Ok(mut map) = self.meta.lock() { + map.clear(); + } + } +} + +/// Whether a statement only reads: anything else (DDL, DML, PL/SQL) may +/// change a table's columns, keys or comments. +fn reads_only(stmt: &str) -> bool { + let head = stmt.trim_start().split(|c: char| !c.is_ascii_alphabetic()).next().unwrap_or(""); + head.eq_ignore_ascii_case("select") || head.eq_ignore_ascii_case("with") +} + +// ── Oracle Client library ──────────────────────────────────────────────────── + +/// Shown when ODPI-C cannot load the Oracle Client library (DPI-1047). +pub const INSTANT_CLIENT_HELP: &str = "Oracle Instant Client is required to connect to Oracle, and it was not found. \ +Download the Basic or Basic Light package for your platform from \ +https://www.oracle.com/database/technologies/instant-client/downloads.html, unzip it, \ +then set \"Instant Client folder\" on this connection (or the STROKE_ORACLE_CLIENT_DIR \ +environment variable) to the unzipped folder. On Linux, also install libaio \ +(libaio1 / libaio1t64)."; + +#[cfg(target_os = "windows")] +const CLIENT_LIB: &str = "oci.dll"; +#[cfg(target_os = "macos")] +const CLIENT_LIB: &str = "libclntsh.dylib"; +#[cfg(not(any(target_os = "windows", target_os = "macos")))] +const CLIENT_LIB: &str = "libclntsh.so"; + +/// Folders an unzipped Instant Client usually lands in, newest first. +pub(crate) fn client_dir_candidates(explicit: &str) -> Vec { + let mut out: Vec = Vec::new(); + let explicit = explicit.trim(); + if !explicit.is_empty() { + out.push(PathBuf::from(explicit)); + } + if let Ok(dir) = std::env::var("STROKE_ORACLE_CLIENT_DIR") { + if !dir.trim().is_empty() { + out.push(PathBuf::from(dir.trim())); + } + } + if let Ok(home) = std::env::var("ORACLE_HOME") { + let home = PathBuf::from(home); + out.push(home.join("lib")); + out.push(home.join("bin")); + out.push(home); + } + let mut parents: Vec = Vec::new(); + if let Some(home) = dirs_home() { + parents.push(home.clone()); + // Not on macOS: Downloads is a protected folder there, and listing it + // (even just to look for an `instantclient_*`) makes macOS ask whether + // Stroke may read your Downloads, at launch, for a client most people + // never use. Unzipped there, it is found once its path is set in the + // connection or STROKE_ORACLE_CLIENT_DIR. + if !cfg!(target_os = "macos") { + parents.push(home.join("Downloads")); + } + parents.push(home.join("oracle")); + } + if cfg!(target_os = "windows") { + parents.push(PathBuf::from("C:\\oracle")); + parents.push(PathBuf::from("C:\\")); + } else { + parents.push(PathBuf::from("/opt/oracle")); + parents.push(PathBuf::from("/usr/local/lib")); + parents.push(PathBuf::from("/opt/homebrew/lib")); + } + for parent in parents { + let Ok(rd) = std::fs::read_dir(&parent) else { continue }; + let mut found: Vec = rd + .flatten() + .map(|e| e.path()) + .filter(|p| { + p.is_dir() + && p.file_name() + .and_then(|n| n.to_str()) + .is_some_and(|n| n.to_ascii_lowercase().starts_with("instantclient")) + }) + .collect(); + // instantclient_23_26 before instantclient_19_8. + found.sort_by(|a, b| version_key(b).cmp(&version_key(a))); + out.extend(found); + } + // Linux RPM installs. + if let Ok(rd) = std::fs::read_dir("/usr/lib/oracle") { + for e in rd.flatten() { + out.push(e.path().join("client64").join("lib")); + } + } + out +} + +fn dirs_home() -> Option { + std::env::var_os(if cfg!(target_os = "windows") { "USERPROFILE" } else { "HOME" }).map(PathBuf::from) +} + +fn version_key(p: &Path) -> Vec { + p.file_name() + .and_then(|n| n.to_str()) + .unwrap_or("") + .split(|c: char| !c.is_ascii_digit()) + .filter_map(|s| s.parse().ok()) + .collect() +} + +/// Load the Oracle Client library once per process. A failed load is not +/// cached, so installing Instant Client and connecting again works without a +/// restart. +fn ensure_client(explicit_dir: &str) -> Result<(), String> { + if InitParams::is_initialized() { + return Ok(()); + } + let explicit = explicit_dir.trim(); + let mut first_err: Option = None; + let mut candidates = client_dir_candidates(explicit).into_iter(); + // A folder set on the connection comes first; then the platform's own + // search path (ldconfig / LD_LIBRARY_PATH, PATH, DYLD paths); then the + // usual unzip locations. + let try_dir = |dir: &Path, first_err: &mut Option| -> bool { + if !dir.join(CLIENT_LIB).is_file() { + if !explicit.is_empty() && dir == Path::new(explicit) && first_err.is_none() { + *first_err = Some(format!( + "No {CLIENT_LIB} in the Instant Client folder \"{}\". Point it at the folder you unzipped Instant Client into.", + dir.display() + )); + } + return false; + } + let mut params = InitParams::new(); + match params.oracle_client_lib_dir(dir.as_os_str().to_owned()).and_then(|p| p.init()) { + Ok(_) => true, + Err(e) => { + log::warn!("oracle: could not load the client from {}: {e}", dir.display()); + if first_err.is_none() { + *first_err = Some(load_error(dir, &clean_err(&e.to_string()))); + } + false + } + } + }; + if !explicit.is_empty() { + if let Some(dir) = candidates.next() { + if try_dir(&dir, &mut first_err) { + return Ok(()); + } + } + } + let default_err = match InitParams::new().init() { + Ok(_) => return Ok(()), + Err(e) => clean_err(&e.to_string()), + }; + for dir in candidates { + if try_dir(&dir, &mut first_err) { + return Ok(()); + } + } + Err(match first_err { + Some(found) => found, + None if default_err.contains("DPI-1047") => format!("{INSTANT_CLIENT_HELP} ({default_err})"), + None => format!("Oracle client initialization failed: {default_err}"), + }) +} + +/// Why Instant Client in `dir` would not load, and what fixes it. +fn load_error(dir: &Path, msg: &str) -> String { + let hint = if msg.contains("libaio") { + " Instant Client needs libaio: install it (libaio1, or libaio1t64 on Ubuntu 24.04 and later, where \ + libaio.so.1 also needs a link: sudo ln -s /usr/lib/x86_64-linux-gnu/libaio.so.1t64 /usr/lib/x86_64-linux-gnu/libaio.so.1)." + .to_string() + } else if cfg!(target_os = "linux") && msg.contains("cannot open shared object file") { + // Linux Instant Client libraries carry no $ORIGIN run path, so their + // own dependencies (libnnz.so) resolve only through the linker's path. + format!( + " On Linux the Instant Client folder must also be on the library path: run \ + sudo sh -c 'echo {} > /etc/ld.so.conf.d/oracle-instantclient.conf' && sudo ldconfig, then restart Stroke \ + (or start Stroke with LD_LIBRARY_PATH set to that folder).", + dir.display() + ) + } else { + String::new() + }; + format!("Could not load Oracle Instant Client from \"{}\": {msg}{hint}", dir.display()) +} + +// ── Connect ────────────────────────────────────────────────────────────────── + +/// The connect string ODPI-C gets: the connection's own Easy Connect / TNS +/// string when set, else one built from host, port and service name or SID. +pub fn connect_string(cfg: &OracleConfig) -> String { + let custom = cfg.connect_string.trim(); + if !custom.is_empty() { + return custom.to_string(); + } + let host = cfg.host.trim(); + let port = if cfg.port == 0 { 1521 } else { cfg.port }; + let sid = cfg.sid.trim(); + if !sid.is_empty() { + // Easy Connect has no SID form, so a SID goes in a full descriptor. + return format!( + "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST={host})(PORT={port}))(CONNECT_DATA=(SID={sid})))" + ); + } + let service = cfg.service_name.trim(); + if service.is_empty() { + format!("//{host}:{port}") + } else { + format!("//{host}:{port}/{service}") + } +} + +/// Host and port to probe before the real connect, when the connect string +/// names them. A tnsnames.ora alias names neither, and is not probed. +pub fn probe_target(cfg: &OracleConfig) -> Option<(String, u16)> { + let cs = connect_string(cfg); + let upper = cs.to_ascii_uppercase(); + if upper.contains("(HOST=") || upper.contains("(HOST =") { + let host = descriptor_value(&cs, "HOST")?; + let port = descriptor_value(&cs, "PORT").and_then(|p| p.parse().ok()).unwrap_or(1521); + return Some((host, port)); + } + // Easy Connect: [tcp[s]://]host[:port][/service][:server][/instance][?params] + let mut rest = cs.as_str(); + for prefix in ["tcps://", "tcp://", "TCPS://", "TCP://"] { + if let Some(r) = rest.strip_prefix(prefix) { + rest = r; + } + } + let rest = rest.trim_start_matches("//"); + if rest.contains('(') || rest.contains(' ') { + return None; + } + let addr = rest.split(['/', '?']).next()?; + if addr.is_empty() { + return None; + } + // [::1]:1521 + if let Some(v6) = addr.strip_prefix('[') { + let (host, tail) = v6.split_once(']')?; + let port = tail.strip_prefix(':').and_then(|p| p.parse().ok()).unwrap_or(1521); + return Some((host.to_string(), port)); + } + match addr.split_once(':') { + Some((h, p)) => Some((h.to_string(), p.parse().ok()?)), + // No port and no slash: a bare word is a tnsnames alias, not a host. + None if rest.contains('/') => Some((addr.to_string(), 1521)), + None => None, + } +} + +fn descriptor_value(desc: &str, key: &str) -> Option { + let upper = desc.to_ascii_uppercase(); + let at = upper.find(&format!("({key}"))?; + let after = &desc[at + key.len() + 1..]; + let after = after.trim_start().strip_prefix('=')?; + let end = after.find(')')?; + Some(after[..end].trim().to_string()) +} + +/// Session settings every Oracle connection starts with: dates and timestamps +/// read and written as ISO text (what the grid shows is what an edit binds +/// back), and `.` as the decimal separator whatever the client locale. +const SESSION_SETUP: &str = "ALTER SESSION SET \ + NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS' \ + NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF' \ + NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS.FF TZH:TZM' \ + NLS_NUMERIC_CHARACTERS = '.,'"; + +/// Load the Oracle Client library before anything touches the network, so a +/// missing Instant Client is the error the user sees - not a reachability +/// verdict about a server it could never have talked to anyway. +pub async fn prepare_client(explicit_dir: &str) -> Result<(), String> { + let dir = explicit_dir.to_string(); + tokio::task::spawn_blocking(move || ensure_client(&dir)) + .await + .map_err(|e| format!("Oracle client task failed: {e}"))? +} + +/// Open a session (blocking). Callers bound it with `connect_racing_probe`. +pub async fn connect(cfg: &OracleConfig) -> Result { + let cfg = cfg.clone(); + tokio::task::spawn_blocking(move || { + ensure_client(&cfg.client_lib_dir)?; + let cs = connect_string(&cfg); + let conn = Connector::new(cfg.user.trim(), cfg.password.as_str(), cs.as_str()) + .driver_name("Stroke") + .connect() + .map_err(|e| explain_connect_error(&e.to_string()))?; + conn.execute(SESSION_SETUP, &[]).map_err(|e| format!("Oracle session setup failed: {}", clean_err(&e.to_string())))?; + Ok(std::sync::Arc::new(OracleSession { conn, lock: Mutex::new(()), meta: Mutex::new(HashMap::new()) })) + }) + .await + .map_err(|e| format!("Oracle connect task failed: {e}"))? +} + +fn explain_connect_error(err: &str) -> String { + let e = clean_err(err); + if e.contains("DPI-1047") { + return format!("{INSTANT_CLIENT_HELP} ({e})"); + } + if e.contains("ORA-12514") { + return format!("Oracle connection failed: the listener does not know that service name. Check the service name (e.g. FREEPDB1, XEPDB1, ORCLPDB1), or connect by SID. ({e})"); + } + if e.contains("ORA-12505") { + return format!("Oracle connection failed: the listener does not know that SID. Check the SID, or connect by service name. ({e})"); + } + if e.contains("ORA-01017") { + return format!("Oracle connection failed: invalid username or password. ({e})"); + } + if e.contains("ORA-12541") { + return format!("Oracle connection failed: no listener at that host and port. ({e})"); + } + format!("Oracle connection failed: {e}") +} + +/// The driver's error text without its "OCI Error: " / "DPI Error: " prefix. +fn clean_err(e: &str) -> String { + e.trim_start_matches("OCI Error: ").trim_start_matches("DPI Error: ").trim().to_string() +} + +fn ora_err(e: oracle::Error) -> String { + let msg = clean_err(&e.to_string()); + if msg.contains("ORA-01013") { + return super::query::QUERY_CANCELLED.to_string(); + } + format!("Oracle error: {msg}") +} + +/// Run a closure against the locked session on a blocking thread. +async fn run_blocking(handle: &OracleHandle, f: F) -> Result +where + F: FnOnce(&Connection) -> Result + Send + 'static, + T: Send + 'static, +{ + let h = handle.clone(); + tokio::task::spawn_blocking(move || { + let _guard = h.lock.lock().map_err(|_| "Oracle session lock poisoned".to_string())?; + f(&h.conn) + }) + .await + .map_err(|e| format!("Oracle task failed: {e}"))? +} + +/// The container this session is in: a pluggable database (FREEPDB1) or the +/// root (CDB$ROOT). On 11g, which has no containers, the database name. +pub async fn container_name(handle: &OracleHandle) -> Result { + let r = match query_binds(handle, "SELECT SYS_CONTEXT('USERENV', 'CON_NAME') FROM DUAL".into(), vec![]).await { + Ok(r) => r, + Err(_) => query_binds(handle, "SELECT SYS_CONTEXT('USERENV', 'DB_NAME') FROM DUAL".into(), vec![]).await?, + }; + Ok(r.rows.first().and_then(|row| text(row, 0)).unwrap_or_default()) +} + +pub async fn ping(handle: &OracleHandle) -> Result<(), String> { + run_blocking(handle, |conn| { + conn.query_row("SELECT 1 FROM DUAL", &[]).map(|_| ()).map_err(|e| format!("Oracle ping failed: {}", clean_err(&e.to_string()))) + }) + .await +} + +/// Interrupt whatever the session is running (Stop in the SQL editor). The +/// interrupted call fails with ORA-01013, which `ora_err` reports as a cancel. +pub async fn cancel(handle: &OracleHandle) { + let h = handle.clone(); + let _ = tokio::task::spawn_blocking(move || h.conn.break_execution()).await; +} + +// ── Identifiers and literals ───────────────────────────────────────────────── + +/// `"name"`: Oracle folds unquoted names to upper case, so catalog names are +/// always quoted exactly as stored. +pub fn quote_ident(ident: &str) -> String { + super::sql_util::quote_double(ident) +} + +fn table_ref(schema: &str, table: &str) -> String { + if schema.is_empty() { + quote_ident(table) + } else { + format!("{}.{}", quote_ident(schema), quote_ident(table)) + } +} + +// ── Statement splitting ────────────────────────────────────────────────────── + +/// Whether a statement is PL/SQL (or a stored unit's DDL), which keeps its +/// `;` and runs to a `/` line instead of ending at the first `;`. +fn is_plsql_head(words: &[String]) -> bool { + let w: Vec<&str> = words.iter().map(|s| s.as_str()).collect(); + match w.first().copied() { + Some("BEGIN") | Some("DECLARE") => true, + Some("CREATE") => { + let mut i = 1; + if w.get(i) == Some(&"OR") && w.get(i + 1) == Some(&"REPLACE") { + i += 2; + } + if matches!(w.get(i).copied(), Some("EDITIONABLE") | Some("NONEDITIONABLE") | Some("EDITIONING")) { + i += 1; + } + matches!(w.get(i).copied(), Some("FUNCTION") | Some("PROCEDURE") | Some("PACKAGE") | Some("TRIGGER") | Some("TYPE") | Some("LIBRARY")) + } + _ => false, + } +} + +/// The first few upper-cased words at `from`, skipping whitespace and comments. +fn leading_words(chars: &[char], from: usize, max: usize) -> Vec { + let mut out = Vec::new(); + let mut i = from; + while i < chars.len() && out.len() < max { + let c = chars[i]; + if c.is_whitespace() { + i += 1; + } else if c == '-' && chars.get(i + 1) == Some(&'-') { + while i < chars.len() && chars[i] != '\n' { + i += 1; + } + } else if c == '/' && chars.get(i + 1) == Some(&'*') { + i += 2; + while i + 1 < chars.len() && !(chars[i] == '*' && chars[i + 1] == '/') { + i += 1; + } + i += 2; + } else if c.is_alphanumeric() || c == '_' || c == '$' || c == '#' { + let s = i; + while i < chars.len() && (chars[i].is_alphanumeric() || chars[i] == '_' || chars[i] == '$' || chars[i] == '#') { + i += 1; + } + out.push(chars[s..i].iter().collect::().to_ascii_uppercase()); + } else { + break; + } + } + out +} + +/// Whether the line holding `i` is a lone `/` (SQL*Plus's "run the buffer"). +fn slash_line_at(chars: &[char], i: usize) -> Option { + if chars[i] != '/' { + return None; + } + let mut s = i; + while s > 0 && chars[s - 1] != '\n' { + s -= 1; + if !chars[s].is_whitespace() { + return None; + } + } + let mut e = i + 1; + while e < chars.len() && chars[e] != '\n' { + if !chars[e].is_whitespace() { + return None; + } + e += 1; + } + Some(e) +} + +/// Split a script the way SQL*Plus / SQL Developer do: SQL ends at `;` or a +/// `/` line (the `;` is dropped - OCI rejects it), PL/SQL blocks and stored +/// unit DDL run to a `/` line or the end and keep their final `;`. Strings +/// (including `q'[...]'` quoting), quoted names and comments never split. +pub fn split_statements(sql: &str) -> Vec { + let chars: Vec = sql.chars().collect(); + let n = chars.len(); + let mut out: Vec = Vec::new(); + let mut start = 0usize; + let mut plsql: Option = None; + let mut i = 0usize; + + let flush = |out: &mut Vec, from: usize, to: usize, plsql: bool| { + let text: String = chars[from..to].iter().collect(); + let mut t = text.trim().to_string(); + if !plsql { + while t.ends_with(';') { + t.pop(); + t = t.trim_end().to_string(); + } + } + let meaningful = !leading_words(&t.chars().collect::>(), 0, 1).is_empty() + || t.trim_start().starts_with('('); + if !t.is_empty() && meaningful { + out.push(t); + } + }; + + while i < n { + let c = chars[i]; + if plsql.is_none() && !c.is_whitespace() { + let words = leading_words(&chars, i, 6); + if !words.is_empty() { + plsql = Some(is_plsql_head(&words)); + } + } + match c { + '-' if chars.get(i + 1) == Some(&'-') => { + while i < n && chars[i] != '\n' { + i += 1; + } + } + '/' if chars.get(i + 1) == Some(&'*') => { + i += 2; + while i + 1 < n && !(chars[i] == '*' && chars[i + 1] == '/') { + i += 1; + } + i = (i + 2).min(n); + } + '/' => { + if let Some(end) = slash_line_at(&chars, i) { + flush(&mut out, start, i, plsql.unwrap_or(false)); + start = end; + i = end; + plsql = None; + } else { + i += 1; + } + } + 'q' | 'Q' if chars.get(i + 1) == Some(&'\'') && (i == 0 || !(chars[i - 1].is_alphanumeric() || chars[i - 1] == '_')) => { + // q'X...X' with ([{< closed by their pair. + let open = chars.get(i + 2).copied().unwrap_or('\''); + let close = match open { + '(' => ')', + '[' => ']', + '{' => '}', + '<' => '>', + other => other, + }; + i += 3; + while i < n && !(chars[i] == close && chars.get(i + 1) == Some(&'\'')) { + i += 1; + } + i = (i + 2).min(n); + } + '\'' | '"' => { + i += 1; + while i < n { + if chars[i] == c { + if chars.get(i + 1) == Some(&c) { + i += 2; + continue; + } + break; + } + i += 1; + } + i = (i + 1).min(n); + } + ';' if plsql != Some(true) => { + flush(&mut out, start, i, false); + i += 1; + start = i; + plsql = None; + } + _ => i += 1, + } + } + flush(&mut out, start, n, plsql.unwrap_or(false)); + out +} + +/// The statement with its string literals (`'...'`, `q'[...]'`) and comments +/// blanked out, for the access classifier: words inside a literal are data. +pub fn strip_literals(stmt: &str) -> String { + let chars: Vec = stmt.chars().collect(); + let n = chars.len(); + let mut out = String::with_capacity(stmt.len()); + let mut i = 0; + while i < n { + let c = chars[i]; + if c == '-' && chars.get(i + 1) == Some(&'-') { + while i < n && chars[i] != '\n' { + i += 1; + } + out.push(' '); + } else if c == '/' && chars.get(i + 1) == Some(&'*') { + i += 2; + while i + 1 < n && !(chars[i] == '*' && chars[i + 1] == '/') { + i += 1; + } + i = (i + 2).min(n); + out.push(' '); + } else if (c == 'q' || c == 'Q') + && chars.get(i + 1) == Some(&'\'') + && (i == 0 || !(chars[i - 1].is_alphanumeric() || chars[i - 1] == '_')) + { + let open = chars.get(i + 2).copied().unwrap_or('\''); + let close = match open { + '(' => ')', + '[' => ']', + '{' => '}', + '<' => '>', + other => other, + }; + i += 3; + while i < n && !(chars[i] == close && chars.get(i + 1) == Some(&'\'')) { + i += 1; + } + i = (i + 2).min(n); + out.push_str(" '' "); + } else if c == '\'' { + i += 1; + while i < n { + if chars[i] == '\'' { + if chars.get(i + 1) == Some(&'\'') { + i += 2; + continue; + } + break; + } + i += 1; + } + i = (i + 1).min(n); + out.push_str(" '' "); + } else { + out.push(c); + i += 1; + } + } + out +} + +// ── Cell conversion ────────────────────────────────────────────────────────── + +/// Grid label for a result column's type. Oracle's DATE holds a time of day +/// too, so the grid edits it as a date-time - labelled `DATE` it would get a +/// date-only editor that refuses (or drops) the time. +fn type_label(t: &OracleType) -> String { + match t { + OracleType::Date => "DATETIME".to_string(), + other => other.to_string(), + } +} + +/// NUMBER arrives as exact decimal text. Integers within JSON's safe range and +/// decimals that survive the round trip become JSON numbers; anything wider +/// stays text so no digit is lost. +fn number_json(text: &str) -> Value { + let t = text.trim(); + if let Ok(i) = t.parse::() { + if i.unsigned_abs() <= (1u64 << 53) { + return Value::from(i); + } + return Value::String(t.to_string()); + } + if let Ok(f) = t.parse::() { + if f.is_finite() { + let back = f.to_string(); + let norm = |s: &str| s.trim_start_matches('+').replace("-.", "-0.").to_string(); + let mut canon = norm(t); + if canon.starts_with('.') { + canon.insert(0, '0'); + } + if back == canon { + if let Some(n) = serde_json::Number::from_f64(f) { + return Value::Number(n); + } + } + } + } + Value::String(t.to_string()) +} + +fn fmt_timestamp(ts: &Timestamp, date_only_precision: bool, with_tz: bool) -> String { + let mut s = format!( + "{:04}-{:02}-{:02} {:02}:{:02}:{:02}", + ts.year(), + ts.month(), + ts.day(), + ts.hour(), + ts.minute(), + ts.second() + ); + if !date_only_precision && ts.nanosecond() > 0 { + let frac = format!("{:09}", ts.nanosecond()); + s.push('.'); + s.push_str(frac.trim_end_matches('0')); + } + if with_tz { + let h = ts.tz_hour_offset(); + let m = ts.tz_minute_offset(); + let sign = if h < 0 || m < 0 { '-' } else { '+' }; + s.push_str(&format!(" {sign}{:02}:{:02}", h.abs(), m.abs())); + } + s +} + +fn capped_text(s: String, ty: &str) -> Value { + if s.len() > super::sql_util::CELL_VALUE_CAP { + super::sql_util::oversize_cell(ty, s.len(), s.as_bytes()) + } else { + Value::String(s) + } +} + +fn hex_upper(bytes: &[u8]) -> String { + let mut s = String::with_capacity(bytes.len() * 2); + for b in bytes { + s.push_str(&format!("{b:02X}")); + } + s +} + +fn cell_to_json(v: &SqlValue, ty: &OracleType) -> Value { + if v.is_null().unwrap_or(true) { + return Value::Null; + } + match ty { + OracleType::Number(_, _) | OracleType::Float(_) => v.get::().map(|s| number_json(&s)).unwrap_or(Value::Null), + OracleType::Int64 => v.get::().map(Value::from).unwrap_or(Value::Null), + OracleType::UInt64 => v.get::().map(Value::from).unwrap_or(Value::Null), + OracleType::BinaryFloat | OracleType::BinaryDouble => v + .get::() + .ok() + .and_then(serde_json::Number::from_f64) + .map(Value::Number) + .unwrap_or(Value::Null), + OracleType::Boolean => v.get::().map(Value::Bool).unwrap_or(Value::Null), + OracleType::Date => v.get::().map(|t| Value::String(fmt_timestamp(&t, true, false))).unwrap_or(Value::Null), + OracleType::Timestamp(_) | OracleType::TimestampLTZ(_) => { + v.get::().map(|t| Value::String(fmt_timestamp(&t, false, false))).unwrap_or(Value::Null) + } + OracleType::TimestampTZ(_) => v.get::().map(|t| Value::String(fmt_timestamp(&t, false, true))).unwrap_or(Value::Null), + OracleType::Raw(_) | OracleType::LongRaw => match v.get::>() { + Ok(b) if b.len() * 2 > super::sql_util::CELL_VALUE_CAP => Value::String(format!("", b.len())), + Ok(b) => Value::String(hex_upper(&b)), + Err(_) => Value::Null, + }, + // Without lob_locator() the driver fetches a LOB inline, as bytes. + OracleType::BLOB => match v.get::>() { + Ok(b) => Value::String(format!("", b.len())), + Err(_) => Value::String("".into()), + }, + OracleType::BFILE => Value::String("".into()), + OracleType::RefCursor => Value::String("".into()), + OracleType::Object(o) => Value::String(format!("<{}>", o.name())), + OracleType::CLOB | OracleType::NCLOB | OracleType::Long => { + v.get::().map(|s| capped_text(s, "clob")).unwrap_or(Value::Null) + } + _ => v.get::().map(|s| capped_text(s, "varchar2")).unwrap_or(Value::Null), + } +} + +// ── Binds ──────────────────────────────────────────────────────────────────── + +/// A value bound to a `:n` placeholder. +#[derive(Debug, Clone, PartialEq)] +pub enum Bind { + Null, + Int(i64), + Text(String), +} + +impl Bind { + /// From a grid value. Non-integer numbers bind as their decimal text so + /// NUMBER gets exactly the digits shown (a binary double would not). + pub fn from_json(v: &Value) -> Bind { + match v { + Value::Null => Bind::Null, + Value::Bool(b) => Bind::Int(i64::from(*b)), + Value::Number(n) => match n.as_i64() { + Some(i) => Bind::Int(i), + None => Bind::Text(n.to_string()), + }, + Value::String(s) => Bind::Text(s.clone()), + other => Bind::Text(other.to_string()), + } + } + + /// `from_json` for a column of a known catalog type: date-time text is + /// put in the shape the session's NLS formats read back exactly. + pub fn for_column(v: &Value, data_type: Option<&str>) -> Bind { + match (Bind::from_json(v), data_type) { + (Bind::Text(s), Some(t)) => Bind::Text(normalize_temporal(t, &s)), + (b, _) => b, + } + } +} + +/// Date-time text as the session's NLS formats read it: a `T` separator +/// becomes a space, and for zoned types `Z`/`UTC` becomes `+00:00` and a +/// value without fractional seconds gets `.0`. Without that last step +/// `... 04:05:06 -07:00` is read as `+07:00` - Oracle's lenient matching of the +/// missing `.FF` swallows the sign. +pub fn normalize_temporal(data_type: &str, s: &str) -> String { + let t = data_type.to_ascii_uppercase(); + let trimmed = s.trim(); + if !(t.starts_with("DATE") || t.starts_with("TIMESTAMP")) || !trimmed.is_ascii() { + return s.to_string(); + } + let mut v = trimmed.to_string(); + let b = v.as_bytes(); + let date_like = b.len() >= 10 && b[4] == b'-' && b[7] == b'-' && b[..4].iter().all(u8::is_ascii_digit); + if !date_like { + return s.to_string(); + } + if b.len() > 10 && b[10] == b'T' { + v.replace_range(10..11, " "); + } + if t.contains("TIME ZONE") { + for suffix in ["Z", " UTC", " GMT", "UTC", "GMT"] { + if let Some(rest) = v.strip_suffix(suffix) { + v = format!("{} +00:00", rest.trim_end()); + break; + } + } + let b = v.as_bytes(); + let time_like = b.len() >= 19 && b[13] == b':' && b[16] == b':'; + if time_like { + let after = v[19..].trim_start(); + if after.starts_with('+') || after.starts_with('-') { + v = format!("{}.0 {}", &v[..19], after); + } + } + } + v +} + +fn bind_refs(binds: &[Bind]) -> Vec> { + binds + .iter() + .map(|b| -> Box { + match b { + Bind::Null => Box::new(Option::::None), + Bind::Int(i) => Box::new(*i), + Bind::Text(s) => Box::new(s.clone()), + } + }) + .collect() +} + +// ── Execute ────────────────────────────────────────────────────────────────── + +/// Run one statement (no terminator, unless PL/SQL) on a locked session. +fn run_one(conn: &Connection, sql: &str, binds: &[Bind]) -> Result { + let t0 = Instant::now(); + let boxed = bind_refs(binds); + let params: Vec<&dyn ToSql> = boxed.iter().map(|b| b.as_ref()).collect(); + let mut stmt = conn.statement(sql).fetch_array_size(500).build().map_err(ora_err)?; + if stmt.is_query() { + let rs = stmt.query(¶ms).map_err(ora_err)?; + let infos = rs.column_info().to_vec(); + let columns: Vec = infos + .iter() + .map(|c| { + let mut ci = ColumnInfo::new(c.name().to_string(), type_label(c.oracle_type())); + ci.nullable = c.nullable(); + ci + }) + .collect(); + let mut rows: Vec> = Vec::new(); + for row in rs { + let row = row.map_err(ora_err)?; + rows.push(row.sql_values().iter().zip(infos.iter()).map(|(v, c)| cell_to_json(v, c.oracle_type())).collect()); + } + let row_count = Some(rows.len() as i64); + return Ok(SqlResult { columns, rows, row_count, message: None, query_ms: t0.elapsed().as_millis() as u64, sql: sql.to_string() }); + } + stmt.execute(¶ms).map_err(ora_err)?; + let (row_count, message) = if stmt.is_dml() { + let n = stmt.row_count().unwrap_or(0) as i64; + (Some(n), Some(format!("{n} row(s) affected"))) + } else if stmt.is_plsql() { + (None, Some("PL/SQL procedure successfully completed".to_string())) + } else { + (None, Some("Statement executed".to_string())) + }; + Ok(SqlResult { columns: vec![], rows: vec![], row_count, message, query_ms: t0.elapsed().as_millis() as u64, sql: sql.to_string() }) +} + +/// `run_one`, committed when it wrote (a query has nothing to commit). +fn run_commit(conn: &Connection, sql: &str, binds: &[Bind]) -> Result { + let r = run_one(conn, sql, binds); + match &r { + Ok(res) if res.message.is_some() => conn.commit().map_err(ora_err)?, + Ok(_) => {} + // Oracle undoes a failed statement itself; nothing else is open. + Err(_) => { + let _ = conn.rollback(); + } + } + r +} + +/// Run a script: each statement in turn, the last one's result returned. +pub async fn execute_sql(handle: &OracleHandle, sql: &str) -> Result { + let stmts = split_statements(sql); + if stmts.is_empty() { + return Err("Query is empty".into()); + } + let original = sql.trim().to_string(); + let writes = !stmts.iter().all(|s| reads_only(s)); + let out = run_blocking(handle, move |conn| { + let mut last = None; + for s in &stmts { + last = Some(run_commit(conn, s, &[])?); + } + let mut out = last.expect("non-empty"); + out.sql = original; + Ok(out) + }) + .await; + // Even a failed script may have run its first statements. + if writes { + handle.forget_meta(); + } + out +} + +/// `execute_sql` for read-only callers (the MCP server at "Read only"): the +/// statements run inside `SET TRANSACTION READ ONLY`, so any DML fails with +/// ORA-01456, and the transaction is rolled back after. Oracle DDL commits on +/// its own and is not covered by this - the access classifier refuses DDL at +/// that level before it gets here. +pub async fn execute_sql_readonly(handle: &OracleHandle, sql: &str) -> Result { + let stmts = split_statements(sql); + if stmts.is_empty() { + return Err("Query is empty".into()); + } + let original = sql.trim().to_string(); + run_blocking(handle, move |conn| { + let run = |conn: &Connection| -> Result, String> { + conn.rollback().map_err(ora_err)?; + conn.execute("SET TRANSACTION READ ONLY", &[]).map_err(ora_err)?; + let mut last = None; + for s in &stmts { + last = Some(run_one(conn, s, &[])?); + } + Ok(last) + }; + let out = run(conn); + let _ = conn.rollback(); + let mut r = out?.expect("non-empty"); + r.sql = original; + Ok(r) + }) + .await +} + +/// One bound query, for introspection. +async fn query_binds(handle: &OracleHandle, sql: String, binds: Vec) -> Result { + run_blocking(handle, move |conn| run_one(conn, &sql, &binds)).await +} + +fn text(row: &[Value], i: usize) -> Option { + match row.get(i)? { + Value::String(s) if !s.is_empty() => Some(s.clone()), + Value::Number(n) => Some(n.to_string()), + _ => None, + } +} + +fn int(row: &[Value], i: usize) -> Option { + match row.get(i)? { + Value::Number(n) => n.as_i64(), + Value::String(s) => s.parse().ok(), + _ => None, + } +} + +// ── Explain ────────────────────────────────────────────────────────────────── + +/// `EXPLAIN PLAN` + `DBMS_XPLAN.DISPLAY`, as text lines. Does not run the query. +pub async fn explain_lines(handle: &OracleHandle, sql: &str) -> Result, String> { + let stmts = split_statements(sql); + let target = stmts.into_iter().next().ok_or("Query is empty")?; + let target = match target.get(..8) { + Some(p) if p.eq_ignore_ascii_case("EXPLAIN ") => target[8..].trim_start().to_string(), + _ => target, + }; + let id = format!("STROKE{}", std::process::id()); + run_blocking(handle, move |conn| { + conn.execute(&format!("EXPLAIN PLAN SET STATEMENT_ID = '{id}' FOR {target}"), &[]) + .map_err(|e| format!("EXPLAIN failed: {}", clean_err(&e.to_string())))?; + let r = run_one(conn, "SELECT PLAN_TABLE_OUTPUT FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, :1, 'TYPICAL'))", &[Bind::Text(id.clone())])?; + let _ = conn.execute("DELETE FROM PLAN_TABLE WHERE STATEMENT_ID = :1", &[&id]); + let _ = conn.commit(); + Ok(r.rows.iter().filter_map(|row| text(row, 0)).collect()) + }) + .await +} + +// ── Schema introspection ───────────────────────────────────────────────────── + +/// Schemas (users) that own a table or view, plus the connected user, which +/// comes first. Oracle-maintained users (SYS, XDB, MDSYS, ...) sort last; the +/// UI keeps them behind its system-schema toggle. +pub async fn list_schemas(handle: &OracleHandle) -> Result, String> { + // ORACLE_MAINTAINED is 12c+; 11g falls back to the plain list. + let modern = "SELECT u.USERNAME FROM ALL_USERS u \ + WHERE u.USERNAME = USER OR u.USERNAME IN (SELECT OWNER FROM ALL_TABLES UNION SELECT OWNER FROM ALL_VIEWS) \ + ORDER BY CASE WHEN u.USERNAME = USER THEN 0 WHEN u.ORACLE_MAINTAINED = 'Y' THEN 2 ELSE 1 END, u.USERNAME"; + let legacy = "SELECT u.USERNAME FROM ALL_USERS u \ + WHERE u.USERNAME = USER OR u.USERNAME IN (SELECT OWNER FROM ALL_TABLES UNION SELECT OWNER FROM ALL_VIEWS) \ + ORDER BY CASE WHEN u.USERNAME = USER THEN 0 ELSE 1 END, u.USERNAME"; + let r = match query_binds(handle, modern.into(), vec![]).await { + Ok(r) => r, + Err(_) => query_binds(handle, legacy.into(), vec![]).await?, + }; + Ok(r.rows.iter().filter_map(|row| text(row, 0)).collect()) +} + +/// Below this many rows (by the optimizer statistics) a table gets an exact +/// count instead: statistics are often stale or missing on small tables. Same +/// threshold as the Postgres and MySQL paths. +const ESTIMATE_THRESHOLD: i64 = 100_000; + +pub async fn list_tables(handle: &OracleHandle, schema: &str) -> Result, String> { + // ORACLE_MAINTAINED (12c+) marks Oracle's own objects - the AQ$_, LOGMNR, + // MVIEW$ tables that fill SYSTEM - so "Hide system tables" can hide them. + // 11g has no such column and gets the list without it. + let modern = TABLES_SQL.replace("{maintained}", "o.ORACLE_MAINTAINED"); + let legacy = TABLES_SQL.replace("{maintained}", "NULL"); + let binds = vec![Bind::Text(schema.to_string())]; + let r = match query_binds(handle, modern, binds.clone()).await { + Ok(r) => r, + Err(_) => query_binds(handle, legacy, binds).await?, + }; + Ok(r.rows + .iter() + .filter_map(|row| { + let name = text(row, 0)?; + let is_view = text(row, 1).as_deref() == Some("VIEW"); + // NUM_ROWS is the optimizer's estimate from the last statistics + // run. Trusted only for big tables; the rest (-1) get an exact + // COUNT(*) from `table_row_counts`. Views report 0, as on MySQL + // and SQL Server. + let row_count = if is_view { + 0 + } else { + match int(row, 2) { + Some(n) if n >= ESTIMATE_THRESHOLD => n, + _ => -1, + } + }; + Some(TableInfo { + name, + kind: if is_view { "view" } else { "table" }.to_string(), + row_count, + rls_enabled: None, + created_at: text(row, 3), + create_order: int(row, 4), + system: text(row, 5).map(|m| m == "Y"), + }) + }) + .collect()) +} + +const TABLES_SQL: &str = "SELECT o.OBJECT_NAME, o.OBJECT_TYPE, t.NUM_ROWS, \ + TO_CHAR(o.CREATED, 'YYYY-MM-DD HH24:MI:SS'), o.OBJECT_ID, {maintained} \ + FROM ALL_OBJECTS o \ + LEFT JOIN ALL_TABLES t ON t.OWNER = o.OWNER AND t.TABLE_NAME = o.OBJECT_NAME \ + WHERE o.OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND o.OBJECT_TYPE IN ('TABLE', 'VIEW') \ + AND o.SECONDARY = 'N' AND o.OBJECT_NAME NOT LIKE 'BIN$%' \ + AND (t.NESTED IS NULL OR t.NESTED = 'NO') \ + AND (t.IOT_TYPE IS NULL OR t.IOT_TYPE = 'IOT') \ + ORDER BY o.OBJECT_NAME"; + +/// Exact `COUNT(*)` of one table. +pub async fn exact_row_count(handle: &OracleHandle, schema: &str, table: &str) -> Result { + let r = query_binds(handle, format!("SELECT COUNT(*) FROM {}", table_ref(schema, table)), vec![]).await?; + Ok(r.rows.first().and_then(|row| int(row, 0)).unwrap_or(0)) +} + +pub async fn list_indexes(handle: &OracleHandle, schema: &str) -> Result, String> { + let sql = "SELECT i.INDEX_NAME, i.TABLE_NAME, i.UNIQUENESS, i.INDEX_TYPE, \ + CASE WHEN EXISTS (SELECT 1 FROM ALL_CONSTRAINTS c WHERE c.OWNER = i.TABLE_OWNER \ + AND c.TABLE_NAME = i.TABLE_NAME AND c.CONSTRAINT_TYPE = 'P' AND c.INDEX_NAME = i.INDEX_NAME) \ + THEN 1 ELSE 0 END, \ + (SELECT LISTAGG(ic.COLUMN_NAME, ',') WITHIN GROUP (ORDER BY ic.COLUMN_POSITION) \ + FROM ALL_IND_COLUMNS ic WHERE ic.INDEX_OWNER = i.OWNER AND ic.INDEX_NAME = i.INDEX_NAME) \ + FROM ALL_INDEXES i \ + WHERE i.TABLE_OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND i.INDEX_TYPE <> 'LOB' AND i.TABLE_NAME NOT LIKE 'BIN$%' \ + ORDER BY i.TABLE_NAME, i.INDEX_NAME"; + let r = query_binds(handle, sql.into(), vec![Bind::Text(schema.to_string())]).await?; + Ok(r.rows + .iter() + .filter_map(|row| { + Some(IndexInfo { + name: text(row, 0)?, + table_name: text(row, 1)?, + columns: text(row, 5).unwrap_or_default(), + index_type: text(row, 3).unwrap_or_default().to_lowercase(), + is_unique: text(row, 2).as_deref() == Some("UNIQUE"), + is_primary: int(row, 4) == Some(1), + condition: None, + comment: None, + }) + }) + .collect()) +} + +/// Every column of a schema (or one table/view of it): the declared type with +/// its length, precision and scale, nullability, default, primary key +/// membership, the foreign key it belongs to and its comment. +/// +/// The key and foreign-key facts come from two derived tables filtered to the +/// same owner (and table), joined once - a correlated subquery per column +/// against the ALL_* dictionary views took most of a second for one table. +/// Each filter gets its own bind, since a positional bind is per occurrence. +fn columns_query(schema: &str, table: Option<&str>) -> (String, Vec) { + const OWNER: &str = "NVL(:{n}, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA'))"; + let mut binds: Vec = Vec::new(); + let filter = |alias: &str, binds: &mut Vec| -> String { + binds.push(Bind::Text(schema.to_string())); + let mut f = format!("{alias}.OWNER = {}", OWNER.replace("{n}", &binds.len().to_string())); + if let Some(t) = table { + binds.push(Bind::Text(t.to_string())); + f.push_str(&format!(" AND {alias}.TABLE_NAME = :{}", binds.len())); + } + f + }; + let main = filter("c", &mut binds); + let pk = filter("p", &mut binds); + let fk = filter("f", &mut binds); + let sql = format!( + "SELECT c.TABLE_NAME, c.COLUMN_ID, c.COLUMN_NAME, c.DATA_TYPE, c.DATA_LENGTH, c.CHAR_LENGTH, \ + c.CHAR_USED, c.DATA_PRECISION, c.DATA_SCALE, c.NULLABLE, c.DATA_DEFAULT, \ + NVL2(pk.COLUMN_NAME, 1, 0), fk.REF, cm.COMMENTS \ + FROM ALL_TAB_COLUMNS c \ + LEFT JOIN ( \ + SELECT DISTINCT pc.TABLE_NAME, pc.COLUMN_NAME FROM ALL_CONSTRAINTS p \ + JOIN ALL_CONS_COLUMNS pc ON pc.OWNER = p.OWNER AND pc.CONSTRAINT_NAME = p.CONSTRAINT_NAME \ + WHERE p.CONSTRAINT_TYPE = 'P' AND {pk} \ + ) pk ON pk.TABLE_NAME = c.TABLE_NAME AND pk.COLUMN_NAME = c.COLUMN_NAME \ + LEFT JOIN ( \ + SELECT fc.TABLE_NAME, fc.COLUMN_NAME, \ + MIN(r.OWNER || '.' || rc.TABLE_NAME || '.' || rc.COLUMN_NAME || CHR(1) || f.CONSTRAINT_NAME) AS REF \ + FROM ALL_CONSTRAINTS f \ + JOIN ALL_CONS_COLUMNS fc ON fc.OWNER = f.OWNER AND fc.CONSTRAINT_NAME = f.CONSTRAINT_NAME \ + JOIN ALL_CONSTRAINTS r ON r.OWNER = f.R_OWNER AND r.CONSTRAINT_NAME = f.R_CONSTRAINT_NAME \ + JOIN ALL_CONS_COLUMNS rc ON rc.OWNER = r.OWNER AND rc.CONSTRAINT_NAME = r.CONSTRAINT_NAME AND rc.POSITION = fc.POSITION \ + WHERE f.CONSTRAINT_TYPE = 'R' AND {fk} \ + GROUP BY fc.TABLE_NAME, fc.COLUMN_NAME \ + ) fk ON fk.TABLE_NAME = c.TABLE_NAME AND fk.COLUMN_NAME = c.COLUMN_NAME \ + LEFT JOIN ALL_COL_COMMENTS cm ON cm.OWNER = c.OWNER AND cm.TABLE_NAME = c.TABLE_NAME AND cm.COLUMN_NAME = c.COLUMN_NAME \ + WHERE {main} AND c.TABLE_NAME NOT LIKE 'BIN$%' \ + ORDER BY c.TABLE_NAME, c.COLUMN_ID" + ); + (sql, binds) +} + +/// `VARCHAR2(50 CHAR)`, `NUMBER(10,2)`, `RAW(16)` from the catalog's parts. +pub fn format_column_type(data_type: &str, length: Option, char_length: Option, char_used: Option<&str>, precision: Option, scale: Option) -> String { + match data_type { + "VARCHAR2" | "CHAR" => { + if char_used == Some("C") { + format!("{data_type}({} CHAR)", char_length.unwrap_or(0)) + } else { + format!("{data_type}({})", length.unwrap_or(0)) + } + } + "NVARCHAR2" | "NCHAR" => format!("{data_type}({})", char_length.unwrap_or(0)), + "RAW" | "UROWID" => format!("{data_type}({})", length.unwrap_or(0)), + "NUMBER" => match (precision, scale) { + (Some(p), Some(0)) => format!("NUMBER({p})"), + (Some(p), Some(s)) => format!("NUMBER({p},{s})"), + (None, Some(0)) => "INTEGER".to_string(), + _ => "NUMBER".to_string(), + }, + "FLOAT" => match precision { + Some(p) if p != 126 => format!("FLOAT({p})"), + _ => "FLOAT".to_string(), + }, + other => other.to_string(), + } +} + +fn columns_from_rows(rows: &[Vec]) -> Vec<(String, ColumnStructureRow)> { + rows.iter() + .enumerate() + .filter_map(|(idx, row)| { + let table = text(row, 0)?; + let name = text(row, 2)?; + let data_type = format_column_type( + &text(row, 3).unwrap_or_default(), + int(row, 4), + int(row, 5), + text(row, 6).as_deref(), + int(row, 7), + int(row, 8), + ); + let (foreign_key, fk_constraint_name) = match text(row, 12) { + Some(s) => match s.split_once('\u{1}') { + Some((r, c)) => (Some(r.to_string()), Some(c.to_string())), + None => (Some(s), None), + }, + None => (None, None), + }; + Some(( + table, + ColumnStructureRow { + ordinal_position: int(row, 1).unwrap_or((idx + 1) as i64) as i32, + name, + data_type, + is_nullable: text(row, 9).as_deref() != Some("N"), + // DATA_DEFAULT keeps the text as typed, trailing newline included. + column_default: text(row, 10).map(|d| d.trim().to_string()).filter(|d| !d.is_empty()), + is_primary_key: int(row, 11) == Some(1), + foreign_key, + fk_constraint_name, + comment: text(row, 13), + }, + )) + }) + .collect() +} + +pub async fn get_column_structure(handle: &OracleHandle, schema: &str, table: &str) -> Result, String> { + let (sql, binds) = columns_query(schema, Some(table)); + let r = query_binds(handle, sql, binds).await?; + Ok(columns_from_rows(&r.rows).into_iter().map(|(_, c)| c).collect()) +} + +pub async fn schema_column_structure(handle: &OracleHandle, schema: &str) -> Result, String> { + let (sql, binds) = columns_query(schema, None); + let r = query_binds(handle, sql, binds).await?; + let mut out: Vec = Vec::new(); + for (table, col) in columns_from_rows(&r.rows) { + match out.last_mut() { + Some(last) if last.table == table => last.columns.push(col), + _ => out.push(TableColumnStructure { table, columns: vec![col] }), + } + } + Ok(out) +} + +/// Foreign keys of one table, for the grid's jump-to-referenced-row links. +pub async fn foreign_keys(handle: &OracleHandle, schema: &str, table: &str) -> Result, String> { + let sql = "SELECT f.CONSTRAINT_NAME, fc.COLUMN_NAME, r.OWNER, rc.TABLE_NAME, rc.COLUMN_NAME \ + FROM ALL_CONSTRAINTS f \ + JOIN ALL_CONS_COLUMNS fc ON fc.OWNER = f.OWNER AND fc.CONSTRAINT_NAME = f.CONSTRAINT_NAME \ + JOIN ALL_CONSTRAINTS r ON r.OWNER = f.R_OWNER AND r.CONSTRAINT_NAME = f.R_CONSTRAINT_NAME \ + JOIN ALL_CONS_COLUMNS rc ON rc.OWNER = r.OWNER AND rc.CONSTRAINT_NAME = r.CONSTRAINT_NAME AND rc.POSITION = fc.POSITION \ + WHERE f.CONSTRAINT_TYPE = 'R' AND f.OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND f.TABLE_NAME = :2 \ + ORDER BY f.CONSTRAINT_NAME, fc.POSITION"; + let r = query_binds(handle, sql.into(), vec![Bind::Text(schema.into()), Bind::Text(table.into())]).await?; + let mut out: Vec<(String, ForeignKeyInfo)> = Vec::new(); + for row in &r.rows { + let (Some(name), Some(col), Some(rs), Some(rt), Some(rcol)) = (text(row, 0), text(row, 1), text(row, 2), text(row, 3), text(row, 4)) else { + continue; + }; + match out.last_mut() { + Some((n, fk)) if *n == name => { + fk.columns.push(col); + fk.referenced_columns.push(rcol); + } + _ => out.push((name, ForeignKeyInfo { columns: vec![col], referenced_schema: rs, referenced_table: rt, referenced_columns: vec![rcol] })), + } + } + Ok(out.into_iter().map(|(_, fk)| fk).collect()) +} + +/// Foreign keys elsewhere that point at this table. +pub async fn incoming_foreign_keys(handle: &OracleHandle, schema: &str, table: &str) -> Result, String> { + let sql = "SELECT f.OWNER, f.TABLE_NAME, fc.COLUMN_NAME, rc.COLUMN_NAME, f.CONSTRAINT_NAME \ + FROM ALL_CONSTRAINTS r \ + JOIN ALL_CONSTRAINTS f ON f.R_OWNER = r.OWNER AND f.R_CONSTRAINT_NAME = r.CONSTRAINT_NAME AND f.CONSTRAINT_TYPE = 'R' \ + JOIN ALL_CONS_COLUMNS fc ON fc.OWNER = f.OWNER AND fc.CONSTRAINT_NAME = f.CONSTRAINT_NAME \ + JOIN ALL_CONS_COLUMNS rc ON rc.OWNER = r.OWNER AND rc.CONSTRAINT_NAME = r.CONSTRAINT_NAME AND rc.POSITION = fc.POSITION \ + WHERE r.OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND r.TABLE_NAME = :2 AND r.CONSTRAINT_TYPE IN ('P', 'U') \ + ORDER BY f.OWNER, f.TABLE_NAME, f.CONSTRAINT_NAME, fc.POSITION"; + let r = query_binds(handle, sql.into(), vec![Bind::Text(schema.into()), Bind::Text(table.into())]).await?; + let mut map: std::collections::BTreeMap<(String, String, String), (Vec, Vec)> = Default::default(); + for row in &r.rows { + let key = (text(row, 0).unwrap_or_default(), text(row, 1).unwrap_or_default(), text(row, 4).unwrap_or_default()); + let e = map.entry(key).or_default(); + e.0.push(text(row, 2).unwrap_or_default()); + e.1.push(text(row, 3).unwrap_or_default()); + } + Ok(map + .into_iter() + .map(|((fs, ft, cn), (fc, tc))| IncomingForeignKey { from_schema: fs, from_table: ft, from_columns: fc, to_columns: tc, constraint_name: cn }) + .collect()) +} + +/// `DBMS_METADATA.GET_DDL` for a table or view; a synthesized CREATE TABLE +/// when the user may not call it on that object. +pub async fn get_ddl(handle: &OracleHandle, schema: &str, table: &str) -> Result { + let kind = query_binds( + handle, + "SELECT OBJECT_TYPE FROM ALL_OBJECTS WHERE OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND OBJECT_NAME = :2 AND OBJECT_TYPE IN ('TABLE', 'VIEW')".into(), + vec![Bind::Text(schema.into()), Bind::Text(table.into())], + ) + .await? + .rows + .first() + .and_then(|row| text(row, 0)) + .unwrap_or_else(|| "TABLE".to_string()); + let ddl = query_binds( + handle, + "SELECT DBMS_METADATA.GET_DDL(:1, :2, :3) FROM DUAL".into(), + vec![Bind::Text(kind), Bind::Text(table.into()), Bind::Text(schema.into())], + ) + .await; + if let Ok(r) = ddl { + if let Some(d) = r.rows.first().and_then(|row| text(row, 0)) { + let d = d.trim(); + if !d.is_empty() { + return Ok(format!("{d};")); + } + } + } + let cols = get_column_structure(handle, schema, table).await?; + if cols.is_empty() { + return Ok(String::new()); + } + let mut lines: Vec = cols + .iter() + .map(|c| { + let def = c.column_default.as_deref().map(|d| format!(" DEFAULT {d}")).unwrap_or_default(); + let null = if c.is_nullable { "" } else { " NOT NULL" }; + format!(" {} {}{def}{null}", quote_ident(&c.name), c.data_type) + }) + .collect(); + let pk: Vec = cols.iter().filter(|c| c.is_primary_key).map(|c| quote_ident(&c.name)).collect(); + if !pk.is_empty() { + lines.push(format!(" PRIMARY KEY ({})", pk.join(", "))); + } + Ok(format!("CREATE TABLE {} (\n{}\n);", table_ref(schema, table), lines.join(",\n"))) +} + +// ── Data browsing ──────────────────────────────────────────────────────────── + +/// A column the WHERE builder knows by name and catalog type. +#[derive(Debug, Clone)] +pub struct FilterColumn { + pub name: String, + pub data_type: String, +} + +impl FilterColumn { + fn base_type(&self) -> String { + self.data_type.split('(').next().unwrap_or("").trim().to_ascii_uppercase() + } + + /// The column as text for LIKE, or None when it has no text form to match + /// (BLOB, LONG, BFILE, object types). + fn as_text(&self) -> Option { + let q = quote_ident(&self.name); + let t = self.base_type(); + match t.as_str() { + "VARCHAR2" | "NVARCHAR2" | "CHAR" | "NCHAR" | "CLOB" | "NCLOB" => Some(q), + "RAW" => Some(format!("RAWTOHEX({q})")), + "BLOB" | "LONG" | "LONG RAW" | "BFILE" | "XMLTYPE" | "JSON" => None, + _ if t.contains('.') || t.starts_with("SDO_") || t.starts_with("ANYDATA") => None, + _ => Some(format!("TO_CHAR({q})")), + } + } + + /// `col = :n`, comparing the way the column's type allows. + fn equals(&self, ph: &str) -> Option { + let q = quote_ident(&self.name); + match self.base_type().as_str() { + "CLOB" | "NCLOB" => Some(format!("DBMS_LOB.COMPARE({q}, TO_CLOB({ph})) = 0")), + "RAW" => Some(format!("{q} = HEXTORAW({ph})")), + "BLOB" | "LONG" | "LONG RAW" | "BFILE" => None, + _ => Some(format!("{q} = {ph}")), + } + } +} + +fn escape_like(s: &str) -> String { + super::sql_util::escape_like_backslash(s) +} + +/// WHERE clause with `:n` binds numbered from `first_bind`. Search and the +/// text filters are case-insensitive, like ILIKE on Postgres. +pub fn build_where(cols: &[FilterColumn], search: Option<&str>, filters: &[RowFilter], first_bind: usize) -> Result<(String, Vec), String> { + let mut parts: Vec<(Option<&'static str>, String)> = Vec::new(); + let mut binds: Vec = Vec::new(); + let mut ph = |binds: &mut Vec, v: String| -> String { + binds.push(Bind::Text(v)); + format!(":{}", first_bind + binds.len() - 1) + }; + + let any_text = |binds: &mut Vec, ph: &mut dyn FnMut(&mut Vec, String) -> String, op: &str, v: &str| -> Option { + let ors: Vec = cols + .iter() + .filter_map(|c| { + let t = c.as_text()?; + Some(match op { + "eq" => format!("{t} = {}", ph(binds, v.to_string())), + "starts_with" => format!("UPPER({t}) LIKE UPPER({}) ESCAPE '\\'", ph(binds, format!("{}%", escape_like(v)))), + "ends_with" => format!("UPPER({t}) LIKE UPPER({}) ESCAPE '\\'", ph(binds, format!("%{}", escape_like(v)))), + _ => format!("UPPER({t}) LIKE UPPER({}) ESCAPE '\\'", ph(binds, format!("%{}%", escape_like(v)))), + }) + }) + .collect(); + if ors.is_empty() { + None + } else { + Some(format!("({})", ors.join(" OR "))) + } + }; + + if let Some(term) = search.map(str::trim).filter(|s| !s.is_empty()) { + if let Some(c) = any_text(&mut binds, &mut ph, "contains", term) { + parts.push((None, c)); + } + } + + for f in filters { + let conj: Option<&'static str> = if parts.is_empty() { + None + } else if f.conjunct.as_deref().is_some_and(|s| s.eq_ignore_ascii_case("or")) { + Some("OR") + } else { + Some("AND") + }; + if f.op == super::sql_util::RAW_SQL_FILTER_OP { + let raw = f.value.as_deref().unwrap_or(""); + if let Some(expr) = super::sql_util::raw_where_expr(raw, super::sql_util::RawFilterDialect::Oracle)? { + parts.push((conj, expr)); + } + continue; + } + if f.column == "__any__" { + let v = f.value.as_deref().unwrap_or("").trim(); + if !v.is_empty() { + if let Some(c) = any_text(&mut binds, &mut ph, f.op.as_str(), v) { + parts.push((conj, c)); + } + } + continue; + } + let Some(col) = cols.iter().find(|c| c.name == f.column) else { + return Err(format!("Unknown column: {}", f.column)); + }; + let q = quote_ident(&col.name); + let v = normalize_temporal(&col.data_type, f.value.as_deref().unwrap_or("").trim()); + let op = match f.op.as_str() { + "=" => "eq", + "!=" | "<>" | "ne" => "neq", + ">" => "gt", + ">=" => "gte", + "<" => "lt", + "<=" => "lte", + other => other, + }; + let text_col = || col.as_text().ok_or_else(|| format!("Column {} can't be searched as text", col.name)); + let clause = match op { + "is_null" => format!("{q} IS NULL"), + "is_not_null" => format!("{q} IS NOT NULL"), + _ if v.is_empty() => continue, + "eq" => { + let p = ph(&mut binds, v); + col.equals(&p).ok_or_else(|| format!("Column {} can't be compared", col.name))? + } + "neq" => { + let p = ph(&mut binds, v); + let eq = col.equals(&p).ok_or_else(|| format!("Column {} can't be compared", col.name))?; + format!("({q} IS NULL OR NOT ({eq}))") + } + "gt" => format!("{q} > {}", ph(&mut binds, v)), + "gte" => format!("{q} >= {}", ph(&mut binds, v)), + "lt" => format!("{q} < {}", ph(&mut binds, v)), + "lte" => format!("{q} <= {}", ph(&mut binds, v)), + "contains" => format!("UPPER({}) LIKE UPPER({}) ESCAPE '\\'", text_col()?, ph(&mut binds, format!("%{}%", escape_like(&v)))), + // A NULL cell contains nothing, so it satisfies "does not contain". + "not_contains" => { + let t = text_col()?; + format!("({q} IS NULL OR UPPER({t}) NOT LIKE UPPER({}) ESCAPE '\\')", ph(&mut binds, format!("%{}%", escape_like(&v)))) + } + "starts_with" => format!("UPPER({}) LIKE UPPER({}) ESCAPE '\\'", text_col()?, ph(&mut binds, format!("{}%", escape_like(&v)))), + "ends_with" => format!("UPPER({}) LIKE UPPER({}) ESCAPE '\\'", text_col()?, ph(&mut binds, format!("%{}", escape_like(&v)))), + "between" => { + let mut it = v.splitn(2, ','); + let from = it.next().unwrap_or("").trim().to_string(); + let to = it.next().unwrap_or("").trim().to_string(); + let a = ph(&mut binds, from); + let b = ph(&mut binds, to); + format!("({q} >= {a} AND {q} <= {b})") + } + other => return Err(format!("Unsupported filter operator: {other}")), + }; + parts.push((conj, clause)); + } + + if parts.is_empty() { + return Ok((String::new(), binds)); + } + let mut out = String::from(" WHERE "); + for (i, (conj, cond)) in parts.into_iter().enumerate() { + if i > 0 { + out.push(' '); + out.push_str(conj.unwrap_or("AND")); + out.push(' '); + } + out.push_str(&cond); + } + Ok((out, binds)) +} + +/// `ORDER BY` for a page: the chosen sort with the key as tie-breaker, else +/// the primary key, else ROWID for a table, so pages never overlap or skip. +pub fn order_by(sort: Option<(&str, bool)>, pk: &[String], is_table: bool) -> String { + let mut terms: Vec = Vec::new(); + if let Some((c, desc)) = sort { + terms.push(format!("{} {}", quote_ident(c), if desc { "DESC" } else { "ASC" })); + terms.extend(pk.iter().filter(|k| k.as_str() != c).map(|k| quote_ident(k))); + if pk.is_empty() && is_table { + terms.push("ROWID".into()); + } + } else if !pk.is_empty() { + terms.extend(pk.iter().map(|k| quote_ident(k))); + } else if is_table { + terms.push("ROWID".into()); + } + if terms.is_empty() { + String::new() + } else { + format!(" ORDER BY {}", terms.join(", ")) + } +} + +/// `OFFSET n ROWS FETCH NEXT m ROWS ONLY` (Oracle 12c and later). +pub fn page_clause(limit: i64, offset: i64) -> String { + format!(" OFFSET {} ROWS FETCH NEXT {} ROWS ONLY", offset.max(0), limit.max(0)) +} + +#[allow(clippy::too_many_arguments)] +pub async fn get_table_rows( + handle: &OracleHandle, + schema: &str, + table: &str, + limit: i64, + offset: i64, + search: Option, + sort_column: Option, + sort_direction: Option, + filters: Option>, + include_meta: bool, +) -> Result { + let t0 = Instant::now(); + let meta = table_meta(handle, schema, table).await?; + let structure = &meta.structure; + let is_table = meta.is_table; + let cols: Vec = structure.iter().map(|c| FilterColumn { name: c.name.clone(), data_type: c.data_type.clone() }).collect(); + let pk: Vec = structure.iter().filter(|c| c.is_primary_key).map(|c| c.name.clone()).collect(); + + let filters = filters.unwrap_or_default(); + let (where_sql, binds) = build_where(&cols, search.as_deref(), &filters, 1)?; + let tq = table_ref(schema, table); + let sort = sort_column + .as_deref() + .filter(|c| !c.trim().is_empty() && cols.iter().any(|k| k.name == *c)) + .map(|c| (c, sort_direction.as_deref().is_some_and(|d| d.eq_ignore_ascii_case("desc")))); + let order = order_by(sort, &pk, is_table); + + let count_sql = format!("SELECT COUNT(*) FROM {tq}{where_sql}"); + let data_sql = format!("SELECT * FROM {tq}{where_sql}{order}{}", page_clause(limit, offset)); + let (count_q, data_q) = (count_sql.clone(), data_sql.clone()); + let (total, mut result) = run_blocking(handle, move |conn| { + let total = run_one(conn, &count_q, &binds)?.rows.first().and_then(|r| int(r, 0)).unwrap_or(0); + let data = run_one(conn, &data_q, &binds)?; + Ok((total, data)) + }) + .await?; + + // Catalog facts the insert row needs: NOT NULL and defaults. + let by_name: HashMap<&str, &ColumnStructureRow> = structure.iter().map(|c| (c.name.as_str(), c)).collect(); + for c in result.columns.iter_mut() { + if let Some(s) = by_name.get(c.name.as_str()) { + c.nullable = s.is_nullable; + c.has_default = s.column_default.is_some(); + c.auto_generated = s.column_default.as_deref().is_some_and(|d| d.contains(".nextval") || d.contains("ISEQ$$")); + } + } + let fks = if include_meta { meta.foreign_keys.clone() } else { Vec::new() }; + + Ok(TableRows { + preview_columns: Vec::new(), + columns: result.columns, + rows: result.rows, + total, + query_ms: t0.elapsed().as_millis() as u64, + primary_key: if include_meta { pk } else { Vec::new() }, + foreign_keys: fks, + sql: format!("{data_sql}\n{count_sql}"), + }) +} + +/// A table's columns, whether it is a table (not a view) and its foreign keys: +/// from the session's cache, or the catalog on a miss. +async fn table_meta(handle: &OracleHandle, schema: &str, table: &str) -> Result, String> { + let key = (schema.to_string(), table.to_string()); + if let Some(meta) = handle.cached_meta(&key) { + return Ok(meta); + } + let structure = get_column_structure(handle, schema, table).await?; + if structure.is_empty() { + return Err(format!("Table {schema}.{table} not found, or you have no access to it")); + } + let is_table = query_binds( + handle, + "SELECT 1 FROM ALL_TABLES WHERE OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND TABLE_NAME = :2".into(), + vec![Bind::Text(schema.into()), Bind::Text(table.into())], + ) + .await + .map(|r| !r.rows.is_empty()) + .unwrap_or(false); + let foreign_keys = foreign_keys(handle, schema, table).await.unwrap_or_default(); + let meta = std::sync::Arc::new(TableMeta { structure, is_table, foreign_keys }); + handle.store_meta(key, meta.clone()); + Ok(meta) +} + +// ── Row editing ────────────────────────────────────────────────────────────── + +/// `WHERE "K1" = :n AND ...` for one row's key, binds numbered from `first`. +pub fn key_where(pk: &HashMap, first: usize) -> (String, Vec) { + let mut keys: Vec<&String> = pk.keys().collect(); + keys.sort(); + let mut binds = Vec::new(); + let mut parts = Vec::new(); + for k in keys { + let v = &pk[k]; + if v.is_null() { + parts.push(format!("{} IS NULL", quote_ident(k))); + } else { + parts.push(format!("{} = :{}", quote_ident(k), first + binds.len())); + binds.push(Bind::from_json(v)); + } + } + (format!(" WHERE {}", parts.join(" AND ")), binds) +} + +/// Column name -> catalog type, for shaping bound values. Empty on failure: +/// values then bind as given. +async fn column_types(handle: &OracleHandle, schema: &str, table: &str) -> HashMap { + let sql = "SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS \ + WHERE OWNER = NVL(:1, SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')) AND TABLE_NAME = :2"; + match query_binds(handle, sql.into(), vec![Bind::Text(schema.into()), Bind::Text(table.into())]).await { + Ok(r) => r.rows.iter().filter_map(|row| Some((text(row, 0)?, text(row, 1)?))).collect(), + Err(_) => HashMap::new(), + } +} + +pub async fn update_table_cell( + handle: &OracleHandle, + schema: &str, + table: &str, + primary_key: HashMap, + column: &str, + value: &Value, +) -> Result<(), String> { + if primary_key.is_empty() { + return Err("Cannot update row: table has no primary key".into()); + } + let types = column_types(handle, schema, table).await; + let (where_sql, key_binds) = key_where(&primary_key, 2); + let sql = format!("UPDATE {} SET {} = :1{where_sql}", table_ref(schema, table), quote_ident(column)); + let mut binds = vec![Bind::for_column(value, types.get(column).map(String::as_str))]; + binds.extend(key_binds); + let res = run_blocking(handle, move |conn| run_commit(conn, &sql, &binds)).await?; + if res.row_count == Some(0) { + return Err("No row updated (row may have changed)".into()); + } + Ok(()) +} + +/// Insert one row and return it as stored (defaults, identity and trigger +/// values filled in), read back by the ROWID the insert produced. +pub async fn insert_table_row( + handle: &OracleHandle, + schema: &str, + table: &str, + values: HashMap, +) -> Result, String> { + let mut cols: Vec = values.keys().cloned().collect(); + cols.sort(); + let tq = table_ref(schema, table); + let sql = if cols.is_empty() { + format!("INSERT INTO {tq} VALUES (DEFAULT)") + } else { + let names = cols.iter().map(|c| quote_ident(c)).collect::>().join(", "); + let phs = (1..=cols.len()).map(|i| format!(":{i}")).collect::>().join(", "); + format!("INSERT INTO {tq} ({names}) VALUES ({phs})") + }; + let types = column_types(handle, schema, table).await; + let binds: Vec = cols.iter().map(|c| Bind::for_column(&values[c], types.get(c).map(String::as_str))).collect(); + run_blocking(handle, move |conn| { + let boxed = bind_refs(&binds); + let params: Vec<&dyn ToSql> = boxed.iter().map(|b| b.as_ref()).collect(); + let mut stmt = conn.statement(&sql).build().map_err(ora_err)?; + if let Err(e) = stmt.execute(¶ms) { + let _ = conn.rollback(); + return Err(ora_err(e)); + } + conn.commit().map_err(ora_err)?; + let rowid = stmt.last_row_id().ok().flatten(); + if let Some(rowid) = rowid { + if let Ok(r) = run_one(conn, &format!("SELECT * FROM {tq} WHERE ROWID = CHARTOROWID(:1)"), &[Bind::Text(rowid)]) { + if let Some(row) = r.rows.into_iter().next() { + return Ok(row); + } + } + } + Ok(Vec::new()) + }) + .await +} + +pub async fn delete_table_rows( + handle: &OracleHandle, + schema: &str, + table: &str, + primary_keys: Vec>, +) -> Result { + if primary_keys.is_empty() { + return Ok(0); + } + if primary_keys.iter().any(|pk| pk.is_empty()) { + return Err("Cannot delete rows: table has no primary key".into()); + } + let tq = table_ref(schema, table); + run_blocking(handle, move |conn| { + // All or nothing: one transaction, one commit at the end. + let run = |conn: &Connection| -> Result { + let mut deleted = 0u64; + for pk in &primary_keys { + let (where_sql, binds) = key_where(pk, 1); + let boxed = bind_refs(&binds); + let params: Vec<&dyn ToSql> = boxed.iter().map(|b| b.as_ref()).collect(); + let mut stmt = conn.statement(&format!("DELETE FROM {tq}{where_sql}")).build().map_err(ora_err)?; + stmt.execute(¶ms).map_err(ora_err)?; + deleted += stmt.row_count().unwrap_or(0); + } + conn.commit().map_err(ora_err)?; + Ok(deleted) + }; + let out = run(conn); + if out.is_err() { + let _ = conn.rollback(); + } + out + }) + .await +} + +/// Against a real server: `STROKE_ORACLE_TEST=user/password@//host:1521/SERVICE +/// cargo test --lib oracle_live -- --ignored --nocapture`, with a CUSTOMERS +/// table in that user's schema. +#[cfg(test)] +mod live_tests { + use super::*; + + fn live_cfg() -> Option { + let url = std::env::var("STROKE_ORACLE_TEST").ok()?; + let (creds, cs) = url.split_once('@')?; + let (user, password) = creds.split_once('/')?; + Some(OracleConfig { + name: "live".into(), + host: String::new(), + port: 1521, + service_name: String::new(), + sid: String::new(), + connect_string: cs.into(), + user: user.into(), + password: password.into(), + client_lib_dir: std::env::var("STROKE_ORACLE_CLIENT_DIR").unwrap_or_default(), + }) + } + + #[tokio::test] + #[ignore] + async fn oracle_live_meta_cache_and_flags() { + let Some(cfg) = live_cfg() else { return }; + let h = connect(&cfg).await.expect("connect"); + println!("container: {}", container_name(&h).await.unwrap()); + + let t = Instant::now(); + let first = get_table_rows(&h, "", "CUSTOMERS", 100, 0, None, None, None, None, true).await.unwrap(); + let first_ms = t.elapsed().as_millis(); + let t = Instant::now(); + let second = get_table_rows(&h, "", "CUSTOMERS", 100, 0, None, Some("NAME".into()), Some("desc".into()), None, true).await.unwrap(); + let second_ms = t.elapsed().as_millis(); + println!("first fetch {first_ms}ms, cached {second_ms}ms, rows {}", second.rows.len()); + assert_eq!(first.rows.len(), second.rows.len()); + assert_eq!(second.primary_key, vec!["ID".to_string()]); + + // DDL forgets the cached columns. + execute_sql(&h, "ALTER TABLE CUSTOMERS ADD (NOTE VARCHAR2(10))").await.unwrap(); + let after = get_table_rows(&h, "", "CUSTOMERS", 100, 0, None, None, None, None, true).await.unwrap(); + execute_sql(&h, "ALTER TABLE CUSTOMERS DROP COLUMN NOTE").await.unwrap(); + assert!(after.columns.iter().any(|c| c.name == "NOTE")); + + let sql_filter = |v: &str| Some(vec![RowFilter { column: String::new(), op: "sql".into(), value: Some(v.into()), conjunct: None, data_type: None }]); + let filtered = get_table_rows(&h, "", "CUSTOMERS", 100, 0, None, None, None, sql_filter("ID < 3"), false).await.unwrap(); + assert_eq!(filtered.total, 2, "{:?}", filtered.rows); + assert_eq!(filtered.rows.len(), 2); + + let tables = list_tables(&h, "").await.unwrap(); + let mine = tables.iter().find(|t| t.name == "CUSTOMERS").unwrap(); + assert_eq!(mine.system, Some(false)); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn cfg() -> OracleConfig { + OracleConfig { + name: "t".into(), + host: "db.example.com".into(), + port: 1521, + service_name: "FREEPDB1".into(), + sid: String::new(), + connect_string: String::new(), + user: "scott".into(), + password: "tiger".into(), + client_lib_dir: String::new(), + } + } + + #[test] + fn connect_string_forms() { + assert_eq!(connect_string(&cfg()), "//db.example.com:1521/FREEPDB1"); + let mut c = cfg(); + c.sid = "XE".into(); + assert_eq!( + connect_string(&c), + "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=db.example.com)(PORT=1521))(CONNECT_DATA=(SID=XE)))" + ); + let mut c = cfg(); + c.connect_string = " tcps://h:2484/svc?wallet_location=/w ".into(); + assert_eq!(connect_string(&c), "tcps://h:2484/svc?wallet_location=/w"); + } + + #[test] + fn probe_targets() { + assert_eq!(probe_target(&cfg()), Some(("db.example.com".into(), 1521))); + let mut c = cfg(); + c.sid = "XE".into(); + c.port = 1522; + assert_eq!(probe_target(&c), Some(("db.example.com".into(), 1522))); + let mut c = cfg(); + c.connect_string = "tcps://h:2484/svc".into(); + assert_eq!(probe_target(&c), Some(("h".into(), 2484))); + c.connect_string = "h2/svc".into(); + assert_eq!(probe_target(&c), Some(("h2".into(), 1521))); + c.connect_string = "MYALIAS".into(); + assert_eq!(probe_target(&c), None); + c.connect_string = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = 10.0.0.5)(PORT = 1530))(CONNECT_DATA = (SERVICE_NAME = x)))".into(); + assert_eq!(probe_target(&c), Some(("10.0.0.5".into(), 1530))); + } + + #[test] + fn splits_sql_and_drops_terminators() { + assert_eq!(split_statements("SELECT 1 FROM dual;"), vec!["SELECT 1 FROM dual"]); + assert_eq!(split_statements("select 1 from dual; select 'a;b' from dual ;"), vec!["select 1 from dual", "select 'a;b' from dual"]); + assert_eq!(split_statements("select q'[it's; fine]' from dual; select 2 from dual"), vec!["select q'[it's; fine]' from dual", "select 2 from dual"]); + assert_eq!(split_statements("select \"A;B\" from t -- c; d\n;"), vec!["select \"A;B\" from t -- c; d"]); + assert_eq!(split_statements(" ;; -- nothing\n"), Vec::::new()); + assert_eq!(split_statements("select 1 from dual\n/\nselect 2 from dual\n/\n"), vec!["select 1 from dual", "select 2 from dual"]); + assert_eq!(split_statements("SELECT 10 / 2 FROM dual"), vec!["SELECT 10 / 2 FROM dual"]); + } + + #[test] + fn splits_plsql_blocks_at_slash() { + let script = "BEGIN\n UPDATE t SET a = 1;\n COMMIT;\nEND;\n/\nSELECT * FROM t;"; + assert_eq!(split_statements(script), vec!["BEGIN\n UPDATE t SET a = 1;\n COMMIT;\nEND;", "SELECT * FROM t"]); + let proc_ = "CREATE OR REPLACE PROCEDURE p AS\nBEGIN\n NULL;\nEND;\n/"; + assert_eq!(split_statements(proc_), vec!["CREATE OR REPLACE PROCEDURE p AS\nBEGIN\n NULL;\nEND;"]); + // A block at the end with no slash still runs whole. + assert_eq!(split_statements("DECLARE x NUMBER; BEGIN x := 1; END;"), vec!["DECLARE x NUMBER; BEGIN x := 1; END;"]); + assert_eq!(split_statements("-- lead\nbegin null; end;"), vec!["-- lead\nbegin null; end;"]); + // CREATE TABLE is plain SQL even though it starts with CREATE. + assert_eq!(split_statements("CREATE TABLE t (a NUMBER); INSERT INTO t VALUES (1);"), vec!["CREATE TABLE t (a NUMBER)", "INSERT INTO t VALUES (1)"]); + } + + #[test] + fn numbers_keep_their_digits() { + assert_eq!(number_json("42"), Value::from(42)); + assert_eq!(number_json("-7"), Value::from(-7)); + assert_eq!(number_json("12.5"), serde_json::json!(12.5)); + assert_eq!(number_json(".5"), serde_json::json!(0.5)); + assert_eq!(number_json("-.25"), serde_json::json!(-0.25)); + assert_eq!(number_json("12345678901234567890"), Value::String("12345678901234567890".into())); + assert_eq!(number_json("0.1000000000000000000001"), Value::String("0.1000000000000000000001".into())); + assert_eq!(number_json("9007199254740993"), Value::String("9007199254740993".into())); + } + + #[test] + fn column_types() { + assert_eq!(format_column_type("VARCHAR2", Some(200), Some(50), Some("C"), None, None), "VARCHAR2(50 CHAR)"); + assert_eq!(format_column_type("VARCHAR2", Some(50), Some(50), Some("B"), None, None), "VARCHAR2(50)"); + assert_eq!(format_column_type("NVARCHAR2", Some(100), Some(50), Some("C"), None, None), "NVARCHAR2(50)"); + assert_eq!(format_column_type("NUMBER", Some(22), None, None, Some(10), Some(2)), "NUMBER(10,2)"); + assert_eq!(format_column_type("NUMBER", Some(22), None, None, Some(9), Some(0)), "NUMBER(9)"); + assert_eq!(format_column_type("NUMBER", Some(22), None, None, None, None), "NUMBER"); + assert_eq!(format_column_type("NUMBER", Some(22), None, None, None, Some(0)), "INTEGER"); + assert_eq!(format_column_type("RAW", Some(16), None, None, None, None), "RAW(16)"); + assert_eq!(format_column_type("TIMESTAMP(6) WITH TIME ZONE", Some(13), None, None, None, Some(6)), "TIMESTAMP(6) WITH TIME ZONE"); + } + + fn fc(name: &str, ty: &str) -> FilterColumn { + FilterColumn { name: name.into(), data_type: ty.into() } + } + + #[test] + fn where_uses_numbered_binds() { + let cols = vec![fc("ID", "NUMBER(9)"), fc("NAME", "VARCHAR2(50)"), fc("DOC", "BLOB"), fc("NOTE", "CLOB")]; + let filters = vec![ + RowFilter { column: "ID".into(), op: "gt".into(), value: Some("5".into()), conjunct: None, data_type: None }, + RowFilter { column: "NAME".into(), op: "contains".into(), value: Some("a_b".into()), conjunct: Some("or".into()), data_type: None }, + RowFilter { column: "NOTE".into(), op: "eq".into(), value: Some("x".into()), conjunct: None, data_type: None }, + ]; + let (sql, binds) = build_where(&cols, Some("hi"), &filters, 1).unwrap(); + assert_eq!( + sql, + " WHERE (UPPER(TO_CHAR(\"ID\")) LIKE UPPER(:1) ESCAPE '\\' OR UPPER(\"NAME\") LIKE UPPER(:2) ESCAPE '\\' OR UPPER(\"NOTE\") LIKE UPPER(:3) ESCAPE '\\') \ + AND \"ID\" > :4 OR UPPER(\"NAME\") LIKE UPPER(:5) ESCAPE '\\' AND DBMS_LOB.COMPARE(\"NOTE\", TO_CLOB(:6)) = 0" + ); + assert_eq!( + binds, + vec![ + Bind::Text("%hi%".into()), + Bind::Text("%hi%".into()), + Bind::Text("%hi%".into()), + Bind::Text("5".into()), + Bind::Text("%a\\_b%".into()), + Bind::Text("x".into()), + ] + ); + let bad = vec![RowFilter { column: "NOPE".into(), op: "eq".into(), value: Some("1".into()), conjunct: None, data_type: None }]; + assert!(build_where(&cols, None, &bad, 1).is_err()); + let raw = vec![RowFilter { column: String::new(), op: "sql".into(), value: Some("ID = :x".into()), conjunct: None, data_type: None }]; + assert!(build_where(&cols, None, &raw, 1).is_err()); + let raw = vec![RowFilter { column: String::new(), op: "sql".into(), value: Some("ID > 3 AND NAME LIKE 'a%'".into()), conjunct: None, data_type: None }]; + assert_eq!(build_where(&cols, None, &raw, 1).unwrap().0, " WHERE (ID > 3 AND NAME LIKE 'a%')"); + } + + #[test] + fn page_and_order() { + assert_eq!(page_clause(50, 100), " OFFSET 100 ROWS FETCH NEXT 50 ROWS ONLY"); + assert_eq!(order_by(None, &["ID".into()], true), " ORDER BY \"ID\""); + assert_eq!(order_by(None, &[], true), " ORDER BY ROWID"); + assert_eq!(order_by(None, &[], false), ""); + assert_eq!(order_by(Some(("NAME", true)), &["ID".into()], true), " ORDER BY \"NAME\" DESC, \"ID\""); + assert_eq!(order_by(Some(("NAME", false)), &[], true), " ORDER BY \"NAME\" ASC, ROWID"); + } + + #[test] + fn key_where_binds_in_order() { + let pk = HashMap::from([("B".to_string(), Value::from("x")), ("A".to_string(), Value::from(1))]); + let (sql, binds) = key_where(&pk, 2); + assert_eq!(sql, " WHERE \"A\" = :2 AND \"B\" = :3"); + assert_eq!(binds, vec![Bind::Int(1), Bind::Text("x".into())]); + let pk = HashMap::from([("A".to_string(), Value::Null), ("B".to_string(), Value::from(2.5))]); + let (sql, binds) = key_where(&pk, 1); + assert_eq!(sql, " WHERE \"A\" IS NULL AND \"B\" = :1"); + assert_eq!(binds, vec![Bind::Text("2.5".into())]); + } + + #[test] + fn temporal_text_is_shaped_for_nls() { + let tz = "TIMESTAMP(6) WITH TIME ZONE"; + assert_eq!(normalize_temporal(tz, "2001-02-03 04:05:06 -07:00"), "2001-02-03 04:05:06.0 -07:00"); + assert_eq!(normalize_temporal(tz, "2001-02-03T04:05:06-07:00"), "2001-02-03 04:05:06.0 -07:00"); + assert_eq!(normalize_temporal(tz, "2001-02-03 04:05:06.25 +05:30"), "2001-02-03 04:05:06.25 +05:30"); + assert_eq!(normalize_temporal(tz, "2001-02-03 04:05:06Z"), "2001-02-03 04:05:06.0 +00:00"); + assert_eq!(normalize_temporal(tz, "2001-02-03 04:05:06 UTC"), "2001-02-03 04:05:06.0 +00:00"); + assert_eq!(normalize_temporal(tz, "2001-02-03"), "2001-02-03"); + assert_eq!(normalize_temporal("DATE", "2001-02-03T04:05"), "2001-02-03 04:05"); + assert_eq!(normalize_temporal("VARCHAR2", "2001-02-03 04:05:06 -07:00"), "2001-02-03 04:05:06 -07:00"); + assert_eq!(normalize_temporal(tz, "not a date"), "not a date"); + } + + #[test] + fn quoting() { + assert_eq!(quote_ident("EMP"), "\"EMP\""); + assert_eq!(quote_ident("we\"ird"), "\"we\"\"ird\""); + assert_eq!(table_ref("HR", "EMP"), "\"HR\".\"EMP\""); + } + + // ── Live tests ─────────────────────────────────────────────────────────── + // + // Need a real Oracle and Instant Client. With the gvenzl/oracle-free image + // (`-e ORACLE_PASSWORD=... -e APP_USER=stroke -e APP_USER_PASSWORD=strokepass`): + // + // STROKE_ORACLE_TEST_URL=stroke/strokepass@//127.0.0.1:1521/FREEPDB1 \ + // STROKE_ORACLE_CLIENT_DIR=/path/to/instantclient_23_x \ + // cargo test --lib db::oracle::tests -- --ignored --test-threads=1 + + fn live_config() -> Option { + let url = std::env::var("STROKE_ORACLE_TEST_URL").ok()?; + let (creds, cs) = url.split_once('@')?; + let (user, password) = creds.split_once('/')?; + Some(OracleConfig { + name: "live".into(), + host: String::new(), + port: 1521, + service_name: String::new(), + sid: String::new(), + connect_string: cs.to_string(), + user: user.into(), + password: password.into(), + client_lib_dir: String::new(), + }) + } + + async fn live() -> Option { + let cfg = live_config()?; + Some(crate::db::connection::open_oracle(&cfg).await.expect("connect")) + } + + async fn exec(h: &OracleHandle, sql: &str) -> SqlResult { + execute_sql(h, sql).await.unwrap_or_else(|e| panic!("{sql}: {e}")) + } + + #[tokio::test] + #[ignore] + async fn live_browse_edit_roundtrip() { + let Some(h) = live().await else { return }; + let _ = execute_sql(&h, "DROP TABLE stroke_child PURGE").await; + let _ = execute_sql(&h, "DROP TABLE stroke_types PURGE").await; + exec( + &h, + "CREATE TABLE stroke_types (\ + id NUMBER(9) PRIMARY KEY, \ + name VARCHAR2(50 CHAR) NOT NULL, \ + nname NVARCHAR2(20), \ + price NUMBER(10,2), \ + ratio BINARY_DOUBLE, \ + born DATE, \ + seen TIMESTAMP(6), \ + seen_tz TIMESTAMP WITH TIME ZONE, \ + note CLOB, \ + doc BLOB, \ + tag RAW(4), \ + created DATE DEFAULT SYSDATE)", + ) + .await; + exec(&h, "CREATE TABLE stroke_child (id NUMBER PRIMARY KEY, parent_id NUMBER REFERENCES stroke_types(id))").await; + exec(&h, "COMMENT ON COLUMN stroke_types.name IS 'display name'").await; + // A script: PL/SQL block ended by `/`, then plain SQL with `;`. + let script = "BEGIN\n FOR i IN 1..30 LOOP\n INSERT INTO stroke_types (id, name, nname, price, ratio, born, seen, seen_tz, note, doc, tag) \ + VALUES (i, 'name ' || i, N'ñ' || i, i + 0.5, i / 4, DATE '2024-01-02' + i, TIMESTAMP '2024-01-02 03:04:05.123456', \ + TIMESTAMP '2024-01-02 03:04:05 +05:30', 'clob ' || i, HEXTORAW('DEADBEEF'), HEXTORAW('0A0B0C0D'));\n END LOOP;\nEND;\n/\n\ + INSERT INTO stroke_child VALUES (1, 1);\n"; + for s in split_statements(script) { + exec(&h, &s).await; + } + + let schemas = list_schemas(&h).await.unwrap(); + assert_eq!(schemas.first().map(String::as_str), Some("STROKE"), "{schemas:?}"); + + let tables = list_tables(&h, "STROKE").await.unwrap(); + let t = tables.iter().find(|t| t.name == "STROKE_TYPES").expect("table listed"); + assert_eq!(t.kind, "table"); + assert_eq!(t.row_count, -1, "small tables get an exact count later"); + assert_eq!(exact_row_count(&h, "STROKE", "STROKE_TYPES").await.unwrap(), 30); + + let cols = get_column_structure(&h, "STROKE", "STROKE_TYPES").await.unwrap(); + let col = |n: &str| cols.iter().find(|c| c.name == n).unwrap_or_else(|| panic!("{n}: {cols:?}")); + assert_eq!(col("ID").data_type, "NUMBER(9)"); + assert!(col("ID").is_primary_key); + assert_eq!(col("NAME").data_type, "VARCHAR2(50 CHAR)"); + assert!(!col("NAME").is_nullable); + assert_eq!(col("NAME").comment.as_deref(), Some("display name")); + assert_eq!(col("PRICE").data_type, "NUMBER(10,2)"); + assert_eq!(col("SEEN_TZ").data_type, "TIMESTAMP(6) WITH TIME ZONE"); + assert_eq!(col("CREATED").column_default.as_deref(), Some("SYSDATE")); + let child = get_column_structure(&h, "STROKE", "STROKE_CHILD").await.unwrap(); + assert_eq!(child[1].foreign_key.as_deref(), Some("STROKE.STROKE_TYPES.ID"), "{child:?}"); + let inc = incoming_foreign_keys(&h, "STROKE", "STROKE_TYPES").await.unwrap(); + assert_eq!(inc.len(), 1); + assert_eq!(inc[0].from_table, "STROKE_CHILD"); + let all = schema_column_structure(&h, "STROKE").await.unwrap(); + assert!(all.iter().any(|t| t.table == "STROKE_TYPES" && t.columns.len() == 12)); + let idx = list_indexes(&h, "STROKE").await.unwrap(); + assert!(idx.iter().any(|i| i.table_name == "STROKE_TYPES" && i.is_primary && i.columns == "ID"), "{idx:?}"); + let ddl = get_ddl(&h, "STROKE", "STROKE_TYPES").await.unwrap(); + assert!(ddl.contains("CREATE TABLE") && ddl.contains("\"STROKE_TYPES\""), "{ddl}"); + + // Page 2 of 10, sorted by price descending. + let page = get_table_rows(&h, "STROKE", "STROKE_TYPES", 10, 10, None, Some("PRICE".into()), Some("desc".into()), None, true) + .await + .unwrap(); + assert_eq!(page.total, 30); + assert_eq!(page.rows.len(), 10); + assert_eq!(page.primary_key, vec!["ID".to_string()]); + let names: Vec<&str> = page.columns.iter().map(|c| c.name.as_str()).collect(); + let at = |n: &str| names.iter().position(|c| *c == n).unwrap(); + let first = &page.rows[0]; + assert_eq!(first[at("ID")], Value::from(20)); + assert_eq!(first[at("PRICE")], serde_json::json!(20.5)); + assert_eq!(first[at("RATIO")], serde_json::json!(5.0)); + assert_eq!(first[at("NNAME")], Value::from("ñ20")); + assert_eq!(first[at("BORN")], Value::from("2024-01-22 00:00:00")); + assert_eq!(first[at("SEEN")], Value::from("2024-01-02 03:04:05.123456")); + assert_eq!(first[at("SEEN_TZ")], Value::from("2024-01-02 03:04:05 +05:30")); + assert_eq!(first[at("NOTE")], Value::from("clob 20")); + assert_eq!(first[at("DOC")], Value::from("")); + assert_eq!(first[at("TAG")], Value::from("0A0B0C0D")); + assert_eq!(page.columns[at("BORN")].data_type, "DATETIME"); + assert!(!page.columns[at("NAME")].nullable); + assert!(page.columns[at("CREATED")].has_default); + + // Search + filters, case-insensitively. + let f = |c: &str, op: &str, v: &str| RowFilter { column: c.into(), op: op.into(), value: Some(v.into()), conjunct: None, data_type: None }; + let r = get_table_rows(&h, "STROKE", "STROKE_TYPES", 50, 0, Some("NAME 1".into()), None, None, None, false).await.unwrap(); + assert_eq!(r.total, 11, "name 1, name 10..19"); + let r = get_table_rows(&h, "STROKE", "STROKE_TYPES", 50, 0, None, None, None, Some(vec![f("PRICE", "gte", "25.5"), f("NOTE", "contains", "CLOB 2")]), false) + .await + .unwrap(); + assert_eq!(r.total, 5, "25..29"); + let r = get_table_rows(&h, "STROKE", "STROKE_TYPES", 50, 0, None, None, None, Some(vec![f("BORN", "between", "2024-01-05,2024-01-07")]), false) + .await + .unwrap(); + assert_eq!(r.total, 3); + let r = get_table_rows(&h, "STROKE", "STROKE_TYPES", 50, 0, None, None, None, Some(vec![f("TAG", "eq", "0a0b0c0d")]), false).await; + assert_eq!(r.unwrap().total, 30); + let r = get_table_rows(&h, "STROKE", "STROKE_TYPES", 50, 0, None, None, None, Some(vec![f("", "sql", "ID < 4 AND name LIKE 'name%'")]), false) + .await + .unwrap(); + assert_eq!(r.total, 3); + + // Edits, with values in the formats the grid shows. + let pk = |id: i64| HashMap::from([("ID".to_string(), Value::from(id))]); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "NAME", &Value::from("it's three")).await.unwrap(); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "PRICE", &serde_json::json!(0.1)).await.unwrap(); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "BORN", &Value::from("1999-12-31 23:59:58")).await.unwrap(); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "SEEN", &Value::from("2001-02-03 04:05:06.5")).await.unwrap(); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "SEEN_TZ", &Value::from("2001-02-03 04:05:06 -07:00")).await.unwrap(); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "TAG", &Value::from("FFEE")).await.unwrap(); + update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(3), "NNAME", &Value::Null).await.unwrap(); + assert!(update_table_cell(&h, "STROKE", "STROKE_TYPES", pk(999), "NAME", &Value::from("x")).await.is_err()); + let row = exec(&h, "SELECT name, price, born, seen, seen_tz, tag, nname FROM stroke_types WHERE id = 3").await.rows.remove(0); + assert_eq!( + row, + vec![ + Value::from("it's three"), + serde_json::json!(0.1), + Value::from("1999-12-31 23:59:58"), + Value::from("2001-02-03 04:05:06.5"), + Value::from("2001-02-03 04:05:06 -07:00"), + Value::from("FFEE"), + Value::Null + ] + ); + + let inserted = insert_table_row( + &h, + "STROKE", + "STROKE_TYPES", + HashMap::from([("ID".to_string(), Value::from(100)), ("NAME".to_string(), Value::from("new")), ("PRICE".to_string(), Value::from("12.34"))]), + ) + .await + .unwrap(); + assert_eq!(inserted.len(), 12, "the whole stored row comes back"); + assert_eq!(inserted[0], Value::from(100)); + assert!(inserted[11].as_str().is_some_and(|s| s.len() == 19), "DEFAULT SYSDATE filled in: {:?}", inserted[11]); + + let deleted = delete_table_rows(&h, "STROKE", "STROKE_TYPES", vec![pk(100), pk(30), pk(29)]).await.unwrap(); + assert_eq!(deleted, 3); + // A failing batch deletes nothing: row 1 is referenced by STROKE_CHILD. + assert!(delete_table_rows(&h, "STROKE", "STROKE_TYPES", vec![pk(28), pk(1)]).await.is_err()); + assert_eq!(exact_row_count(&h, "STROKE", "STROKE_TYPES").await.unwrap(), 28); + + // Read only: queries run, writes are refused and leave nothing behind. + let r = execute_sql_readonly(&h, "SELECT COUNT(*) AS n FROM stroke_types").await.unwrap(); + assert_eq!(r.rows, vec![vec![Value::from(28)]]); + let e = execute_sql_readonly(&h, "DELETE FROM stroke_types").await.unwrap_err(); + assert!(e.contains("ORA-01456"), "{e}"); + assert_eq!(exact_row_count(&h, "STROKE", "STROKE_TYPES").await.unwrap(), 28); + + // Messages, the explain plan and an error the UI can show. + assert_eq!(exec(&h, "UPDATE stroke_types SET ratio = 1 WHERE id < 5;").await.message.as_deref(), Some("4 row(s) affected")); + assert_eq!(exec(&h, "begin null; end;").await.message.as_deref(), Some("PL/SQL procedure successfully completed")); + let plan = explain_lines(&h, "SELECT * FROM stroke_types WHERE id = 5").await.unwrap(); + assert!(plan.iter().any(|l| l.contains("INDEX UNIQUE SCAN") || l.contains("TABLE ACCESS")), "{plan:?}"); + let e = execute_sql(&h, "SELECT * FROM no_such_table").await.unwrap_err(); + assert!(e.starts_with("Oracle error: ORA-00942"), "{e}"); + + // A view (no ROWID to page by) and a table without a key (ROWID order, read-only grid). + exec(&h, "CREATE OR REPLACE VIEW stroke_v AS SELECT id, name FROM stroke_types").await; + let _ = execute_sql(&h, "DROP TABLE stroke_nokey PURGE").await; + exec(&h, "CREATE TABLE stroke_nokey (a NUMBER)").await; + exec(&h, "INSERT INTO stroke_nokey SELECT LEVEL FROM dual CONNECT BY LEVEL <= 3").await; + let tables = list_tables(&h, "").await.unwrap(); + assert!(tables.iter().any(|t| t.name == "STROKE_V" && t.kind == "view" && t.row_count == 0), "{tables:?}"); + let v = get_table_rows(&h, "STROKE", "STROKE_V", 5, 0, None, None, None, None, true).await.unwrap(); + assert_eq!((v.total, v.rows.len(), v.primary_key.len()), (28, 5, 0)); + let k = get_table_rows(&h, "STROKE", "STROKE_NOKEY", 2, 1, None, None, None, None, true).await.unwrap(); + assert!(k.sql.contains("ORDER BY ROWID"), "{}", k.sql); + assert_eq!(k.rows, vec![vec![Value::from(2)], vec![Value::from(3)]]); + assert!(update_table_cell(&h, "STROKE", "STROKE_NOKEY", HashMap::new(), "A", &Value::from(9)).await.is_err()); + + exec(&h, "DROP VIEW stroke_v").await; + exec(&h, "DROP TABLE stroke_nokey PURGE").await; + exec(&h, "DROP TABLE stroke_child PURGE").await; + exec(&h, "DROP TABLE stroke_types PURGE").await; + } + + #[tokio::test] + #[ignore] + async fn live_cancel_interrupts_the_call() { + let Some(h) = live().await else { return }; + let slow = "SELECT COUNT(*) FROM all_objects a, all_objects b, all_objects c"; + let run = { + let h = h.clone(); + tokio::spawn(async move { execute_sql(&h, slow).await }) + }; + tokio::time::sleep(std::time::Duration::from_millis(800)).await; + let t0 = Instant::now(); + cancel(&h).await; + let r = run.await.unwrap(); + assert_eq!(r.unwrap_err(), super::super::query::QUERY_CANCELLED); + assert!(t0.elapsed() < std::time::Duration::from_secs(10)); + // The session is still usable. + ping(&h).await.unwrap(); + } + + #[tokio::test] + #[ignore] + async fn live_connect_forms_and_errors() { + // The other ways to say where: fields, and a full descriptor. + let Some(mut cfg) = live_config() else { return }; + cfg.connect_string = String::new(); + cfg.host = "127.0.0.1".into(); + cfg.service_name = "FREEPDB1".into(); + ping(&crate::db::connection::open_oracle(&cfg).await.expect("fields")).await.unwrap(); + cfg.connect_string = "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=127.0.0.1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=FREEPDB1)))".into(); + ping(&crate::db::connection::open_oracle(&cfg).await.expect("descriptor")).await.unwrap(); + cfg.connect_string = String::new(); + cfg.sid = "NO_SUCH_SID".into(); + let e = crate::db::connection::open_oracle(&cfg).await.err().expect("must fail"); + assert!(e.contains("ORA-12505"), "{e}"); + + let Some(mut cfg) = live_config() else { return }; + cfg.connect_string = "//127.0.0.1:1521/NO_SUCH_SERVICE".into(); + let e = crate::db::connection::open_oracle(&cfg).await.err().expect("must fail"); + assert!(e.contains("ORA-12514") && e.contains("service name"), "{e}"); + let Some(mut cfg) = live_config() else { return }; + cfg.password = "wrong".into(); + let e = crate::db::connection::open_oracle(&cfg).await.err().expect("must fail"); + assert!(e.contains("ORA-01017"), "{e}"); + } + + /// Run alone, with no Instant Client anywhere the app looks (no + /// STROKE_ORACLE_CLIENT_DIR, no LD_LIBRARY_PATH entry): + /// STROKE_ORACLE_EXPECT_NO_CLIENT=1 cargo test --lib live_missing_client -- --ignored + #[tokio::test] + #[ignore] + async fn live_missing_client_is_explained() { + if std::env::var("STROKE_ORACLE_EXPECT_NO_CLIENT").is_err() { + return; + } + let e = crate::db::connection::test_oracle_connection(cfg()).await.unwrap_err(); + assert!(e.starts_with("Oracle Instant Client is required"), "{e}"); + let mut c = cfg(); + c.client_lib_dir = "/nonexistent/instantclient".into(); + let e = crate::db::connection::test_oracle_connection(c).await.unwrap_err(); + assert!(e.contains("/nonexistent/instantclient"), "{e}"); + } +} diff --git a/src-tauri/src/db/parked.rs b/src-tauri/src/db/parked.rs new file mode 100644 index 00000000..6c84f0ff --- /dev/null +++ b/src-tauri/src/db/parked.rs @@ -0,0 +1,97 @@ +//! Labs, several connections at once. A switch sets the open pool aside +//! instead of closing it; switching back takes it out again, so returning to a +//! database costs nothing. Only the pool handle moves (it is an Arc inside), +//! so this never touches the network. + +use super::connection::{close_in_background, Db}; + +/// More than this many set-aside pools and the oldest is closed. +const MAX_PARKED: usize = 8; + +/// Set the open connection aside under `key`. Nothing open: nothing to do. +#[tauri::command] +pub fn park_connection(state: Db<'_>, key: String) -> Result<(), String> { + let Some(conn) = state.conn.lock().map_err(|e| e.to_string())?.take() else { + return Ok(()); + }; + *state.active_key.lock().map_err(|e| e.to_string())? = None; + let mut parked = state.parked.lock().map_err(|e| e.to_string())?; + parked.retain(|(k, _)| k != &key); + parked.push((key, conn)); + while parked.len() > MAX_PARKED { + let (_, old) = parked.remove(0); + close_in_background(old); + } + Ok(()) +} + +/// Make the pool set aside under `key` the open one. False when there is none +/// (the page then connects as usual). +#[tauri::command] +pub fn resume_connection(state: Db<'_>, key: String) -> Result { + let found = { + let mut parked = state.parked.lock().map_err(|e| e.to_string())?; + parked.iter().position(|(k, _)| k == &key).map(|i| parked.remove(i).1) + }; + let Some(conn) = found else { return Ok(false) }; + let previous = state.conn.lock().map_err(|e| e.to_string())?.replace(conn); + *state.active_key.lock().map_err(|e| e.to_string())? = Some(key); + if let Some(prev) = previous { + close_in_background(prev); + } + Ok(true) +} + +/// Close set-aside pools: one by key, every database of one saved connection +/// (`connection`, its id: it was edited or deleted, so a kept pool would point +/// at the old server or login), or all of them (Labs turned off). +#[tauri::command] +pub fn close_parked( + state: Db<'_>, + key: Option, + connection: Option, +) -> Result<(), String> { + let prefix = connection.map(|id| format!("{id}|")); + let mut parked = state.parked.lock().map_err(|e| e.to_string())?; + let (gone, kept): (Vec<_>, Vec<_>) = parked.drain(..).partition(|(k, _)| match (&key, &prefix) { + (Some(want), _) => want == k, + (None, Some(p)) => k.starts_with(p.as_str()), + (None, None) => true, + }); + *parked = kept; + for (_, c) in gone { + close_in_background(c); + } + Ok(()) +} + +/// Which connections are set aside, oldest first. +#[tauri::command] +pub fn parked_connections(state: Db<'_>) -> Result, String> { + Ok(state.parked.lock().map_err(|e| e.to_string())?.iter().map(|(k, _)| k.clone()).collect()) +} + +/// The page's key for the pool now in front, after it connects fresh (a +/// resume sets it itself). Routed calls compare against it. +#[tauri::command] +pub fn set_active_conn_key(state: Db<'_>, key: Option) -> Result<(), String> { + *state.active_key.lock().map_err(|e| e.to_string())? = key.filter(|k| !k.is_empty()); + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::super::connection::ActiveConnection; + use super::*; + + /// park/resume/close_parked are sync commands: they run on a thread with + /// no Tokio runtime. Closing a pool there used to call tokio::spawn, which + /// panics outside a runtime, and release builds abort on a panic: turning + /// Labs off or disconnecting closed the app. + #[test] + fn closing_a_pool_outside_a_runtime_does_not_panic() { + let pool = tauri::async_runtime::block_on(sqlx::SqlitePool::connect("sqlite::memory:")).unwrap(); + let handle = std::thread::spawn(move || close_in_background(ActiveConnection::Sqlite(pool))); + handle.join().expect("close_in_background panicked outside a runtime"); + } +} diff --git a/src-tauri/src/db/posthog.rs b/src-tauri/src/db/posthog.rs index d4878ec0..02dd8844 100644 --- a/src-tauri/src/db/posthog.rs +++ b/src-tauri/src/db/posthog.rs @@ -263,7 +263,7 @@ pub async fn list_tables(config: &PosthogConfig) -> Result, Strin _ => "table", }; let row_count = t["row_count"].as_f64().map(|n| n as i64).unwrap_or(-1); - Some(TableInfo { name, kind: kind.to_string(), row_count, rls_enabled: None, created_at: None, create_order: None }) + Some(TableInfo { name, kind: kind.to_string(), row_count, rls_enabled: None, created_at: None, create_order: None, system: None }) }) .collect() }) diff --git a/src-tauri/src/db/query.rs b/src-tauri/src/db/query.rs index 712049c1..3edeb457 100644 --- a/src-tauri/src/db/query.rs +++ b/src-tauri/src/db/query.rs @@ -1,4 +1,5 @@ -use super::connection::{require_conn, require_pool, ActiveConnection, DbState}; +use crate::db::connection::Db; +use super::connection::{require_conn, require_pool, ActiveConnection}; use super::schema::validate_ident; use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime, Utc}; use futures::TryStreamExt; @@ -9,7 +10,6 @@ use sqlx::{Column, Decode, Postgres, Row, TypeInfo, ValueRef}; use std::collections::HashMap; use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; use std::sync::Arc; -use tauri::State; use uuid::Uuid; #[derive(Debug, Clone, Serialize)] @@ -1333,6 +1333,137 @@ pub(super) async fn fetch_table_column_names( .collect()) } +/// A column as the grid search sees it: its name and its type's category +/// (`pg_type.typcategory`: N numeric, D date/time, B boolean, S string, ...). +#[derive(Clone, Debug)] +pub(super) struct SearchColumn { + pub name: String, + pub category: char, + pub type_name: String, +} + +static SEARCH_COLUMNS_CACHE: std::sync::OnceLock< + std::sync::Mutex)>>, +> = std::sync::OnceLock::new(); + +/// Columns with their types, for the search predicate. Cached like the names. +pub(super) async fn cached_search_columns( + pool: &sqlx::PgPool, + schema: &str, + table: &str, + refresh: bool, +) -> Result, String> { + let key = pool_table_key(pool, schema, table); + let cache = SEARCH_COLUMNS_CACHE.get_or_init(|| std::sync::Mutex::new(HashMap::new())); + if !refresh { + if let Some((at, cols)) = cache.lock().ok().and_then(|m| m.get(&key).cloned()) { + if at.elapsed() < COLUMN_NAMES_TTL { + return Ok(cols); + } + } + } + validate_ident(schema)?; + validate_ident(table)?; + let rows = sqlx::query( + r#" + SELECT a.attname::text, t.typcategory::text, t.typname::text + FROM pg_attribute a + JOIN pg_class c ON c.oid = a.attrelid + JOIN pg_namespace n ON n.oid = c.relnamespace + JOIN pg_type t ON t.oid = a.atttypid + WHERE n.nspname = $1 AND c.relname = $2 AND a.attnum > 0 AND NOT a.attisdropped + ORDER BY a.attnum + "#, + ) + .bind(schema) + .bind(table) + .fetch_all(pool) + .await + .map_err(|e| format!("Failed to load columns: {e}"))?; + let cols: Vec = rows + .iter() + .filter_map(|r| { + Some(SearchColumn { + name: r.try_get::(0).ok()?, + category: r.try_get::(1).ok()?.chars().next().unwrap_or('X'), + type_name: r.try_get::(2).ok().unwrap_or_default(), + }) + }) + .collect(); + if let Ok(mut m) = cache.lock() { + if m.len() > 4096 { + m.retain(|_, (at, _)| at.elapsed() < COLUMN_NAMES_TTL); + } + m.insert(key, (std::time::Instant::now(), cols.clone())); + } + Ok(cols) +} + +/// Whether a column's text form could contain `term` at all, judged by its +/// type alone. A number prints as digits, a sign, a point and an exponent, so +/// "Product 1" can never be inside one; skipping it saves casting every value +/// of it to text on every row of the scan, which on a 5M-row table was most +/// of a search's time. Conservative: anything unsure is searched. +pub(super) fn column_could_contain(col: &SearchColumn, term: &str) -> bool { + let lower = term.to_lowercase(); + match col.category { + // Numbers: 12.5, -3, 1e+21, NaN, Infinity. + 'N' if col.type_name != "money" => { + term.chars().all(|c| c.is_ascii_digit() || ".-+eE".contains(c)) + || "nan".contains(lower.as_str()) + || "-infinity".contains(lower.as_str()) + } + // Dates and times: 2026-11-15 13:00:00.5+00, 13:00, plus infinity and BC. + 'D' => { + term.chars().all(|c| c.is_ascii_digit() || "-:. +".contains(c)) + || "-infinity".contains(lower.as_str()) + || lower.trim() == "bc" + } + 'B' => "true".contains(lower.as_str()) || "false".contains(lower.as_str()), + _ if col.type_name == "uuid" => term.chars().all(|c| c.is_ascii_hexdigit() || c == '-'), + _ => true, + } +} + +/// The plain (not regex) search across `columns`, as fast as Postgres can run +/// it while matching what ILIKE / LIKE would. +/// +/// - Columns that can't contain the term are left out (column_could_contain); +/// with none left the predicate is FALSE and the scan never starts. +/// - A term without letters has no case, so case-insensitive is plain LIKE: +/// on 5M rows a numeric search took 6.4s with ILIKE and 1.4s with LIKE. +/// - Otherwise `lower(col) LIKE lower($1)`: the same rows as ILIKE, about +/// twice as fast (2.05s vs 1.2s on one text column). +fn build_plain_search( + builder: &mut QueryBuilder, + columns: &[SearchColumn], + term: &str, + case_sensitive: bool, +) { + let has_case = term.chars().any(|c| c.is_lowercase() || c.is_uppercase()); + let insensitive = !case_sensitive && has_case; + let searched: Vec<&SearchColumn> = columns.iter().filter(|c| column_could_contain(c, term)).collect(); + if searched.is_empty() { + builder.push_condition("FALSE".to_string(), None); + return; + } + let pattern = builder.push_bind(format!("%{}%", escape_ilike_pattern(term))); + let parts: Vec = searched + .iter() + .filter_map(|c| quoted_column(&c.name).ok().map(|col| (col, c))) + .map(|(col, c)| { + // Text needs no cast; everything else is searched as it prints. + let text = if c.category == 'S' { col } else { format!("{col}::text") }; + if insensitive { + format!("lower({text}) LIKE lower({pattern}) ESCAPE '\\'") + } else { + format!("{text} LIKE {pattern} ESCAPE '\\'") + } + }) + .collect(); + builder.push_condition(format!("({})", parts.join(" OR ")), None); +} + fn ensure_column(column: &str, allowed: &[String]) -> Result<(), String> { if allowed.iter().any(|c| c == column) { Ok(()) @@ -1620,11 +1751,26 @@ pub(super) fn build_where( search_is_regex: bool, search_case_sensitive: bool, filters: &[RowFilter], +) -> Result { + build_where_typed(columns, None, search, search_is_regex, search_case_sensitive, filters) +} + +/// build_where with the columns' types, which lets a plain search skip the +/// columns that can't match (see build_plain_search). +pub(super) fn build_where_typed( + columns: &[String], + search_columns: Option<&[SearchColumn]>, + search: Option<&str>, + search_is_regex: bool, + search_case_sensitive: bool, + filters: &[RowFilter], ) -> Result { let mut builder = QueryBuilder::new(); if let Some(term) = search.map(str::trim).filter(|s| !s.is_empty()) { - if search_is_regex { + if let (false, Some(typed)) = (search_is_regex, search_columns) { + build_plain_search(&mut builder, typed, term, search_case_sensitive); + } else if search_is_regex { let op = if search_case_sensitive { "~" } else { "~*" }; let pattern = builder.push_bind(term.to_string()); let parts: Vec = columns @@ -1786,6 +1932,7 @@ pub(crate) fn raw_filter_dialect(conn: &ActiveConnection) -> super::sql_util::Ra ActiveConnection::Mysql(_) => D::Mysql, ActiveConnection::Sqlite(_) | ActiveConnection::D1(_) | ActiveConnection::LibSql(_) => D::Sqlite, ActiveConnection::Duckdb(_) => D::Duckdb, + ActiveConnection::Oracle(_) => D::Oracle, ActiveConnection::Mssql(_) | ActiveConnection::Clickhouse(_) | ActiveConnection::Posthog(_) | ActiveConnection::Redis(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => D::Literal, } } @@ -1811,7 +1958,7 @@ pub(crate) fn validate_raw_filters(conn: &ActiveConnection, filters: Option<&[Ro } pub async fn get_table_rows( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, limit: i64, @@ -1906,6 +2053,11 @@ pub async fn get_table_rows( &h, &schema, &table, limit, offset, search, sort_column, sort_direction, filters, include_meta, ).await; } + ActiveConnection::Oracle(h) => { + return super::oracle::get_table_rows( + &h, &schema, &table, limit, offset, search, sort_column, sort_direction, filters, include_meta, + ).await; + } ActiveConnection::Postgres(_) => {} } let pool = require_pool(&state)?; @@ -1925,7 +2077,12 @@ pub async fn get_table_rows( } else { vec![] }; - let where_clause = build_where(&table_columns, search.as_deref(), search_is_regex, search_case_sensitive, &filters)?; + let search_columns = if has_search && !search_is_regex { + cached_search_columns(&pool, &schema, &table, include_meta).await.ok() + } else { + None + }; + let where_clause = build_where_typed(&table_columns, search_columns.as_deref(), search.as_deref(), search_is_regex, search_case_sensitive, &filters)?; let order_by = build_order_by( &table_columns, sort_column.as_deref(), @@ -2368,6 +2525,54 @@ const ROW_ESTIMATE_SQL: &str = "SELECT GREATEST(c.reltuples::bigint, COALESCE(s. LEFT JOIN pg_catalog.pg_stat_all_tables s ON s.relid = c.oid \ WHERE c.oid = $1::regclass"; +/// Long-running grid queries by key, so a newer one can cancel the older on +/// the server. Searching a large table runs a filtered COUNT(*) per settled +/// keystroke; each scans the whole table, and without this they all ran to +/// the end (or the 30s timeout) on the database while only the last one was +/// wanted. +struct Superseded { + /// key -> (ticket, backend pid) of the query in flight under that key. + running: std::sync::Mutex>, + next: std::sync::atomic::AtomicU64, +} + +impl Superseded { + /// Register `pid` under `key`, cancelling whatever ran there before. + fn claim(&self, pool: &sqlx::PgPool, key: &str, pid: i32) -> u64 { + let ticket = self.next.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let prev = self + .running + .lock() + .ok() + .and_then(|mut m| m.insert(key.to_string(), (ticket, pid))); + if let Some((_, old)) = prev { + if old != pid { + let pool = pool.clone(); + // Fire and forget: the old query's own caller sees 57014 and + // reports "unknown", which nobody is waiting for any more. + tokio::spawn(async move { + let _ = sqlx::query("SELECT pg_cancel_backend($1)").bind(old).execute(&pool).await; + }); + } + } + ticket + } + + /// Forget `key` if it still holds this ticket (a newer query may own it). + fn release(&self, key: &str, ticket: u64) { + if let Ok(mut m) = self.running.lock() { + if m.get(key).is_some_and(|(t, _)| *t == ticket) { + m.remove(key); + } + } + } +} + +static SUPERSEDED: std::sync::LazyLock = std::sync::LazyLock::new(|| Superseded { + running: std::sync::Mutex::new(HashMap::new()), + next: std::sync::atomic::AtomicU64::new(1), +}); + /// Row count for the main grid, fetched separately so `get_table_rows` can /// return rows immediately (include_count = false) while the UI fills the total /// in asynchronously. Mirrors the count logic in `get_table_rows`: planner @@ -2375,7 +2580,7 @@ const ROW_ESTIMATE_SQL: &str = "SELECT GREATEST(c.reltuples::bigint, COALESCE(s. /// (the WHERE clause bounds the scan). Non-Postgres engines return -1 - their /// `get_table_rows` already carries a real total, so the UI keeps that. pub async fn count_table_rows( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, search: Option, @@ -2386,6 +2591,8 @@ pub async fn count_table_rows( filters: Option>, // Skip the planner estimate and COUNT(*) a large table too. exact: bool, + // A newer count under the same key cancels this one (see Superseded). + supersede: Option, ) -> Result { validate_raw_filters(&require_conn(&state)?, filters.as_deref())?; match require_conn(&state)? { @@ -2407,7 +2614,14 @@ pub async fn count_table_rows( } else { vec![] }; - let where_clause = build_where(&table_columns, search.as_deref(), search_is_regex, search_case_sensitive, &filters)?; + // The same typed predicate as the rows query, or the pager would count + // something other than what the grid shows. + let search_columns = if has_search && !search_is_regex { + cached_search_columns(&pool, &schema, &table, false).await.ok() + } else { + None + }; + let where_clause = build_where_typed(&table_columns, search_columns.as_deref(), search.as_deref(), search_is_regex, search_case_sensitive, &filters)?; let table_ref = format!(r#""{schema}"."{table}""#); const ESTIMATE_THRESHOLD: i64 = 100_000; @@ -2446,7 +2660,22 @@ pub async fn count_table_rows( let _ = sqlx::query(&format!("SET LOCAL statement_timeout = '{GRID_COUNT_TIMEOUT}'")) .execute(&mut *tx) .await; + // This backend's pid, only when a newer count may need to cancel this one. + // Inside the transaction it is the backend that runs the COUNT, even behind + // a transaction-pooling proxy, so cancelling it can't hit anything else. + let pid: Option = if supersede.is_some() { + sqlx::query_scalar::<_, i32>("SELECT pg_backend_pid()").fetch_one(&mut *tx).await.ok() + } else { + None + }; + let ticket = match (&supersede, pid) { + (Some(key), Some(pid)) => Some(SUPERSEDED.claim(&pool, key, pid)), + _ => None, + }; let count = count_query.fetch_one(&mut *tx).await; + if let (Some(key), Some(t)) = (&supersede, ticket) { + SUPERSEDED.release(key, t); + } // COMMIT resets SET LOCAL exactly like ROLLBACK does, and unlike ROLLBACK it // is accepted by Nile's proxy (see execute_sql_pg). let _ = tx.commit().await; @@ -2460,7 +2689,7 @@ pub async fn count_table_rows( } pub async fn update_table_cell( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_key: HashMap, @@ -2496,6 +2725,9 @@ pub async fn update_table_cell( ActiveConnection::Mssql(h) => { return super::mssql::update_table_cell(&h, &schema, &table, primary_key, &column, &value).await; } + ActiveConnection::Oracle(h) => { + return super::oracle::update_table_cell(&h, &schema, &table, primary_key, &column, &value).await; + } ActiveConnection::Postgres(_) => {} } let pool = require_pool(&state)?; @@ -2676,7 +2908,7 @@ pub(crate) async fn pg_insert_meta( } pub async fn insert_table_row( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, values: HashMap, @@ -2716,6 +2948,10 @@ pub async fn insert_table_row( let row = super::duckdb::insert_table_row(&h, &table, values).await?; return Ok(InsertRowResult { row }); } + ActiveConnection::Oracle(h) => { + let row = super::oracle::insert_table_row(&h, &schema, &table, values).await?; + return Ok(InsertRowResult { row }); + } ActiveConnection::Mssql(h) => { let row = super::mssql::insert_table_row(&h, &schema, &table, values).await?; return Ok(InsertRowResult { row }); @@ -2798,7 +3034,7 @@ pub async fn insert_table_row( } pub async fn delete_table_row( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_key: HashMap, @@ -2811,7 +3047,7 @@ pub async fn delete_table_row( } pub async fn delete_table_rows( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_keys: Vec>, @@ -2845,6 +3081,9 @@ pub async fn delete_table_rows( ActiveConnection::Mssql(h) => { return super::mssql::delete_table_rows(&h, &schema, &table, primary_keys).await; } + ActiveConnection::Oracle(h) => { + return super::oracle::delete_table_rows(&h, &schema, &table, primary_keys).await; + } ActiveConnection::Postgres(_) => {} } let pool = require_pool(&state)?; @@ -3024,7 +3263,7 @@ pub(crate) fn describe_pk(pk_columns: &[String], primary_key: &HashMap, + state: Db<'_>, schema: String, table: String, updates: Vec, @@ -3275,7 +3514,7 @@ pub(crate) fn is_row_returning_sql(sql: &str) -> bool { /// Execute a single DDL statement that must run outside a transaction (e.g. /// CREATE DATABASE), directly on the connection, on any engine that has DDL. -pub async fn execute_ddl(state: State<'_, DbState>, sql: String) -> Result<(), String> { +pub async fn execute_ddl(state: Db<'_>, sql: String) -> Result<(), String> { let sql_str = sql.trim(); if sql_str.is_empty() { return Err("Statement is empty".into()); @@ -3284,7 +3523,7 @@ pub async fn execute_ddl(state: State<'_, DbState>, sql: String) -> Result<(), S } pub async fn execute_sql( - state: State<'_, DbState>, + state: Db<'_>, sql: String, query_id: Option, ) -> Result { @@ -3308,6 +3547,20 @@ pub async fn execute_sql( // when the receiver fires. ActiveConnection::Postgres(pool) => execute_sql_pg(&pool, &sql_str, Some(cancel_rx)).await, ActiveConnection::Mysql(pool) => super::mysql::execute_sql(&pool, &sql_str, Some(cancel_rx)).await, + // Oracle interrupts the running call (OCIBreak); it then fails with + // ORA-01013, reported as a cancel. + ActiveConnection::Oracle(h) => { + let run = super::oracle::execute_sql(&h, &sql_str); + tokio::pin!(run); + tokio::select! { + r = &mut run => r, + _ = async { let _ = cancel_rx.await; } => { + super::oracle::cancel(&h).await; + let _ = run.await; + Err(QUERY_CANCELLED.to_string()) + } + } + } // Engines without a server-side cancel primitive: abandon the future when // cancelled so the UI unblocks (the statement then finishes or times out // server-side). This preserves the previous behavior for these engines. @@ -3324,7 +3577,7 @@ pub async fn execute_sql( ActiveConnection::Doc(d) => super::docdb::query(&d, &sql_str).await, ActiveConnection::Duckdb(h) => super::duckdb::execute_sql(&h, &sql_str).await, ActiveConnection::Mssql(h) => super::mssql::execute_sql(&h, &sql_str).await, - ActiveConnection::Postgres(_) | ActiveConnection::Mysql(_) => unreachable!(), + ActiveConnection::Postgres(_) | ActiveConnection::Mysql(_) | ActiveConnection::Oracle(_) => unreachable!(), } } => r, _ = async { let _ = cancel_rx.await; } => Err(QUERY_CANCELLED.to_string()), @@ -3375,6 +3628,7 @@ pub async fn execute_sql_on_conn( AnyConnectionConfig::Redis(c) => super::redis::query(&c, sql).await, AnyConnectionConfig::Dynamodb(c) => super::dynamodb::query(&c, sql).await, AnyConnectionConfig::Couchdb(c) => super::couchdb::query(&c, sql).await, + AnyConnectionConfig::Elasticsearch(c) => super::elastic::query(&c, sql).await, AnyConnectionConfig::Mongodb(_) => Err("Run MongoDB queries from the MongoDB console".into()), AnyConnectionConfig::Duckdb(c) => { let h = super::connection::open_duckdb(&c).await?; @@ -3384,6 +3638,10 @@ pub async fn execute_sql_on_conn( let h = super::connection::open_mssql(&c).await?; super::mssql::execute_sql(&h, sql).await } + AnyConnectionConfig::Oracle(c) => { + let h = super::connection::open_oracle(&c).await?; + super::oracle::execute_sql(&h, sql).await + } }; if let Ok(r) = &mut result { r.sql = sql.to_string(); @@ -3990,6 +4248,15 @@ fn next_word(sql: &str, from: usize) -> (&str, usize) { /// or the BEGIN … END body of a routine or trigger (`CREATE TRIGGER … BEGIN /// UPDATE …; END;` is one statement). Comment-only fragments are dropped. /// Mirrors `src/lib/sql-statements.js`. +/// `split_sql_statements`, or the engine's own splitter where its scripts +/// differ: Oracle runs PL/SQL blocks to a `/` line and rejects a trailing `;`. +pub(crate) fn split_sql_for(conn: &ActiveConnection, sql: &str) -> Vec { + match conn { + ActiveConnection::Oracle(_) => super::oracle::split_statements(sql), + _ => split_sql_statements(sql), + } +} + pub(crate) fn split_sql_statements(sql: &str) -> Vec { let b = sql.as_bytes(); let n = b.len(); @@ -4212,7 +4479,7 @@ async fn run_sql_multi_pg( } pub async fn execute_sql_multi( - state: State<'_, DbState>, + state: Db<'_>, sql: String, query_id: Option, ) -> Result, String> { @@ -4224,7 +4491,7 @@ pub async fn execute_sql_multi( let (cancel_tx, cancel_rx) = tokio::sync::oneshot::channel::<()>(); let cancel_key = query_id.unwrap_or_else(|| "default".to_string()); super::connection::register_cancel(&state, &cancel_key, cancel_tx); - let stmts = split_sql_statements(&sql_str); + let stmts = split_sql_for(&conn, &sql_str); if stmts.is_empty() { super::connection::unregister_cancel(&state, &cancel_key); return Err("Query is empty".into()); @@ -4238,6 +4505,11 @@ pub async fn execute_sql_multi( super::connection::unregister_cancel(&state, &cancel_key); return result; } + // Oracle's blocking call outlives the dropped future; interrupt it too. + let oracle_h = match &conn { + ActiveConnection::Oracle(h) => Some(h.clone()), + _ => None, + }; let result = tokio::select! { r = async move { // Other engines: execute sequentially, one result set per statement. @@ -4256,7 +4528,12 @@ pub async fn execute_sql_multi( } Ok(results) } => r, - _ = async { let _ = cancel_rx.await; } => Err(QUERY_CANCELLED.to_string()), + _ = async { let _ = cancel_rx.await; } => { + if let Some(h) = &oracle_h { + super::oracle::cancel(h).await; + } + Err(QUERY_CANCELLED.to_string()) + } }; super::connection::unregister_cancel(&state, &cancel_key); result @@ -4277,6 +4554,7 @@ async fn run_one_other(conn: &ActiveConnection, stmt: &str) -> Result super::docdb::query(d, stmt).await, ActiveConnection::Duckdb(h) => super::duckdb::execute_sql(h, stmt).await, ActiveConnection::Mssql(h) => super::mssql::execute_sql(h, stmt).await, + ActiveConnection::Oracle(h) => super::oracle::execute_sql(h, stmt).await, } } @@ -4316,7 +4594,7 @@ impl StatementOutcome { /// A single Postgres statement streams through `sink` (into the result store, /// in store mode). A script's results come back in the reply. pub async fn execute_sql_script( - state: State<'_, DbState>, + state: Db<'_>, sql: String, query_id: Option, sink: Option<&mut RowSink>, @@ -4326,7 +4604,7 @@ pub async fn execute_sql_script( return Err("Query is empty".into()); } let conn = require_conn(&state)?; - let stmts = split_sql_statements(&sql_str); + let stmts = split_sql_for(&conn, &sql_str); if stmts.is_empty() { return Err("Query is empty".into()); } @@ -4415,6 +4693,9 @@ async fn run_script_other( tokio::select! { _ = run => {} _ = async { let _ = cancel_rx.await; } => { + if let ActiveConnection::Oracle(h) = conn { + super::oracle::cancel(h).await; + } out.lock().unwrap().push(StatementOutcome::failed("", 0, QUERY_CANCELLED.into())); } } @@ -4865,7 +5146,7 @@ const CELL_FETCH_HARD_MAX: i64 = 64 * 1024 * 1024; const CELL_FETCH_DEFAULT_MAX: i64 = 32 * 1024 * 1024; pub async fn fetch_cell_value( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, primary_key: HashMap, @@ -4979,7 +5260,7 @@ pub async fn fetch_cell_value( } pub async fn get_column_stats( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, column: String, @@ -5073,7 +5354,7 @@ pub async fn get_column_stats( /// MIN/MAX/DISTINCT on the column type (e.g. JSON/BLOB), so the panel still shows /// total + null counts instead of erroring. async fn column_stats_generic( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, column: String, @@ -5100,6 +5381,14 @@ async fn column_stats_generic( }; (t, quote_bracket(&column)) } + ActiveConnection::Oracle(_) => { + let t = if schema.is_empty() { + quote_double(&table) + } else { + format!("{}.{}", quote_double(&schema), quote_double(&table)) + }; + (t, quote_double(&column)) + } // SQLite, D1, LibSQL, DuckDB - double-quoted, single-namespace (no // PostgreSQL-style schema qualifier). _ => (quote_double(&table), quote_double(&column)), @@ -5160,6 +5449,7 @@ async fn dispatch_stats_sql(conn: &ActiveConnection, sql: &str) -> Result Err("Column statistics are not supported on DynamoDB".into()), ActiveConnection::Duckdb(h) => super::duckdb::execute_sql(h, sql).await, ActiveConnection::Mssql(h) => super::mssql::execute_sql(h, sql).await, + ActiveConnection::Oracle(h) => super::oracle::execute_sql(h, sql).await, } } @@ -5167,7 +5457,7 @@ async fn dispatch_stats_sql(conn: &ActiveConnection, sql: &str) -> Result) -> Result<(), String> { +pub async fn ping_connection(state: Db<'_>) -> Result<(), String> { let conn = require_conn(&state)?; match conn { // Remote pools: touch every idle connection, not just one (see keepalive_idle). @@ -5180,6 +5470,7 @@ pub async fn ping_connection(state: State<'_, DbState>) -> Result<(), String> { ActiveConnection::D1(_) | ActiveConnection::LibSql(_) | ActiveConnection::Clickhouse(_) | ActiveConnection::Redis(_) | ActiveConnection::Posthog(_) | ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Ok(()), ActiveConnection::Duckdb(h) => super::duckdb::execute_sql(&h, "SELECT 1").await.map(|_| ()), ActiveConnection::Mssql(h) => super::mssql::execute_sql(&h, "SELECT 1").await.map(|_| ()), + ActiveConnection::Oracle(h) => super::oracle::ping(&h).await, } } @@ -5547,3 +5838,184 @@ mod nosql_console_live { assert!(!run(&rc, "DBSIZE").await.rows.is_empty()); } } + +#[cfg(test)] +mod typed_search_tests { + use super::*; + + fn col(name: &str, category: char, type_name: &str) -> SearchColumn { + SearchColumn { name: name.into(), category, type_name: type_name.into() } + } + + fn cols() -> Vec { + vec![ + col("id", 'N', "int4"), + col("name", 'S', "text"), + col("price", 'N', "numeric"), + col("at", 'D', "timestamptz"), + col("ok", 'B', "bool"), + col("uid", 'U', "uuid"), + col("doc", 'U', "jsonb"), + ] + } + + fn sql(term: &str, case_sensitive: bool) -> String { + let names: Vec = cols().iter().map(|c| c.name.clone()).collect(); + build_where_typed(&names, Some(&cols()), Some(term), false, case_sensitive, &[]).unwrap().sql + } + + #[test] + fn a_word_skips_numbers_dates_and_uuids() { + let w = sql("Product 1", false); + assert!(w.contains(r#"lower("name") LIKE lower($1)"#), "{w}"); + assert!(w.contains(r#"lower("doc"::text)"#), "{w}"); + assert!(!w.contains(r#""id""#) && !w.contains(r#""price""#) && !w.contains(r#""at""#) && !w.contains(r#""uid""#), "{w}"); + assert!(!w.contains("ILIKE"), "{w}"); + } + + #[test] + fn digits_have_no_case_so_plain_like_everywhere_they_can_print() { + let w = sql("4999", false); + assert!(w.contains(r#""id"::text LIKE $1"#), "{w}"); + assert!(w.contains(r#""price"::text LIKE $1"#), "{w}"); + assert!(w.contains(r#""at"::text LIKE $1"#), "{w}"); + assert!(w.contains(r#""uid"::text LIKE $1"#), "{w}"); + assert!(!w.contains("lower("), "{w}"); + assert!(!w.contains(r#""ok""#), "{w}"); + } + + #[test] + fn nothing_could_match_so_nothing_is_scanned() { + let only_numbers = vec![col("id", 'N', "int4"), col("at", 'D', "date")]; + let w = build_where_typed(&["id".into(), "at".into()], Some(&only_numbers), Some("abc"), false, false, &[]).unwrap(); + assert_eq!(w.sql, " WHERE FALSE"); + } + + #[test] + fn match_case_keeps_the_case() { + let w = sql("Product", true); + assert!(w.contains(r#""name" LIKE $1"#) && !w.contains("lower("), "{w}"); + } + + #[test] + fn booleans_and_specials_still_match() { + assert!(column_could_contain(&col("ok", 'B', "bool"), "tru")); + assert!(column_could_contain(&col("x", 'N', "float8"), "NaN")); + assert!(column_could_contain(&col("x", 'D', "timestamp"), "infinity")); + assert!(column_could_contain(&col("x", 'D', "date"), "2026-11")); + assert!(!column_could_contain(&col("x", 'D', "date"), "nov")); + assert!(column_could_contain(&col("m", 'N', "money"), "$")); + } +} + +// ── Search index ───────────────────────────────────────────────────────────── +// A trigram GIN index on lower(column) for each text column makes the grid's +// `lower(col) LIKE lower($1)` search an index scan: on a 5M-row table a search +// went from 1.2-2s to 18-110ms. Offered from the grid when a search is slow; +// nothing here runs on its own. + +/// What the search index would cover, and what exists already. +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct SearchIndexStatus { + /// Text columns the search reads (the ones an index would cover). + pub text_columns: Vec, + /// Of those, the ones that already have a trigram index on lower(col). + pub indexed: Vec, + /// pg_trgm is installed, or can be installed here. + pub trgm_available: bool, + /// Planner estimate of the table's rows. + pub row_estimate: i64, +} + +/// Name for the index on one column, within Postgres' 63-byte limit. +fn search_index_name(table: &str, column: &str) -> String { + let mut name = format!("{table}_{column}_search_trgm"); + if name.len() > 63 { + let mut cut = 63; + while !name.is_char_boundary(cut) { cut -= 1; } + name.truncate(cut); + } + name +} + +pub async fn search_index_status( + state: Db<'_>, + schema: String, + table: String, +) -> Result { + let pool = require_pool(&state)?; + validate_ident(&schema)?; + validate_ident(&table)?; + let cols = cached_search_columns(&pool, &schema, &table, true).await?; + let text_columns: Vec = cols.iter().filter(|c| c.category == 'S').map(|c| c.name.clone()).collect(); + // Existing trigram indexes on lower(col) or col, by the columns they cover. + let defs: Vec = sqlx::query_scalar( + "SELECT indexdef FROM pg_indexes WHERE schemaname = $1 AND tablename = $2 AND indexdef ILIKE '%gin_trgm_ops%'", + ) + .bind(&schema) + .bind(&table) + .fetch_all(&pool) + .await + .unwrap_or_default(); + // Postgres prints the expression as `lower("Name")` or, for varchar, + // `lower((name)::text)`: compare with quotes, parens and the cast removed. + let flat = |d: &str| d.replace("::text", "").replace(['"', '(', ')'], ""); + let flat_defs: Vec = defs.iter().map(|d| flat(d)).collect(); + let indexed = text_columns + .iter() + .filter(|c| { + let want = format!("lower{c} gin_trgm_ops"); + flat_defs.iter().any(|d| d.contains(&want)) + }) + .cloned() + .collect(); + let trgm_available: bool = sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM pg_available_extensions WHERE name = 'pg_trgm')") + .fetch_one(&pool) + .await + .unwrap_or(false); + let row_estimate: i64 = sqlx::query_scalar::<_, i64>(ROW_ESTIMATE_SQL) + .bind(format!(r#""{schema}"."{table}""#)) + .fetch_optional(&pool) + .await + .ok() + .flatten() + .unwrap_or(-1); + Ok(SearchIndexStatus { text_columns, indexed, trgm_available, row_estimate }) +} + +/// Create the missing trigram indexes. CONCURRENTLY, so the table stays +/// readable and writable while they build; returns the indexes created. +pub async fn create_search_index( + state: Db<'_>, + schema: String, + table: String, +) -> Result, String> { + let pool = require_pool(&state)?; + let status = search_index_status(state, schema.clone(), table.clone()).await?; + if !status.trgm_available { + return Err("This server doesn't have the pg_trgm extension, which the search index needs.".into()); + } + sqlx::query("CREATE EXTENSION IF NOT EXISTS pg_trgm") + .execute(&pool) + .await + .map_err(|e| format!("Couldn't enable pg_trgm (it needs a role that can create extensions): {e}"))?; + let mut created = Vec::new(); + for col in status.text_columns.iter().filter(|c| !status.indexed.contains(c)) { + validate_ident(col)?; + let name = search_index_name(&table, col); + // CONCURRENTLY can't run in a transaction; a plain pool execute is + // its own statement. The statement timeout is lifted for it: a big + // table takes longer than the session default allows. + let mut conn = pool.acquire().await.map_err(|e| e.to_string())?; + let _ = sqlx::query("SET statement_timeout = 0").execute(&mut *conn).await; + let sql = format!( + r#"CREATE INDEX CONCURRENTLY IF NOT EXISTS "{name}" ON "{schema}"."{table}" USING gin (lower("{col}") gin_trgm_ops)"# + ); + let res = sqlx::query(&sql).execute(&mut *conn).await; + let _ = sqlx::query("RESET statement_timeout").execute(&mut *conn).await; + res.map_err(|e| format!("Couldn't index {col}: {e}"))?; + created.push(name); + } + Ok(created) +} diff --git a/src-tauri/src/db/schema.rs b/src-tauri/src/db/schema.rs index 5d37bd38..3495ccc2 100644 --- a/src-tauri/src/db/schema.rs +++ b/src-tauri/src/db/schema.rs @@ -1,7 +1,7 @@ -use super::connection::{require_conn, ActiveConnection, AnyConnectionConfig, DbState}; +use crate::db::connection::Db; +use super::connection::{require_conn, ActiveConnection, AnyConnectionConfig}; use serde::Serialize; use sqlx::{MySqlPool, PgPool, Row}; -use tauri::State; use super::mysql::{my_int, my_text, my_text_named}; @@ -21,6 +21,11 @@ pub struct TableInfo { /// of the SQLite catalog row, the DuckDB object id. Larger is newer. For /// sorting only; the numbers mean nothing on their own. pub create_order: Option, + /// The engine's own object rather than the user's (Oracle's + /// ORACLE_MAINTAINED). None where the engine doesn't say; the sidebar then + /// goes by name. + #[serde(skip_serializing_if = "Option::is_none")] + pub system: Option, } #[derive(Debug, Clone, Serialize)] @@ -214,7 +219,7 @@ pub struct TableRowCount { /// `list_tables` already returns exact counts inline (SQLite/D1/libSQL/…) /// never report -1, so they return an empty list here. pub async fn table_row_counts( - state: State<'_, DbState>, + state: Db<'_>, schema: String, tables: Vec, ) -> Result, String> { @@ -265,6 +270,21 @@ pub async fn table_row_counts( .await; Ok(counts) } + ActiveConnection::Oracle(h) => { + // One session: the counts run one after another. + let mut counts = Vec::with_capacity(tables.len()); + for name in tables { + let row_count = match super::oracle::exact_row_count(&h, &schema, &name).await { + Ok(n) => n, + Err(e) => { + eprintln!("[row count] {schema}.{name}: {e}"); + -1 + } + }; + counts.push(TableRowCount { name, row_count }); + } + Ok(counts) + } _ => Ok(Vec::new()), } } @@ -310,6 +330,7 @@ async fn list_tables_pg(pool: &PgPool, schema: &str) -> Result, S rls_enabled: r.try_get::(3).ok(), created_at: None, create_order: r.try_get::(4).ok(), + system: None, }) }) .collect(); @@ -369,7 +390,7 @@ async fn list_tables_sqlite(pool: &sqlx::SqlitePool) -> Result, S let ty = r.try_get::, _>(1).ok().flatten().unwrap_or_default(); let kind = if ty == "view" { "view".to_string() } else { "table".to_string() }; let create_order = r.try_get::(2).ok(); - Some(TableInfo { name, kind, row_count: -1, rls_enabled: None, created_at: None, create_order }) + Some(TableInfo { name, kind, row_count: -1, rls_enabled: None, created_at: None, create_order, system: None }) }) .collect(); @@ -523,7 +544,7 @@ async fn list_tables_d1(cfg: &super::connection::D1Config) -> Result Result Result= 100_000 { est } else { -1 }; // NULL for views; a real timestamp for tables. let created_at = my_text(r, 3); - Some(TableInfo { name, kind, row_count, rls_enabled: None, created_at, create_order: None }) + Some(TableInfo { name, kind, row_count, rls_enabled: None, created_at, create_order: None, system: None }) }) .collect(); @@ -1117,7 +1138,7 @@ async fn list_sequences_pg(pool: &PgPool, schema: &str) -> Result) -> Result, String> { +pub async fn list_schemas(state: Db<'_>) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => list_schemas_pg(&pool).await, ActiveConnection::Mysql(pool) => list_schemas_mysql(&pool).await, @@ -1127,11 +1148,12 @@ pub async fn list_schemas(state: State<'_, DbState>) -> Result, Stri ActiveConnection::Dynamodb(_) => Ok(vec![super::docdb::FLAT_SCHEMA.to_string()]), ActiveConnection::Doc(d) => super::docdb::list_schemas(&d).await, ActiveConnection::Mssql(h) => super::mssql::list_schemas(&h).await, + ActiveConnection::Oracle(h) => super::oracle::list_schemas(&h).await, ActiveConnection::Sqlite(_) | ActiveConnection::D1(_) | ActiveConnection::LibSql(_) | ActiveConnection::Duckdb(_) => Ok(vec!["main".to_string()]), } } -pub async fn list_tables(state: State<'_, DbState>, schema: String) -> Result, String> { +pub async fn list_tables(state: Db<'_>, schema: String) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { validate_ident(&schema)?; @@ -1159,6 +1181,7 @@ pub async fn list_tables(state: State<'_, DbState>, schema: String) -> Result super::docdb::list_tables_in(&d, &schema).await, ActiveConnection::Duckdb(h) => super::duckdb::list_tables(&h).await, ActiveConnection::Mssql(h) => super::mssql::list_tables(&h, &schema).await, + ActiveConnection::Oracle(h) => super::oracle::list_tables(&h, &schema).await, } } @@ -1174,7 +1197,7 @@ pub async fn list_tables(state: State<'_, DbState>, schema: String) -> Result, + state: Db<'_>, schema: String, table: Option, ) -> Result, String> { @@ -1198,6 +1221,7 @@ pub async fn list_indexes_for( ActiveConnection::Doc(d) => super::docdb::indexes_in(&d, &schema, table.as_deref()).await, ActiveConnection::Duckdb(h) => super::duckdb::list_indexes(&h).await, ActiveConnection::Mssql(h) => super::mssql::list_indexes(&h, &schema).await, + ActiveConnection::Oracle(h) => super::oracle::list_indexes(&h, &schema).await, } } @@ -1271,7 +1295,7 @@ async fn list_functions_mysql(pool: &sqlx::MySqlPool, schema: &str) -> Result, schema: String) -> Result, String> { +pub async fn list_functions(state: Db<'_>, schema: String) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { validate_ident(&schema)?; @@ -1285,7 +1309,7 @@ pub async fn list_functions(state: State<'_, DbState>, schema: String) -> Result } } -pub async fn list_enums(state: State<'_, DbState>, schema: String) -> Result, String> { +pub async fn list_enums(state: Db<'_>, schema: String) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { validate_ident(&schema)?; @@ -1295,7 +1319,7 @@ pub async fn list_enums(state: State<'_, DbState>, schema: String) -> Result, schema: String) -> Result, String> { +pub async fn list_triggers(state: Db<'_>, schema: String) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { validate_ident(&schema)?; @@ -1310,7 +1334,7 @@ pub async fn list_triggers(state: State<'_, DbState>, schema: String) -> Result< } } -pub async fn list_sequences(state: State<'_, DbState>, schema: String) -> Result, String> { +pub async fn list_sequences(state: Db<'_>, schema: String) -> Result, String> { match require_conn(&state)? { ActiveConnection::Postgres(pool) => { validate_ident(&schema)?; @@ -1325,19 +1349,19 @@ pub async fn list_sequences(state: State<'_, DbState>, schema: String) -> Result // Both spell the statement per engine in `admin`; this used to know Postgres // and SQLite only and told every other engine it was unsupported. -pub async fn truncate_table(state: State<'_, DbState>, schema: String, table: String) -> Result<(), String> { +pub async fn truncate_table(state: Db<'_>, schema: String, table: String) -> Result<(), String> { let conn = require_conn(&state)?; super::admin::truncate_table(&conn, &schema, &table).await } -pub async fn drop_table(state: State<'_, DbState>, schema: String, table: String, cascade: bool, kind: Option) -> Result<(), String> { +pub async fn drop_table(state: Db<'_>, schema: String, table: String, cascade: bool, kind: Option) -> Result<(), String> { let kind = super::admin::ObjectKind::parse(kind.as_deref())?; let conn = require_conn(&state)?; super::admin::drop_object(&conn, &schema, &table, kind, cascade).await } pub async fn get_table_column_structure( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result, String> { @@ -1347,6 +1371,8 @@ pub async fn get_table_column_structure( match require_conn(&state)? { ActiveConnection::Dynamodb(cfg) => return Ok(super::dynamodb::describe_shape(&cfg, &table).await?.0), ActiveConnection::Doc(d) => return super::docdb::columns_in(&d, &schema, &table).await, + // Oracle binds the names as values, and they may hold `$` or `#`. + ActiveConnection::Oracle(h) => return super::oracle::get_column_structure(&h, &schema, &table).await, _ => {} } validate_ident(&schema)?; @@ -1375,7 +1401,7 @@ pub async fn get_table_column_structure( super::redis::get_column_structure(&cfg, &table).await } // Answered above. - ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Ok(vec![]), + ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) | ActiveConnection::Oracle(_) => Ok(vec![]), ActiveConnection::Duckdb(h) => { super::duckdb::get_column_structure(&h, &table).await } @@ -1694,12 +1720,13 @@ async fn get_column_structure_pg( /// MongoDB, CouchDB, Firestore, DynamoDB and Redis have no schema-wide column /// catalog: they answer empty at once, and the caller reads per table. pub async fn get_schema_column_structure( - state: State<'_, DbState>, + state: Db<'_>, schema: String, ) -> Result, String> { match require_conn(&state)? { ActiveConnection::Doc(d) => return super::docdb::schema_columns_in(&d, &schema).await, ActiveConnection::Dynamodb(_) | ActiveConnection::Redis(_) => return Ok(vec![]), + ActiveConnection::Oracle(h) => return super::oracle::schema_column_structure(&h, &schema).await, _ => {} } validate_ident(&schema)?; @@ -2475,10 +2502,13 @@ async fn get_incoming_fks_mssql(handle: &super::connection::MssqlHandle, schema: } pub async fn get_incoming_foreign_keys( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result, String> { + if let ActiveConnection::Oracle(h) = require_conn(&state)? { + return super::oracle::incoming_foreign_keys(&h, &schema, &table).await; + } validate_ident(&schema)?; validate_ident(&table)?; match require_conn(&state)? { @@ -2647,10 +2677,13 @@ async fn get_ddl_libsql( } pub async fn get_table_ddl( - state: State<'_, DbState>, + state: Db<'_>, schema: String, table: String, ) -> Result { + if let ActiveConnection::Oracle(h) = require_conn(&state)? { + return super::oracle::get_ddl(&h, &schema, &table).await; + } validate_ident(&schema)?; validate_ident(&table)?; match require_conn(&state)? { @@ -2665,6 +2698,7 @@ pub async fn get_table_ddl( ActiveConnection::Dynamodb(_) | ActiveConnection::Doc(_) => Ok(String::new()), ActiveConnection::Duckdb(h) => super::duckdb::get_ddl(&h, &table).await, ActiveConnection::Mssql(h) => super::mssql::get_ddl(&h, &schema, &table).await, + ActiveConnection::Oracle(_) => unreachable!("answered above"), } } @@ -2687,6 +2721,10 @@ pub async fn list_schemas_on_conn(config: AnyConnectionConfig) -> Result super::clickhouse::list_schemas(&c).await, + AnyConnectionConfig::Oracle(c) => { + let h = super::connection::open_oracle(&c).await?; + super::oracle::list_schemas(&h).await + } _ => Ok(vec!["main".to_string()]), } } @@ -2725,6 +2763,7 @@ pub async fn list_tables_on_conn( AnyConnectionConfig::Redis(c) => super::redis::list_tables(&c).await.map(to_names), AnyConnectionConfig::Dynamodb(c) => super::dynamodb::list_tables(&c).await.map(to_names), AnyConnectionConfig::Couchdb(c) => super::couchdb::list_databases(&c).await, + AnyConnectionConfig::Elasticsearch(c) => Ok(super::elastic::list_indices(&c).await?.into_iter().map(|i| i.name).collect()), AnyConnectionConfig::Mongodb(_) => Ok(Vec::new()), AnyConnectionConfig::Duckdb(c) => { let h = super::connection::open_duckdb(&c).await?; @@ -2734,6 +2773,10 @@ pub async fn list_tables_on_conn( let h = super::connection::open_mssql(&c).await?; super::mssql::list_tables(&h, &schema).await.map(to_names) } + AnyConnectionConfig::Oracle(c) => { + let h = super::connection::open_oracle(&c).await?; + super::oracle::list_tables(&h, &schema).await.map(to_names) + } } } @@ -2744,6 +2787,10 @@ pub async fn get_table_ddl_on_conn( table: String, ) -> Result { use super::connection::{open_mysql, open_pg, open_sqlite}; + if let AnyConnectionConfig::Oracle(c) = &config { + let h = super::connection::open_oracle(c).await?; + return super::oracle::get_ddl(&h, &schema, &table).await; + } validate_ident(&schema)?; validate_ident(&table)?; match config { @@ -2770,7 +2817,10 @@ pub async fn get_table_ddl_on_conn( AnyConnectionConfig::Clickhouse(c) => super::clickhouse::get_ddl(&c, &schema, &table).await, AnyConnectionConfig::Posthog(c) => super::posthog::get_ddl(&c, &table).await, AnyConnectionConfig::Redis(c) => super::redis::get_ddl(&c, &table).await, - AnyConnectionConfig::Dynamodb(_) | AnyConnectionConfig::Couchdb(_) | AnyConnectionConfig::Mongodb(_) => Ok(String::new()), + AnyConnectionConfig::Dynamodb(_) + | AnyConnectionConfig::Couchdb(_) + | AnyConnectionConfig::Elasticsearch(_) + | AnyConnectionConfig::Mongodb(_) => Ok(String::new()), AnyConnectionConfig::Duckdb(c) => { let h = super::connection::open_duckdb(&c).await?; super::duckdb::get_ddl(&h, &table).await @@ -2779,6 +2829,7 @@ pub async fn get_table_ddl_on_conn( let h = super::connection::open_mssql(&c).await?; super::mssql::get_ddl(&h, &schema, &table).await } + AnyConnectionConfig::Oracle(_) => unreachable!("answered above"), } } diff --git a/src-tauri/src/db/sql_util.rs b/src-tauri/src/db/sql_util.rs index f151531a..962380b4 100644 --- a/src-tauri/src/db/sql_util.rs +++ b/src-tauri/src/db/sql_util.rs @@ -118,6 +118,8 @@ pub enum RawFilterDialect { Mysql, Sqlite, Duckdb, + /// Oracle: `:name` / `:1` are its bind placeholders. + Oracle, /// Engines whose filters are inlined literals with no bind placeholders /// (SQL Server, ClickHouse, HogQL). Literal, @@ -195,7 +197,7 @@ fn scan_raw_where(expr: &str, dialect: RawFilterDialect, backslash_escapes: bool '$' | '@' if dialect == Sqlite => { return Err(format!("'{c}' starts a query parameter here; write the value itself")) } - ':' if dialect == Sqlite && next.is_some_and(|n| n.is_ascii_alphanumeric() || n == '_') => { + ':' if matches!(dialect, Sqlite | Oracle) && next.is_some_and(|n| n.is_ascii_alphanumeric() || n == '_') => { return Err("':name' is a query parameter here; write the value itself".into()) } _ => {} diff --git a/src-tauri/src/db/terminal.rs b/src-tauri/src/db/terminal.rs index 7b20833f..105d4c27 100644 --- a/src-tauri/src/db/terminal.rs +++ b/src-tauri/src/db/terminal.rs @@ -28,8 +28,8 @@ use serde::{Deserialize, Serialize}; use tauri::State; use super::connection::{ - resolve_mysql_ssh, resolve_pg_ssh, AnyConnectionConfig, MssqlConfig, MysqlConfig, PgConfig, - RedisConfig, SqliteConfig, + resolve_mysql_ssh, resolve_pg_ssh, AnyConnectionConfig, MssqlConfig, MysqlConfig, OracleConfig, + PgConfig, RedisConfig, SqliteConfig, }; use super::ssh_tunnel::SshTunnel; @@ -48,7 +48,10 @@ struct Client { const PSQL_INSTALL: &str = if cfg!(target_os = "macos") { "brew install libpq" } else if cfg!(windows) { - "Install PostgreSQL from postgresql.org/download/windows. The Command Line Tools component alone is enough." + // No client-only package exists on Windows: this is the full PostgreSQL + // install (server and service included), which puts psql in + // Program Files\PostgreSQL\17\bin, where install_dirs() looks. + "winget install PostgreSQL.PostgreSQL.17" } else { "Install the PostgreSQL client: postgresql-client on Debian and Ubuntu, postgresql on Arch and Fedora." }; @@ -56,7 +59,9 @@ const PSQL_INSTALL: &str = if cfg!(target_os = "macos") { const MYSQL_INSTALL: &str = if cfg!(target_os = "macos") { "brew install mysql-client" } else if cfg!(windows) { - "Install the MySQL client from dev.mysql.com/downloads/installer, or MariaDB from mariadb.org/download. Either client works." + // As with psql, the client comes with the server: MariaDB's mysql.exe + // speaks to MySQL, TiDB and MariaDB, and lands in Program Files\MariaDB *\bin. + "winget install MariaDB.Server" } else { "Install a MySQL client: mysql-client or mariadb-client on Debian and Ubuntu, mariadb-clients on Arch." }; @@ -77,6 +82,13 @@ const SQLCMD_INSTALL: &str = if cfg!(target_os = "macos") { "Install go-sqlcmd from github.com/microsoft/go-sqlcmd/releases." }; +/// SQL*Plus ships as an Instant Client package; SQLcl (`sql`) is a separate +/// Java download. Neither has a package-manager install that works everywhere. +const SQLPLUS_INSTALL: &str = "Install SQL*Plus: download the Instant Client \"SQL*Plus\" package for your platform \ +from https://www.oracle.com/database/technologies/instant-client/downloads.html and unzip it into the same folder \ +as Instant Client Basic, then add that folder to PATH. Or install SQLcl (the `sql` command) from \ +https://www.oracle.com/database/sqldeveloper/technologies/sqlcl/."; + const REDIS_INSTALL: &str = if cfg!(target_os = "macos") { "brew install redis" } else if cfg!(windows) { @@ -158,6 +170,7 @@ fn client_for(config: &AnyConnectionConfig) -> Result { ), AnyConnectionConfig::Sqlite(_) => client("sqlite3", &["sqlite3"], SQLITE_INSTALL), AnyConnectionConfig::Mssql(_) => client("sqlcmd", &["sqlcmd"], SQLCMD_INSTALL), + AnyConnectionConfig::Oracle(_) => client("sqlplus", &["sqlplus", "sql"], SQLPLUS_INSTALL), AnyConnectionConfig::Redis(_) => client("redis-cli", &["redis-cli", "valkey-cli", "memurai-cli"], REDIS_INSTALL), AnyConnectionConfig::Duckdb(_) => Err( "DuckDB lets one process at a time open a database file, and Stroke already has this one open." @@ -181,7 +194,7 @@ fn client_for(config: &AnyConnectionConfig) -> Result { Err("PostHog is queried over its HTTP API and has no shell.".into()) } AnyConnectionConfig::Mongodb(_) => client("mongosh", &["mongosh", "mongo"], MONGOSH_INSTALL), - AnyConnectionConfig::Dynamodb(_) | AnyConnectionConfig::Couchdb(_) => { + AnyConnectionConfig::Dynamodb(_) | AnyConnectionConfig::Couchdb(_) | AnyConnectionConfig::Elasticsearch(_) => { Err("This database has no interactive shell here; use the query editor instead.".into()) } } @@ -245,10 +258,26 @@ async fn find_binary(names: &[&str]) -> Option { dirs.extend(install_dirs()); names.iter().find_map(|name| { let file = if cfg!(windows) { format!("{name}.exe") } else { (*name).to_string() }; - dirs.iter().map(|d| d.join(&file)).find(|p| is_executable(p)) + dirs.iter() + .map(|d| d.join(&file)) + .find(|p| is_executable(p) && (*name != "sql" || !is_other_sql(p))) }) } +/// A `sql` that is not Oracle's SQLcl. GNU Parallel (Homebrew's `parallel`, +/// Debian's `parallel`) installs a Perl script named `sql` that takes a +/// "dburl" and exits on anything else, so it would shadow SQLcl on PATH. +fn is_other_sql(path: &Path) -> bool { + use std::io::Read; + let Ok(file) = std::fs::File::open(path) else { return false }; + let mut head = Vec::new(); + if file.take(64 * 1024).read_to_end(&mut head).is_err() { + return false; + } + let text = String::from_utf8_lossy(&head).to_ascii_lowercase(); + text.contains("gnu parallel") || text.contains("dburl") +} + /// Client installs that stay off PATH: keg-only Homebrew formulas (libpq, /// mysql-client), Postgres.app, Debian's per-version PostgreSQL directories, /// and the Windows installers, which give every version a folder of its own. @@ -268,8 +297,31 @@ fn install_dirs() -> Vec { ] { out.push(PathBuf::from(d)); } + // SQLcl from the Homebrew cask, which links no `sql` onto PATH when + // GNU Parallel already owns that name. + for caskroom in ["/opt/homebrew/Caskroom/sqlcl", "/usr/local/Caskroom/sqlcl"] { + out.extend(versioned(Path::new(caskroom), "", "sqlcl/bin")); + } + out.push(PathBuf::from("/opt/oracle/sqlcl/bin")); out.extend(versioned(Path::new("/usr/lib/postgresql"), "", "bin")); } + // SQL*Plus unzipped into the Instant Client folder, wherever the Oracle + // connection itself looks for it (home, Downloads, /opt/oracle, C:\oracle), + // and SQLcl unzipped next to it. + out.extend(super::oracle::client_dir_candidates("")); + if let Some(home) = home_dir() { + // Downloads only off macOS: touching it there raises the "would like + // to access files in your Downloads folder" prompt (see oracle.rs). + let mut parents = vec![home.clone(), home.join("oracle")]; + if !cfg!(target_os = "macos") { + parents.push(home.join("Downloads")); + } + for parent in parents { + out.push(parent.join("sqlcl").join("bin")); + } + } + #[cfg(windows)] + out.push(PathBuf::from("C:\\oracle\\sqlcl\\bin")); // winget and the per-user MSI put mongosh here, which a running app's PATH // often does not include until the next sign-in. #[cfg(windows)] @@ -499,6 +551,13 @@ async fn mysql_launch(config: MysqlConfig, mariadb: bool) -> Result args.extend(["--ssl".into(), "--skip-ssl-verify-server-cert".into()]), } + // On Windows the MariaDB client does TLS through Schannel, whose TLS 1.3 + // fails against TiDB Cloud and other servers with "ERROR 2026 ... The + // specified data could not be decrypted (SEC_E_DECRYPT_FAILURE)". + // TLS 1.2 is what those servers and the client agree on. + if cfg!(windows) && mode != "DISABLED" { + args.push("--tls-version=TLSv1.2".into()); + } } else { args.push(format!("--ssl-mode={mode}")); } @@ -568,6 +627,26 @@ fn mssql_launch(c: &MssqlConfig) -> Launch { } } +/// `sqlplus user@connect_string`. Neither SQL*Plus nor SQLcl reads a password +/// from the environment, and one on the command line is visible to every +/// process, so the client asks for it in the terminal. +/// SQL*Plus takes the target alone. SQLcl gets `-thin` first: it otherwise +/// switches to the OCI (thick) driver whenever it sees an Instant Client or an +/// Oracle Home nearby, and without Oracle's native library on its +/// java.library.path that fails with "no ocijdbc23 in java.library.path", +/// whatever the password. The thin driver needs nothing installed. +fn oracle_launch(c: &OracleConfig, sqlcl: bool) -> Launch { + let cs = super::oracle::connect_string(c); + let user = c.user.trim(); + let target = if user.is_empty() { format!("/@{cs}") } else { format!("{user}@{cs}") }; + let mut args = Vec::new(); + if sqlcl { + args.push("-thin".to_string()); + } + args.push(target); + Launch { args, env: Vec::new(), unset: &["TWO_TASK"], tunnel: None } +} + fn redis_launch(c: &RedisConfig) -> Launch { let mut args = vec!["-h".to_string(), c.host.clone(), "-p".to_string(), c.port.to_string()]; if c.db != 0 { @@ -741,7 +820,14 @@ pub async fn terminal_open( AnyConnectionConfig::Libsql(_) => crate::providers::turso_cli_token(&app).await, _ => None, }; - open_session(&state, config, cols, rows, turso_token).await + let turso_config = match (&config, &turso_token) { + (AnyConnectionConfig::Libsql(_), Some(_)) => { + use tauri::Manager; + app.path().app_config_dir().ok().and_then(|dir| quiet_turso_config(&dir.join("turso-cli"))) + } + _ => None, + }; + open_session(&state, config, cols, rows, turso_token, turso_config).await } /// Install the connection's client with this OS's package manager, without a @@ -765,7 +851,7 @@ pub async fn terminal_install_client(config: AnyConnectionConfig) -> Result { - let mut v: Vec = ["winget", "install", "--silent", "--accept-source-agreements", "--accept-package-agreements"] + let mut v: Vec = ["winget", "install", "--silent", "--disable-interactivity", "--accept-source-agreements", "--accept-package-agreements"] .iter() .map(|s| s.to_string()) .collect(); @@ -789,6 +875,9 @@ pub async fn terminal_install_client(config: AnyConnectionConfig) -> Result, id: String) -> Resu Ok(()) } +/// Make the Turso CLI config folder Stroke points its shell at: autoupdate off. +/// Returns None when it can't be written, and the CLI keeps its own. +fn quiet_turso_config(dir: &Path) -> Option { + std::fs::create_dir_all(dir).ok()?; + let file = dir.join("settings.json"); + if !file.exists() { + std::fs::write(&file, r#"{"config":{"autoupdate":"off"}}"#).ok()?; + } + Some(dir.to_path_buf()) +} + async fn open_session( state: &TerminalState, config: AnyConnectionConfig, cols: u16, rows: u16, turso_token: Option, + turso_config: Option, ) -> Result { reap_unattached(state); let t0 = Instant::now(); @@ -849,6 +950,10 @@ async fn open_session( } AnyConnectionConfig::Sqlite(c) => sqlite_launch(&c, client_version(&binary).await.as_deref()), AnyConnectionConfig::Mssql(c) => mssql_launch(&c), + AnyConnectionConfig::Oracle(c) => { + let sqlcl = binary.file_stem().and_then(|s| s.to_str()).is_some_and(|s| s.eq_ignore_ascii_case("sql")); + oracle_launch(&c, sqlcl) + } AnyConnectionConfig::Redis(c) => redis_launch(&c), // The URI carries host, credentials and options; mongosh reads it as is. // `--quiet` drops the startup banner. @@ -870,6 +975,12 @@ async fn open_session( let mut env = Vec::new(); if let Some(token) = turso_token { env.push(("TURSO_API_TOKEN", token)); + // A config folder of Stroke's own with autoupdate off: the CLI + // otherwise updates itself through Homebrew when the shell exits. + // The token above signs it in, so the user's own login isn't needed. + if let (false, Some(dir)) = (via_wsl, &turso_config) { + env.push(("TURSO_CONFIG_FOLDER", dir.to_string_lossy().into_owned())); + } // WSL only forwards the Windows variables WSLENV names. if via_wsl { env.push(("WSLENV", "TURSO_API_TOKEN/u".to_string())); @@ -1183,6 +1294,27 @@ fn write_loop(mut writer: Box, keystrokes: mpsc::Receiver client_version(b).await, None => None }; + println!("{names:?}: find {find:?}, version {:?} -> {:?} {:?}", t.elapsed(), found, v); + } + } + use super::*; #[cfg(target_os = "linux")] @@ -1193,6 +1325,19 @@ mod tests { assert_eq!(linux_install("sqlcmd"), None); } + #[test] + fn gnu_parallel_sql_is_not_sqlcl() { + let dir = std::env::temp_dir().join(format!("stroke-sql-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let parallel = dir.join("parallel-sql"); + std::fs::write(¶llel, "#!/usr/bin/perl\n# GNU Parallel: sql - execute a command on a database determined by a dburl\n").unwrap(); + let sqlcl = dir.join("sqlcl-sql"); + std::fs::write(&sqlcl, "#!/bin/bash\n# SQLcl launcher\nexec java -jar \"$SQLCL_HOME/lib/dbtools-sqlcl.jar\" \"$@\"\n").unwrap(); + assert!(super::is_other_sql(¶llel)); + assert!(!super::is_other_sql(&sqlcl)); + let _ = std::fs::remove_dir_all(&dir); + } + #[test] fn versions_sort_numerically() { let root = std::env::temp_dir().join(format!("stroke-term-{}", std::process::id())); @@ -1316,7 +1461,7 @@ mod tests { name: "t".into(), file_path: db.display().to_string(), }); - let session = open_session(&state, config, 80, 24, None).await.unwrap(); + let session = open_session(&state, config, 80, 24, None, None).await.unwrap(); let wrong = session.url.replace("token=", "token=x"); assert!(tokio_tungstenite::connect_async(&wrong).await.is_err(), "a wrong token must not attach"); diff --git a/src-tauri/src/db/tx.rs b/src-tauri/src/db/tx.rs index 8fb238f9..b03fab1d 100644 --- a/src-tauri/src/db/tx.rs +++ b/src-tauri/src/db/tx.rs @@ -11,6 +11,7 @@ as it stays open, so a session that is left behind is closed automatically after `IDLE_TIMEOUT` - see `sweep_idle`. */ +use crate::db::connection::Db; use std::collections::HashMap; use std::sync::Mutex; use std::time::{Duration, Instant}; @@ -21,7 +22,7 @@ use serde_json::Value; use sqlx::{Column, Row, TypeInfo}; use tauri::State; -use super::connection::{require_conn, ActiveConnection, DbState}; +use super::connection::{require_conn, ActiveConnection}; use super::query::{ cell_to_json, is_row_returning_sql, pg_type_label, split_sql_statements, ColumnInfo, SqlResult, EXECUTE_SQL_TIMEOUT_MS, @@ -122,7 +123,7 @@ fn no_session(id: &str) -> String { /// Open a transaction and hold its connection until commit or rollback. #[tauri::command] pub async fn tx_begin( - db: State<'_, DbState>, + db: Db<'_>, tx: State<'_, TxState>, session_id: String, ) -> Result { @@ -141,7 +142,7 @@ pub async fn tx_begin( ActiveConnection::Sqlite(pool) => TxConn::Sqlite( pool.acquire().await.map_err(|e| format!("Could not open a transaction: {e}"))?, ), - ActiveConnection::Duckdb(_) | ActiveConnection::Mssql(_) => { + ActiveConnection::Duckdb(_) | ActiveConnection::Mssql(_) | ActiveConnection::Oracle(_) => { return Err("Explicit transactions are not supported for this engine yet".into()) } ActiveConnection::D1(_) | ActiveConnection::LibSql(_) => { diff --git a/src-tauri/src/display_backend.rs b/src-tauri/src/display_backend.rs new file mode 100644 index 00000000..9103d6ef --- /dev/null +++ b/src-tauri/src/display_backend.rs @@ -0,0 +1,121 @@ +//! Linux: native Wayland by default, X11 where it can't draw. +//! +//! Stroke used to force X11 (XWayland) everywhere, because on some Wayland +//! setups WebKit's web process aborts while creating its EGL display and the +//! window stays blank (issue #112 and the EGL_BAD_ALLOC reports). Most setups +//! draw fine on Wayland and look sharper there, so Wayland is now the default, +//! with a launch check: +//! +//! - Before GTK starts, a Wayland launch leaves a `wayland-pending` marker. +//! - The page's first finished screen (`reveal_window`) removes it. +//! - If the next launch still finds it, the last Wayland launch never drew a +//! page: this one, and every one after, uses X11 (`use-x11`). +//! +//! Overrides: `GDK_BACKEND` set by the user wins; `STROKE_X11=1` forces X11; +//! `STROKE_WAYLAND=1` forces Wayland and forgets an earlier fallback. + +#[cfg(target_os = "linux")] +mod imp { + use std::path::PathBuf; + + use std::sync::atomic::{AtomicBool, Ordering}; + + const PENDING: &str = "wayland-pending"; + /// This launch is a Wayland trial (it left the marker). + static TRIAL: AtomicBool = AtomicBool::new(false); + const FALLBACK: &str = "use-x11"; + + /// `~/.local/state/`: Stroke and Stroke Nightly keep their own. + fn state_dir() -> Option { + let base = std::env::var_os("XDG_STATE_HOME") + .map(PathBuf::from) + .or_else(|| std::env::var_os("HOME").map(|h| PathBuf::from(h).join(".local/state")))?; + let exe = std::env::current_exe().ok()?; + let name = exe.file_stem()?.to_string_lossy().to_lowercase(); + Some(base.join(format!("{name}-display"))) + } + + fn set(backend: &str) { + // SAFETY: called from run() before any thread is spawned. + unsafe { std::env::set_var("GDK_BACKEND", backend) }; + } + + /// Pick the backend. Must run before GTK initialises and before any thread. + pub fn choose() { + let Some(dir) = state_dir() else { return }; + let pending = dir.join(PENDING); + let fallback = dir.join(FALLBACK); + if std::env::var("STROKE_WAYLAND").as_deref() == Ok("1") { + let _ = std::fs::remove_file(&fallback); + set("wayland"); + return; + } + if std::env::var("STROKE_X11").as_deref() == Ok("1") { + set("x11"); + return; + } + // The user (or their desktop) already chose. + if std::env::var_os("GDK_BACKEND").is_some() { + return; + } + // An X11 session: nothing to decide. + if std::env::var_os("WAYLAND_DISPLAY").is_none() { + return; + } + if fallback.exists() { + set("x11"); + return; + } + if pending.exists() { + log::warn!("display: the last Wayland launch never drew a page; using X11 from now on (STROKE_WAYLAND=1 to try Wayland again)"); + let _ = std::fs::write(&fallback, "The last Wayland launch didn't draw. Delete this file, or start Stroke with STROKE_WAYLAND=1, to try Wayland again.\n"); + let _ = std::fs::remove_file(&pending); + set("x11"); + return; + } + let _ = std::fs::create_dir_all(&dir); + if std::fs::write(&pending, b"").is_ok() { + TRIAL.store(true, Ordering::Relaxed); + } + } + + /// A Wayland launch that hasn't drawn in 25s won't: the web process died + /// creating its EGL display and the window stays hidden. Restart on X11 + /// now instead of leaving a blank launch. Release builds only (a dev + /// build's first page waits on the Vite dev server). + pub fn watch(app: tauri::AppHandle) { + if cfg!(debug_assertions) || !TRIAL.load(Ordering::Relaxed) { + return; + } + std::thread::spawn(move || { + std::thread::sleep(std::time::Duration::from_secs(25)); + let Some(dir) = state_dir() else { return }; + if !dir.join(PENDING).exists() { + return; + } + log::warn!("display: no page drawn on Wayland after 25s; restarting on X11"); + let _ = std::fs::write(dir.join(FALLBACK), "A Wayland launch didn't draw. Delete this file, or start Stroke with STROKE_WAYLAND=1, to try Wayland again.\n"); + let _ = std::fs::remove_file(dir.join(PENDING)); + app.restart(); + }); + } + + /// The page drew: this backend works here. + pub fn drawn() { + if let Some(dir) = state_dir() { + let _ = std::fs::remove_file(dir.join(PENDING)); + } + } +} + +#[cfg(target_os = "linux")] +pub use imp::{choose, drawn, watch}; + +#[cfg(not(target_os = "linux"))] +pub fn choose() {} + +#[cfg(not(target_os = "linux"))] +pub fn drawn() {} + +#[cfg(not(target_os = "linux"))] +pub fn watch(_app: tauri::AppHandle) {} diff --git a/src-tauri/src/docker.rs b/src-tauri/src/docker.rs index 2b016635..7d60d334 100644 --- a/src-tauri/src/docker.rs +++ b/src-tauri/src/docker.rs @@ -3,6 +3,7 @@ use std::process::Stdio; use tauri::Emitter; use tokio::io::AsyncBufReadExt; use tokio::process::Command as Cmd; +use serde_json::Value; /// `docker …`, with no console window on Windows. The container scan runs on the /// connection screen, so a bare spawn flashed a cmd.exe window at app start. @@ -16,63 +17,12 @@ fn docker() -> Cmd { // TCP-open ≠ database-ready. MySQL/Postgres accept TCP connections seconds before // they finish initialization. We retry actual SQL pings to confirm readiness. -async fn wait_mysql_ready(host_port: u16, password: &str) -> bool { - let url = format!( - "mysql://root:{password}@127.0.0.1:{host_port}/mysql?ssl-mode=disabled" - ); - for _ in 0..60u32 { - tokio::time::sleep(std::time::Duration::from_secs(1)).await; - if let Ok(pool) = sqlx::mysql::MySqlPoolOptions::new() - .max_connections(1) - .acquire_timeout(std::time::Duration::from_secs(2)) - .connect(&url) - .await - { - let ok = sqlx::query("SELECT 1").execute(&pool).await.is_ok(); - pool.close().await; - if ok { return true; } - } - } - false -} - -async fn wait_postgres_ready(host_port: u16, password: &str) -> bool { - let url = format!( - "postgres://postgres:{password}@127.0.0.1:{host_port}/postgres?sslmode=disable" - ); - for _ in 0..60u32 { - tokio::time::sleep(std::time::Duration::from_secs(1)).await; - if let Ok(pool) = sqlx::postgres::PgPoolOptions::new() - .max_connections(1) - .acquire_timeout(std::time::Duration::from_secs(2)) - .connect(&url) - .await - { - let ok = sqlx::query("SELECT 1").execute(&pool).await.is_ok(); - pool.close().await; - if ok { return true; } - } - } - false -} - #[derive(serde::Serialize, Clone)] pub struct DockerLog { pub line: String, pub kind: String, } -#[derive(serde::Serialize, Clone)] -pub struct DockerConnInfo { - pub db_type: String, - pub host: String, - pub port: u16, - pub user: String, - pub password: String, - pub database: String, - pub name: String, -} - fn emit_log(app: &tauri::AppHandle, event: &str, line: &str, kind: &str) { let _ = app.emit( event, @@ -114,153 +64,244 @@ pub async fn docker_check() -> Result { Ok(if v.is_empty() { "ok".to_string() } else { v }) } +// ── The launch catalog ──────────────────────────────────────────────────────── +// Every engine Stroke can open, as a container it can start with one click: the +// image, how to start it, the credentials it starts with, and how to tell it is +// ready. The palette lists these (docker_templates) and docker_run_db launches +// one, returning a connection the app opens as it is. + +/// How to tell a fresh container is ready for a client. +#[derive(Clone, Copy)] +enum Ready { + /// `SELECT 1` over the Postgres protocol (Postgres, CockroachDB). + Postgres, + /// `SELECT 1` over the MySQL protocol (MySQL, MariaDB). + Mysql, + /// This line in the container's log, at least this many times (MongoDB + /// prints it once for its setup run and again for the real server). + Log(&'static str, usize), + /// Any HTTP answer at this path. Not a bare TCP connect: Docker's port + /// proxy accepts those before anything listens inside the container. + Http(&'static str), +} + +struct Template { + id: &'static str, + label: &'static str, + /// Stroke engine id: the connection type, and the palette's icon. + engine: &'static str, + /// Icon when it isn't the engine's own (pgvector shows Postgres anyway). + icon: &'static str, + group: &'static str, + image: &'static str, + note: &'static str, + /// Port inside the container. + port: u16, + /// Preferred host port; the next free one is used when it is taken. + host_port: u16, + env: &'static [&'static str], + /// Arguments after the image (the container's command). + cmd: &'static [&'static str], + user: &'static str, + password: &'static str, + database: &'static str, + ready: Ready, + /// Seconds to wait for `ready`; Oracle and SQL Server start slowly. + wait_secs: u64, +} + +const PG_ENV: &[&str] = &["POSTGRES_PASSWORD=postgres", "POSTGRES_USER=postgres", "POSTGRES_DB=postgres"]; + +const TEMPLATES: &[Template] = &[ + Template { id: "postgres", label: "PostgreSQL 17", engine: "postgres", icon: "postgres", group: "Relational", image: "postgres:17-alpine", note: "", port: 5432, host_port: 5433, env: PG_ENV, cmd: &[], user: "postgres", password: "postgres", database: "postgres", ready: Ready::Postgres, wait_secs: 90 }, + Template { id: "postgres16", label: "PostgreSQL 16", engine: "postgres", icon: "postgres", group: "Relational", image: "postgres:16-alpine", note: "", port: 5432, host_port: 5434, env: PG_ENV, cmd: &[], user: "postgres", password: "postgres", database: "postgres", ready: Ready::Postgres, wait_secs: 90 }, + Template { id: "pgvector", label: "PostgreSQL + pgvector", engine: "postgres", icon: "postgres", group: "Relational", image: "pgvector/pgvector:pg17", note: "Vector search", port: 5432, host_port: 5435, env: PG_ENV, cmd: &[], user: "postgres", password: "postgres", database: "postgres", ready: Ready::Postgres, wait_secs: 90 }, + Template { id: "postgis", label: "PostGIS", engine: "postgres", icon: "postgres", group: "Relational", image: "postgis/postgis:17-3.5", note: "Geospatial", port: 5432, host_port: 5436, env: PG_ENV, cmd: &[], user: "postgres", password: "postgres", database: "postgres", ready: Ready::Postgres, wait_secs: 120 }, + Template { id: "timescale", label: "TimescaleDB", engine: "postgres", icon: "timescale", group: "Relational", image: "timescale/timescaledb:latest-pg17", note: "Time series", port: 5432, host_port: 5437, env: PG_ENV, cmd: &[], user: "postgres", password: "postgres", database: "postgres", ready: Ready::Postgres, wait_secs: 120 }, + // MySQL 8.4 defaults to caching_sha2_password, which needs TLS or an RSA + // exchange; native passwords let the driver connect over loopback. + Template { id: "mysql", label: "MySQL 8.4", engine: "mysql", icon: "mysql", group: "Relational", image: "mysql:8.4", note: "", port: 3306, host_port: 3307, env: &["MYSQL_ROOT_PASSWORD=mysql", "MYSQL_DATABASE=app"], cmd: &["--mysql-native-password=ON"], user: "root", password: "mysql", database: "app", ready: Ready::Mysql, wait_secs: 120 }, + Template { id: "mariadb", label: "MariaDB 11", engine: "mariadb", icon: "mariadb", group: "Relational", image: "mariadb:11", note: "", port: 3306, host_port: 3308, env: &["MARIADB_ROOT_PASSWORD=mariadb", "MARIADB_DATABASE=app"], cmd: &[], user: "root", password: "mariadb", database: "app", ready: Ready::Mysql, wait_secs: 120 }, + Template { id: "mssql", label: "SQL Server 2022", engine: "mssql", icon: "mssql", group: "Relational", image: "mcr.microsoft.com/mssql/server:2022-latest", note: "x86-64 only", port: 1433, host_port: 1434, env: &["ACCEPT_EULA=Y", "MSSQL_SA_PASSWORD=Stroke_Passw0rd!"], cmd: &[], user: "sa", password: "Stroke_Passw0rd!", database: "master", ready: Ready::Log("SQL Server is now ready for client connections", 1), wait_secs: 180 }, + Template { id: "oracle", label: "Oracle Database Free", engine: "oracle", icon: "oracle", group: "Relational", image: "gvenzl/oracle-free:slim", note: "Needs Instant Client", port: 1521, host_port: 1521, env: &["ORACLE_PASSWORD=oracle", "APP_USER=app", "APP_USER_PASSWORD=app"], cmd: &[], user: "app", password: "app", database: "FREEPDB1", ready: Ready::Log("DATABASE IS READY TO USE!", 1), wait_secs: 300 }, + Template { id: "cockroachdb", label: "CockroachDB", engine: "cockroachdb", icon: "cockroachdb", group: "Relational", image: "cockroachdb/cockroach:latest", note: "Single node", port: 26257, host_port: 26257, env: &[], cmd: &["start-single-node", "--insecure"], user: "root", password: "", database: "defaultdb", ready: Ready::Postgres, wait_secs: 120 }, + Template { id: "mongodb", label: "MongoDB 8", engine: "mongodb", icon: "mongodb", group: "Document and key-value", image: "mongo:8", note: "", port: 27017, host_port: 27018, env: &["MONGO_INITDB_ROOT_USERNAME=mongo", "MONGO_INITDB_ROOT_PASSWORD=mongo"], cmd: &[], user: "mongo", password: "mongo", database: "app", ready: Ready::Log("Waiting for connections", 2), wait_secs: 90 }, + Template { id: "redis", label: "Redis 7", engine: "redis", icon: "redis", group: "Document and key-value", image: "redis:7-alpine", note: "", port: 6379, host_port: 6380, env: &[], cmd: &[], user: "", password: "", database: "", ready: Ready::Log("Ready to accept connections", 1), wait_secs: 60 }, + Template { id: "valkey", label: "Valkey 8", engine: "valkey", icon: "valkey", group: "Document and key-value", image: "valkey/valkey:8-alpine", note: "", port: 6379, host_port: 6381, env: &[], cmd: &[], user: "", password: "", database: "", ready: Ready::Log("Ready to accept connections", 1), wait_secs: 60 }, + Template { id: "couchdb", label: "CouchDB 3", engine: "couchdb", icon: "couchdb", group: "Document and key-value", image: "couchdb:3", note: "", port: 5984, host_port: 5985, env: &["COUCHDB_USER=admin", "COUCHDB_PASSWORD=couchdb"], cmd: &[], user: "admin", password: "couchdb", database: "", ready: Ready::Log("Apache CouchDB has started", 1), wait_secs: 90 }, + // Security off and one node: a local sandbox over plain HTTP. The heap is + // capped so the default (half the machine's memory) doesn't swamp a laptop. + Template { id: "elasticsearch", label: "Elasticsearch 8", engine: "elasticsearch", icon: "elasticsearch", group: "Search", image: "docker.elastic.co/elasticsearch/elasticsearch:8.15.3", note: "Security off, single node", port: 9200, host_port: 9201, env: &["discovery.type=single-node", "xpack.security.enabled=false", "ES_JAVA_OPTS=-Xms512m -Xmx512m"], cmd: &[], user: "", password: "", database: "", ready: Ready::Http("/_cluster/health?wait_for_status=yellow&timeout=1s"), wait_secs: 180 }, + Template { id: "opensearch", label: "OpenSearch 2", engine: "opensearch", icon: "opensearch", group: "Search", image: "opensearchproject/opensearch:2.17.1", note: "Security off, single node", port: 9200, host_port: 9202, env: &["discovery.type=single-node", "DISABLE_SECURITY_PLUGIN=true", "DISABLE_INSTALL_DEMO_CONFIG=true", "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m"], cmd: &[], user: "", password: "", database: "", ready: Ready::Http("/_cluster/health?wait_for_status=yellow&timeout=1s"), wait_secs: 180 }, + Template { id: "dynamodb", label: "DynamoDB Local", engine: "dynamodb", icon: "dynamodb", group: "Document and key-value", image: "amazon/dynamodb-local:latest", note: "", port: 8000, host_port: 8001, env: &[], cmd: &[], user: "", password: "", database: "", ready: Ready::Http("/"), wait_secs: 60 }, + Template { id: "cassandra", label: "Cassandra 5", engine: "cassandra", icon: "cassandra", group: "Document and key-value", image: "cassandra:5", note: "Starts in about a minute", port: 9042, host_port: 9043, env: &[], cmd: &[], user: "", password: "", database: "", ready: Ready::Log("Starting listening for CQL clients", 1), wait_secs: 240 }, + Template { id: "clickhouse", label: "ClickHouse", engine: "clickhouse", icon: "clickhouse", group: "Analytics", image: "clickhouse/clickhouse-server:latest", note: "", port: 8123, host_port: 8124, env: &["CLICKHOUSE_USER=default", "CLICKHOUSE_PASSWORD=clickhouse", "CLICKHOUSE_DEFAULT_ACCESS_MANAGEMENT=1"], cmd: &[], user: "default", password: "clickhouse", database: "default", ready: Ready::Http("/ping"), wait_secs: 90 }, + Template { id: "libsql", label: "libSQL server", engine: "libsql", icon: "turso", group: "Analytics", image: "ghcr.io/tursodatabase/libsql-server:latest", note: "Turso, self-hosted", port: 8080, host_port: 8081, env: &[], cmd: &[], user: "", password: "", database: "", ready: Ready::Http("/health"), wait_secs: 60 }, +]; + +/// What the palette lists for one launchable image. +#[derive(serde::Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DockerTemplate { + pub id: &'static str, + pub label: &'static str, + pub engine: &'static str, + pub icon: &'static str, + pub group: &'static str, + pub image: &'static str, + pub note: &'static str, + pub port: u16, +} + #[tauri::command] -pub async fn docker_run_db( - app: tauri::AppHandle, - db_type: String, - event_id: String, -) -> Result { - let evt = format!("docker-log:{event_id}"); +pub fn docker_templates() -> Vec { + TEMPLATES + .iter() + .map(|t| DockerTemplate { id: t.id, label: t.label, engine: t.engine, icon: t.icon, group: t.group, image: t.image, note: t.note, port: t.host_port }) + .collect() +} - let (image, host_port, cont_port): (&str, u16, u16) = match db_type.as_str() { - "postgres" => ("postgres:17-alpine", 5433, 5432), - "mysql" => ("mysql:8.4", 3307, 3306), - other => return Err(format!("Unsupported database type: {other}")), - }; +/// The preferred port if nothing listens there, else the next free one. +fn free_port(preferred: u16) -> u16 { + let free = |p: u16| std::net::TcpListener::bind(("127.0.0.1", p)).is_ok(); + (preferred..preferred.saturating_add(100)).find(|p| free(*p)).unwrap_or_else(|| { + std::net::TcpListener::bind(("127.0.0.1", 0)).and_then(|l| l.local_addr()).map(|a| a.port()).unwrap_or(preferred) + }) +} - let (user, password, database): (&str, &str, &str) = match db_type.as_str() { - "postgres" => ("postgres", "postgres", "postgres"), - "mysql" => ("root", "mysql", "mysql"), - _ => unreachable!(), - }; +/// The saved-connection object the app opens for a launched template. +fn connection_for(t: &Template, port: u16) -> Value { + let host = "127.0.0.1"; + let name = format!("Docker {} (:{port})", t.label); + match t.engine { + "mongodb" => serde_json::json!({ "type": "mongodb", "name": name, "uri": format!("mongodb://{}:{}@{host}:{port}/?authSource=admin", t.user, t.password), "database": t.database }), + "redis" | "valkey" => serde_json::json!({ "type": t.engine, "name": name, "host": host, "port": port, "db": 0 }), + "couchdb" => serde_json::json!({ "type": "couchdb", "name": name, "url": format!("http://{host}:{port}"), "user": t.user, "password": t.password }), + "elasticsearch" | "opensearch" => serde_json::json!({ "type": t.engine, "name": name, "url": format!("http://{host}:{port}"), "insecure": false }), + "dynamodb" => serde_json::json!({ "type": "dynamodb", "name": name, "region": "us-east-1", "endpoint": format!("http://{host}:{port}"), "accessKeyId": "local", "secretAccessKey": "local" }), + "cassandra" => serde_json::json!({ "type": "cassandra", "name": name, "hosts": format!("{host}:{port}") }), + "libsql" => serde_json::json!({ "type": "libsql", "name": name, "url": format!("http://{host}:{port}") }), + "oracle" => serde_json::json!({ "type": "oracle", "name": name, "host": host, "port": port, "serviceName": t.database, "user": t.user, "password": t.password }), + "mssql" => serde_json::json!({ "type": "mssql", "name": name, "host": host, "port": port, "user": t.user, "password": t.password, "database": t.database, "trustCert": true }), + _ => serde_json::json!({ "type": t.engine, "name": name, "host": host, "port": port, "user": t.user, "password": t.password, "database": t.database, "ssl": false }), + } +} - let ts = std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .unwrap_or_default() - .as_secs(); - let container_name = format!("stroke-{db_type}-{ts}"); - let port_map = format!("{host_port}:{cont_port}"); +async fn wait_ready(t: &Template, container: &str, port: u16) -> bool { + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(t.wait_secs); + while std::time::Instant::now() < deadline { + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + let ok = match t.ready { + Ready::Postgres => { + let url = format!("postgres://{}:{}@127.0.0.1:{port}/{}?sslmode=disable", t.user, t.password, t.database); + match sqlx::postgres::PgPoolOptions::new().max_connections(1).acquire_timeout(std::time::Duration::from_secs(2)).connect(&url).await { + Ok(pool) => { + let ok = sqlx::query("SELECT 1").execute(&pool).await.is_ok(); + pool.close().await; + ok + } + Err(_) => false, + } + } + Ready::Mysql => { + let url = format!("mysql://{}:{}@127.0.0.1:{port}/{}?ssl-mode=disabled", t.user, t.password, t.database); + match sqlx::mysql::MySqlPoolOptions::new().max_connections(1).acquire_timeout(std::time::Duration::from_secs(2)).connect(&url).await { + Ok(pool) => { + let ok = sqlx::query("SELECT 1").execute(&pool).await.is_ok(); + pool.close().await; + ok + } + Err(_) => false, + } + } + Ready::Http(path) => { + let url = format!("http://127.0.0.1:{port}{path}"); + let client = reqwest::Client::builder().timeout(std::time::Duration::from_secs(2)).build(); + match client { + Ok(c) => c.get(&url).send().await.is_ok(), + Err(_) => false, + } + } + Ready::Log(line, times) => { + let logs = docker().args(["logs", container]).output().await; + logs.is_ok_and(|o| { + let all = format!("{}{}", String::from_utf8_lossy(&o.stdout), String::from_utf8_lossy(&o.stderr)); + all.matches(line).count() >= times + }) + } + }; + if ok { + return true; + } + } + false +} - // ── Pull ────────────────────────────────────────────────────────────────── - emit_log(&app, &evt, &format!("Pulling {image}…"), "info"); +/// Pull and run one catalog image, wait until it takes connections, and +/// return the connection to open. `db_type` is a template id (`postgres`, +/// `mongodb`, ...); logs stream as `docker-log:{event_id}`. +#[tauri::command] +pub async fn docker_run_db(app: tauri::AppHandle, db_type: String, event_id: String) -> Result { + let evt = format!("docker-log:{event_id}"); + let t = TEMPLATES + .iter() + .find(|t| t.id == db_type) + .ok_or_else(|| format!("No Docker image for {db_type}"))?; + let host_port = free_port(t.host_port); + if host_port != t.host_port { + emit_log(&app, &evt, &format!("Port {} is taken, using {host_port}.", t.host_port), "info"); + } + let ts = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default().as_secs(); + let container_name = format!("stroke-{}-{ts}", t.id); + // ── Pull ────────────────────────────────────────────────────────────────── + emit_log(&app, &evt, &format!("Pulling {}…", t.image), "info"); let mut pull_child = docker() - .args(["pull", image]) + .args(["pull", t.image]) .stdout(Stdio::null()) .stderr(Stdio::piped()) .spawn() .map_err(|e| format!("Failed to start docker pull: {e}"))?; - if let Some(stderr) = pull_child.stderr.take() { let mut lines = tokio::io::BufReader::new(stderr).lines(); while let Ok(Some(line)) = lines.next_line().await { - let t = line.trim(); - if !t.is_empty() { - emit_log(&app, &evt, t, "info"); + let line = line.trim(); + if !line.is_empty() { + emit_log(&app, &evt, line, "info"); } } } - let pull_status = pull_child.wait().await.map_err(|e| e.to_string())?; if !pull_status.success() { - return Err(format!( - "docker pull {image} failed. Check your internet connection." - )); + return Err(format!("docker pull {} failed. Check your internet connection, or whether the image runs on this machine ({}).", t.image, std::env::consts::ARCH)); } - emit_log(&app, &evt, "Image ready. Starting container…", "info"); // ── Run ─────────────────────────────────────────────────────────────────── let mut run_cmd = docker(); - run_cmd - .arg("run") - .arg("-d") - .arg("--name") - .arg(&container_name) - .arg("-p") - .arg(&port_map); - - match db_type.as_str() { - "postgres" => { - run_cmd - .arg("-e") - .arg("POSTGRES_PASSWORD=postgres") - .arg("-e") - .arg("POSTGRES_USER=postgres") - .arg("-e") - .arg("POSTGRES_DB=postgres"); - } - "mysql" => { - run_cmd - .arg("-e") - .arg("MYSQL_ROOT_PASSWORD=mysql") - .arg("-e") - .arg("MYSQL_DATABASE=mysql") - // MySQL 8.4 uses caching_sha2_password by default which requires SSL or - // RSA key exchange. Enable mysql_native_password so sqlx can connect - // without TLS on loopback connections. - .arg("--mysql-native-password=ON"); - } - _ => {} + run_cmd.args(["run", "-d", "--name", &container_name, "-p", &format!("{host_port}:{}", t.port)]); + for e in t.env { + run_cmd.args(["-e", e]); } - run_cmd.arg(image); - - let run_out = run_cmd - .output() - .await - .map_err(|e| format!("Failed to launch container: {e}"))?; - + run_cmd.arg(t.image).args(t.cmd); + let run_out = run_cmd.output().await.map_err(|e| format!("Failed to launch container: {e}"))?; if !run_out.status.success() { let err = String::from_utf8_lossy(&run_out.stderr); - let msg = if err.contains("port is already allocated") - || err.contains("address already in use") - { + return Err(if err.contains("port is already allocated") || err.contains("address already in use") { format!("Port {host_port} is already in use. Stop the existing service and try again.") } else { format!("Container failed to start: {err}") - }; - return Err(msg); + }); } - let cid = String::from_utf8_lossy(&run_out.stdout).trim().to_string(); - let short = &cid[..cid.len().min(12)]; - emit_log(&app, &evt, &format!("Container {short} started."), "info"); - emit_log( - &app, - &evt, - "Waiting for database to accept connections…", - "info", - ); + emit_log(&app, &evt, &format!("Container {} started on port {host_port}.", &cid[..cid.len().min(12)]), "info"); + emit_log(&app, &evt, "Waiting for the database to accept connections…", "info"); - let ready = match db_type.as_str() { - "mysql" => wait_mysql_ready(host_port, password).await, - "postgres" => wait_postgres_ready(host_port, password).await, - _ => false, - }; - - if !ready { - return Err(format!( - "Database did not become ready within 60s. Container: {container_name}" - )); + if !wait_ready(t, &container_name, host_port).await { + return Err(format!("{} did not become ready within {}s. Container: {container_name} (docker logs {container_name})", t.label, t.wait_secs)); } - emit_log(&app, &evt, "Database is ready.", "info"); - - let label = if db_type == "postgres" { - "PostgreSQL" - } else { - "MySQL" - }; - - Ok(DockerConnInfo { - db_type, - host: "127.0.0.1".to_string(), - port: host_port, - user: user.to_string(), - password: password.to_string(), - database: database.to_string(), - name: format!("Docker {label} (:{host_port})"), - }) + Ok(connection_for(t, host_port)) } // ── Databases already running in Docker ─────────────────────────────────────── @@ -270,7 +311,6 @@ pub async fn docker_run_db( // running containers and hands back ready-to-open connections. use serde::Serialize; -use serde_json::Value; #[derive(Debug, Serialize, Clone)] #[serde(rename_all = "camelCase")] @@ -302,6 +342,12 @@ fn engine_port(engine: &str) -> u16 { "clickhouse" => 8123, "mssql" => 1433, "cockroachdb" => 26257, + "oracle" => 1521, + "mongodb" => 27017, + "couchdb" => 5984, + "elasticsearch" | "opensearch" => 9200, + "cassandra" => 9042, + "dynamodb" => 8000, _ => 5432, } } @@ -332,6 +378,30 @@ fn engine_for_image(image: &str) -> Option<&'static str> { if i.contains("mssql") || i.contains("sqlserver") || i.contains("azure-sql-edge") { return Some("mssql"); } + // gvenzl/oracle-free, gvenzl/oracle-xe, container-registry.oracle.com/database/free. + if i.contains("oracle-free") || i.contains("oracle-xe") || i.contains("oracle/database") || i.contains("oracle.com/database") { + return Some("oracle"); + } + // Admin UIs share the name but are not databases. + if i.contains("mongo") && !i.contains("mongo-express") { + return Some("mongodb"); + } + if i.contains("couchdb") { + return Some("couchdb"); + } + // Dashboards and Kibana share the names but are UIs, not the server. + if i.contains("opensearch") && !i.contains("dashboards") { + return Some("opensearch"); + } + if i.contains("elasticsearch") { + return Some("elasticsearch"); + } + if i.contains("cassandra") || i.contains("scylla") { + return Some("cassandra"); + } + if i.contains("dynamodb-local") { + return Some("dynamodb"); + } None } @@ -348,6 +418,11 @@ fn engine_for_ports(ports: &Value) -> Option<&'static str> { 8123 => "clickhouse", 1433 => "mssql", 26257 => "cockroachdb", + 1521 => "oracle", + 27017 => "mongodb", + 5984 => "couchdb", + 9200 => "elasticsearch", + 9042 => "cassandra", _ => continue, }; return Some(engine); @@ -403,6 +478,29 @@ fn credentials(engine: &str, env: &std::collections::HashMap) -> String::new(), ), "cockroachdb" => ("root".into(), String::new(), "defaultdb".into()), + // gvenzl images: SYSTEM with ORACLE_PASSWORD; the official image uses ORACLE_PWD. + // The database is the pluggable one the image opens (set in database_from_inspect). + "oracle" => ("system".into(), get(&["ORACLE_PASSWORD", "ORACLE_PWD"]).unwrap_or_default(), String::new()), + "mongodb" => ( + get(&["MONGO_INITDB_ROOT_USERNAME"]).unwrap_or_default(), + get(&["MONGO_INITDB_ROOT_PASSWORD"]).unwrap_or_default(), + get(&["MONGO_INITDB_DATABASE"]).unwrap_or_default(), + ), + "couchdb" => ( + get(&["COUCHDB_USER"]).unwrap_or_default(), + get(&["COUCHDB_PASSWORD"]).unwrap_or_default(), + String::new(), + ), + "cassandra" | "dynamodb" => (String::new(), String::new(), String::new()), + // The official images log in as `elastic` / `admin` with these set. + "elasticsearch" => match get(&["ELASTIC_PASSWORD"]) { + Some(p) => ("elastic".into(), p, String::new()), + None => (String::new(), String::new(), String::new()), + }, + "opensearch" => match get(&["OPENSEARCH_INITIAL_ADMIN_PASSWORD"]) { + Some(p) if get(&["DISABLE_SECURITY_PLUGIN"]).as_deref() != Some("true") => ("admin".into(), p, String::new()), + _ => (String::new(), String::new(), String::new()), + }, _ => { let user = get(&["POSTGRES_USER", "PGUSER"]).unwrap_or_else(|| "postgres".into()); let db = get(&["POSTGRES_DB", "PGDATABASE"]).unwrap_or_else(|| user.clone()); @@ -447,7 +545,14 @@ fn database_from_inspect(c: &Value) -> Option { .to_string(); let env = env_map(config.get("Env").unwrap_or(&Value::Null)); - let (user, password, database) = credentials(&engine, &env); + let (user, password, mut database) = credentials(&engine, &env); + if engine == "oracle" && database.is_empty() { + // Free (23ai) opens FREEPDB1, XE opens XEPDB1; APP_USER-style images may add one. + database = env + .get("ORACLE_DATABASE") + .cloned() + .unwrap_or_else(|| if image.to_lowercase().contains("xe") { "XEPDB1".into() } else { "FREEPDB1".into() }); + } let name = c .get("Name") .and_then(Value::as_str) @@ -578,6 +683,16 @@ mod tests { fn recognises_database_images_and_ignores_the_rest() { assert_eq!(engine_for_image("postgres:17-alpine"), Some("postgres")); assert_eq!(engine_for_image("pgvector/pgvector:pg16"), Some("postgres")); + assert_eq!(engine_for_image("gvenzl/oracle-free:slim"), Some("oracle")); + assert_eq!(engine_for_image("container-registry.oracle.com/database/free:latest"), Some("oracle")); + assert_eq!(engine_for_image("mongo:7"), Some("mongodb")); + assert_eq!(engine_for_image("mongo-express"), None); + assert_eq!(engine_for_image("couchdb:3"), Some("couchdb")); + assert_eq!(engine_for_image("docker.elastic.co/elasticsearch/elasticsearch:8.15.3"), Some("elasticsearch")); + assert_eq!(engine_for_image("opensearchproject/opensearch:2"), Some("opensearch")); + assert_eq!(engine_for_image("opensearchproject/opensearch-dashboards:2"), None); + assert_eq!(engine_for_image("scylladb/scylla"), Some("cassandra")); + assert_eq!(engine_for_image("amazon/dynamodb-local"), Some("dynamodb")); assert_eq!(engine_for_image("timescale/timescaledb:latest-pg16"), Some("postgres")); assert_eq!(engine_for_image("mariadb:11"), Some("mariadb")); assert_eq!(engine_for_image("mysql:8.4"), Some("mysql")); @@ -683,3 +798,28 @@ mod action_tests { docker_container_action("stroke-test-mysql".into(), "restart".into()).await.unwrap(); } } + +#[cfg(test)] +mod catalog_tests { + use super::*; + + #[test] + fn template_ids_are_unique_and_connections_name_their_engine() { + let mut ids: Vec<&str> = TEMPLATES.iter().map(|t| t.id).collect(); + ids.sort(); + ids.dedup(); + assert_eq!(ids.len(), TEMPLATES.len()); + for t in TEMPLATES { + let c = connection_for(t, 4000); + assert_eq!(c["type"], t.engine, "{}", t.id); + assert!(c["name"].as_str().unwrap().contains(":4000"), "{}", t.id); + } + } + + #[test] + fn free_port_skips_a_taken_one() { + let taken = std::net::TcpListener::bind(("127.0.0.1", 0)).unwrap(); + let port = taken.local_addr().unwrap().port(); + assert_ne!(free_port(port), port); + } +} diff --git a/src-tauri/src/launch.rs b/src-tauri/src/launch.rs new file mode 100644 index 00000000..4b2ed430 --- /dev/null +++ b/src-tauri/src/launch.rs @@ -0,0 +1,176 @@ +//! Opening Stroke at a database from outside the app: a `stroke://open?url=…` +//! link (what `npx strokedb` and stroke.click/studio fire) or a connection on the +//! command line (`stroke postgres://…`, `stroke ./app.db`). +//! +//! Nothing here connects. A link can come from any web page, so all it does is +//! open the New Connection dialog with the fields filled in; the person reads +//! them and presses Connect. The request is parked in `LaunchState` and the page +//! takes it with `take_open_request`, either on startup (a link that launched +//! the app arrives before the page can listen) or when `open-request` pings it +//! (a link to the app that is already running - macOS hands it over that way). + +use std::path::Path; +use std::sync::Mutex; +use tauri::{AppHandle, Emitter, Manager, Url}; + +/// Schemes this build answers to. Stable and Nightly are separate apps, so each +/// has its own and a link opens the one it names. +pub const SCHEMES: &[&str] = &["stroke", "stroke-nightly"]; + +/// Longest connection string accepted from outside. Real ones are a few hundred +/// characters; this only stops a hostile link from parking megabytes. +const MAX_URI: usize = 4096; +const MAX_NAME: usize = 120; + +#[derive(Clone, Debug, PartialEq, serde::Serialize)] +pub struct OpenRequest { + /// A connection URL, a `.env` line, or an absolute path to a database file. + pub uri: String, + /// What to call the connection, e.g. the project folder `npx strokedb` ran in. + pub name: Option, +} + +#[derive(Default)] +pub struct LaunchState(Mutex>); + +/// `stroke://open?url=&name=
+ {@const lay = resultLayout.get(item.id)} + {#if lay?.more} + + {/if} +
@@ -4969,7 +5597,7 @@ {/if} {#if !item.error} {formatCompactCount(item.total)} {item.total === 1 ? "row" : "rows"} @@ -5014,30 +5642,33 @@ showSelection={false} />
- {#if item.total > 15} -

- Showing 15 of {formatCompactCount(item.total)} rows{item.capped - ? ` (limited to ${AI_ROW_LIMIT})` - : ""} -

- {/if} - -
- - {#each [["csv", "CSV"], ["json", "JSON"], ["md", "Markdown"]] as [fmt, label]} + +
+ + {#if item.total > 15} + 15 of {formatCompactCount(item.total)} rows{item.capped ? ` (limited to ${AI_ROW_LIMIT})` : ""} + {/if} + + + {#each [["csv", "CSV"], ["xlsx", "Excel"]] as [fmt, label]} {/each} + + More + + {#each EXPORT_FORMATS as f (f.id)} + exportResultAs(item, f.id)}> + {f.label} + {f.hint} + + {/each} + +
{/if} {/if} @@ -5128,6 +5759,40 @@
+ {:else if item.kind === "share-confirm"} + +
+
+ + Share {item.summary.what} as a link? +
+
+

{item.summary.title}

+

+ {#if item.summary.rows}{item.summary.rows.toLocaleString()} row{item.summary.rows === 1 ? '' : 's'} · {/if}expires in {item.summary.expires}{item.summary.once ? ' · opens once' : ''} +

+ {#if item.summary.masked.length} +

Masked: {item.summary.masked.join(', ')}

+ {/if} +

Encrypted here first; stroke.click can't read it.

+
+
+ + +
+
+ + {:else if item.kind === "share-link"} +
+ + {item.title} + +
+ {:else if item.kind === "chart"}
@@ -5402,18 +6067,28 @@ to hide behind a Details toggle. --> {#if error} {@const failure = describeAiError(error)} -
+ +
+ {/if} @@ -5759,7 +6436,7 @@ const { clearConversations } = await import( "$lib/stores/conversations.js" ); - await clearConversations(connectionId || undefined); + await clearConversations(chatMatch ?? (connectionId || undefined)); convList = []; await newConversation(); }} @@ -6040,6 +6717,17 @@ > Open +
{#if accessLabel} - - {accessLabel} + + + {/if}
diff --git a/src/lib/components/AiSettingsDialog.svelte b/src/lib/components/AiSettingsDialog.svelte index 9bcff8e1..6381ca72 100644 --- a/src/lib/components/AiSettingsDialog.svelte +++ b/src/lib/components/AiSettingsDialog.svelte @@ -72,6 +72,20 @@ const CARD_IDLE = "border-border/40 text-foreground/85 hover:border-border/70 hover:bg-muted/30 hover:text-foreground"; + /** One line under each provider in the wizard: what you get, and what it takes. */ + const PROVIDER_BLURB = /** @type {Record} */ ({ + stroke: "Free models, no key needed", + openrouter: "One key, hundreds of models", + openai: "GPT models with your OpenAI key", + anthropic: "Claude models with your Anthropic key", + copilot: "Your Copilot plan, sign in with GitHub", + mistral: "Mistral models with your key", + google: "Gemini models with your Google AI key", + ollama: "Models running on this computer", + omniroute: "A local gateway in front of many providers", + custom: "Any OpenAI-compatible endpoint", + }); + /** Marks for the providers brand-icons.js has no logo for. */ const PROVIDER_ICON_FALLBACK = /** @type {Record} */ ({ stroke: Sparkles, @@ -500,14 +514,14 @@ {#if view === "list"} -
-
- AI Models - - Right-click a model to edit or delete it. +
+
+ AI models + + The models chat and SQL suggestions can use. Pick one to use it.
- +
@@ -546,9 +560,12 @@
+ > @@ -578,13 +595,11 @@ {/if}
-
- +
@@ -631,25 +646,33 @@ {#if step === 0} - -
+ +
{#each PROVIDERS as p (p.id)} {@const selected = formProvider === p.id} {@const Fallback = PROVIDER_ICON_FALLBACK[p.id] ?? Sparkles} {/each}
@@ -1080,7 +1103,10 @@ {/if}
- {#if step < STEPS.length - 1} + {#if step === 0} + + + {:else if step < STEPS.length - 1} +{/snippet} + +{#snippet logPanel()} + + +
{ if (logEl) followLog = logEl.scrollHeight - logEl.scrollTop - logEl.clientHeight < 24 }} + class="app-scroll min-h-0 flex-1 overflow-y-auto border-t border-border/60 bg-background/40 px-4 py-2 font-mono text-ui-xs leading-relaxed" + role="log" + aria-label="Live log" + tabindex="0" + > + {#each logLines as line (line.id)} +
+ {logTime(line.at)} + {line.text} +
+ {:else} +

Waiting for the first line…

+ {/each} +
+{/snippet} + {#snippet progressBar(/** @type {number} */ fraction, /** @type {'running'|'done'|'warn'|'error'} */ tone)}
Restore + + Scheduled + {dbLabel}
- {#if activeTab === 'backup'} + {#if activeTab === 'schedule'} + + {:else if activeTab === 'backup'}
{@render progressBar( @@ -731,7 +867,9 @@ {/if}
- {#if runTables.length} + {#if showLog} + {@render logPanel()} + {:else if runTables.length}
    {#each runTables as t (t.key)}
  • @@ -785,6 +923,13 @@

    {/if} + {#if importInfo?.encrypted} + + + End-to-end encrypted + A scheduled backup. It opens on this computer with your sync key; unlock sync first if it's locked. + + {/if} {#if importInfo?.incomplete} @@ -801,6 +946,19 @@ {/if} + {#if importPath && importInfo && !importInfo.encrypted && importPhase === 'idle'} +
    + + void changeEncoding(v)} + /> +
    + {/if} + {#if importPath && importPhase === 'idle'}

    Confirm

    @@ -836,7 +994,19 @@
    - {#if importPhase === 'idle'} + {#if importPhase === 'idle' && importInfo?.preview} +
    + +

    + {importInfo.previewTruncated ? 'First 100,000 characters' : 'The whole file'} +

    +
    +
    + {#await import('./CodeEditor.svelte') then { default: CodeEditor }} + + {/await} +
    + {:else if importPhase === 'idle'} {@render emptyState( Upload, 'Restore from a .sql file', @@ -853,6 +1023,7 @@ {:else}Restore complete{/if}

    + {@render logToggle()} {fmtElapsed(importElapsed)}
{@render progressBar( @@ -889,7 +1060,9 @@ {/if}
- {#if importResult?.errors.length} + {#if showLog} + {@render logPanel()} + {:else if importResult?.errors.length}
diff --git a/src/lib/components/CellEditorPanel.svelte b/src/lib/components/CellEditorPanel.svelte index 3c9a3667..487f02fa 100644 --- a/src/lib/components/CellEditorPanel.svelte +++ b/src/lib/components/CellEditorPanel.svelte @@ -50,7 +50,9 @@ import X from '@lucide/svelte/icons/x' import { cn } from '$lib/utils.js' import { toast } from '$lib/components/ui/sonner/toast.svelte.js' - import Kbd from './Kbd.svelte' + import { detectOs } from '$lib/platform.js' + /** The stage shortcut as one glyph run: ⌘↵ on a Mac, Ctrl↵ elsewhere. */ + const modEnter = detectOs() === 'macos' ? '⌘↵' : 'Ctrl ↵' let { open = $bindable(false), @@ -575,7 +577,7 @@ -
+
{colName} {#if colType} - {colType} + {colType} {/if} {#if sourceHint} - {sourceHint} + {sourceHint.replace(/^row\b/, "Row")} {/if} {#if isNull && !dirty} NULL @@ -607,8 +609,8 @@ {detached ? 'inside a row · read-only' : 'read-only'} {/if} - - {lines.toLocaleString()}L · {chars.toLocaleString()}c + + · {lines.toLocaleString()} {lines === 1 ? 'line' : 'lines'} · {chars.toLocaleString()} {chars === 1 ? 'character' : 'characters'} {#if oversize} {formatBytes(oversize.bytes)} · not loaded {/if} {#if dirty && !readOnly} - edited + Edited {/if}
@@ -677,16 +679,20 @@ > - + + {/if} + {/snippet} + {#snippet item(it)} + + {it.label} + {#if it.hint}{it.hint}{/if} + {#if String(it.value) === String(value)}{/if} + {/snippet} + {/snippet} {#if requiredAxes.x} @@ -524,18 +560,15 @@ Group
{#if !newGroupMode} - v && (saveGroup = v)}> - - {saveGroup} - - - {#each $chartGroups as g (g)}{g}{/each} - - + { if (v) saveGroup = v }} + items={$chartGroups.map((g) => ({ value: g, label: g, icon: 'folder' }))} + searchPlaceholder="Search groups…" + empty="No group by that name. Use + to add it." + triggerClass="h-8 min-w-0 flex-1" + /> diff --git a/src/lib/components/ChartsPage.svelte b/src/lib/components/ChartsPage.svelte index 658d21a6..3d365c50 100644 --- a/src/lib/components/ChartsPage.svelte +++ b/src/lib/components/ChartsPage.svelte @@ -34,6 +34,15 @@ import LayoutDashboard from "@lucide/svelte/icons/layout-dashboard"; import LayoutGrid from "@lucide/svelte/icons/layout-grid"; import Plus from "@lucide/svelte/icons/plus"; + import Share2 from "@lucide/svelte/icons/share-2"; + import MoreHorizontal from "@lucide/svelte/icons/more-horizontal"; + import LoaderCircle from "@lucide/svelte/icons/loader-circle"; + import * as DropdownMenu from "$lib/components/ui/dropdown-menu/index.js"; + import SearchField from "./SearchField.svelte"; + import SelectMenu from "./SelectMenu.svelte"; + import { executeSql } from "$lib/api.js"; + import { isWriteSql } from "$lib/sql-write.js"; + import { requestShare } from "$lib/share-flow.js"; /** * @typedef {import('$lib/stores/connections.js').SavedConnection} SavedConnection @@ -68,13 +77,53 @@ /** @type {string | null} */ let dashPickerChartId = $state(null); - // Charts per group (derived) - const chartsByGroup = $derived( - $chartGroups.map((g) => ({ - group: g, - charts: $savedCharts.filter((c) => c.group === g), - })), - ); + let query = $state(""); + /** "" = every group. */ + let groupFilter = $state(""); + /** The chart whose share is being prepared (its query re-runs). */ + let sharingId = $state(/** @type {string | null} */ (null)); + + // Charts per group, after the search and the group filter. + const chartsByGroup = $derived.by(() => { + const q = query.trim().toLowerCase(); + return $chartGroups + .filter((g) => !groupFilter || g === groupFilter) + .map((g) => ({ + group: g, + charts: $savedCharts.filter( + (c) => c.group === g && (!q || c.name.toLowerCase().includes(q) || (catalogEntry(c.config.type)?.label ?? c.config.type).toLowerCase().includes(q)), + ), + })); + }); + const shownCount = $derived(chartsByGroup.reduce((n, g) => n + g.charts.length, 0)); + + /** + * Share a saved chart: its query runs again (read only) so the link carries + * the rows as they are now, then the share dialog opens with them. + * @param {import('$lib/stores/saved-charts.js').SavedChart} chart + */ + async function shareChart(chart) { + if (!chart.sql?.trim()) { toast.error("This chart has no query to share"); return; } + if (isWriteSql(chart.sql)) { toast.error("Only charts made from a read-only query can be shared"); return; } + sharingId = chart.id; + try { + const r = await executeSql(chart.sql); + const columns = (r?.columns ?? []).map((/** @type {any} */ c) => ({ name: c.name, type: c.dataType ?? c.data_type })); + const rows = r?.rows ?? []; + if (!rows.length) { toast.message("The query returned no rows to share"); return; } + requestShare({ + kind: "chart", + title: chart.name, + chart: { type: chart.config.type, x: chart.config.xCol, y: [chart.config.yCol].filter(Boolean), group: chart.config.groupCol ?? null }, + columns, + rows, + }); + } catch (e) { + toast.error("Couldn't run the chart's query", { description: String(/** @type {any} */ (e)?.message ?? e) }); + } finally { + sharingId = null; + } + } const totalCharts = $derived($savedCharts.length); @@ -176,6 +225,11 @@ toast.success("SQL opened in Query Editor"); } + /** Focus a field as it appears (rename, new group). @param {HTMLInputElement} el */ + function focusOnMount(el) { + queueMicrotask(() => { el.focus(); el.select(); }); + } + const btnIconSm = "inline-flex size-6 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground"; @@ -209,405 +263,168 @@
{:else}
- -
- - Saved charts - - {totalCharts} - -
- {#if !newGroupOpen} - - {:else} -
- { - if (e.key === "Enter") commitNewGroup(); - if (e.key === "Escape") { - newGroupOpen = false; - } - }} - /> - - -
- {/if} -
+ +
+ {:else} + + {/if}
- -
+
{#if totalCharts === 0} - -
-
- -
-
-

No saved charts yet

-

- Save charts from the Query Editor to see them here -

-
-
- Run a query - - Chart tab - - Save chart -
+
+ +

No saved charts yet

+

Run a query, switch its result to the chart view, and save it. It shows up here, ready to share or put on a dashboard.

+ {:else if shownCount === 0} +

No charts match.

{:else}
{#each chartsByGroup as grpData (grpData.group)} {#if grpData.charts.length > 0} -
- -
- +
+
{#if renamingGroupName === grpData.group} -
- { - if (e.key === "Enter") commitRenameGroup(); - if (e.key === "Escape") { - renamingGroupName = null; - } - }} - /> - - -
+ { + if (e.key === "Enter") commitRenameGroup(); + if (e.key === "Escape") renamingGroupName = null; + }} + onblur={commitRenameGroup} + use:focusOnMount + /> {:else} -

- {grpData.group} -

- {grpData.charts.length} +

{grpData.group}

+ {grpData.charts.length} {#if grpData.group !== "Default"} - - + + + + + + startRenameGroup(grpData.group)}>Rename group + handleDeleteGroup(grpData.group)}>Delete group + + {/if} {/if}
-
-
- {#each grpData.charts as chart (chart.id)} - {@const entry = catalogEntry(chart.config.type)} -
- -
- {#if chart.previewOption && Object.keys(chart.previewOption).length > 0} - - {:else if chart.aiSpec} - -
- -
- {:else} -
- -
- {/if} - - -
-
- -
- +
+
+ {#if renamingChartId === chart.id} + { + if (e.key === "Enter") commitRenameChart(); + if (e.key === "Escape") renamingChartId = null; }} - > - - Dashboard - - {#if dashPickerChartId === chart.id} -
- {#each $dashboards as d (d.id)} - - {/each} -
- -
-
- {/if} -
+ onblur={commitRenameChart} + use:focusOnMount + /> + {:else} +

{chart.name}

+ {/if} +

{entry?.label ?? chart.config.type} · {fmtDate(chart.createdAt)}

-
- -
- - {#if moveChartId === chart.id} -
- {#each $chartGroups.filter((g) => g !== chart.group) as g (g)} - + {#if sharingId === chart.id} + + {/if} + + + + + + handleRunSql(chart)}>Open the query + void shareChart(chart)}>Share… + + Add to dashboard + + {#each $dashboards as d (d.id)} + addToDashboardById(chart.id, d.id)}>{d.name} {/each} - {#if $chartGroups.filter((g) => g !== chart.group).length === 0} -

- No other groups -

- {/if} -
+ {#if $dashboards.length}{/if} + addToNewDashboard(chart.id)}>New dashboard + + + {#if $chartGroups.length > 1} + + Move to group + + {#each $chartGroups.filter((g) => g !== chart.group) as g (g)} + handleMoveChart(chart.id, g)}>{g} + {/each} + + {/if} -
- -
+ startRenameChart(chart.id)}>Rename + + { deleteChart(chart.id); toast.success("Chart deleted"); }}>Delete + +
-
- - -
- {#if renamingChartId === chart.id} -
- { - if (e.key === "Enter") commitRenameChart(); - if (e.key === "Escape") { - renamingChartId = null; - } - }} - /> - - -
- {:else} - {chart.name} - {/if} - {entry?.label ?? chart.config.type} -
-
- {/each} -
-
-
- {/if} - {/each} - - - {#each chartsByGroup as grpData (grpData.group)} - {#if grpData.charts.length === 0} -
-
- -

- {grpData.group} -

- 0 - {#if grpData.group !== "Default"} - - {/if} -
-
- No charts in this group + + {/each} +
{/if} diff --git a/src/lib/components/ChoroplethChart.svelte b/src/lib/components/ChoroplethChart.svelte index c80a1dd3..c1f05f40 100644 --- a/src/lib/components/ChoroplethChart.svelte +++ b/src/lib/components/ChoroplethChart.svelte @@ -1,4 +1,5 @@ - -
-
- - - -
- {#if icon} - - {:else} - - {/if} -
+ +
+
+ {#if icon} + + {:else} + + {/if}
-
-

+

+

{verb}{name ? ` to ${name}` : ''}

{#if detail}

{detail}

{/if} - -

- {#if elapsed >= 8} - {elapsed}s · a far region can take a few seconds to answer - {:else if elapsed >= 2} - {elapsed}s - {/if} -

+
+ + + + + +
+ {#if elapsed >= 8} + {elapsed}s · a far region can take a few seconds + {:else if elapsed >= 2} + {elapsed}s + {/if} {#if oncancel}
+ + diff --git a/src/lib/components/ConnectionModal.svelte b/src/lib/components/ConnectionModal.svelte index 06c7546e..8d853dc3 100644 --- a/src/lib/components/ConnectionModal.svelte +++ b/src/lib/components/ConnectionModal.svelte @@ -24,12 +24,16 @@ connectDuckdb, testMssqlConnection, connectMssql, + testOracleConnection, + connectOracle, testRedis, connectRedis, testDynamo, connectDynamo, testCouch, connectCouch, + testElastic, + connectElastic, testCassandra, connectCassandra, testFirestore, @@ -77,7 +81,7 @@ import { focusTrap } from '$lib/actions/focus-trap.js'; import { toast } from "$lib/components/ui/sonner/toast.svelte.js"; import * as ContextMenu from "$lib/components/ui/context-menu/index.js"; - import { parseConnectionUri, detectConnectionUri } from "$lib/connection-uri.js"; + import { parseConnectionUri, detectConnectionUri, connectionUrl } from "$lib/connection-uri.js"; import { PROVIDERS, providerBuildConnection, providerWarm, providerToSaved, AUTH_FAILURE } from "$lib/providers.js"; import { providerOf, engineLabel } from "$lib/connection-provider.js"; import ConfirmDialog from "./ConfirmDialog.svelte"; @@ -94,6 +98,14 @@ * once consumed so the next plain open starts on the picker again. */ initialEngine = $bindable(""), + /** + * A connection string handed over from outside (a stroke:// link or the + * command line): read as if pasted, so the form opens filled in. Nothing + * connects until Connect is pressed. Cleared once consumed. + */ + initialUri = $bindable(""), + /** Name for that connection, e.g. the project folder it came from. */ + initialName = $bindable(""), onconnected = (conn, id) => {}, maxConnections = Infinity, /** Name of the live session, '' when nothing is connected. Drives Disconnect. */ @@ -103,6 +115,9 @@ onremoved = (/** @type {string} */ id) => {}, } = $props(); + /** The logos on the New connection button. */ + const NEW_CONN_STACK = ["postgres", "mysql", "mongodb", "sqlite"]; + const CATEGORIES = [ { label: "Relational", @@ -124,6 +139,7 @@ desc: "Distributed Postgres-compatible SQL", }, { id: "mssql", label: "SQL Server", desc: "Microsoft SQL Server" }, + { id: "oracle", label: "Oracle", desc: "Oracle Database (uses Oracle Instant Client)" }, ], }, { @@ -176,6 +192,8 @@ { id: "dynamodb", label: "DynamoDB", desc: "AWS key-value and document tables" }, { id: "mongodb", label: "MongoDB", desc: "Document database, also Atlas (mongodb+srv)" }, { id: "couchdb", label: "CouchDB", desc: "JSON documents over HTTP, Mango queries" }, + { id: "elasticsearch", label: "Elasticsearch", desc: "Search and analytics, also Elastic Cloud" }, + { id: "opensearch", label: "OpenSearch", desc: "Search and analytics, also Amazon OpenSearch Service" }, { id: "cassandra", label: "Cassandra", desc: "Wide-column store, also ScyllaDB (CQL)" }, { id: "firestore", label: "Firestore", desc: "Google Cloud document database" }, ], @@ -251,6 +269,7 @@ "mariadb", "cockroachdb", "mssql", + "oracle", "clickhouse", "duckdb", "sqlite-memory", @@ -272,6 +291,8 @@ "dynamodb", "mongodb", "couchdb", + "elasticsearch", + "opensearch", "cassandra", "firestore", ]; @@ -412,6 +433,13 @@ // custom endpoint (DynamoDB Local) are optional. // CouchDB: the server URL; user and password reuse the shared fields. let couchUrl = $state("http://localhost:5984"); + // Elasticsearch / OpenSearch: the server URL (shared with CouchDB's field), + // a login or an API key, and whether to accept a self-signed certificate. + let esAuth = $state(/** @type {'basic' | 'apikey'} */ ("basic")); + let esApiKey = $state(""); + let esInsecure = $state(false); + /** What the last test said the server is, e.g. "OpenSearch 2.17.0". */ + let esServer = $state(""); // Workspace and colour label of the connection being edited or created. let connColor = $state(/** @type {string | null} */ (null)); let connWorkspace = $state(DEFAULT_WORKSPACE_ID); @@ -427,6 +455,23 @@ let fsAuth = $state(/** @type {'serviceAccount'|'gcloud'|'emulator'} */ ("serviceAccount")); let fsKeyFile = $state(""); let fsEmulator = $state("localhost:8080"); + // Oracle: a service name (in the shared `database` field), a SID, or a whole + // connect string; and where Instant Client lives when the app can't find it. + /** Copy URL just copied: the button says so for a moment. */ + let urlCopied = $state(false); + let oraMode = $state(/** @type {'service'|'sid'|'connect'} */ ("service")); + let oraSid = $state(""); + let oraConnect = $state(""); + let oraClientDir = $state(""); + async function pickOracleClientDir() { + try { + const { open } = await import("@tauri-apps/plugin-dialog"); + const path = await open({ title: "Select the Instant Client folder", directory: true }); + if (typeof path === "string" && path) oraClientDir = path; + } catch { + /* browser/non-Tauri env - the field is still typeable */ + } + } async function pickFirestoreKey() { try { const { open } = await import("@tauri-apps/plugin-dialog"); @@ -642,7 +687,7 @@ // A string for another engine switches the engine too - refusing it and // making the user go and change the dropdown first would be pedantry. if (hit && hit.type !== dbType && !DISABLED_TABS.has(hit.type)) switchDriver(hit.type); - const ok = applyConnectionUriFrom(raw, hit?.uriType ?? uriTypeFor(dbType)); + const ok = applyConnectionUriFrom(hit?.uri ?? raw, hit?.uriType ?? uriTypeFor(dbType)); if (ok) { importUri = ""; // Left open would mean an empty bar sitting over the fields it just @@ -707,7 +752,7 @@ if (!raw) return; const hit = detectConnectionUri(raw); if (!hit) { - quickHint = "That does not look like a connection string. Try postgres://…, mysql://…, or a path to a .db file."; + quickHint = "That does not look like a connection string. Paste a URL (postgres://, mysql://, sqlserver://, oracle://, mongodb://, redis://, libsql://, …), a .env line, host=… dbname=…, or a path to a .db file."; return; } if (DISABLED_TABS.has(hit.type)) { @@ -725,7 +770,9 @@ const known = ALL_DRIVERS.find((d) => d.id === hit.provider); quickHint = `Recognised a ${known?.label ?? PROVIDER_LABELS[hit.provider] ?? hit.provider} endpoint.`; } - const ok = applyConnectionUriFrom(raw, hit.uriType); + // `hit.uri`: the string with its .env wrapper or driver suffix taken off, + // or a key/value string turned into a URL. + const ok = applyConnectionUriFrom(hit.uri ?? raw, hit.uriType); if (!ok && !quickHint) quickHint = "Could not read that string."; quickUri = ""; // Used. Coming back to the picker should not hand it over again. @@ -798,11 +845,18 @@ * is a dropdown on the form now - searchable, in reading order with the rest * of the connection - so the step is gone and so is the grid. */ + /** The engine dropdown on the form. */ + let enginePickerOpen = $state(false); + + /** New connection: the form, with the engine list already open to pick from. */ function newConnectionForm() { error = ""; testOk = false; quickHint = ""; pickEngine("postgres"); + // After the form step has rendered its trigger, or the popover has + // nothing to anchor to. + tick().then(() => requestAnimationFrame(() => (enginePickerOpen = true))); } /** Every driver, for the form's engine dropdown. */ @@ -968,6 +1022,19 @@ encrypt, trustCert, }; + if (dbType === "oracle") + return { + type: "oracle", + name: nm, + host, + port, + serviceName: oraMode === "service" ? database.trim() : "", + sid: oraMode === "sid" ? oraSid.trim() : "", + connectString: oraMode === "connect" ? oraConnect.trim() : "", + user, + password, + clientLibDir: oraClientDir.trim(), + }; if (dbType === "mongodb") return { type: "mongodb", name: nm, uri: mongoUri.trim(), database: mongoDb.trim() }; if (dbType === "firestore") @@ -984,6 +1051,14 @@ return { type: "cassandra", name: nm, hosts: cqlHosts.trim(), user: user.trim(), password, keyspace: cqlKeyspace.trim() }; if (dbType === "couchdb") return { type: "couchdb", name: nm, url: couchUrl.trim(), user: user.trim(), password }; + if (dbType === "elasticsearch" || dbType === "opensearch") + return { + type: dbType, + name: nm, + url: couchUrl.trim(), + ...(esAuth === "apikey" ? { apiKey: esApiKey.trim(), user: "", password: "" } : { user: user.trim(), password, apiKey: "" }), + insecure: esInsecure, + }; if (dbType === "dynamodb") return { type: "dynamodb", @@ -1092,9 +1167,23 @@ fsEmulator = conn.emulatorHost ?? "localhost:8080"; cqlHosts = conn.hosts ?? "127.0.0.1:9042"; cqlKeyspace = conn.keyspace ?? ""; + if (conn.type === "oracle") { + database = conn.serviceName ?? ""; + oraSid = conn.sid ?? ""; + oraConnect = conn.connectString ?? ""; + oraClientDir = conn.clientLibDir ?? ""; + oraMode = oraConnect ? "connect" : oraSid ? "sid" : "service"; + } connColor = conn.color ?? null; connWorkspace = conn.workspaceId ?? DEFAULT_WORKSPACE_ID; - couchUrl = conn.type === "couchdb" ? (conn.url ?? "http://localhost:5984") : "http://localhost:5984"; + couchUrl = + conn.type === "couchdb" ? (conn.url ?? "http://localhost:5984") + : conn.type === "elasticsearch" || conn.type === "opensearch" ? (conn.url ?? "http://localhost:9200") + : "http://localhost:5984"; + esAuth = conn.apiKey ? "apikey" : "basic"; + esApiKey = conn.apiKey ?? ""; + esInsecure = !!conn.insecure; + esServer = ""; awsAuth = conn.accessKeyId ? "keys" : "profile"; awsProfile = conn.profile ?? ""; awsKeyId = conn.accessKeyId ?? ""; @@ -1144,6 +1233,10 @@ connWorkspace = getStore(activeWorkspaceId); cqlHosts = "127.0.0.1:9042"; cqlKeyspace = ""; + oraMode = "service"; + oraSid = ""; + oraConnect = ""; + oraClientDir = ""; mongoUri = "mongodb://localhost:27017"; mongoDb = ""; fsProject = ""; @@ -1286,6 +1379,7 @@ cockroachdb: 26257, clickhouse: 8123, mssql: 1433, + oracle: 1521, redis: 6379, valkey: 6379, }; @@ -1309,10 +1403,13 @@ mariadb: { database: "", user: "root" }, clickhouse: { database: "default", user: "default" }, mssql: { database: "master", user: "sa" }, + oracle: { database: "", user: "" }, redis: { database: "0", user: "" }, valkey: { database: "0", user: "" }, dynamodb: { database: "", user: "" }, couchdb: { database: "", user: "admin" }, + elasticsearch: { database: "", user: "elastic" }, + opensearch: { database: "", user: "admin" }, cassandra: { database: "", user: "" }, firestore: { database: "", user: "" }, mongodb: { database: "", user: "" }, @@ -1338,6 +1435,11 @@ if (user === "" || user === prev.user) user = next.user ?? ""; if (id === "sqlite-memory" || id === "duckdb-memory") filePath = ":memory:"; if (id === "duckdb") filePath = ""; + // The HTTP engines share one URL field; move it to the new engine's port + // while it still holds the previous one's local default. + const HTTP_DEFAULT = /** @type {Record} */ ({ couchdb: "http://localhost:5984", elasticsearch: "http://localhost:9200", opensearch: "http://localhost:9200" }); + if (HTTP_DEFAULT[id] && (!couchUrl.trim() || Object.values(HTTP_DEFAULT).includes(couchUrl.trim()))) couchUrl = HTTP_DEFAULT[id]; + esServer = ""; error = ""; testOk = false; connectionUri = ""; @@ -1357,12 +1459,14 @@ if (t === "sqlite" || t === "sqlite-memory" || t === "duckdb" || t === "duckdb-memory") return "sqlite"; if (t === "mysql" || t === "mariadb") return "mysql"; if (t === "mssql") return "mssql"; + if (t === "oracle") return "oracle"; if (t === "clickhouse") return "clickhouse"; // Redis and LibSQL have their own shapes: a `redis://` string read by the // Postgres parser loses the database index and mistakes a bare password for // a username, and a Turso URL carries its token in the query string. if (t === "redis" || t === "valkey") return "redis"; if (t === "libsql") return "libsql"; + if (t === "couchdb" || t === "elasticsearch" || t === "opensearch") return "couchdb"; return "postgres"; } @@ -1402,6 +1506,39 @@ uriHint = "Fields updated from URL"; return true; } + // CouchDB: the server URL, the login in its own fields. + if ("couch" in parsed) { + couchUrl = parsed.url; + if (parsed.user) user = parsed.user; + if (parsed.password) password = parsed.password; + flashFields(["user", "password"]); + uriHint = "Fields updated from URL"; + return true; + } + // Cassandra / ScyllaDB: contact points, login, keyspace. + if ("cql" in parsed) { + cqlHosts = parsed.hosts; + cqlKeyspace = parsed.keyspace; + if (parsed.user) user = parsed.user; + if (parsed.password) password = parsed.password; + flashFields(["user", "password"]); + uriHint = "Fields updated from URL"; + return true; + } + // Oracle: host + service or SID, or a descriptor kept whole. + if ("oracle" in parsed) { + host = parsed.host || host; + port = parsed.port; + database = parsed.serviceName; + oraSid = parsed.sid; + oraConnect = parsed.connectString; + oraMode = parsed.connectString ? "connect" : parsed.sid ? "sid" : "service"; + if (parsed.user) user = parsed.user; + if (parsed.password) password = parsed.password; + flashFields(["host", "port", "database", "user", "password"]); + uriHint = "Fields updated from connection string"; + return true; + } // LibSQL / Turso: the URL, with any `authToken` moved to its own field. if ("authToken" in parsed) { libsqlUrl = parsed.url; @@ -1464,6 +1601,14 @@ libsqlUrl.replace(/^\w+:\/\//, "").split(".")[0] || activeDriver.label ); if (dbType === "d1") return "Cloudflare D1"; + // DynamoDB has no host field of its own: the form's leftover 127.0.0.1 + // named an AWS connection after localhost. Name it after where it goes. + if (dbType === "dynamodb") { + const ep = awsEndpoint.trim(); + if (ep) return `DynamoDB@${ep.replace(/^\w+:\/\//, "")}`; + const where = awsRegion.trim() || (awsAuth === "profile" ? (awsProfile.trim() || "default") : "AWS"); + return `DynamoDB · ${where}`; + } if (host.trim()) return `${database.trim() || activeDriver.label}@${host.trim()}`; return activeDriver.label; @@ -1476,6 +1621,7 @@ cockroachdb: "postgresql://user:pass@host:26257/defaultdb", clickhouse: "clickhouse://user:pass@host:8123/db", mssql: "sqlserver://user:pass@host:1433;database=db", + oracle: "oracle://user:pass@host:1521/FREEPDB1", redis: "rediss://:password@host:6379/0", libsql: "libsql://db-org.turso.io?authToken=…", sqlite: "/path/to/app.db", @@ -1493,7 +1639,7 @@ const suffix = id.replace(/^cn-/, ""); for (const cid of [ id, - ...["ch", "mssql", "redis"].map((p) => `cn-${p}-${suffix}`), + ...["ch", "mssql", "ora", "redis"].map((p) => `cn-${p}-${suffix}`), ]) { const el = /** @type {HTMLInputElement | null} */ ( document.getElementById(cid) @@ -1530,6 +1676,13 @@ }, }; } + if (/oracle instant client|dpi-1047/.test(e)) + return { + title: "Oracle Instant Client is not installed here", + hint: "Stroke talks to Oracle through Oracle's own client library. Download Instant Client Basic for this platform, unzip it, and point Instant Client folder at it.", + actionLabel: "Set Instant Client folder", + action: () => focusField("cn-ora-client"), + }; // A REST 401 comes first: `auth.*error` used to swallow "401 Unauthorized: // …\"errors\":[…]" and report a wrong *password* for a token that had simply // expired, which sends people to a field that isn't the problem. @@ -1642,7 +1795,7 @@ if (conn.type === "posthog") return conn.projectId ? `project ${conn.projectId}` : "—"; // Redis has no database name worth showing; where it lives is the useful bit. if (engineFamily(conn.type) === "redis") return conn.host ? `${conn.host}${conn.port ? `:${conn.port}` : ""}` : "—"; - if (conn.type === "couchdb") return conn.url || "—"; + if (conn.type === "couchdb" || conn.type === "elasticsearch" || conn.type === "opensearch") return conn.url || "—"; if (conn.type === "mongodb") return conn.uri ? uriHost(conn.uri) : "—"; if (conn.type === "firestore") return [conn.projectId, conn.auth === "emulator" ? "emulator" : null].filter(Boolean).join(" · ") || "—"; if (conn.type === "cassandra") return [conn.hosts, conn.keyspace].filter(Boolean).join(" · ") || "—"; @@ -1677,6 +1830,13 @@ engineQuery = ""; quickUri = ""; quickHint = ""; + if (initialUri) { + quickUri = initialUri; + useQuickUri(); + if (initialName && step !== "pick") name = initialName; + initialUri = ""; + initialName = ""; + } void refreshLocal(); // The paste bar takes focus: the modal opens, you paste, you press Enter. // Focus alone does not read the clipboard - only a click into the bar does. @@ -1735,9 +1895,11 @@ if (conn.type === "posthog") return connectPosthog(conn); if (conn.type === "duckdb") return connectDuckdb(conn); if (conn.type === "mssql") return connectMssql(conn); + if (conn.type === "oracle") return connectOracle(conn); if (engineFamily(conn.type) === "redis") return connectRedis(conn); if (conn.type === "dynamodb") return connectDynamo(conn); if (conn.type === "couchdb") return connectCouch(conn); + if (conn.type === "elasticsearch" || conn.type === "opensearch") return connectElastic(conn); if (conn.type === "cassandra") return connectCassandra(conn); if (conn.type === "firestore") return connectFirestore(conn); if (conn.type === "mongodb") return connectMongo(conn); @@ -2012,6 +2174,34 @@ }); } + /** + * The engine-specific connection fields for a container. The SQL engines and + * Redis take host/port/user; the document engines take a URI or endpoint. + * @param {import('$lib/api.js').DockerDatabase} d + */ + function dockerConnFields(d) { + const hp = `${d.host}:${d.port}`; + const auth = d.user ? `${encodeURIComponent(d.user)}:${encodeURIComponent(d.password)}@` : ""; + switch (d.engine) { + case "oracle": + return { host: d.host, port: d.port, serviceName: d.database, sid: "", connectString: "", user: d.user, password: d.password }; + case "mongodb": + return { uri: `mongodb://${auth}${hp}/${d.user ? "?authSource=admin" : ""}`, database: d.database }; + case "couchdb": + return { url: `http://${hp}`, user: d.user, password: d.password }; + case "elasticsearch": + case "opensearch": + return { url: `http://${hp}`, user: d.user, password: d.password, apiKey: "", insecure: false }; + case "cassandra": + return { hosts: hp, user: d.user, password: d.password, keyspace: "" }; + case "dynamodb": + // DynamoDB Local accepts any keys; it still wants some. + return { region: "us-east-1", endpoint: `http://${hp}`, accessKeyId: "local", secretAccessKey: "local", sessionToken: "" }; + default: + return { host: d.host, port: d.port, user: d.user, password: d.password, database: d.database }; + } + } + /** @param {import('$lib/api.js').DockerDatabase} d */ function dockerTarget(d) { const label = `Docker · ${d.image}`; @@ -2024,7 +2214,9 @@ ? { encrypt: true, trustCert: true } : d.engine === "clickhouse" ? { secure: false } - : { ssl: false }; + : ["oracle", "mongodb", "couchdb", "elasticsearch", "opensearch", "cassandra", "dynamodb"].includes(d.engine) + ? {} + : { ssl: false }; return /** @type {LocalTarget} */ ({ id: `docker:${d.name}`, container: d.name, @@ -2042,13 +2234,9 @@ id: `docker:${d.name}`, type: d.engine, name: d.name, - host: d.host, - port: d.port, - user: d.user, - password: d.password, - database: d.database, origin: "docker", toolLabel: label, + ...dockerConnFields(d), ...extras, }, }); @@ -2203,9 +2391,16 @@ else if (p.type === "clickhouse") await testClickhouseConnection(p); else if (p.type === "duckdb") await testDuckdbConnection(p); else if (p.type === "mssql") await testMssqlConnection(p); + else if (p.type === "oracle") await testOracleConnection(p); else if (engineFamily(p.type) === "redis") await testRedis(p); else if (p.type === "dynamodb") await testDynamo(p); else if (p.type === "couchdb") await testCouch(p); + else if (p.type === "elasticsearch" || p.type === "opensearch") { + const info = await testElastic(p); + esServer = `${info.flavor === "opensearch" ? "OpenSearch" : "Elasticsearch"} ${info.version}`.trim(); + // A server that answers as the other engine: switch the label to match. + if (info.flavor !== p.type && dbType === p.type) dbType = info.flavor; + } else if (p.type === "cassandra") await testCassandra(p); else if (p.type === "firestore") await testFirestore(p); else if (p.type === "mongodb") await testMongo(p); @@ -2248,6 +2443,7 @@ * close guard stops treating the form as dirty. */ function handleSave() { + if (isProviderTab) return; // nothing in the form to save; pick a database if (!editingId && saved.length >= maxConnections) { failWith( `Free plan allows ${maxConnections} saved connections. Upgrade to Stroke Pro for unlimited.`, @@ -2289,6 +2485,12 @@ * @param {{ save?: boolean }} [opts] */ async function handleConnect(opts = {}) { + // A provider (TiDB Cloud, Neon, D1...) connects by picking a database in its + // panel; the form behind it is empty, so dialling it hit 127.0.0.1. + if (isProviderTab) { + failWith("Pick a database above to connect."); + return; + } const save = opts.save !== false; if (save && !editingId && saved.length >= maxConnections) { failWith( @@ -2313,25 +2515,16 @@ try { const existing = editingId ? saved.find((s) => s.id === editingId) : null; const id = existing?.id ?? newConnectionId(); - // Persist BEFORE dialling. Everything the user typed is worth keeping the - // moment they commit to it, and a connect can fail for reasons that have - // nothing to do with what they entered - no network, VPN down, server - // asleep. Saving only on success meant a dropped connection threw the - // whole form away and they had to retype it, which is what made - // connections look like they were disappearing after being saved. - // lastConnectedAt stays at its previous value so a failed attempt does not - // jump the row to the top of the recents list or turn it into "Resume". - if (save) { - saved = upsertConnection( - buildSavedConn(payload, id, existing?.lastConnectedAt), - ).sort(byLastConnected); - editingId = id; - } + // Saved only once the connection works. Saving before dialling kept + // failed attempts (a wrong host, a provider default of 127.0.0.1) in the + // list and synced them to every device. A failed attempt keeps the form + // open with everything typed, and Save files it without connecting. await openConnection(payload); if (myOp !== opId) return; // cancelled by the user const saved_conn = buildSavedConn(payload, id, Date.now()); if (save) { saved = upsertConnection(saved_conn).sort(byLastConnected); + editingId = id; setLastConnectionId(id); } open = false; @@ -2417,6 +2610,10 @@ sshKeepalive, sslMode, sslCaPath, + oraMode, + oraSid, + oraConnect, + oraClientDir, ]); } const isDirty = $derived(snapshot() !== baseline); @@ -2431,11 +2628,13 @@ return filePath || ":memory:"; if (dbType === "libsql") return uriHost(libsqlUrl.replace(/^libsql:/, "https:")) || libsqlUrl || "—"; if (dbType === "d1") return databaseId ? `${databaseId.slice(0, 8)}…` : "—"; - if (dbType === "couchdb") return couchUrl || "—"; + if (dbType === "couchdb" || dbType === "elasticsearch" || dbType === "opensearch") return couchUrl || "—"; if (dbType === "cassandra") return cqlHosts || "—"; if (dbType === "firestore") return fsProject || "—"; if (dbType === "mongodb") return uriHost(mongoUri) || "—"; if (dbType === "dynamodb") return awsEndpoint || awsRegion || (awsAuth === "profile" ? awsProfile || "default profile" : "AWS"); + if (dbType === "oracle" && oraMode === "connect") return oraConnect || "—"; + if (dbType === "oracle" && oraMode === "sid") return `${host || "—"}:${port || "—"}:${oraSid || ""}`; return `${host || "—"}:${port || "—"}/${database || ""}`; }); @@ -3049,7 +3248,7 @@ {#snippet readOnlyRow()}
{:else} -
-

- No saved connections -

-
+ +

+ Saved connections show up here. +

{/if}
@@ -3440,25 +3642,41 @@
- {#if !railOpen} - + +
+ {#if !railOpen} + + {/if} + + - {/if} +
@@ -3484,13 +3702,15 @@ nobody has picked yet. --> +
{#if step === "pick"} -
+

@@ -3518,17 +3738,41 @@ aria-hidden="true" /> + + +

Paste a connection string, or pick where your database lives.

{:else} - - -
+ +
+ + {@const copyUrl = connectionUrl(formPayload())} + {#if copyUrl} + + {/if} {/if}
{/if} @@ -3629,12 +3897,13 @@ auto-fill grid with 200px of air between them, and "New connection" put its chevron 1800px from its own label. One 42rem column, one leading edge. --> - + -
+
-
-

- Something else -

- -
- -
-
@@ -3953,7 +4198,7 @@ same layout roomier - and the panel already runs the header, the footer and the URL bar edge to edge, so a capped form in the middle of them was the odd one out. --> -
+
@@ -4094,15 +4339,15 @@
@@ -4266,15 +4511,15 @@
+ + {:else if dbType === "oracle"} +
+ Connect with +
+ {#each [{ v: "service", l: "Service name" }, { v: "sid", l: "SID" }, { v: "connect", l: "Connect string" }] as m (m.v)} + + {/each} +
+
+ + {#if oraMode === "connect"} +
+ + +

+ Easy Connect (tcps://host:2484/service), a full + (DESCRIPTION=…) descriptor, or a tnsnames.ora alias + (with TNS_ADMIN set). +

+
+ {:else} +
+
+ + +
+
+ + +
+
+ {#if oraMode === "sid"} +
+ + +
+ {:else} +
+ + + {#if !database.trim()} +

+ Blank uses the listener's default, often the root container (CDB$ROOT), which holds only Oracle's own schemas. Your tables are usually in a pluggable database such as FREEPDB1. +

+ {/if} +
+ {/if} + {/if} + +
+
+ + +
+
+ + +
+
+ +
+ +
+ + +
+

+ Stroke uses Oracle Instant Client, a free download from Oracle. Leave this empty when it is + installed system-wide or in a usual place (ORACLE_HOME, /opt/oracle, your home or Downloads folder). +

+
+ {:else if isRedisForm}
@@ -4576,17 +4944,71 @@ Leave the user blank for a server without a login (admin party).

+ + {:else if dbType === "elasticsearch" || dbType === "opensearch"} +
+ + + {#if esServer} +

Server: {esServer}

+ {/if} +
+
+ Sign in with +
+ {#each [{ v: "basic", l: "User and password" }, { v: "apikey", l: "API key" }] as m (m.v)} + + {/each} +
+
+ {#if esAuth === "basic"} +
+
+ + +
+
+ + +
+
+

+ Leave the user blank for a server with security turned off. +

+ {:else} +
+ + +

The encoded key Kibana shows once when the key is created.

+
+ {/if} + + {:else if dbType === "dynamodb"}
Credentials -
+
{#each [{ v: "profile", l: "AWS profile" }, { v: "keys", l: "Access keys" }] as m (m.v)}
-
+
@@ -4876,8 +5298,14 @@ {/if} -
-
+
+ +
- + + + Create database + A new {engineLabel} database on this server. + - -
-
- -
-
- Create database -

- {engineLabel} · on this server -

-
-
- - -
- - -
- + +
+
+ -

- Letters, digits, underscores · max 63 characters +

+ Letters, digits and underscores, up to 63 characters.

@@ -169,21 +155,16 @@ type="button" aria-expanded={showAdvanced} aria-controls="cdb-advanced" - class="flex w-full items-center gap-1.5 rounded-md py-1.5 text-left text-ui-xs text-muted-foreground transition-colors hover:text-foreground" + class="-mb-3 flex w-fit items-center gap-1.5 rounded-md text-left text-ui-xs text-muted-foreground transition-colors duration-150 hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring" onclick={() => (showAdvanced = !showAdvanced)} > - - Advanced options - {#if !showAdvanced} - - encoding, collation, owner - - {/if} + + {isMySQL ? 'Character set and collation' : 'Encoding, collation and owner'} {#if showAdvanced} -
+
{#if !isMySQL} @@ -277,26 +258,17 @@ {/if} {/if} - {#if error} -

{error}

+ {/if} +
- -
- +
+ +
+ + + {#snippet child({ props })} + + {/snippet} + + + {#each PRESETS as p (p.label)} + addCols(p.make())}> + {p.label} + {p.hint} + + {/each} + + + +
{#if err('columns')}

{err('columns')}

{/if}