Skip to content

Instantly share code, notes, and snippets.

@dantman
Created June 4, 2025 14:05
Show Gist options
  • Save dantman/db670eed34b45f84a2d79948b0b4a3c9 to your computer and use it in GitHub Desktop.
Save dantman/db670eed34b45f84a2d79948b0b4a3c9 to your computer and use it in GitHub Desktop.
Prompt to automate Drizzle migration process

An AI agent should attempt to run cli commands on behalf of the user when they are needed.

Before running any command as a terminal command, the AI agent should check if there is a relevant MCP (Machine Command Protocol) command available in the project. If there is, it should use that instead of running a terminal command directly.

Check type safety using the npm run typecheck task instead of tsc directly.

Drizzle

The database schema can be found in schema.ts. For timestamp and date column types use { mode: "date" } to give the column the Date type.

When the database schema is changed run npm run generate to generate a migration.

Review all generated migrations and if ok run npm run migrate to apply them to the database.

When the schema of the connected PostgreSQL database changes npm run seed:sync must be run to update the types used by the seed script. A TypeScript check should be done to verify the seed script is still valid.

title description
Database Migration Process
A guide for creating, running, and verifying database migrations in the application.

Your goal is to create, run, and verify database migrations for any schema changes in the application.

Instructions

After making changes to the database schema in src/db/schema.ts, follow these steps to create and apply a migration:

  1. Generate a migration based on the schema changes:

    npm run generate
  2. Review the generated migration file to ensure it correctly reflects your schema changes:

    • Find the most recently created migration file in the drizzle/ directory
    • Examine the SQL statements to confirm they match your expected changes
  3. Apply the migration to update the database:

    npm run migrate
  4. Update seed script types to reflect the schema changes:

    npm run seed:sync
  5. Verify TypeScript compatibility by running a type check:

    npm run typecheck

Helpful Reminders

  • Always review migration files before applying them to avoid unwanted changes
  • After applying migrations, make sure to update any affected parts of the codebase
  • For timestamp and date column types, use { mode: "date" } to give the column the Date type
  • When changing enum values, make sure to follow the proper migration process (prepare/execute)

Example

For example, if you add a new column to a table:

  1. Add the column to the table definition in src/db/schema.ts
  2. Run npm run generate to create a migration file
  3. Review the generated migration file in drizzle/
  4. Run npm run migrate to apply the migration
  5. Run npm run seed:sync to update seed script types
  6. Run npm run typecheck to ensure type compatibility

Notes

  • The database schema can be found in src/db/schema.ts
  • Migrations are stored in the drizzle/ directory
  • When the schema changes, remember to update any affected parts of your application code
@dantman
Copy link
Author

dantman commented Jun 4, 2025

This has been incredibly useful for automating the steps of turning Drizzle schema changes into a migration.

copilot-instructions.md is a part of your repository .github/copilot-instructions.md.
Our primary goal is to get Copilot to use it's ability to execute npm tasks directly instead of it's ability to run an arbitrary terminal command.
This way you are able to give it workspace level permission to always run the safe generate, seed:sync, and typecheck commands without prompting.

migration.prompt.md lives in .github/prompts/migration.prompt.md inside your repo and creates a /migration command you can use in Copilot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment