Set up Neon and Hyperdrive
Use this guide at the first production deploy. Cloud provisioning and baseline
migration generation remain deferred under
NAT-198.
For daily development, follow the local setup.
Local development and CI continue to use plain Postgres and db:push.
Prepare the baseline at launch
- Finish the v1 schema changes and pass the repository checks, including the web production build and the plain-Postgres CI job.
- Generate the baseline from that schema with
bun run db:generatefrom the repository root. Review the SQL and commit the generatedpackages/db/drizzledirectory, including its snapshots, before deploying. - Apply
bun run db:migrateto an empty, isolated local Postgres database withDATABASE_URLset to that database. Run the integration checks in the database guide and source-storage guide. - On that disposable database, run
bun run db:pushand confirm it reports no schema changes. Investigate any difference before proceeding. - Add
db:migrateto the deployment workflow before either Worker deploys. Make a migration failure stop deployment. KeepDATABASE_URLscoped to the target database and use its direct connection with TLS.
The current deploy scripts do not run database migrations. Complete the workflow change at launch. After the baseline is committed, generate a new reviewed migration for each schema change. Preserve migrations already applied to production. See Drizzle migrations.
The repository has no SQL scripts or generated migrations before production setup. For a new production database, apply the baseline once. For any existing deployment, inspect its actual schema and migration history, then prepare and review the required upgrade separately.
Provision the production database and Hyperdrive
- Create a Neon project in the chosen database region. Use
mainfor production. Record the provider and region for Worker placement. - Save the direct Postgres connection string in the production deployment secret
store. Keep production credentials out of local
.envand.dev.varsfiles. Turn off Neon's connection-pooling option when retrieving the Hyperdrive origin connection. Follow the Neon connection guide. - In the intended Cloudflare account, create the production Hyperdrive configuration. Disable query caching before binding it to the Worker. Keep credentials out of tracked files and command history.
- Verify the configuration's origin and
caching.disabledvalue in the dashboard or with the project-localwrangler hyperdrive getcommand. Record its ID for the production binding.
Caching belongs to the Hyperdrive configuration, not the Wrangler binding.
Cloudflare enables it by default. See
Disable caching
for the --caching-disabled option when creating or updating configurations.
Configure the production Workers
Texfolia has two environments: local development and production. Local development uses loopback Postgres, a Docker compiler container, and local Worker storage. Production uses Cloudflare Workers, R2, queues, Durable Objects, Neon through Hyperdrive, and the compiler image on a container host of your choice. These environments do not share resources or secrets.
Edit apps/web/wrangler.jsonc before building the production deployment artifact.
- Replace the existing
env.production.hyperdriveplaceholder ID with the cache-disabled production configuration. Keep the binding nameHYPERDRIVE. - Add a production
placementsetting with the matching database region. For example, use"placement": { "region": "aws:us-east-1" }only for a database in that region. See Placement hints. - Configure production routes, auth origins, Turnstile keys, OAuth callbacks, Stripe settings, email delivery, and Worker secrets in the production account. Never copy production secrets into local environment files.
- Provision the production source bucket
texfolia-sources, the build artifact buckettexfolia-compilations, and thecompile-pro,compile-free,compile-dlq, andbackgroundqueues. The product app consumes the queues. - Run the compiler image from
apps/compiler/Dockerfilebehind https with a long randomCOMPILER_TOKEN. It has no storage of its own. Keep it off any internal network, since biber and LuaTeX run code a document controls. Setenv.production.vars.COMPILER_URLto its address, and set the same token on the web Worker withwrangler secret put COMPILER_TOKEN --env production. - Run
bun run cf:typegenafter changing bindings.
The top-level web configuration is for local emulation. Its local Worker,
and source bucket names are distinct from production. Remote
Vite bindings are disabled. Keep DATABASE_URL and
CLOUDFLARE_HYPERDRIVE_LOCAL_CONNECTION_STRING_HYPERDRIVE on loopback hosts.
The local Hyperdrive placeholder is not a cloud configuration ID.
Migrate, deploy, and verify production
- Run the full app checks locally before deployment. Verify sign-in, project creation, source save and reload, collaboration, compilation, and PDF retrieval against local Postgres and local storage.
- Run the reviewed, committed migrations from the production deployment context
with
DATABASE_URLset to Neon's direct connection. Usedb:migrate.db:pushaccepts only local databases. - Run
bun run deployafter the migration succeeds. Turbo builds the web app forenv.productionand deploys it. Deploy a new compiler image before a web deploy that needs it. Both Worker deploy scripts explicitly pass--env production.bun run --cwd apps/web buildexplicitly setsCLOUDFLARE_ENV=production. Inspect the generated Worker name, Hyperdrive ID, source bucket, andCOMPILER_URLbefore deploying the artifact. Selecting an environment only at deploy time does not retarget a Vite build. See Cloudflare Vite environments. - Check that
GET /api/healthon the app answers 200. It answers 503 when the database does not answer within five seconds, so an uptime monitor can use it. - Check the deployed production app and inspect Worker logs for database and binding errors. Confirm saved writes are visible immediately and Hyperdrive query caching remains disabled.
The current deploy scripts do not run database migrations. Complete that launch workflow before the first production deploy. Account, route, secret provisioning, and the existing production placeholder values remain launch setup work.