How to migrate a database programmatically? prisma - prisma

I'm planning to create a secondary database for testing purposes. As of now I'm using Prisma's Client to perform CRUD to my secondary database. Good thing it worked fine.
My problem is the migration part. I still need to do it manually. Like editing the schema.prisma file, change the database url, and run prisma migrate dev manually.
I already browsed to Prisma's github, but I can't find an exact solution. I would like to ask here, maybe someone has an idea on how to achieve it.
Working code below.
import { PrismaClient } from '#prisma/client'
const prisma = new PrismaClient({
datasources: {
db: {
url: 'postgresql://capstone:capstone#postgres:5432/blogpost_test?schema=public'
}
}
})
export default prisma
I expect something like below, in which we can just migrate the database programmatically??
prisma.$migrate or prisma.migrate()

This is not directly supported by Prisma. There is an open feature request that you could +1 here.
Best workaround for now would be to writes scripts that use the CLI commands.

Related

Error in connector: missing field `info` - Prisma Introspection (npx prisma db pull)

I have created an Express, Apollo GraphQL server which accepts requests and returns data from a MongoDB database via Prisma.
I have tested my application with a simple database from MongoDB Atlas and it all seems to work fine.
Now that I know everything works fine, I want to 'plug in' a MongoDB database which contains a lot of data already, located in my Azure portal (Cosmos DB).
I have changed the database connection string to point to this new database (a read only connection string) and now attempting to use the Introspection feature of Prisma (https://www.prisma.io/docs/getting-started/setup-prisma/add-to-existing-project/mongodb/introspection-typescript-mongodb#introspecting-mongodb-with-prisma).
After running the npx prisma db pull --force command, I get the following error:
I can't figure out if this is an issue with my connection string, or something else.
I have also attempted to use the Read/ Write connection string but that doesn't seem to work either. I get the same error message.
There does not seem to be any information online regarding this error message other than this:
https://www.prisma.io/docs/reference/api-reference/error-reference#prismaclientvalidationerror
But I am unsure if this is at all related, because it seems to relate to creating a new record, rather than anything to do with introspection.

How to run Prisma schema update without erasing the PostgreSQL data?

I have a PostgreSQL db that is used by a Nest.Js / Prisma app.
We changed the name of a field in the Prisma schema and added a new field.
Now, when we want to update the PostreSQL structure, I'm running, as suggested by Prisma, the following commands:
npx prisma generate
and then
npx prisma migrate dev --name textSettings-added --create-only
The idea is to use the --create-only flag to review the migration before it is actually made.
However, when I run it I get a list of the changes to be made to the DB and the following message:
We need to reset the PostgreSQL database "my_database" at "my_db_name#cluster.us-east-1.rds.amazonaws.com:5432".
Do you want to continue? All data will be lost.
Of course I choose not to continue, because I don't want to lose the data. Upon inspection I see that the migration file actually contains DROP TABLE for the tables that I simply wanted to modify. How to avoid that?
So how do I run the update without affecting the data?
UPDATE:
I saw that running with --create-only creates a migration which can then be implemented on the DB level using prisma migrate dev, however, in that migration file there are still some commands that drop my previous tables because of some new parameters inside. How can I run prisma migration without deleting my PostgreSQL data?
UPDATE 2:
I don't want Prisma to drop my tables when I just updated them. The migration file generated, however, drops them and then alters them. Do you know what's the best procedure to avoid this drop? I saw somewhere I could first manually update the DB with the new options and then run the migration, so Prisma can find a way to update it, but that seems too manual to me... Maybe some other ideas?
For some cases like renaming tables or columns, Prisma's generated migration files need to be updated if they already contain data.
If that applies to your use case, Prisma's docs suggest to:
Make updates to the prisma schema
Create migration file without applying it (--create-only flag)
Update the migration script to remove the drops and instead write your custom query (e.g. RENAME <table_name> TO <new_name>)
Save and apply the migration (npx prisma migrate dev)
Note that those changes can lead to downtime (renaming a field or model), for which they have outlined the expand and contract pattern.
It might be a Prisma bug: https://github.com/prisma/prisma/issues/8053
I also recently had this situation. It probably should not try to run migration if you only want to create migration file.
But overall it is expected with Prisma to recreate your db sometimes. If you migration is breaking then it will be required to reset the data anyway when you apply it.
I suggest you to create some seeding script so you could consistently re-create the database state, it's very useful for your development environment.
More info

Can I use prisma and node-postgres tougether

I started using prisma especially for handling database migrations. It handles that well. But there are many open issues for things that it does not handle well related to queries (biggest are related to queryRaw not always working as expected and with no straight forward way to use postgreSQL Row Level Security). Everything I've found to be a problem related to queries in prsima can easily be done in node-postgres.
I realize from the docs that some of these issues are not a problem in knexjs but prisma has a more feature rich migration setup (automatic and can be customized).
Can I safely use prisma and node-postgres together? If so, how? For example use prisma for schema design and database migrations and use node-postgres for all my query logic?
Yes, you can safely use prisma and node-postgres together.
Your workflow will look like the following:
Create a Prisma Schema to define your models and underlying database tables.
Use the Prisma CLI (the prisma npm library) to run migrations to your database. This will generate the client in node_modules/.prisma/client directory, which you could optionally delete as you won't be using it.
Instead of using generated Prisma Client inside your node application, use the node-postgres library to run queries against the database in your application.
Since you're only using Prisma to make any kind of migration or changes to the database structure, there's no risk of your prisma schema not being synced with your database. On the off chance this does happen, you can always get it back in sync using prisma db pull.
Furthermore, since your application is not going to use Prisma client to connect to the database for running queries, node-postgres will handle the connection pool.

Prisma ORM how to create migration

I'm new to Prisma ORM, & I'm trying to make migrations in Prisma
I see that I way to do this is to update data.model & then just run:
prisma deploy
But what if I want to create a migrations for specific versions of app how could I do that ??
As the prisma documentation describes there are two ways of doing database migrations in prisma:
Using the Prisma CLI
Performing a manual DB migration with plain SQL
If you follow the first approach and edit your data model the changes will be carried out automagically once you run prisma deploy. You can specify the service and stage this will be rolled out to via the PRISMA_ENDPOINT environment variable:
PRISMA_ENDPOINT="http://localhost:4466/{SERVICE}/{STAGE}"
This way you can roll out and test you data model changes in a different stage or on a different service.
The second approach is to manually change the database model via plain SQL. Be careful to ensure the database schema and your data model are in sync.
For more information check out:
https://www.prisma.io/docs/datamodel-and-migrations/migrations-POSTGRES-asd4/

Connecting Postgresql db with Sails.js

I am brand-new to Sails and I'm looking to build my first application in javascript land! I am currently trying to connect my sails application to a postgresql database and I want to make sure that I am doing this correctly.
I started by creating a postgresql db with dbName, userName, password. I have added all this information into my connections.js file:
somePostgresqlServer: {
adapter: 'sails-postgresql',
host: 'localhost',
user: '<username>', // optional
password: '<password>', // optional
database: '<databasename>' //optional
}
I want to be sure I can migrate and perform all operations on my own so my models.js is set to migrate: safe. I ran npm install sails-postgresql. Now, to my understanding if I have migrate set to safe I will need the sails-db-migrate module (https://github.com/building5/sails-db-migrate). I followed this module step by step. I generated a User model by running sails generate api user. After this I ran grunt db:migrate. After all this, I check my psql database and no User table has been created. I know there is something I am missing, or maybe there is a more simple way to see if my postgresql db is connected.
Any advice or suggestions on how I should approach this would be greatly appreciated.
I was able to connect my PSQL DB to my application by simply:
creating db in psql
configuring it in sails app/connections.js
running npm install sails-postgresql
Adding to config/env/development.js:
models: {
connection: 'somePostgresqlServer'
}
running sails generate api user
sails lift
When asked for migration mode I chose options 2 which is alter
Opened up psequal and sure enough there was my new user table.
I'm not sure which of these actually triggered it to working from adding my db name to development.js, leaving off a PW on my DB, or changing migration mode to alter. But these steps helped it to connect. Will look further into.