Gustavo
08/19/2022, 3:58 AMschema.prisma
e.g.
- 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:
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:
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
- name: "Run migrations"
run: yarn migrate
package.json
{
"scripts": {
"migrate": "node migrate.js"
}
}
migrate.js
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.
😞Gustavo
08/19/2022, 9:46 AMmigrate.js
was doing this:
const { PrismaClient } = require('@prisma/client');
when it should've been doing this:
const { PrismaClient } = require('./src/packages/prisma/client');
and now it works 🙂