Prisma.Schema will not Update - prisma

I am having the dumbest issue ever with my schema file. It has nothing to do with the Models or any properties or actual information, but rather the file is just refusing to save, or update the saved changes.
The prisma.schema file worked perfectly and we had to make a couple of minor changes, literally just changing the names of some of the models' values. For some reason, this made my file unable to read the contents of my .env file... fine, I only needed it for the URL anyway. This being said I then changed the following code from this...
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
to this...
datasource db {
provider = "postgresql"
url = "postgresql://nick:Abc!123#localhost:5432/tom?schema=public"
}
But whenever I run any command that will involve the schema file in some way (node server.js, npx prisma migrate dev etc) it will give me the following error...
PrismaClientInitializationError: error: Environment variable not found: DATABASE_URL.
--> schema.prisma:8
|
7 | provider = "postgresql"
8 | url = env("DATABASE_URL")
|
which is clearly just not what I have in my code anymore. I've never seen anything like this, I've closed vscode, I've restarted my machine, any ideas?

I believe you need to run prisma generate command after you make any changes to the schema.prisma file.

Related

How to connect to Postgres with Kotlin/Ktor with plugin start

I have decided to start learning Kotlin/Ktor. I have created a project in Intellij and selected the Postgres plugin. I'm reading the instructions of how to set it up from the file it created:
/**
* Makes a connection to a Postgres database.
*
* In order to connect to your running Postgres process,
* please specify the following parameters in your configuration file:
* postgres.url -- Url of your running database process.
* postgres.user -- Username for database connection
* postgres.password -- Password for database connection
**/
But I cant figure out where to set the username or password (or url). It refers to a configuration file but I have no idea where that is.
Here are all my file structure if it helps.
All files part 1
All files part 2
All files part 3
I have tried the Navigate - > Search everywhere" option for keywords such as password and postgres but found nothing that looks like where I can configure the password or username.
Any help appreciated.
There is supposed to be an application.conf file in your resources folder, if it is not there, that means you chose your configurations to be in code when you created the project the first time.
Look at the following image: Ktor Project creation, in the bottom it asks where to set up your configurations, choose HOCON file instead of Code. By this way, you will have application.conf file in your resources folder.
In that file, add the following code snippet to configure Postgres:
postgres {
url = "Your URL"
user = "Your USERNAME"
password = "Your PASSWORD"
}
However, for security purposes, you should not write your database credentials as plaintext in your configuration file. For your production, consider using environment variables and configure Postgres like this:
postgres {
url = ${pg_url}
user = ${pg_username}
password = ${pg_password}
}
where pg_url, pg_username, and pg_password are the defined environment variables in your system.
Finally, to access the url, user, and password in your code, Do it like this:
val url = environment.config.property("postgres.url").getString()
val user = environment.config.property("postgres.user").getString()
val password = environment.config.property("postgres.password").getString()
For more info about configuring your project in the file, refer to the documentation. Hope my answer helps.

Found non-empty schema(s) "public" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table

I know this has been asked previously but I am spinning in circles here....
I have a postgres 14 database and a Springboot application running flyway-db:7.13.0 and flyway-core:8.5.10
I am using RDS. I created a new database manually using root account. When I run my springboot my flyway migration fails with the error
Found non-empty schema(s) "public" but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table.
I understand the reason why this is happening but when I add the baselineVersion and baselineOnMigrate to my flyway gradle config it still doesn't work. I even added
flyway {
url = dbUrl
user = dbUser
password = dbPassword
schemas = ['public']
baselineVersion = '0.0'
baselineOnMigrate = true
locations = ["filesystem:${dbMigrationPath}"]
}
also I added
spring.flyway.baseline-on-migrate = true
to my application.properties
What confuses me the most is if I start the application pointing to the default postgres database with the root user. The migration works and it creates the flyway history table. but when I run it pointing to the newly created database it doesn't work.
Any idea?
I figured it out. It turned out I needed to add the baselineOnMigration flag to my configuration
Flyway.configure().baselineOnMigrate(true).dataSource(new TransactionAwareDataSourceProxy(dataSource)).load();
Whether to automatically call baseline when migrate is executed against a non-empty schema with no schema history
table. This schema will then be initialized with the baselineVersion before executing the migrations.
Only migrations above baselineVersion will then be applied.
This is useful for initial Flyway production deployments on projects with an existing DB.
Be careful when enabling this as it removes the safety net that ensures
Flyway does not migrate the wrong database in case of a configuration mistake! (default: false)
flyway.baselineOnMigrate=
flyway.baselineOnMigrate=true
I added flyway.baselineOnMigrate=true under the statement above and it worked.

