-
Notifications
You must be signed in to change notification settings - Fork 72
feat(@workflow/world-postgres): implemented a script for setting up the required tables #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
VaguelySerious
merged 4 commits into
vercel:main
from
karthikscale3:karthik/improve-postgres-world-dx
Oct 30, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
909252d
DX Improvement: Export schema and a cli utility to initialize the dat…
karthikscale3 fa1cd74
Add the changeset
karthikscale3 956d02a
Update packages/world-postgres/src/cli.ts
karthikscale3 b3d05ba
chore: regenerate pnpm-lock.yaml after merge
karthikscale3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@workflow/world-postgres": minor | ||
| --- | ||
|
|
||
| Exported the database schema and added a script for initializing the database with all the required tables for the setup. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| // Simple wrapper to run the CLI from the bin directory | ||
| import('../dist/cli.js') | ||
| .then((module) => { | ||
| // Call the setupDatabase function | ||
| return module.setupDatabase(); | ||
| }) | ||
| .catch((err) => { | ||
| console.error('Failed to load CLI:', err); | ||
| process.exit(1); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #!/usr/bin/env node | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it's not needed given we except this to either be imported by the bin (and run), or imported in a module |
||
|
|
||
| import { config } from 'dotenv'; | ||
| import { readFile } from 'node:fs/promises'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import postgres from 'postgres'; | ||
|
|
||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| async function setupDatabase() { | ||
| // Load .env file if it exists | ||
| config(); | ||
|
|
||
| const connectionString = | ||
| process.env.WORKFLOW_POSTGRES_URL || | ||
| process.env.DATABASE_URL || | ||
| 'postgres://world:world@localhost:5432/world'; | ||
|
|
||
| console.log('🔧 Setting up database schema...'); | ||
| console.log( | ||
| `📍 Connection: ${connectionString.replace(/^(\w+:\/\/)([^@]+)@/, '$1[redacted]@')}` | ||
| ); | ||
|
|
||
| try { | ||
| const sql = postgres(connectionString); | ||
|
|
||
| // Read the migration SQL file | ||
| // The migrations are in src/drizzle/migrations, and this CLI is in dist/ | ||
| // So we need to go up one level from dist/ to reach src/ | ||
| const migrationPath = join( | ||
| __dirname, | ||
| '..', | ||
| 'src', | ||
| 'drizzle', | ||
| 'migrations', | ||
| '0000_redundant_smasher.sql' | ||
| ); | ||
| const migrationSQL = await readFile(migrationPath, 'utf-8'); | ||
|
|
||
| // Execute the migration | ||
| await sql.unsafe(migrationSQL); | ||
|
|
||
| console.log('✅ Database schema created successfully!'); | ||
| console.log('\nCreated tables:'); | ||
| console.log(' - workflow_runs'); | ||
| console.log(' - workflow_events'); | ||
| console.log(' - workflow_steps'); | ||
| console.log(' - workflow_hooks'); | ||
| console.log(' - workflow_stream_chunks'); | ||
|
|
||
| await sql.end(); | ||
| process.exit(0); | ||
| } catch (error) { | ||
| console.error('❌ Failed to setup database:', error); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| // Check if running as main module | ||
| if (import.meta.url === `file://${process.argv[1]}`) { | ||
| setupDatabase(); | ||
| } | ||
|
|
||
| export { setupDatabase }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question for @Schniz - curious if we could just keep this as the main bin? that way
npx @workflow/world-postgresi'm indifferent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a strong opinion, but I think since it's a one-time setup script separate from regular intended execution, it makes sense for it to be named like it's in the PR