DEV Community

Roman Tsegelskyi
Roman Tsegelskyi

Posted on

1

Using GenAI to Tackle Complex Prisma Model Migrations

TL;DR: Need to write complex Prisma migrations (renaming, splitting, merging models)? Instead of writing SQL by hand:

  1. Save your old schema
  2. Make changes in schema.prisma
  3. Run prisma migrate dev --create-only
  4. Let GenAI handle the SQL data transformations

Been using this approach while building Stems - saved hours on migrations while preserving data and relationships. Works great for anything from simple renames to complex model splits.

While building Stems, I needed to split our monolithic Track model into three separate models: OfficialTrack, Stem, and Remix. This better represented our domain - official tracks have stems, which users can download to create remixes. Here's how I handled this data model evolution using GenAI to save time on SQL migrations.

The change involved rougly the following:

// Before: Everything in one model
model Track {
  id          String   @id
  title       String
  artist      String
  previewPath String?
  stemPaths   String[] // All stems stored here
  isRemix     Boolean
  parentTrack Track?   @relation("RemixOf")
  remixes     Track[]  @relation("RemixOf")
}
// After: Split into domain-specific models
model OfficialTrack {
  id     String @id
  title  String
  artist String
  stems  Stem[]
  remixes Remix[]
}
model Stem {
  id        String       @id
  title     String      // e.g., "vocals", "drums"
  audioUrl  String
  track     OfficialTrack @relation(fields: [trackId], references: [id])
  trackId   String
}
model Remix {
  id        String        @id
  title     String
  artist    String
  audioUrl  String
  original  OfficialTrack @relation(fields: [trackId], references: [id])
  trackId   String
}
Enter fullscreen mode Exit fullscreen mode

Instead of manually writing the data migration, here's what worked:

  1. Save the current schema: cp schema.prisma schema-old.prisma
  2. Update schema.prisma with the new models
  3. Generate migration scaffold: npx prisma migrate dev --create-only
  4. Prompt for AI assistance:

I'm splitting the Track model into OfficialTrack, Stem, and Remix models.
schema-old.prisma has the original Track model where:

  • Non-remix tracks (isRemix=false) should become OfficialTrack records
  • stemPaths array should be split into individual Stem records
  • Remix tracks (isRemix=true) should become Remix records
  • All relationships need to be preserved Please modify the SQL migration to handle this data transformation.
  1. Review the generated SQL carefully - this kind of split is complex enough that you might need to tweak the migration or ask for adjustments

Key learnings:

  • This approach works best when you can clearly describe the transformation rules
  • For complex splits like this, I always test the migration on a copy of production data first
  • Breaking down the migration into smaller steps (create new tables → migrate data → update relationships → drop old table) made it easier to verify correctness
  • Having the old schema file is crucial for AI to understand the full context This method significantly reduced the time I would've spent writing and debugging SQL migrations, though I still needed to review and test thoroughly given the complexity of the model split.

Billboard image

Use Playwright to test. Use Playwright to monitor.

Join Vercel, CrowdStrike, and thousands of other teams that run end-to-end monitors on Checkly's programmable monitoring platform.

Get started now!

Top comments (1)

Collapse
 
werzydev profile image
Werzy Dev

This is quite interesting approach, thanks for sharing

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay