rogasper commited on
Commit
65bc265
·
1 Parent(s): 14be139

feat: update deployment documentation for Coolify, clarifying post-deployment commands and environment variable setup. Enhance error handling in drizzle.config.ts to ensure DATABASE_URL is properly configured for production and local development.

Browse files
Files changed (2) hide show
  1. deploy/COOLIFY.md +23 -8
  2. packages/db/drizzle.config.ts +17 -4
deploy/COOLIFY.md CHANGED
@@ -122,21 +122,35 @@ PLATFORM_AI_MODEL=
122
  FREE_CREDITS_ENABLED=false
123
  ```
124
 
125
- ### Pre-deploy command
126
 
127
- Run on first deploy and after schema changes:
128
 
129
  ```bash
130
- bun run db:push
131
  ```
132
 
133
- Or, if using migration files:
134
 
135
- ```bash
136
- bun run db:migrate
 
 
 
 
 
 
 
 
137
  ```
138
 
139
- Ensure `DATABASE_URL` is set when this command runs. Drizzle reads it from the environment (`packages/db/drizzle.config.ts`).
 
 
 
 
 
 
140
 
141
  ### Verify
142
 
@@ -193,7 +207,7 @@ Rebuild the web app whenever the API URL changes.
193
 
194
  1. [ ] Deploy PostgreSQL and Redis; confirm both are healthy.
195
  2. [ ] Create server app, set all runtime env vars.
196
- 3. [ ] Run pre-deploy `bun run db:push` (or `db:migrate`).
197
  4. [ ] Deploy server; confirm `https://api.example.com/` returns `OK`.
198
  5. [ ] Create web app, set `VITE_SERVER_URL` as **buildtime** env.
199
  6. [ ] Deploy web; confirm app loads and API calls reach the server.
@@ -206,6 +220,7 @@ Rebuild the web app whenever the API URL changes.
206
 
207
  | Symptom | Likely cause | Fix |
208
  |---------|--------------|-----|
 
209
  | API calls go to `localhost:3000` | `VITE_SERVER_URL` not set at build time | Rebuild web with buildtime env |
210
  | CORS or auth errors | `CORS_ORIGIN` ≠ web URL | Match exact public web URL on server |
211
  | Server crashes on start | Missing or invalid env (SMTP, secrets) | Check Coolify logs; compare with `apps/server/.env.example` |
 
122
  FREE_CREDITS_ENABLED=false
123
  ```
124
 
125
+ ### Post-deployment command (migrations)
126
 
127
+ In the server application: **Configuration Advanced → Post-deployment Command**:
128
 
129
  ```bash
130
+ bun run db:migrate
131
  ```
132
 
133
+ Run on first deploy and after schema changes. Alternative for local-style sync: `bun run db:push`.
134
 
135
+ **`DATABASE_URL` must be set in Coolify Environment Variables** before deploy. The container does not have `apps/server/.env` (it is gitignored). Drizzle reads `DATABASE_URL` from `process.env` in production.
136
+
137
+ To get the connection string from a Coolify PostgreSQL service:
138
+
139
+ 1. Open your PostgreSQL resource → **Configuration** or **Connect**
140
+ 2. Copy the **internal** connection URL (hostname is the Docker service name, not `localhost`)
141
+ 3. Paste into the server app env:
142
+
143
+ ```env
144
+ DATABASE_URL=postgresql://postgres:YOUR_PASSWORD@YOUR_POSTGRES_HOST:5432/labas
145
  ```
146
 
147
+ If migrate fails with `url: ''`, `DATABASE_URL` is missing or empty in the server app's environment fix that and redeploy.
148
+
149
+ Optional seed (run once via **Terminal** on the server app):
150
+
151
+ ```bash
152
+ cd packages/db && bun run db:seed
153
+ ```
154
 
155
  ### Verify
156
 
 
207
 
208
  1. [ ] Deploy PostgreSQL and Redis; confirm both are healthy.
209
  2. [ ] Create server app, set all runtime env vars.
210
+ 3. [ ] Set **Post-deployment Command** to `bun run db:migrate` (after `DATABASE_URL` is set).
211
  4. [ ] Deploy server; confirm `https://api.example.com/` returns `OK`.
212
  5. [ ] Create web app, set `VITE_SERVER_URL` as **buildtime** env.
213
  6. [ ] Deploy web; confirm app loads and API calls reach the server.
 
220
 
221
  | Symptom | Likely cause | Fix |
222
  |---------|--------------|-----|
223
+ | Migrate fails: `url: ''` / `Please provide required params` | `DATABASE_URL` not set in Coolify env | Add internal Postgres URL to server app Environment Variables, redeploy |
224
  | API calls go to `localhost:3000` | `VITE_SERVER_URL` not set at build time | Rebuild web with buildtime env |
225
  | CORS or auth errors | `CORS_ORIGIN` ≠ web URL | Match exact public web URL on server |
226
  | Server crashes on start | Missing or invalid env (SMTP, secrets) | Check Coolify logs; compare with `apps/server/.env.example` |
packages/db/drizzle.config.ts CHANGED
@@ -1,15 +1,28 @@
 
 
 
1
  import dotenv from "dotenv";
2
  import { defineConfig } from "drizzle-kit";
3
 
4
- dotenv.config({
5
- path: "../../apps/server/.env",
6
- });
 
 
 
 
 
 
 
 
 
 
7
 
8
  export default defineConfig({
9
  schema: "./src/schema",
10
  out: "./src/migrations",
11
  dialect: "postgresql",
12
  dbCredentials: {
13
- url: process.env.DATABASE_URL || "",
14
  },
15
  });
 
1
+ import { existsSync } from "node:fs";
2
+ import { dirname, resolve } from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
  import dotenv from "dotenv";
5
  import { defineConfig } from "drizzle-kit";
6
 
7
+ const serverEnvPath = resolve(dirname(fileURLToPath(import.meta.url)), "../../apps/server/.env");
8
+
9
+ // Coolify/production inject DATABASE_URL via process.env; .env file is local-dev only.
10
+ if (!process.env.DATABASE_URL && existsSync(serverEnvPath)) {
11
+ dotenv.config({ path: serverEnvPath });
12
+ }
13
+
14
+ const databaseUrl = process.env.DATABASE_URL;
15
+ if (!databaseUrl) {
16
+ throw new Error(
17
+ "DATABASE_URL is not set. Add it to Coolify environment variables (or apps/server/.env for local dev).",
18
+ );
19
+ }
20
 
21
  export default defineConfig({
22
  schema: "./src/schema",
23
  out: "./src/migrations",
24
  dialect: "postgresql",
25
  dbCredentials: {
26
+ url: databaseUrl,
27
  },
28
  });