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 renames my indexes & throws Drift detected error even when nothing changes in DB/Schema

See original GitHub issue

Bug description

I am trying to move an existing Postgres DB with ~190 tables to Prisma. My prisma schema was created successfully by introspecting and I also created an initial migration by taking sql dump of my DB schema. But the problem is that the 2nd time I run npx prisma migrate dev --preview-feature, it creates a new migration which renames my existing unique indexes. Also on each run, I keep getting Drift detected error & need to reset my DB everytime. So the migration sequence is something like this.

Migration 1: Full SQL dump of my existing schema created manually Migration 2: Auto created by Prisma migrate after 1st run of migrate dev as below (only few sample lines)

-- AlterIndex
ALTER INDEX "django_celery_beat_periodictask_name_key" RENAME TO "django_celery_beat_periodictask.name_unique";

-- AlterIndex
ALTER INDEX "django_celery_results_taskresult_task_id_key" RENAME TO "django_celery_results_taskresult.task_id_unique";

-- AlterIndex
ALTER INDEX "django_rest_passwordreset_resetpasswordtoken_key_f1b65873_uniq" RENAME TO "django_rest_passwordreset_resetpasswordtoken.key_unique";

-- AlterIndex
ALTER INDEX "django_site_domain_a2e37b91_uniq" RENAME TO "django_site.domain_unique";

-- AlterIndex
ALTER INDEX "emailtemplate_emaillayout_name_key" RENAME TO "emailtemplate_emaillayout.name_unique";

-- AlterIndex
ALTER INDEX "emailtemplate_emailtemplate_name_key" RENAME TO "emailtemplate_emailtemplate.name_unique";

On each migrate run - Keep getting Drift detected error. Logs below.

Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "masterdb", schema "public" at "nest-postgres:5432"

warn "nativeTypes" is a preview feature. There may be bugs and it's not recommended to use it in production environments.
  MigrateEngine:rpc starting migration engine with binary: /usr/src/app/node_modules/@prisma/engines/migration-engine-linux-musl +0ms
  MigrateEngine:rpc SENDING RPC CALL {"id":1,"jsonrpc":"2.0","method":"devDiagnostic","params":{"migrationsDirectoryPath":"/usr/src/app/prisma/migrations"}} +8ms
  MigrateEngine:stderr Jan 26 06:52:21.738  INFO migration_engine: Starting migration engine RPC server git_hash="e51dc3b5a9ee790a07104bec1c9477d51740fe54" +0ms
  MigrateEngine:stderr Jan 26 06:52:21.792  INFO quaint::single: Starting a postgresql connection. +52ms
  MigrateEngine:stderr Jan 26 06:52:24.259  INFO DevDiagnostic:calculate_drift:sql_schema_from_migration_history: quaint::single: Starting a postgresql connection. +2s
  MigrateEngine:stderr Jan 26 06:52:48.719  INFO DevDiagnostic:validate_migrations:sql_schema_from_migration_history: quaint::single: Starting a postgresql connection. +24s
  migrate:dev {
  migrate:dev   devDiagnostic: '{\n' +
  migrate:dev     '  "action": {\n' +
  migrate:dev     '    "tag": "reset",\n' +
  migrate:dev     '    "reason": "Drift detected: Your database schema is not in sync with your migration history."\n' +
  migrate:dev     '  }\n' +
  migrate:dev     '}'
  migrate:dev } +0ms
? Drift detected: Your database schema is not in sync with your migration history.

We need to reset the PostgreSQL database "masterdb" at "nest-postgres:5432"

How to reproduce

Not sure about this. I can share my complete schema file privately if needed.

Expected behavior

Once the schema is in sync with DB after 1st migration, subsequent runs of migrate should not create any migration and no drift detected error should occur if anything has not changed.

Prisma information

