question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Prisma Migrate: programmatic access to CLI commands (deploy, reset...)

See original GitHub issue

Problem

It would be nice to have a way to run the migrate programmatically. I am running into an issue where my api is using lambdas (serverless) and my database is only accessible from within an aws vpc. Because of this I cannot “execute commands” on deploy in an easy way. My current solution is to ssh into an ec2 that is within the vpc, execute the prisma commands, and then spin the ec2 back down. While this is working, it is far from ideal. If I was instead able to have a separate lambda that had all the migration data bundled with it I could execute the migration from a lambda (that is within the vpc).

While this may seem like a specialized use case, databases that are not publicly accessible are common place, and solutions for migration that don’t include running npm commands from the cli I think become critical.

Solution

import {migrate} from '@prisma/cli';
migrate('up');

Alternatives

Stated above, no great alternatives for common usecases.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:268
  • Comments:67 (11 by maintainers)

github_iconTop GitHub Comments

17reactions
KrishnaPGcommented, May 27, 2020

Recently I was working on Knex Migration API. Prisma Migration can benefit heavily if it can be run programmatically completely. That is:

  • The schemas should be input as strings to the migration function, and the migration should run without having to rely on the external file system folders. (i.e. the migration storage could be abstracted out so that user can plugin any storage system, similar to Knex Migration API)

This enables complete end-to-end functionality such as:

  • Allow users to define schemas on a website UI (text box), and send it server to generate the database schemas on the fly, and expose them as GraphQL or REST API using the Prisma 2

The migrations, if required, could be stored inside the database itself as meta tables and expose it to the user to rollback or migrate up inside Web UI. Or they could be stored or S3 or similar, based on user-configured storage systems inside their application. Or no storage at all, it could all be in memory.

Prisma can expose base Migrate Storage interface and users can be allowed to implement their custom sources.

15reactions
PhilippMolitorcommented, Dec 25, 2020

I had some thoughts on this feature and already shared them with @albertoperdomo from the Prisma team. To me, a very essential feature that would be possible with this addition would be to run a custom seeder. Imagine freshly deploying the app onto a server, and when it detects that there are 0 migrations in the database yet, it runs a custom function creating some essential entries.

Imagine something like this:

const connection = new PrismaClient();

connection.$connect()
  .then(() => {
    const migrations = connection.$migrations();

    const isFreshInstall = migrations.databaseIsInitialized;

    if(migrations.pending > 0) {
      migrations.runPendingMigrations();
    } else if (migrations.schemaNotMatching) {
      throw new Error('server schema newer or not compatible with application schema');
    }

    if(isFreshInstall) {
      connections.mymodel.create({data: { ... }});
      console.log('database was seeded!');
    }
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma CLI Command Reference
migrate reset. For use in development environments only. This command deletes and recreates the database, or performs a 'soft reset' by removing all...
Read more >
How to migrate a database programmatically? prisma
Best workaround for now would be to writes scripts that use the CLI commands. Share.
Read more >
How To Build a REST API with Prisma and PostgreSQL
Prisma Migrate : A powerful data modeling and migration system. ... Start by installing the Prisma CLI with the following command:.
Read more >
How to Build a Fullstack App with Next.js, Prisma, and ... - Vercel
Prisma is a next-generation ORM that can be used to access a database in ... the Prisma CLI to bootstrap a basic Prisma...
Read more >
CLI Configuration Commands [Cisco SD-WAN]
To access vtysh commands, see Quagga docs on the Quagga Routing website. If you type ? at the prompt after entering configuration mode,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found