Prisma migrate command gets stuck - postgresql

I am trying to use Prisma with a local instance of Supabase running on docker. I created a very basic model inside prisma/schema.prisma file:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DB_URL")
}
model Post {
id String #id #default(uuid())
title String
}
I also have the DB_URL variable in the .env file:
DB_URL="postgresql://postgres:postgres#localhost:54322/postgres"
When I run npx prisma migrate dev --name init to create a migration, I get the following message on the console and the process just runs without any result until I break it.
Environment variables loaded from .env
Prisma schema loaded from prisma/schema.prisma
Datasource "db": PostgreSQL database "postgres", schema "public" at "localhost:54322"
Am I missing something? Any ideas?

I resolved the problem by using ShadowDatabaseUrl

Related

Prisma - SQL Server - Wrong catalog

I'm trying to initialize the database with Prisma using the following command:
npx prisma migrate dev --name init
However, it complains that my database is not in sync and shows me table names from another database and ask if I want to reset it.
Here is the schema:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
model Test {
id Int #id #default(autoincrement())
}
And the connection string:
sqlserver://localhost\SQLEXPRESS;initialCatalog=sample;integratedSecurity=true;trustServerCertificate=true;
The catalog "sample" is defined there, but for some reason, it is trying to connect to another existing database.
I tried to create the "sample" database manually but it doesn't change anything.
What am I missing here ?
Ok I changed initialCatalog to database and it's working.
sqlserver://localhost\SQLEXPRESS;database=sample;integratedSecurity=true;trustServerCertificate=true;

Getting "The 'mongodb' provider is not supported with this command" Error when try to do mongoDB migrate with Prisma

I'm developing some simple Todo App BE using NestJS with Prisma ORM and use MongoDB as the DB. I'm using a FREE and SHARED MongoDB cluster that is hosted in MongoDB Altas cloud. Also I added 0.0.0.0/0 to the network access tab so anyone can connect to the DB.
schema.prisma file
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Task {
id String #id #default(auto()) #map("_id") #db.ObjectId
name String?
description String?
status TaskStatus #default(TODO)
}
enum TaskStatus {
TODO
INPROGRESS
DONE
}
.env file
DATABASE_URL="mongodb+srv://<username>:<password>#todoappdb.jfo3m2c.mongodb.net/?retryWrites=true&w=majority"
But when I try to run npx prisma migrate dev --name init command it gives following output
D:\todoapp-backend>npx prisma migrate dev --name init
Environment variables loaded from .env
Prisma schema loaded from prisma\schema.prisma
Datasource "db"
Error: The "mongodb" provider is not supported with this command. For more info see https://www.prisma.io/docs/concepts/database-connectors/mongodb
0: migration_core::state::DevDiagnostic
at migration-engine\core\src\state.rs:250
Can someone point me what is the problem?
After reading some content, found that prisma migrate commands are for the SQL databases only since they have a rigid table structure. But MongoDB is a document database and those data are unstructured
So rather than running prisma migrate command we can use following command
npx prisma generate
This command creates the Prisma client that gives type-safe access to our database.
Reference - https://www.youtube.com/watch?v=b4nxOv91vWI&ab_channel=Prisma

Prisma export database & models

is there a way to create a dump of your database with the models, so that I can just push it on another database and just have to update my database address and all works out fine?
Exporting from Prisma CLI itself is not possible as of now.
But what you can do is dump the db from the postgres cli.
And then Import the dump in your new DB : Guide from Prisma
Connect to this DB in your new application add a schema.prisma with base configuration, without models and pull the DB: Introspection Guide from Prisma

Prisma migration fail with supabase

I'm trying to generate some migrations using Prisma.
I'm using Supabase which is using Postgres under the hood.
Also, I tried to run the following command with the local emulator and with the "real project".
When I run prisma db push it's working, so the communication between prisma and supabase can be established.
But when I try to run prisma migrate dev I get the following error
Error: db error: ERROR: no such database: prisma_migrate_shadow_db_b2ce3e4e-c5ef-41f6-830f-2203a082f1db
0: sql_migration_connector::flavour::postgres::sql_schema_from_migration_history
at migration-engine/connectors/sql-migration-connector/src/flavour/postgres.rs:367
1: migration_core::api::DevDiagnostic
at migration-engine/core/src/api.rs:108
Supabase CLI : 0.15.3
Prisma : 3.6.0
I also asked this question on Prisma repo : https://github.com/prisma/prisma/issues/10575
The solution is to create a shadow database as mentionned in the documentation
https://www.prisma.io/docs/concepts/components/prisma-migrate/shadow-database#cloud-hosted-shadow-databases-must-be-created-manually
Prisma
Prisma will try to drop the db during the migration and SupaBase doesn't like that so Prisma requires you to make a "Shadow Database"
Prisma Docs: Shadow Databases
SupaBase
To create the shadow db and get its connection string you have to connect to the SupaBase db with psql, create a new database and modify your connection string to point to the new db. (because prisma IS allowed to delete this second db during migration)
SupaBase Docs: Prisma (Shadow db)

Knex migrations with Postgres always say 'Already up to date'

I am trying to alter my Postgres database using Knex migrations but they are not working.
My knexfile.js looks like this:
module.exports = {
development: {
client: 'pg',
connection: {
database: 'cms_dev',
},
},
};
Then I have a db.js that looks like this:
const config = require('../knexfile.js');
const env = 'development';
const knex = require('knex')(config[env]);
module.exports = knex;
knex.migrate.latest([config]);
If I am starting with a fresh database my first migration works but if I try to update the database with a new (2nd) migration it does nothing.
When I try running knex migrate:latest --env development it says:
Using environment: development
Already up to date
*****EDIT*****
I ended up generating another new (3rd) migration and pasted in the exact same code from the previous one that was being ignored and it worked. No clue why the 2nd migration silently failed and the 3rd one worked.
I figured out the issue. I had forgotten to configure nodemon to ignore my migrations folder.
Because I have this line in db.js:
knex.migrate.latest([config]);
If I saved at all while creating the migration, my server would restart and cause the latest and incomplete migration to fire off. Since migrations only run once, Knex would think the database was already up to date.
If you're just looking to re-run any migrations from the start, delete the migrations tables in the schema/database of your choice.