question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migrate: `--create-only` flag applies non-applied migrations

See original GitHub issue

Bug description

The --create-only flag does not seem to work correctly. We (at work) were working on a feature branch, consisting of some migration files. When the first migration file was created with the --create-only flag it wasn’t directly applied, like expected. But when we created another migration with the same flag the previous migration got applied, because it wasn’t yet applied. This was not the expected behavior.

Context: https://github.com/prisma/prisma/issues/5553#issuecomment-775974318

How to reproduce

Steps to reproduce the behavior:

  1. Create a migration file with the --create-only flag applied
  2. Create another migration file with the --create-only flag applied
  3. See that the previous, unapplied migration got applied

Expected behavior

No migration file applies.

Possible solutions

See https://github.com/prisma/prisma/issues/5553#issuecomment-775909044

Environment & setup

  • OS: Mac OS
  • Database: Aurora MySQL
  • Node.js version: v12.18.4
  • Prisma version: 2.16.1
Environment variables loaded from .env
prisma               : 2.16.1
@prisma/client       : 2.16.1
Current platform     : darwin
Query Engine         : query-engine 8b74ad57aaf2cc6c155f382a18a8e3ba95aceb03 (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 8b74ad57aaf2cc6c155f382a18a8e3ba95aceb03 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 8b74ad57aaf2cc6c155f382a18a8e3ba95aceb03 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 8b74ad57aaf2cc6c155f382a18a8e3ba95aceb03 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Studio               : 0.346.0

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

7reactions
nickreynkecommented, Feb 9, 2021

Totally agree on that.

Our workflow is a bit different. In our case we are working with the live/production database since we are working serverless and do not have a locally running database. Means that we are in any case communicating with the live database to ensure that we are building upon the correct, live deployed state of the current database.

The argument that --create-only should apply migrations is misleading and wrong. I understand why it wants to apply the previous migration, since it needs a reference on what the next migration should build upon (I think). But when it says, that it only wants to create a migration but not apply it, why does it apply it in the first place?

As I suggested (https://github.com/prisma/prisma/issues/5553#issuecomment-775909044) there may be three, or more, possible solutions to this problem.

2reactions
janpiocommented, Jul 4, 2021

I totally agree, just hit this myself and it was 100% non intuitive this would happen. I absolutely expected it to just create a new migration for me to review:

C:\Users\Jan\Documents\throwaway\sqlServerCycles>npx prisma migrate dev --create-only
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db" - SQL Server

The following migration(s) have been applied:

migrations/
  └─ 20210704124441_init_noaction/
    └─ migration.sql
√ Enter a name for the new migration: ... init noaction correct model
Prisma Migrate created the following migration without applying it 20210704124540_init_noaction_correct_model

You can now edit it and apply it by running prisma migrate dev.

This is pretty dangerous. At the very least, we should prompt for confirmation if there are older non applied migrations that will be applied.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrations - Django documentation
Migrations ¶. Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your...
Read more >
django-admin migrate [app_label] [migration_name] - Fig.io
Make sure that the current database schema matches your initial migration before using this flag. Django will only check for an existing table...
Read more >
Django "You have unapplied migrations". Which ones?
A minor modification on Kevin's answer using grep, to only show unapplied migrations: Django 1.7: python manage.py migrate --list | grep -v '\[X\]'....
Read more >
Prisma Migrate | Database, Schema, SQL Migration Tool
Does not apply for MongoDB. Instead of migrate dev and related commands, use db push for MongoDB. Prisma Migrate is an imperative database...
Read more >
Overcome Common Migration Pitfalls - Create a Web ...
This technique only works if the migrations don't affect the same field on the same model. If they do, your best course of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found