prisma - getting environment variable not found error message when running graphql query

I am getting this error message from prisma when I am running the GraphQL query.
Environment variable not found: DATABASE_URL.\n --> schema.prisma:6\n | \n 5 | provider = \"postgresql\"\n 6 | url = env(\"DATABASE_URL\")\n | \n\nValidation Error Count: 1",
At first, I didn't have the .env file in any of my project folders, then I added it with the link to the database url, still not working.
Here is the folder structure:
This is what I have inside my .env file looks like -
DATABASE_URL="postgres://postgres:mypassword#db.pqtgawtgpfhpqxpgidrn.supabase.co:5432/postgres"
If anybody running into this issue, just run npx prisma generate. This will re-establish the link between schema.prisma and .env file.
In my case I wanted to run Prisma Studio with NextJS that stores all environment variables in .env.local, so I need to load the file first.
npm install -g dotenv-cli
dotenv -e .env.local -- npx prisma studio
Here is a link to the official Prisma docs on how to load .env files manualy.
I had this issue in my NextJs project. after changing the .env.local file to .env everything worked.
Others like me (new to Prisma, following the Remix.run jokes-app tutorial) might be relieved to learn it's not just you: there was a regression in Prisma 3.9.0, fixed in 3.9.1 in early Feb 2022. https://github.com/prisma/prisma/issues/11570
"prisma db pull doesn't read .env file and errors with Environment variable not found: DATABASE_URL"
If you try with a schema completed and an empty db, you have this error. Try "prisma db push" first and after verify with "prisma studio".
In my case I encountered a weird problem with the .env file itself, I created the file using Powershell's echo. Apparently, manually creating it in Vscode solves the problem.

Unknown property errors trying to do a with Flyway migration with per script config files

My company is evaluating Flyway for database releases. We have an AWS PostgreSQL version 11.2 database and I have installed Flyway Community Edition version 6.1.2.
I have successfully baselined the database and run several basic DDL scripts using Flyway migrate. However now I am testing a more complicated scenario in which I need to run multiple scripts as one migration but each script has to connect as a different PostgreSqL user. I have tried to do this by setting up two sql files each with their own config file as described here: https://flywaydb.org/documentation/scriptconfigfiles
Every time I run the migrate command I get a property error: "ERROR: Unknown script configuration property: flyway.user" or "ERROR: Unknown script configuration property: user", etc, etc.
For debugging purposes I removed one sql and config combo so that I now only have one file each. The files are named V2020.1.14.08.41.00__role_test1.sql and V2020.1.14.08.41.00__role_test1.sql.conf. I did confirm that any changes to that config file are being picked up by the migrate command. My config file contains the following properties (values changes for security reasons):
flyway.url=jdbc:postgresql://...
flyway.user=user1
flyway.password=password
flyway.schemas=test
I have also tried removing the flyway prefix:
url=jdbc:postgresql://...
user=user1
password=password
schemas=test
And removing the url parameter (both flyway.url and url) so the migration reads that value from the default flyway.conf file. Example:
user=user1
password=password
schemas=test
I get the errors every time. Anyone have any ideas? All help is greatly appreciated.
There is a typo in your code:
flyeay.user=user1
It should be:
flyway.user=user1

localdb does not create database file if its missing

I use this connection-string:
Server=(localdb)\\MyInstance;Timeout=30;Database=MyDB;AttachDBFilename="C:\Temp\MyDB.mdf";Trusted_Connection=True;
Once I run a migration from my code using
dbContext.Database.Migrate();
Normally, this "simply" works. The db is not just migrated, it is also getting its file created for it.
However, on the device of a colleague, the same code results in this error message:
System.Data.SqlClient.SqlException: "Cannot attach the file 'C:\Temp\MyDB.mdf' as database 'MyDB'."
If I give my database files to my colleague and he places them in the appropiate directory first, everything works as expected and the other code in that program can access everything in it, as it would do normally.
We've tried different paths and always checked the file-system rights. LocalDB or entity-framework (I'm not sure which is normally responsible for creating database files) simply won't create the database-file if it's missing on his device.
Are there any switches causing this? Can I explicitly tell localdb with the connection-string that it should create the database file?