Hi guys, I've got a hopefully not unique situation...
# prisma-migrate
g
Hi guys, I've got a hopefully not unique situation here (multi-tenant architecture), I'd appreciate any inputs/suggestions 🙂 I have developed an API running on nodejs. that is split into different packages, where each package has its own
schema.prisma
e.g.
Copy code
- project_folder
-- migrate.js
-- src
---- index.ts
---- packages
------- package_1
---------- index.ts
---------- prisma
------------- client
------------- migrations
------------- schema.prisma
------- package_2
---------- index.ts
---------- prisma
------------- client
------------- migrations
------------- schema.prisma
package_1 > prisma > schema.prisma
looks like:
Copy code
generator client {
    provider        = "prisma-client-js"
    previewFeatures = ["interactiveTransactions"]
    output          = "./client"
}

datasource db {
    provider = "mysql"
    url      = env("ORGANISATION_CONTEXT_DATABASE_URL")
}
ORGANISATION_CONTEXT_DATABASE_URL
is always the same for a given env (dev, prod, etc...)
package_2 > prisma > schema.prisma
looks like:
Copy code
generator client {
    provider        = "prisma-client-js"
    previewFeatures = ["interactiveTransactions"]
    output          = "./client"
}

datasource db {
    provider = "mysql"
    url      = env("DATABASE_URL")
}
DATABASE_URL
changes depending the user making a request. Everything locally works just fine. The issue I have is at the moment of applying migrations when deploying through github actions. At the moment I'm testing the following. Whenever everything has been deployed run the command:
ci.yml
Copy code
- name: "Run migrations"
     run: yarn migrate
package.json
Copy code
{
  "scripts": {
    "migrate": "node migrate.js"
  }
}
migrate.js
Copy code
process.env.ORGANISATION_CONTEXT_DATABASE_URL = 'postgressql://....'

const organisationContextSchema='./src/packages/package_1/prisma/prisma.schema'
const cmd1 = spawnSync('npx', ['prisma', 'generate', `--schema=${organisationContextSchema}`]);

const cmd2 = spawnSync('npx', ['prisma', 'migrate', 'deploy', `--schema=${organisationContextSchema}`]);

// HERE IS WHERE PROBLEMS HAPPEN!!!! :(
const orgContDbClient = new PrismaClient({
    datasources: { db: { url: process.env.ORGANISATION_CONTEXT_DATABASE_URL } },
});

console.log('connected! Fetching orgs...');
const dbOrgs = await orgContDbClient.organisation.findMany();

console.log('dbOrgs: ', dbOrgs);
Error:
Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
😞
✅ 1
I was too tired, to keep looking at this, after some break, I realised the issue with my code is that the file
migrate.js
was doing this:
Copy code
const { PrismaClient } = require('@prisma/client');
when it should've been doing this:
Copy code
const { PrismaClient } = require('./src/packages/prisma/client');
and now it works 🙂