Errorname / prisma-multi-tenant

🧭 Use Prisma as a multi-tenant provider for your application
MIT License
390 stars 45 forks source link

Prisma2 Multi-Tenant datamodel migration issue for postgres database. #21

Closed SameerChorge94 closed 4 years ago

SameerChorge94 commented 4 years ago

Description:

I've been trying the Prisma2 multi-tenant with postgres database, But after changing the datamodel and executing prisma-multi-tenant migrate up command it shows ✅ Successfuly migrated up all tenants but datamodel changes are not getting migrated on postgres database.

Similar response for prisma-multi-tenant migrate db up -- --auto-approve and prisma-multi-tenant migrate db up commands datamodel changes are not getting migrated to postgres database.

Datamodel:

datasource tenant {
      provider = "postgresql"
      url      = env("PMT_URL")
}

datasource management {
      provider = "postgresql"
      url      = "postgresql://*****:*****@localhost:5432/management"
      enabled  = false
}

// End of prisma-multi-tenant

generator client {
    provider = "prisma-client-js"
}

model User {
      UserId      String   @default(cuid()) @id
      FirstName   String
      LastName    String?
      Address     String?
      Email       String?
      CompanyName String?
      NoOfUsers   String?
      ZipCode     String?
      Country     String?
      TestField   String?
      TestField2  String?
}

model App {
    id                String @default(cuid()) @id
    appId             String @default("") @unique
    name              String
    TestField         String?
}

Steps to reproduce the issue:

  1. First setup single tenant i.e. normal prisma2 datamodel by using prisma2 init command
  2. Provide the mandatory details like datasource url (postgres database url) and execute prisma2 migrate save --name "migration-name" --experimental and prisma2 migrate up --experimental commands respectively ( It will deploy changes to the postgres database ) 3.Initialize the multi-tenant using prisma-multi-tenant init command.
  3. After multi-tenant setup add or remove fields from datamodel and execute prisma-multi-tenant migrate up command. ( It shows datamodel changes successfully migrated, but it does not reflected in database ).

Actual Result: After executing prisma-multi-tenant migrate up command it returns ✅ Successfuly migrated up all tenants, But datamodel changes does not get migrated on postgres database.

Expected Result: After executing prisma-multi-tenant migrate up command for specific tenant or all tenant, datamodel changes should reflect in actual database.

System: prisma2: "^2.0.0-preview022" prisma-multi-tenant:"^2.0.0-alpha21" @prisma/client : "^2.0.0-preview022"

Errorname commented 4 years ago

Thank you for the issue 🙏

After removing fields, do you re-save the datamodel ? (You can use prisma-multi-tenant env db -- prisma2 migrate save, I'll soon make the prisma-multi-tenant migrate save command 🙂)

SameerChorge94 commented 4 years ago

Yes,

After adding/Removing fields in datamodel first i tried prisma2 migrate up command it was not working, So i also tried prisma-multi-tenant migrate db up -- --auto-approve and prisma-multi-tenant env db -- prisma2 migrate save --experimental commands but it was returning error.

Errorname commented 4 years ago

What is the error returned by prisma-multi-tenant env db -- prisma2 migrate save --experimental ?

SameerChorge94 commented 4 years ago

its returning this below error

Running `prisma2 migrate save --experimental` on tenant "db"

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn prisma2 ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)

For detailed log for above error i executed prisma-multi-tenant env db --verbose -- prisma2 migrate save --experimental Command. It returns the below result

Running `prisma2 migrate save --experimental` on tenant "db"

  prisma-client { engineConfig:
  prisma-client    { cwd:
  prisma-client       'C:\\Users\\Sameer.Chorge\\AppData\\Roaming\\npm\\node_modules\\prisma-multi-tenant\\build\\cli\\prisma',    
  prisma-client      debug: true,
  prisma-client      datamodelPath:
  prisma-client       'D:\\demos\\Prisma2_Test\\Multi_Tenant_PostgresDB_Demo2\\node_modules\\@prisma\\prisma-multi-tenant\\management\\schema.prisma',
  prisma-client      prismaPath: undefined,
  prisma-client      datasources: [],
  prisma-client      generator:
  prisma-client       { name: 'client',
  prisma-client         provider: 'prisma-client-js',
  prisma-client         output:
  prisma-client          'D:\\demos\\Prisma2_Test\\Multi_Tenant_PostgresDB_Demo2\\node_modules\\@prisma\\prisma-multi-tenant\\management',
  prisma-client         binaryTargets: [],
  prisma-client         config: {} },
  prisma-client      showColors: true,
  prisma-client      logLevel: undefined,
  prisma-client      logQueries: undefined } } +0ms
  prisma-client Request: +119ms
  prisma-client query {
  prisma-client   findOneTenant(where: {
  prisma-client     name: "db"
  prisma-client   }) {
  prisma-client     id
  prisma-client     name
  prisma-client     provider
  prisma-client     url
  prisma-client   }
  prisma-client } +33ms
  prisma-client disconnection promise doesnt exist +4ms
  getos { platform: 'win32', libssl: undefined } +0ms
  engine { PRISMA_DML_PATH:
  engine    'D:\\demos\\Prisma2_Test\\Multi_Tenant_PostgresDB_Demo2\\node_modules\\@prisma\\prisma-multi-tenant\\management\\schema.prisma',
  engine   PORT: '55394',
  engine   RUST_BACKTRACE: '1',
  engine   RUST_LOG: 'info',
  engine   OVERWRITE_DATASOURCES: '[]',
  engine   CLICOLOR_FORCE: '1' } +0ms
  engine { cwd:
  engine    'C:\\Users\\Sameer.Chorge\\AppData\\Roaming\\npm\\node_modules\\prisma-multi-tenant\\build\\cli\\prisma' } +5ms        
  plusX Have to call plusX on D:\demos\Prisma2_Test\Multi_Tenant_PostgresDB_Demo2\node_modules\@prisma\prisma-multi-tenant\management\runtime\query-engine-windows.exe +0ms
  engine stderr Printing to stderr for debugging
