Skip to content

Instantly share code, notes, and snippets.

@perryraskin
Last active August 2, 2024 20:18
Show Gist options
  • Save perryraskin/81042f47ba98a390b507ea12827d3e2f to your computer and use it in GitHub Desktop.
Save perryraskin/81042f47ba98a390b507ea12827d3e2f to your computer and use it in GitHub Desktop.
Prisma Migrations Automated Git Pipeline
const { execSync } = require("child_process")
const readline = require("readline")
// Get the current branch name
const branchName = execSync(`git rev-parse --abbrev-ref HEAD`).toString().trim()
const migrationName = `${branchName}`
// Create readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
// Prompt the user for a commit message
rl.question(
"💡 Enter a commit message (or press enter to use the default): ",
(userMessage) => {
const commitMessage = userMessage.trim() || migrationName
try {
// Run prisma format
execSync(`npx prisma format`, { stdio: "inherit" })
// Run prisma validate
execSync(`npx prisma validate`, { stdio: "inherit" })
// Run prisma generate
execSync(`npx prisma generate`, { stdio: "inherit" })
// Run prisma migrate
execSync(`npx prisma migrate dev --create-only --name ${migrationName}`, {
stdio: "inherit"
})
// Add all changes to git
execSync(`git add .`, { stdio: "inherit" })
// Commit with the user-provided or default migration name
execSync(`git commit -m "${commitMessage}"`, { stdio: "inherit" })
// Push to the current branch and set upstream if it doesn't exist
execSync(`git push -u origin ${branchName}`, {
stdio: "inherit"
})
} catch (error) {
console.error("An error occurred:", error)
process.exit(1)
} finally {
rl.close()
}
}
)
{
"name": "my-project",
"version": "0.0.1",
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"postinstall": "prisma generate",
"migrate": "node scripts/createPrismaMigration.js"
},
"dependencies": {
"@prisma/client": "^5.17.0",
"prisma": "^5.17.0"
},
"devDependencies": {
"@types/node": "^18.0.3",
"prisma-dbml-generator": "^0.9.1",
"typescript": "^4.7.4"
}
}

Prisma Migrations Workflow

This document outlines the workflow for managing database migrations using Prisma in a project.

Steps to Create and Push a Migration

  1. Make Changes to the Schema:

    • Modify the desired Prisma schema file to reflect the changes you want to make to the database schema.
    • Ensure changes are made to the proper schema file based on the category of the schema change.
  2. Run the Migration Command:

    • Execute the following command to create a migration:

      yarn migrate
    • This command will:

      • Validate the schema via npx prisma validate.
      • Create a migration file named after the current branch.
      • Create a commit with the migration file.
      • Push the commit to the branch.
    • You will be prompted to enter a commit message. This is optional, and if you do not provide one, the default will be the name of the branch.

  3. Review the Migration:

    • Note that running the migration command only creates the migration file. It does not apply the migration to the database.
    • This allows for the migration to be reviewed in a pull request.
  4. Pipeline Validation:

    • After pushing the migration, a pipeline will run to validate the schema using prisma validate.
    • The validation ensures that the schema changes are correct and do not introduce any issues.
  5. Pull Request Approval:

    • Once the PR is created, it needs to be reviewed and approved.
    • Only upon approval can the PR be merged into the UAT branch.
  6. Processing the Migration:

    • When the PR is merged into the UAT branch, a pipeline will run to apply the migration to the UAT database.
    • The same process occurs when merging into the staging and production branches, ensuring that the migration is applied to the respective databases.

Summary

  • Schema Changes: Modify schema file.
  • Create Migration: Run yarn migrate.
  • Review Migration: Migration file is created, not applied.
  • Pipeline Validation: Schema validated using prisma validate.
  • Approval: PR reviewed and approved.
  • Apply Migration: Merged into UAT, staging, and production to apply the migration.

By following this workflow, we ensure that database schema changes are reviewed, validated, and applied in a controlled and systematic manner.

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