Logs already shared above. Sample schema below.

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["nativeTypes"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model django_celery_beat_periodictask {
  id                                  Int                                  @id @default(autoincrement()) @db.Integer
  name                                String                               @unique @db.VarChar(200)
  task                                String                               @db.VarChar(200)
  args                                String                               @db.Text
  kwargs                              String                               @db.Text
  queue                               String?                              @db.VarChar(200)
  exchange                            String?                              @db.VarChar(200)
  routing_key                         String?                              @db.VarChar(200)
  expires                             DateTime?                            @db.Timestamptz(6)
  enabled                             Boolean                              @db.Boolean
  last_run_at                         DateTime?                            @db.Timestamptz(6)
  total_run_count                     Int                                  @db.Integer
  date_changed                        DateTime                             @db.Timestamptz(6)
  description                         String                               @db.Text
  crontab_id                          Int?                                 @db.Integer
  interval_id                         Int?                                 @db.Integer
  solar_id                            Int?                                 @db.Integer
  one_off                             Boolean                              @db.Boolean
  start_time                          DateTime?                            @db.Timestamptz(6)
  priority                            Int?                                 @db.Integer
  headers                             String                               @db.Text
  clocked_id                          Int?                                 @db.Integer
  django_celery_beat_clockedschedule  django_celery_beat_clockedschedule?  @relation(fields: [clocked_id], references: [id])
  django_celery_beat_crontabschedule  django_celery_beat_crontabschedule?  @relation(fields: [crontab_id], references: [id])
  django_celery_beat_intervalschedule django_celery_beat_intervalschedule? @relation(fields: [interval_id], references: [id])
  django_celery_beat_solarschedule    django_celery_beat_solarschedule?    @relation(fields: [solar_id], references: [id])

  @@index([clocked_id], name: "django_celery_beat_periodictask_clocked_id_47a69f82")
  @@index([crontab_id], name: "django_celery_beat_periodictask_crontab_id_d3cba168")
  @@index([interval_id], name: "django_celery_beat_periodictask_interval_id_a8ca27da")
  @@index([solar_id], name: "django_celery_beat_periodictask_solar_id_a87ce72c")
}

model django_celery_results_taskresult {
  id               Int      @id @default(autoincrement()) @db.Integer
  task_id          String   @unique @db.VarChar(255)
  status           String   @db.VarChar(50)
  content_type     String   @db.VarChar(128)
  content_encoding String   @db.VarChar(64)
  result           String?  @db.Text
  date_done        DateTime @db.Timestamptz(6)
  traceback        String?  @db.Text
  hidden           Boolean  @db.Boolean
  meta             String?  @db.Text
  task_args        String?  @db.Text
  task_kwargs      String?  @db.Text
  task_name        String?  @db.VarChar(255)

  @@index([date_done], name: "django_celery_results_taskresult_date_done_49edada6")
  @@index([hidden], name: "django_celery_results_taskresult_hidden_cd77412f")
  @@index([status], name: "django_celery_results_taskresult_status_cbbed23a")
  @@index([task_name], name: "django_celery_results_taskresult_task_name_90987df3")
}

Environment & setup

  • OS: MacOS Catalina 10.15.1, Running inside docker node:14.15.4-alpine
  • Database: PostgreSQL
  • Node.js version: v14.15.4
  • Prisma version:
@prisma/cli          : 2.15.0
@prisma/client       : 2.15.0
Current platform     : linux-musl
Query Engine         : query-engine e51dc3b5a9ee790a07104bec1c9477d51740fe54 (at node_modules/@prisma/engines/query-engine-linux-musl)
Migration Engine     : migration-engine-cli e51dc3b5a9ee790a07104bec1c9477d51740fe54 (at node_modules/@prisma/engines/migration-engine-linux-musl)
Introspection Engine : introspection-core e51dc3b5a9ee790a07104bec1c9477d51740fe54 (at node_modules/@prisma/engines/introspection-engine-linux-musl)
Format Binary        : prisma-fmt e51dc3b5a9ee790a07104bec1c9477d51740fe54 (at node_modules/@prisma/engines/prisma-fmt-linux-musl)
Studio               : 0.340.0
Preview Features     : nativeTypes

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
tomhoulecommented, Feb 2, 2021

@pandaniell this is a separate issue. It should already be fixed in prisma@dev, and in today’s (to-be-published) release.

1reaction
pimeyscommented, May 12, 2021

I think the constraint work @do4gr is doing should get this one more streamlined too…

Read more comments on GitHub >

github_iconTop Results From Across the Web

DbSchema Schema Synchronization
Convert the data types to the new database. Re-Write the triggers, functions, and procedures. This has to be done manually.
Read more >
Migrations - Drift - Simon Binder
Drift provides a migration API that can be used to gradually apply schema changes after bumping the schemaVersion getter inside the Database class....
Read more >
Prisma 2.17.0 Release - GitClear
Database resets in the context of Prisma Migrate now gracefully fall back to dropping constraints, indexes and tables, if there are insufficient ...
Read more >
Configuration properties | Bitbucket Data Center and Server 8.6
This page describes the configuration properties that can be used to control behavior in Bitbucket Data Center and Server.
Read more >
Command Line Interface | RedwoodJS Docs
Appends a DataMigration model to schema.prisma for tracking which data migrations have already run. Creates a DB migration using yarn redwood prisma migrate...
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