Skip to content

fix: align database layer and API routes with SQLite backend#7

Merged
ehsanking merged 1 commit into
mainfrom
claude/analyze-debug-project-FRzCe
May 4, 2026
Merged

fix: align database layer and API routes with SQLite backend#7
ehsanking merged 1 commit into
mainfrom
claude/analyze-debug-project-FRzCe

Conversation

@ehsanking
Copy link
Copy Markdown
Owner

The control plane uses better-sqlite3 but several modules still emitted
MySQL-only SQL or referenced columns that were never created, breaking
the production build and core flows (PKI, backups, audit logging).

  • Restore the build by exporting getPkiService from lib/pki-service.ts
    (the new download route imported it) and rewriting the helper to use
    SQLite-compatible upserts (ON CONFLICT(...) DO UPDATE SET ... = excluded.x).
  • Add idempotent column migrations in lib/db.ts so existing panel.sqlite
    files gain the missing client_cert, client_key, and wg_privkey
    columns required by the OpenVPN/WireGuard flows.
  • Replace MySQL NOW() with CURRENT_TIMESTAMP in the audit logger and
    the user-create route, and drop the redundant created_at column from
    the user INSERT (the table already has a default).
  • Detect duplicate-username errors via SQLite's SQLITE_CONSTRAINT_UNIQUE
    and the UNIQUE constraint failed message in app/api/users/route.ts,
    while still honouring the legacy MySQL ER_DUP_ENTRY code.
  • Convert app/api/backup/import/route.ts to SQLite upserts and remove
    references to columns that don't exist (traffic_up, traffic_down,
    connected_clients, last_check); mirror the cleanup in the export.
  • Replace the insecure Math.random() WireGuard private-key generator
    in app/api/client/download/route.ts with crypto.randomBytes(32),
    and persist the generated key per-user so subsequent downloads are
    stable instead of rotating on every request.
  • Reduce app/api/migrate/route.ts to a thin trigger for the new
    in-process migrator; the previous body used MySQL-only ALTER TABLE ... ADD COLUMN IF NOT EXISTS syntax that always failed under SQLite.
  • Normalise the user status enum across lib/schemas.ts,
    lib/db-types.ts, and both user routes so values produced by the UI
    ('disabled') validate consistently.
  • Tidy lint warnings: drop unused imports in the tunnel-nodes view, the
    add-node modal, the subscription route, and lib/tunnel-commands.ts.

https://claude.ai/code/session_01BF6aKfH4Q7CjxF3ycVkRoT

The control plane uses better-sqlite3 but several modules still emitted
MySQL-only SQL or referenced columns that were never created, breaking
the production build and core flows (PKI, backups, audit logging).

- Restore the build by exporting `getPkiService` from `lib/pki-service.ts`
  (the new download route imported it) and rewriting the helper to use
  SQLite-compatible upserts (`ON CONFLICT(...) DO UPDATE SET ... = excluded.x`).
- Add idempotent column migrations in `lib/db.ts` so existing panel.sqlite
  files gain the missing `client_cert`, `client_key`, and `wg_privkey`
  columns required by the OpenVPN/WireGuard flows.
- Replace MySQL `NOW()` with `CURRENT_TIMESTAMP` in the audit logger and
  the user-create route, and drop the redundant `created_at` column from
  the user INSERT (the table already has a default).
- Detect duplicate-username errors via SQLite's `SQLITE_CONSTRAINT_UNIQUE`
  and the `UNIQUE constraint failed` message in `app/api/users/route.ts`,
  while still honouring the legacy MySQL `ER_DUP_ENTRY` code.
- Convert `app/api/backup/import/route.ts` to SQLite upserts and remove
  references to columns that don't exist (`traffic_up`, `traffic_down`,
  `connected_clients`, `last_check`); mirror the cleanup in the export.
- Replace the insecure `Math.random()` WireGuard private-key generator
  in `app/api/client/download/route.ts` with `crypto.randomBytes(32)`,
  and persist the generated key per-user so subsequent downloads are
  stable instead of rotating on every request.
- Reduce `app/api/migrate/route.ts` to a thin trigger for the new
  in-process migrator; the previous body used MySQL-only `ALTER TABLE
  ... ADD COLUMN IF NOT EXISTS` syntax that always failed under SQLite.
- Normalise the user `status` enum across `lib/schemas.ts`,
  `lib/db-types.ts`, and both user routes so values produced by the UI
  ('disabled') validate consistently.
- Tidy lint warnings: drop unused imports in the tunnel-nodes view, the
  add-node modal, the subscription route, and `lib/tunnel-commands.ts`.

https://claude.ai/code/session_01BF6aKfH4Q7CjxF3ycVkRoT
@vercel
Copy link
Copy Markdown

vercel Bot commented May 4, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
power-vpn Ready Ready Preview, Comment, Open in v0 May 4, 2026 2:20pm

@ehsanking ehsanking merged commit 7b58c8d into main May 4, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants