prisma / migrate

Issues for Prisma Migrate are now tracked at prisma/prisma. This repo was used to track issues for Prisma Migrate Experimental and is now deprecated.
https://www.prisma.io/docs/concepts/components/prisma-migrate
Apache License 2.0
766 stars 22 forks source link

Improve flow edited migration + unapplied draft in CLI #650

Closed Jolg42 closed 3 years ago

Jolg42 commented 3 years ago

While investigating https://github.com/prisma/migrations-team/issues/86 I found

Current message

Prisma Schema loaded from prisma/schema.prisma
The following migrations were edited after they were applied:
- 20201117144659_test

Error: The following migrations scripts are different from those that were applied to the database:
["- `20201117144659_test, started at 2020-11-17 14:46:59.977 UTC and finished at 2020-11-17 14:46:59.979 UTC.`"]

To get it

For me it's a bit weird that the information is duplicated.

TLDR it happens when there is an editedMigrationNames and at the same time databaseIsBehind.

  migrate:cmd {
  migrate:cmd   editedMigrationNames: [ '20201117144659_test' ]
                history: {
  migrate:cmd     diagnostic: 'databaseIsBehind',
  migrate:cmd     unappliedMigrationNames: [ '20201117144723_draft' ]
  migrate:cmd   }
  migrate:cmd }

Full DEBUG

env DEBUG="*" ../.././../bin.ts --early-access-feature
Prisma Schema loaded from prisma/schema.prisma
  MigrateEngine:rpc starting migration engine with binary: /Users/j42/Dev/prisma-ter/src/node_modules/.pnpm/@prisma/engines@2.12.0-8.538958d94346122ee7084ce34b93c09e4d933b15/node_modules/@prisma/engines/migration-engine-darwin +0ms
  MigrateEngine:rpc SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"diagnoseMigrationHistory","params":{"migrationsDirectoryPath":"/Users/j42/Dev/prisma-ter/src/packages/migrate/src/__tests__/fixtures/test/prisma/migrations"}} +4ms
  MigrateEngine:stderr Nov 17 15:54:06.650  INFO migration_engine: Starting migration engine RPC server git_hash="538958d94346122ee7084ce34b93c09e4d933b15" +0ms
  MigrateEngine:stderr Nov 17 15:54:06.674  INFO quaint::single: Starting a sqlite connection. +22ms
The following migrations were edited after they were applied:
- 20201117144659_test

  migrate:cmd { drift: null } +0ms
  migrate:cmd {
  migrate:cmd   history: {
  migrate:cmd     diagnostic: 'databaseIsBehind',
  migrate:cmd     unappliedMigrationNames: [ '20201117144723_draft' ]
  migrate:cmd   }
  migrate:cmd } +2ms
  MigrateEngine:rpc SENDING RPC CALL {"id":2,"jsonrpc":"2.0","method":"applyMigrations","params":{"migrationsDirectoryPath":"/Users/j42/Dev/prisma-ter/src/packages/migrate/src/__tests__/fixtures/test/prisma/migrations"}} +73ms
  MigrateEngine:rpc {
  MigrateEngine:rpc   jsonrpc: '2.0',
  MigrateEngine:rpc   error: {
  MigrateEngine:rpc     code: 4466,
  MigrateEngine:rpc     message: 'An error happened. Check the data field for details.',
  MigrateEngine:rpc     data: {
  MigrateEngine:rpc       is_panic: false,
  MigrateEngine:rpc       message: 'The following migrations scripts are different from those that were applied to the database:\n' +
  MigrateEngine:rpc         '["- `20201117144659_test, started at 2020-11-17 14:46:59.977 UTC and finished at 2020-11-17 14:46:59.979 UTC.`"]',
  MigrateEngine:rpc       backtrace: null
  MigrateEngine:rpc     }
  MigrateEngine:rpc   },
  MigrateEngine:rpc   id: 2
  MigrateEngine:rpc } +1ms
Error: Error: The following migrations scripts are different from those that were applied to the database:
["- `20201117144659_test, started at 2020-11-17 14:46:59.977 UTC and finished at 2020-11-17 14:46:59.979 UTC.`"]

    at Object.<anonymous> (/Users/j42/Dev/prisma-ter/src/packages/migrate/src/MigrateEngine.ts:373:24)
    at MigrateEngine.handleResponse (/Users/j42/Dev/prisma-ter/src/packages/migrate/src/MigrateEngine.ts:214:34)
    at LineStream.<anonymous> (/Users/j42/Dev/prisma-ter/src/packages/migrate/src/MigrateEngine.ts:315:16)
    at LineStream.emit (events.js:315:20)
    at LineStream.EventEmitter.emit (domain.js:486:12)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:284:9)
    at LineStream.Readable.push (_stream_readable.js:223:10)
    at LineStream.Transform.push (_stream_transform.js:166:32)
    at LineStream._pushBuffer (/Users/j42/Dev/prisma-ter/src/packages/migrate/src/utils/byline.ts:118:17)

If I delete the draft migration we actually get the expected do you want to reset?

Prisma Schema loaded from prisma/schema.prisma
The following migrations were edited after they were applied:
- 20201117144659_test

? We need to reset the SQLite database "dev.db" at "file:dev.db". All data will be lost.
Do you want to continue? › (y/N)

My expected workflow is that it should go for the reset flow in both cases.

Jolg42 commented 3 years ago

New output when edited + unapplied draft from added test

      Prisma Schema loaded from prisma/schema.prisma
      The following migrations were edited after they were applied:
      - 20201231000000_test

      Prisma Migrate applied the following migration(s):

      migrations/
        └─ 20201231000000_test/
          └─ migration.sql
        └─ 20201231000000_draft/
          └─ migration.sql