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.

After a migration that added a GIN, all subsequent migrations drop and recreate this index

See original GitHub issue

Bug description

We recently added a Generalized Inverted Index to one of our tables.

CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS btree_gin;

-- CreateIndex
CREATE INDEX "workspace_folder_workspaceId_status_name_idx" ON "workspace_folder" USING GIN ("workspaceId", "status" , "name" gin_trgm_ops);

All subsequent migrations now drop this index and recreate it, for example;

-- DropIndex
DROP INDEX "workspace_folder_workspaceId_status_name_idx";

-- CreateIndex
CREATE INDEX "workspace_invite_wid_index" ON "WorkspaceInvite"("workspaceId");

-- CreateIndex
CREATE INDEX "workspace_folder_workspaceId_status_name_idx" ON "workspace_folder" USING GIN ("workspaceId" , "status" , "name" gin_trgm_ops);

How to reproduce

  1. Create a migration that adds a GIN
  2. Create a subsequent migration via migrate dev

Expected behavior

It should not drop and recreate the GIN on subsequent migrations

Prisma information

//...
model WorkspaceInvite {
  userId      String                @db.Uuid
  workspaceId String                @db.Uuid
  role        WorkspaceRole         @default(editor)
  status      WorkspaceInviteStatus
  createdAt   DateTime              @default(now())
  updatedAt   DateTime              @default(now()) @updatedAt
  invitedBy   String?               @db.Uuid
  workspace   Workspace             @relation(fields: [workspaceId], references: [id])

  @@id([userId, workspaceId])
  @@unique([userId, workspaceId])
  @@index([workspaceId])
}

model WorkspaceFolder {
  id                String                  @id @default(uuid()) @db.Uuid
  name              String
  workspaceId       String                  @db.Uuid
  parentId          String?                 @db.Uuid
  createdAt         DateTime                @default(now())
  status            WorkspaceFolderStatus   @default(active)
  updatedAt         DateTime                @default(now()) @updatedAt
  category          WorkspaceFolderCategory @default(default)
  parent            WorkspaceFolder?        @relation("WorkspaceFolderToWorkspaceFolder", fields: [parentId], references: [id], onDelete: Cascade)
  workspace         Workspace               @relation(fields: [workspaceId], references: [id])
  WorkspaceProjects WorkspaceProject[]
  WorkspaceFolder   WorkspaceFolder[]       @relation("WorkspaceFolderToWorkspaceFolder")
  WorkspaceSettings WorkspaceSettings[]

  @@index([workspaceId], map: "workspace_folder_wid_index")
  @@index([parentId], map: "parent_id_folder_index")
  @@index([workspaceId, parentId], map: "workspace_id_folder_index")
  @@index([workspaceId, parentId, status], map: "workspace_id_folder_index_status")
  @@index([workspaceId, parentId, status, category])
  @@index([workspaceId(ops: raw("")), status(ops: raw("")), name(ops: raw("gin_trgm_ops"))], type: Gin)
  @@map("workspace_folder")
}
// ...

Environment & setup

  • OS: macOS
  • Database: PostgreSQL
  • Node.js version: v18.12.0

Prisma Version

4.3.1

Issue Analytics

  • State:open
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewCathcartcommented, Nov 18, 2022

Yes sorry I should have been a bit clearer - the workspace_invite_wid_index is the index I added, but I was expecting that to be the only line in the migration.

To be 100% clear, this is the original SQL in the first migration where the GIN was added;

CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE EXTENSION IF NOT EXISTS btree_gin;

-- CreateIndex
CREATE INDEX "workspace_folder_workspaceId_status_name_idx" ON "workspace_folder" USING GIN ("workspaceId", "status" , "name" gin_trgm_ops);

And here is what gets generated in every migration now, for example running migrate dev on a fresh database;

-- DropIndex
DROP INDEX "workspace_folder_workspaceId_status_name_idx";

-- CreateIndex
CREATE INDEX "workspace_folder_workspaceId_status_name_idx" ON "workspace_folder" USING GIN ("workspaceId" , "status" , "name" gin_trgm_ops);

Running migrate dev 3x results in 3 files with the above DropIndex + CreateIndex

Screenshot 2022-11-18 at 10 50 22
1reaction
AndrewCathcartcommented, Nov 15, 2022

Hi @SevInf apologies, copy pasted the wrong value. Currently using 4.3.1

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does add_index using 'gin' create a 'btree' index instead?
The reason, as explained in the Rails Migrations guide, is that a GIN index is specific to Postgres. When you are using database-specific ......
Read more >
Migration | GORM - GORM
Automatically migrate your schema, to keep your schema up to date. NOTE: AutoMigrate will create tables, missing foreign keys, constraints, columns and indexes....
Read more >
Adding Indexes with EF Migrations | Passion for Coding
I'll add my index creation to the Up() method and a drop statement to the Down() method to enable downgrading the database schema....
Read more >
Digging Deeper Into Django Migrations - Real Python
In this step-by-step Python tutorial, you'll not only take a closer look at the new Django migrations system that is integrated into Django...
Read more >
How to Migrate Sourcegraph from one cluster to another.
Steps to backup Sourcegraph Databases · Verify that the deployment is running. kubectl get pods -A · Stop all connections to the database...
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