P
Prisma•2d ago
hari_narayan

Schema migration automation

Public schema is attached to my node.js app...i want to migrate schema of for y name schema to database.... I have prisma/public/public.schema folder structure and prisma/shema.prisma for schema db structure...so on api request i want to do migration how??? I have used excutesafe but its failed due to multiple commands.... Sql inside of it....
1 Reply
Prisma AI Help
Prisma AI Help•2d ago
You selected the bot's instantaneous insights. Let me compile your answer right away! Based on the information provided in the knowledge sources, I can offer some guidance on automating schema migrations with Prisma in a Nest.js application. However, the specific setup you described with separate schemas for public and other named schemas is not directly addressed in the provided documents. To automate schema migrations on API request, you can create a custom service in your Nest.js application that runs Prisma Migrate commands. Here's a general approach you could consider: 1. Create a PrismaService that extends PrismaClient and implements OnModuleInit:
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
constructor(
@InjectPinoLogger(PrismaService.name)
private readonly logger: Logger,
private readonly configService: ConfigService,
) {}

async onModuleInit(): Promise<void> {
// Migration logic here
}
}
@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
constructor(
@InjectPinoLogger(PrismaService.name)
private readonly logger: Logger,
private readonly configService: ConfigService,
) {}

async onModuleInit(): Promise<void> {
// Migration logic here
}
}
2. In the onModuleInit method, you can add logic to run migrations based on your application's environment and configuration:
if (
(this.configService.nodeEnv === 'production' && this.configService.type === 'scheduler') ||
(this.configService.nodeEnv === 'development' && this.configService.type === 'api')
) {
let command = ['production', 'qa'].includes(this.configService.env)
? 'npx prisma migrate deploy'
: 'npx prisma db push --accept-data-loss';

if (this.configService.nodeEnv === 'development') {
command += ' --schema prisma/schema.prisma';
}

const { stdout, stderr } = await exec(command);
this.logger.info(stdout);
if (stderr) {
this.logger.error(stderr);
}
}
if (
(this.configService.nodeEnv === 'production' && this.configService.type === 'scheduler') ||
(this.configService.nodeEnv === 'development' && this.configService.type === 'api')
) {
let command = ['production', 'qa'].includes(this.configService.env)
? 'npx prisma migrate deploy'
: 'npx prisma db push --accept-data-loss';

if (this.configService.nodeEnv === 'development') {
command += ' --schema prisma/schema.prisma';
}

const { stdout, stderr } = await exec(command);
this.logger.info(stdout);
if (stderr) {
this.logger.error(stderr);
}
}
This approach is adapted from an example provided in a GitHub issue comment. However, it's important to note that running migrations on every API request is not a recommended practice, as it can lead to performance issues and potential conflicts. Typically, migrations are run as part of the deployment pro cess or during application startup. For your specific case with multiple schemas, you might need to modify this approach to handle different schema files and potentially use Prisma's programmatic API for more fine-grained control over migrations.
Given that you're experimenting with custom schema setups and automation in Prisma, it might be helpful to reach out to the Prisma Team for more tailored advice on best practices for your specific use case.
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.

Did you find this page helpful?