Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate dev does not detect drift between database schema and migration history for multiSchema #16634

Closed
janpio opened this issue Dec 5, 2022 · 2 comments
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: drift topic: multiSchema multiple schemas
Milestone

Comments

@janpio
Copy link
Member

janpio commented Dec 5, 2022

  1. Create a PostgreSQL database
  2. Run the following SQL:
    CREATE SCHEMA schema1;
    
    CREATE TABLE schema1.foo (
    	id uuid NOT NULL,
    	CONSTRAINT foo_pkey PRIMARY KEY (id)
    );
    
    CREATE SCHEMA schema2;
    
    CREATE TABLE schema2.bar (
    	id uuid NOT NULL,
    	CONSTRAINT bar_pkey PRIMARY KEY (id)
    );
    ALTER TABLE schema2.bar ADD CONSTRAINT bar_id_fkey FOREIGN KEY (id) REFERENCES schema1.foo(id);
  3. Set up a Prisma project
  4. Modify the schema file minimally:
    generator client {
      provider        = "prisma-client-js"
      previewFeatures = ["multiSchema"]
    }
    
    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
      schemas  = ["schema1", "schema2"]
    }
  5. Run npx prisma db pull to fill the schema
  6. Result of that:
    generator client {
      provider        = "prisma-client-js"
      previewFeatures = ["multiSchema"]
    }
    
    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
      schemas  = ["schema1", "schema2"]
    }
    
    model foo {
      id  String @id @db.Uuid
      bar bar?
    
      @@schema("schema1")
    }
    
    model bar {
      id  String @id @db.Uuid
      foo foo    @relation(fields: [id], references: [id], onDelete: NoAction, onUpdate: NoAction)
    
      @@schema("schema2")
    }
    
  7. Now run npx prisma migrate dev
  8. Observe:
    PS C:\Users\Jan\Documents\throwaway\crossSchemaIntrospection\docker> npx prisma migrate dev
    Environment variables loaded from .env
    Prisma schema loaded from prisma\schema.prisma
    Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:5438"
    
    √ Enter a name for the new migration: ... init
    Applying migration `20221205172823_init`
    Error: P3018
    
    A migration failed to apply. New migrations cannot be applied before the error is recovered from. Read more about how to resolve migration issues in a production database: https://pris.ly/d/migrate-resolve
    
    Migration name: 20221205172823_init
    
    Database error code: 42P07
    
    Database error:
    ERROR: relation "foo" already exists
    
    DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState(E42P07), message: "relation \"foo\" already exists", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("heap.c"), line: Some(1146), routine: Some("heap_create_with_catalog") }
    

I would have 8) to given me a message instead that it detected drift between my history and the the database schema, and that I need to reset before I can continue. Instead it created a valid migration:

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "schema1";

-- CreateSchema
CREATE SCHEMA IF NOT EXISTS "schema2";

-- CreateTable
CREATE TABLE "schema1"."foo" (
    "id" UUID NOT NULL,

    CONSTRAINT "foo_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "schema2"."bar" (
    "id" UUID NOT NULL,

    CONSTRAINT "bar_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "schema2"."bar" ADD CONSTRAINT "bar_id_fkey" FOREIGN KEY ("id") REFERENCES "schema1"."foo"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;

And then failed when executing that, as both schemas and tables of course already exist.

@janpio janpio added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: drift topic: multiSchema multiple schemas labels Dec 5, 2022
@floelhoeffel floelhoeffel added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Dec 8, 2022
@floelhoeffel
Copy link

I can reproduce this with @dev

tomhoule added a commit to prisma/prisma-engines that referenced this issue Dec 8, 2022
The namespaces are from the schema the engine was started with

This should address prisma/prisma#16634 and prisma/prisma#16585
tomhoule added a commit to prisma/prisma-engines that referenced this issue Dec 8, 2022
The namespaces are from the schema the engine was started with

This should address prisma/prisma#16634 and prisma/prisma#16585
tomhoule added a commit to prisma/prisma-engines that referenced this issue Dec 9, 2022
#3481)

The namespaces are from the schema the engine was started with

This should address prisma/prisma#16634 and prisma/prisma#16585
@janpio janpio added this to the 4.8.0 milestone Dec 11, 2022
@pimeys
Copy link
Contributor

pimeys commented Dec 19, 2022

Should be fixed in prisma/prisma-engines#3481

@pimeys pimeys closed this as completed Dec 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: drift topic: multiSchema multiple schemas
Projects
None yet
Development

No branches or pull requests

4 participants