How do I permanently delete failed prisma migrations in productions to keep it in sync with my local prisma migrations folder? - prisma

I'm experiencing an error while deploying my remix app to fly via github actions.
I recently altered one of my schemas, which led to the failed migration. I have deleted the migrations in the prisma/migrations folder and the local _prisma_migrations table and did a fresh prisma migrate dev. When I push to github I get an error that one of the migrations that I deleted could not be applied. How do I delete it completely in the production environment?

Related

Updating Azure Database for PostgreSQL flexible server using Alembic

I'm looking to amend some tables I have in a PostgreSQL instance on Azure, but I cannot work out how to perform the upgrades with Alembic.
I have been following the tutorial here, which includes a Heroku deployment around the 12:01:00 mark. In that case, once the changes have been defined, we can run heroku run "alembic upgrade head" to perform the upgrade. However, I cannot find the equivelant process for Azure.
My postgres instance is housed in a VNet and connected to a web app. Until now, I've made code changes to a server which is running in an attached web app. I push to GitHub which then deploys the changes in Azure. Obviously, if the table already exists in postgres, changes I make to the original schema are not reflected. I considered deleting the table and stating again, but this seems a very risky strategy.
A similar question was asked here, but has remained unanswered. I've also checked the documentation for Alembic and Azure but could not find anything.

Prisma push comand does not update Schema in cloud

I am using Prisma Cloud to quickly prototyping an app. I connected it to Postgres via Heroku.
I have my schema defined in prisma/schema.prisma.
When I modifiy the schema, the docs say to run the command:
npx prisma db push
When I do so, I received this message in terminal:
Your database is now in sync with your schema.
If I go to Prisma Studio, the schema has not change.
However, if I push my code to Github, the schema changes.
I was expecting that npx prisma db push would automatically change the schema in the cloud without the need to push on Github.
Am I wrong?
npx prisma db push just updates your database schema.
To update your schema.prisma file in Prisma Cloud you are supposed to push the code to the GitHub branch with which you have associated your environment. Each environment in Prisma cloud project has one to one relation with a GitHub branch.
Once you push changes to GitHub, Prisma cloud receives a webhook from GitHub and it then updates the schema. You can see the latest schema of your project in schema tab, attached image for reference.
It also shows the last time when schema was updated.

Too many Prisma migration files

I'm currently working on a project that we are using Prisma 2 and postgreSQL as database support. From my understanding, whenever I made changes to the schema.prisma file and I want to migrate the changes to the database, I run prisma migrate dev locally. Then I will push the auto created migration files under migration folder to Github, and then our repo will run prisma migrate deploy to the staging or production server.
So, my concern is, each time I run prisma migrate dev, a new migration file will be created in migrations folder. So, there will be a lot of migration files under the migration folder with the development of the project. Is that what supposed to happen? Or is there a better way?
Thank you for your help. I'm pretty new to Prisma 2 and still trying to find the correct way to work with it. BTW I think Prisma 1 was easier to use :)
I’d recommend using prisma db push while developing locally and generating the migration with prisma migrate dev only when you think the schema changes are good to go. More at https://www.prisma.io/docs/guides/database/prototyping-schema-db-push
So, there will be a lot of migration files under the migration folder
with the development of the project. Is that what supposed to happen?
Yes, this is exactly what's supposed to happen. The migration file contains the history of all changess to your Prisma schema (and underlying database tables/configuration), so they all need to be retained.
I'm not sure if you could go around this somehow, but it's certainly not the recommended way to be using migrate.
You can find a list of best practices regarding migration in the Prisma migrate article in the docs.
It's been a very long time since the question is asked, but i want to warn about something:
As you may notice a table named _prisma_migrations is created by prisma automaticly and every time you add a new migration you are able to see the new migration's data over there. So any manual change made in migrations folder can cause problems since it breaks the relations between folder names and _prisma_migrations table.
Also docs for "squashing migrations": https://www.prisma.io/docs/guides/database/developing-with-prisma-migrate/squashing-migrations

Updating prod database after reverting and adding migrations in dev

I'm running an asp.net core app with ef core and two environments: prod and dev. I created some migrations and applied them to both environments a while ago, but needed to roll them back in development. After I rolled back in dev, I kept developing and now have many migrations in dev that are not in prod, as well as the reverted migrations in prod that I haven't rolled back yet. So now my environment's migrations look like this:
Prod => a,b,c,d
Dev => a,b,e,f,g
(one major thing to note: the migrations i'm reverting in prod (c,d)
are not being used, they were just models I created for
future use - that I ended up changing - so loss of data from these
migrations is not a concern)
I can't run the same commands I ran initially in dev since i'm not adding any migrations now, I just need to update the prod database to the migrations I have locally.
If I update-database b then update-database will this revert the changes in prod then update to my local migrations? Or will this revert the migrations i've added in visual studio? What are my options here?
On the production environment run a downgrade to the latest working migration:
Update-Database –Migration b
Then delete the 2 migrations c and d from your migration folder.
Finally you can update to the latest migration on the production enviroment:
Update-Database

Code first deploy on production

I have copy of prod DB and code on my DEV PC.
On production I saw in system tables that 8 migrations records exist in _MigrationHistory. (The first version of the code and deploy was written by another person).
I need to add few new tables and code for work with them - models and controller. I can add new models for new tables and code for controller.
On the DEV PC I can start command
add-migration myNewMigration
and
update-database with option "-script"
and without it to get tables in my DEV DB and script to execute on Production.
For deploy I need to copy content on bin folder and new views on Prod and to run SQL script to create new tables in DB.
I was wondering is that enough because at the moment I see records for all migrations in my DEV environment corresponding records in Production environment in system tables - _MigrationHistory.
If I only run the SQL script on PROD may be new record for this operation will not appear in _MigrationHistory. So could be that a reason to get error that model is different and to have any problem after deploy.
Before making changes to the production database, ensure you have a complete backup.
I was wondering is that enough because at the moment I see records for all migrations in my Dev environment corresponding records in Production environment in system tables - _MigrationHistory
If you ran the code in your development environment, it will apply your explicit migrations.
If I understand your comment correctly, you see the same migrations in DEV and in PROD. If that is accurate, then PROD may be up-to-date.
Look in your Migrations folder in your project. There you will see a complete list of migrations, each one in its own .cs file (plus related .Designer.cs and .resx files). Compare what you see there to the list of migrations in __MigrationHistory in DEV and in PROD to understand the full set of migrations, and where they have been applied.
If I only run sql script on Prod may be new record for this operation will not appear in _MigrationHistory
Have a look at your migration scripts. They insert the appropriate entry into __MigrationHistory where they are run (including in production). Here's an edited example from one of my projects:
INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
VALUES (N'201409082209285_VDisposer', N'Survey.Model.Migrations.MembershipConfiguration.MembershipConfiguration', 0x1F8B0800000...1A0200 , N'6.1.1-30610')
So could be that a reason to get error that model is different and to have any problem after deploy
If you are having an error that the model does not match the database, there is another reason.
Here's what I suggest to narrow down the differences:
Backup your production database.
Restore it in your DEV environment (use a name different than your usual DEV database, or backup your DEV database first. If using a different name, remember to temporarily change web.config to the new database).
Create a new migration.
Look at the code generated for the new migration. It will point out differences between what is in PROD and the state that DEV is in.
Remember to undo changes to your web.config after you are done.