Listening on 127.0.0.1:55394
 +109ms
  engine Ready after try number 0 +76ms
  agentkeepalive sock[0#localhost:55394:] create, timeout 60000ms +0ms
  agentkeepalive sock[0#localhost:55394:](requests: 1, finished: 1) free +149ms
  prisma-client Response: +386ms
  prisma-client { findOneTenant:
  prisma-client    { id: 'ck6syjqtk0000kwe4upnp07vv',
  prisma-client      name: 'db',
  prisma-client      provider: 'postgresql',
  prisma-client      url: 'postgresql://*****:*****@localhost:5432/app' } } +55ms
  prisma-client disconnectionPromise: stopping engine +99ms
  engine Stopping Prisma engine +354ms
  getos { platform: 'win32', libssl: undefined } +577ms
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn prisma2 ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Errorname commented 4 years ago

This errors says that it cannot find prisma2. Can your run the following commands and tell me if they throw errors ?

prisma2 --help
prisma-multi-tenant env db -- prisma2 --help
SameerChorge94 commented 4 years ago

For prisma2 --help command it returns help content

Prisma is a modern DB toolkit to query, migrate and model your database (https://prisma.io)

Usage

  $ prisma2 [command]

Commands

            init   Setup Prisma for your app
        generate   Generate artifacts (e.g. Prisma Client)

Flags

  --experimental   Show and run experimental Prisma commands

Examples

  Setup a new Prisma project
  $ prisma2 init

  Introspect an existing database
  $ prisma2 introspect

  Generate artifacts (e.g. Prisma Client)
  $ prisma2 generate

But for this command prisma-multi-tenant env db -- prisma2 --help it throws below error

Running `prisma2 --help` on tenant "db"

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn prisma2 ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Errorname commented 4 years ago

Okay, so you correctly have prisma2 installed, yet prisma-multi-tenant cannot seem to find it

Can you run the following commands and show the results ?

prisma-multi-tenant env db -- pwd
prisma-multi-tenant env db -- npm -g bin
prisma-multi-tenant env db -- ls $(npm -g bin) | grep prisma2
SameerChorge94 commented 4 years ago

All the three commands returns the below error :

prisma-multi-tenant env db -- pwd

Running `pwd` on tenant "db"

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn pwd ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)

Running: prisma-multi-tenant env db -- npm -g bin

`npm -g bin` on tenant "db"

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn npm ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)

prisma-multi-tenant env db -- ls $(npm -g bin)

Running `ls C:\Users\Sameer.Chorge\AppData\Roaming\npm` on tenant "db"

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn ls ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
Errorname commented 4 years ago

Ah, this probably comes from the fact that you are using Windows. I'll try to reproduce and fix this on my Windows computer

Errorname commented 4 years ago

Hi! The latest release Alpha22 should fix the issues you had on windows.

After you put in place your .env file to be compatible with prisma2@preview022 and prisma-multi-tenant@alpha22 (see here), you will be able to run prisma2 migrate save and then run prisma-multi-tenant migrate up.

Don't hesitate to re-open this issue if this still isn't fixed 🙂

SameerChorge94 commented 4 years ago

Hi! With latest release i'm able to use prisma2 migrate save command for migration, But this command prisma-multi-tenant migrate up returns

Migrating up all tenants...
No management datasource present in your schema.prisma file

As changes suggested here I updated the datamodel and moved datasource management details (i.e. provider and url ) from schema.prisma to .env files variables. So after executing prisma2 migrate save command it generates the migrations, but after executing the prisma-multi-tenant migrate up for the first time, It does not migrate datamodel changes directly to new database.

It returns the below result:

Migrating up all tenants...

No management datasource present in your schema.prisma file

Here if i execute the prisma2 migrate up --experimental command, Then all the datamodel changes gets deployed to the database.

So for migrating the datamodel changes i need to execute the prisma2 migrate up --experimental command seperately as this command prisma-multi-tenant migrate up does not migrate datamodel to database.

And for some reason when i execute the prisma-multi-tenant init it installs prisma-multi-tenant": "^2.0.0-alpha21 this version and not the updated one (i.e. alpha22).

Errorname commented 4 years ago

It looks like you still have the alpha21 installed globally. Run npm i -g prisma-multi-tenant@2.0.0-alpha22 to get the latest version

SameerChorge94 commented 4 years ago

It looks like you still have the alpha21 installed globally. Run npm i -g prisma-multi-tenant@2.0.0-alpha22 to get the latest version

After installing prisma-multi-tenant updated version globally it works.

Thank you,