Skip to content

Commit 4208375

Browse files
committed
ci: add database migration files and update Dockerfile for migrations
1 parent e657131 commit 4208375

6 files changed

Lines changed: 436 additions & 5 deletions

File tree

.gitignore

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,4 @@ coverage/
3535

3636
# Misc
3737
.cache/
38-
.tanstack/
39-
40-
# migrations
41-
migrations/
42-
**/migrations/
38+
.tanstack/

apps/backend/Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ COPY --from=builder /app/node_modules ./node_modules
2828
COPY --from=builder /app/packages/db/dist ./node_modules/@repo/db/dist
2929
COPY --from=builder /app/packages/shared/dist ./node_modules/@repo/shared/dist
3030
COPY --from=builder /app/packages/email-templates/dist ./node_modules/@repo/email-templates/dist
31+
COPY --from=builder /app/packages/db/migrations ./migrations
3132

3233
ENV NODE_ENV=production
3334
ENV PORT=9999

apps/backend/src/db/migrate.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import path from "node:path";
2+
import { drizzle } from "drizzle-orm/postgres-js";
3+
import { migrate } from "drizzle-orm/postgres-js/migrator";
4+
import postgres from "postgres";
5+
6+
const DATABASE_URL = process.env.DATABASE_URL;
7+
if (!DATABASE_URL) {
8+
console.error("Error: DATABASE_URL environment variable is required.");
9+
process.exit(1);
10+
}
11+
12+
// In the Docker image the runner WORKDIR is /app and migrations are copied to /app/migrations.
13+
// Locally, process.cwd() is the workspace root where packages/db/migrations lives — but the
14+
// recommended approach for local dev is `pnpm db:migrate` (drizzle-kit push).
15+
const migrationsFolder = path.join(process.cwd(), "migrations");
16+
17+
const client = postgres(DATABASE_URL, { max: 1 });
18+
const db = drizzle(client);
19+
20+
async function main() {
21+
console.log(`Running migrations from: ${migrationsFolder}`);
22+
await migrate(db, { migrationsFolder });
23+
console.log("Migrations complete.");
24+
}
25+
26+
main()
27+
.catch((err) => {
28+
console.error("Migration failed:", err);
29+
process.exit(1);
30+
})
31+
.finally(() => client.end());
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
CREATE TYPE "public"."user_role" AS ENUM('user', 'admin');--> statement-breakpoint
2+
CREATE TABLE "users" (
3+
"id" text PRIMARY KEY NOT NULL,
4+
"email" text NOT NULL,
5+
"name" text NOT NULL,
6+
"image" text,
7+
"role" "user_role" DEFAULT 'user' NOT NULL,
8+
"email_verified" boolean DEFAULT false NOT NULL,
9+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
10+
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
11+
CONSTRAINT "users_email_unique" UNIQUE("email")
12+
);
13+
--> statement-breakpoint
14+
CREATE TABLE "accounts" (
15+
"id" text PRIMARY KEY NOT NULL,
16+
"user_id" text NOT NULL,
17+
"account_id" text NOT NULL,
18+
"provider_id" text NOT NULL,
19+
"access_token" text,
20+
"refresh_token" text,
21+
"access_token_expires_at" timestamp with time zone,
22+
"refresh_token_expires_at" timestamp with time zone,
23+
"scope" text,
24+
"password" text,
25+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
26+
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
27+
);
28+
--> statement-breakpoint
29+
CREATE TABLE "sessions" (
30+
"id" text PRIMARY KEY NOT NULL,
31+
"user_id" text NOT NULL,
32+
"token" text NOT NULL,
33+
"expires_at" timestamp with time zone NOT NULL,
34+
"ip_address" text,
35+
"user_agent" text,
36+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
37+
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
38+
CONSTRAINT "sessions_token_unique" UNIQUE("token")
39+
);
40+
--> statement-breakpoint
41+
CREATE TABLE "verifications" (
42+
"id" text PRIMARY KEY NOT NULL,
43+
"identifier" text NOT NULL,
44+
"value" text NOT NULL,
45+
"expires_at" timestamp with time zone NOT NULL,
46+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
47+
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
48+
);
49+
--> statement-breakpoint
50+
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
51+
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;

0 commit comments

Comments
 (0)