Skip to content

Commit 5790cb2

Browse files
authored
World postgres drizzle migrator (#312)
* Use drizzle migrator Signed-off-by: paulhenri-l <[email protected]> * Changeset Signed-off-by: paulhenri-l <[email protected]> --------- Signed-off-by: paulhenri-l <[email protected]>
1 parent a6f5545 commit 5790cb2

File tree

10 files changed

+637
-340
lines changed

10 files changed

+637
-340
lines changed

.changeset/fluffy-peaches-buy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/world-postgres": patch
3+
---
4+
5+
Use drizzle migrator
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Generate migrations
2+
3+
The migrations are generated and managed by drizzle. When you perform schema changes you have to generate new migrations using the following command:
4+
5+
```
6+
pnpm drizzle-kit generate --dialect=postgresql --schema=./src/drizzle/schema.ts --out src/drizzle/migrations
7+
```

packages/world-postgres/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"@workflow/world": "workspace:*",
5151
"@workflow/world-local": "workspace:*",
5252
"dotenv": "^16.4.5",
53-
"drizzle-orm": "^0.31.2",
53+
"drizzle-orm": "^0.44.7",
5454
"pg-boss": "^11.0.7",
5555
"postgres": "^3.4.7",
5656
"ulid": "^3.0.1",
@@ -62,7 +62,7 @@
6262
"@workflow/errors": "workspace:*",
6363
"@workflow/tsconfig": "workspace:*",
6464
"@workflow/world-testing": "workspace:*",
65-
"drizzle-kit": "^0.22.7",
65+
"drizzle-kit": "^0.31.6",
6666
"vitest": "catalog:"
6767
},
6868
"keywords": [],

packages/world-postgres/src/cli.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import { readFile } from 'node:fs/promises';
21
import { dirname, join } from 'node:path';
32
import { fileURLToPath } from 'node:url';
43
import { config } from 'dotenv';
4+
import { drizzle } from 'drizzle-orm/postgres-js';
5+
import { migrate } from 'drizzle-orm/postgres-js/migrator';
56
import postgres from 'postgres';
67

78
const __dirname = dirname(fileURLToPath(import.meta.url));
@@ -21,33 +22,31 @@ async function setupDatabase() {
2122
);
2223

