Should Qwik City routeAction$ migrations avoid request-scoped execution?
0 reputation · 21 May 2021, 09:13 UTC
0 reputation · 21 May 2021, 09:13 UTC
Can a Qwik City routeAction$ safely trigger a database schema migration, or should migrations remain a deployment-time step? The routeAction$ is a documented server-side mutation primitive that runs within a request lifecycle and can validate input before returning data to the client.
Qwik does not manage database transactions, DDL, or migration history. The unresolved decision centers on placement: executing DDL in a request-scoped action couples migration timing to HTTP timeouts, retries, and concurrent replicas. Multiple application replicas make request-triggered DDL riskier unless the migration tool serializes execution across processes.
A failed action response does not prove the database change was reverted; partial schema states can persist. Qwik City actions can return an explicit failure value through the action's fail helper, letting a client distinguish a rejected request from a successful mutation. A request-triggered migration is only as safe as its idempotency and locking.
35775 reputation · 21 May 2021, 12:50 UTC
No. Database schema migrations should run as a separate deployment step, not inside a routeAction$ handler. Request-scoped execution couples irreversible DDL to HTTP timeouts, client retries, and replica concurrency — risks that deployment-time tooling is designed to avoid.
Technically possible but strongly discouraged. It requires reimplementing migration tool internals inside an HTTP handler:
pg_advisory_lock, Redis SETNX, Consul) before DDL.IF NOT EXISTS, CREATE OR REPLACE).CREATE INDEX CONCURRENTLY).This adds significant complexity, observability gaps, and operational risk compared to established CLI tools (Flyway, Liquibase, Prisma Migrate, Drizzle Kit).
A routeAction$ should return application data, not infrastructure state. Use a separate health/readiness endpoint that queries the migration version table, or a dedicated admin action with authentication/authorization separate from user-facing mutations.
| Engine | Transactional DDL | Notes |
|---|---|---|
| PostgreSQL | Yes (most DDL) | Best option for transactional safety in a long-running Node process. |
| MySQL 8.0+ | Atomic DDL for some statements | Not all DDL is transactional; verify per-statement. |
| SQL Server | Yes | Full transactional DDL support. |
| SQLite | No | DDL auto-commits. |
| MariaDB | Limited | Varies by version and statement. |
No Qwik adapter (Node, Bun, Deno, Cloudflare Workers, Vercel Edge, Netlify Functions) provides built-in transaction safety for DDL. Safety depends entirely on the database engine and driver combination.
routeAction$ for application-level mutations that are naturally request-scoped and retry-safe.BEGIN; CREATE TABLE test (id int); ROLLBACK; — verify the table does not exist.CREATE INDEX CONCURRENTLY) with concurrent requests and client retries. Observe duplicate execution and partial state persistence.What database engine, driver, and connection pooler are you using? This determines whether transactional DDL is even possible in your stack and whether a request-scoped approach could be made safe (though still not recommended).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.