2324
try {
24-
const sql = postgres(connectionString);
25+
const pgClient = postgres(connectionString, { max: 1 });
26+
const db = drizzle(pgClient);
2527

2628
// Read the migration SQL file
2729
// The migrations are in src/drizzle/migrations, and this CLI is in dist/
2830
// So we need to go up one level from dist/ to reach src/
29-
const migrationPath = join(
31+
const migrationsFolder = join(
3032
__dirname,
3133
'..',
3234
'src',
3335
'drizzle',
34-
'migrations',
35-
'0000_redundant_smasher.sql'
36+
'migrations'
3637
);
37-
const migrationSQL = await readFile(migrationPath, 'utf-8');
38+
console.log(`📂 Running migrations from: ${migrationsFolder}`);
3839

3940
// Execute the migration
40-
await sql.unsafe(migrationSQL);
41+
await migrate(db, {
42+
migrationsFolder,
43+
migrationsTable: 'workflow_migrations',
44+
migrationsSchema: 'workflow_drizzle',
45+
});
4146

4247
console.log('✅ Database schema created successfully!');
43-
console.log('\nCreated tables:');
44-
console.log(' - workflow_runs');
45-
console.log(' - workflow_events');
46-
console.log(' - workflow_steps');
47-
console.log(' - workflow_hooks');
48-
console.log(' - workflow_stream_chunks');
49-
50-
await sql.end();
48+
49+
await pgClient.end();
5150
process.exit(0);
5251
} catch (error) {
5352
console.error('❌ Failed to setup database:', error);

packages/world-postgres/src/drizzle/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Schema from './schema.js';
55
export { Schema };
66

77
export type Drizzle = ReturnType<typeof createClient>;
8+
89
export function createClient(postgres: Postgres.Sql) {
910
return drizzle(postgres, { schema: Schema });
1011
}

packages/world-postgres/src/drizzle/migrations/0000_redundant_smasher.sql renamed to packages/world-postgres/src/drizzle/migrations/0000_cultured_the_anarchist.sql

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
CREATE SCHEMA "workflow";
2+
--> statement-breakpoint
13
DO $$ BEGIN
24
CREATE TYPE "public"."step_status" AS ENUM('pending', 'running', 'completed', 'failed', 'cancelled');
35
EXCEPTION
@@ -10,7 +12,7 @@ EXCEPTION
1012
WHEN duplicate_object THEN null;
1113
END $$;
1214
--> statement-breakpoint
13-
CREATE TABLE IF NOT EXISTS "workflow_events" (
15+
CREATE TABLE IF NOT EXISTS "workflow"."workflow_events" (
1416
"id" varchar PRIMARY KEY NOT NULL,
1517
"type" varchar NOT NULL,
1618
"correlation_id" varchar,
@@ -19,7 +21,7 @@ CREATE TABLE IF NOT EXISTS "workflow_events" (
1921
"payload" jsonb
2022
);
2123
--> statement-breakpoint
22-
CREATE TABLE IF NOT EXISTS "workflow_hooks" (
24+
CREATE TABLE IF NOT EXISTS "workflow"."workflow_hooks" (
2325
"run_id" varchar NOT NULL,
2426
"hook_id" varchar PRIMARY KEY NOT NULL,
2527
"token" varchar NOT NULL,
@@ -30,7 +32,7 @@ CREATE TABLE IF NOT EXISTS "workflow_hooks" (
3032
"metadata" jsonb
3133
);
3234
--> statement-breakpoint
33-
CREATE TABLE IF NOT EXISTS "workflow_runs" (
35+
CREATE TABLE IF NOT EXISTS "workflow"."workflow_runs" (
3436
"id" varchar PRIMARY KEY NOT NULL,
3537
"output" jsonb,
3638
"deployment_id" varchar NOT NULL,
@@ -46,7 +48,7 @@ CREATE TABLE IF NOT EXISTS "workflow_runs" (
4648
"started_at" timestamp
4749
);
4850
--> statement-breakpoint
49-
CREATE TABLE IF NOT EXISTS "workflow_steps" (
51+
CREATE TABLE IF NOT EXISTS "workflow"."workflow_steps" (
5052
"run_id" varchar NOT NULL,
5153
"step_id" varchar PRIMARY KEY NOT NULL,
5254
"step_name" varchar NOT NULL,
@@ -63,7 +65,7 @@ CREATE TABLE IF NOT EXISTS "workflow_steps" (
6365
"retry_after" timestamp
6466
);
6567
--> statement-breakpoint
66-
CREATE TABLE IF NOT EXISTS "workflow_stream_chunks" (
68+
CREATE TABLE IF NOT EXISTS "workflow"."workflow_stream_chunks" (
6769
"id" varchar NOT NULL,
6870
"stream_id" varchar NOT NULL,
6971
"data" "bytea" NOT NULL,
@@ -72,11 +74,11 @@ CREATE TABLE IF NOT EXISTS "workflow_stream_chunks" (
7274
CONSTRAINT "workflow_stream_chunks_stream_id_id_pk" PRIMARY KEY("stream_id","id")
7375
);
7476
--> statement-breakpoint
75-
CREATE INDEX IF NOT EXISTS "workflow_events_run_id_index" ON "workflow_events" USING btree ("run_id");--> statement-breakpoint
76-
CREATE INDEX IF NOT EXISTS "workflow_events_correlation_id_index" ON "workflow_events" USING btree ("correlation_id");--> statement-breakpoint
77-
CREATE INDEX IF NOT EXISTS "workflow_hooks_run_id_index" ON "workflow_hooks" USING btree ("run_id");--> statement-breakpoint
78-
CREATE INDEX IF NOT EXISTS "workflow_hooks_token_index" ON "workflow_hooks" USING btree ("token");--> statement-breakpoint
79-
CREATE INDEX IF NOT EXISTS "workflow_runs_name_index" ON "workflow_runs" USING btree ("name");--> statement-breakpoint
80-
CREATE INDEX IF NOT EXISTS "workflow_runs_status_index" ON "workflow_runs" USING btree ("status");--> statement-breakpoint
81-
CREATE INDEX IF NOT EXISTS "workflow_steps_run_id_index" ON "workflow_steps" USING btree ("run_id");--> statement-breakpoint
82-
CREATE INDEX IF NOT EXISTS "workflow_steps_status_index" ON "workflow_steps" USING btree ("status");
77+
CREATE INDEX IF NOT EXISTS "workflow_events_run_id_index" ON "workflow"."workflow_events" USING btree ("run_id");--> statement-breakpoint
78+
CREATE INDEX IF NOT EXISTS "workflow_events_correlation_id_index" ON "workflow"."workflow_events" USING btree ("correlation_id");--> statement-breakpoint
79+
CREATE INDEX IF NOT EXISTS "workflow_hooks_run_id_index" ON "workflow"."workflow_hooks" USING btree ("run_id");--> statement-breakpoint
80+
CREATE INDEX IF NOT EXISTS "workflow_hooks_token_index" ON "workflow"."workflow_hooks" USING btree ("token");--> statement-breakpoint
81+
CREATE INDEX IF NOT EXISTS "workflow_runs_name_index" ON "workflow"."workflow_runs" USING btree ("name");--> statement-breakpoint
82+
CREATE INDEX IF NOT EXISTS "workflow_runs_status_index" ON "workflow"."workflow_runs" USING btree ("status");--> statement-breakpoint
83+
CREATE INDEX IF NOT EXISTS "workflow_steps_run_id_index" ON "workflow"."workflow_steps" USING btree ("run_id");--> statement-breakpoint
84+
CREATE INDEX IF NOT EXISTS "workflow_steps_status_index" ON "workflow"."workflow_steps" USING btree ("status");

0 commit comments

Comments
 (0)