Sequelize: How to undo migrations for dropped tables? - express

In my Express Sequelize Postgres app, I created a migration to create a model/table with some attributes.
After the migration (status: up), I dropped the table.
Now I cannot undo the migration - the migration file exists, but I get the following error:
ERROR: relation "public.CustomerAddresses" does not exist
How do I undo the migration, so I can remigrate?

Here are two possible options:
As mentioned, you can undo your manual drop by manually recreating the table. As long as the table exists, you'll be able to undo your migration.
You can remove the entry for your migration from the migrations table to make the state of your schema match the state of the migrations table, since you've already performed the drop manually:
DELETE FROM "SequelizeMeta" WHERE name='<your migration name>';

Related

Redgate migration scripts not running on deployment

I've been reading through the Redgate documentation on migration scripts and I'm trying to add a new column to a table that has a foreign key from another table.
Here's what I have done:
Added the new column, made it null-able and created the
relationship to a new table then I've committed the changes.
I then add static data to the new table so that the migration can
run. I commit this static data.I then add a blank migration script,
and set all null values on the column I've created in the last
commit to be the Id of one of the records in the related table. I
then commit this change.
I then run a deployment of both commits to my testing environment where
records already exist.
The problem I'm having is that the column gets created but the script seems like its not running as the column values stay null. I've verified that the script should actually change the columns as I've attempted to run it manually and it executes successfully.
Am I doing something wrong when using these scripts? Thanks.
I was creating blank migration scripts which lead to SQL Compare to set the column as not null. You have to specifically create a migration script on the schema change that requires it or SQL Compare will override all changes.

Entity Framework Core apply migrations programmatically

I have test and live db. For test db migrations I use Add-Migration ... and Update-Database syntax in package manager console. But for live db I want to do it programmatically when app is started.
The following code didn't help me:
context.Database.Migrate();
I have error Invalid object name 'TempTenants' when I try to add record to table that doesn't exist. This is my new table.
But I have _EFMigrationsHistory table. And there are all my migrations even those that weren't applied. But I don't see new table.
I will have the same result if I manually remove table from test db and try to reproduce error.
So, context.Database.Migrate(); only create new db with all migrations, if it doesn't exist, but don't update (apply migrations) existing db.
Can I do that? And how can I resolve that?
It seems something went wrong. I removed unapplied migrations (records) from __EFMigrationsHistory table (also I needed to revert some table names, primary and foreign keys to previous state) and launched app again.
So, context.Database.Migrate(); apply migrations even for existing databases.

EF Code First rollback database table design

I accidentally deleted my database tables and I need to get them back. I have tried running update-database, but I only get:
Cannot find the object "dbo.ArticleComments" because it does not exist or you do not have permissions.
I also tried running Update-Database -TargetMigration:"name_of_migration" with the migration name but resulted in:
Cannot find the object "dbo.ArticleComments" because it does not exist or you do not have permissions.
I need to know how to get my database tables back with their columns (empty or not I don't care)
This may be the issue on your situation.
check about this problematic table dbo.ArticleComments.If you renamed or deleted it,then it'll give above kind of error.B'cos when you created the old migration script that was there.Now it's not there.When you try to run the same old migration script, now that table is not on your DbSet or having with different name.
Solution :
If that is the case,then you have to manually edit your migration file to reflect the current table changes.

How to restore db table from models in Django

I had two models(Reporter and Article). I did
makemigrations
migrate
it created two tables in the database. Then I accidentally dropped the tables from the database.
Now, when I do migrate again, it says that the table does not exist. It does not create tables from models. The error message is:
relation "first_test_article" does not exist
What am I missing here?
Your last migration is that creates these tables?
The problem is that if you will migrate backwards to previous, exception will probably raise, because it will be trying to delete inexistent tables.
The way you can solve it is to create these tables (empty, without fields) by hands, then you should migrate backwards to previous migration. It will delete these tables, and then you will be able to migrate forwards and create these tables again.

SchemaExport, NHibernate and deleting foreign keys

I am building my mapping and then using schema export to update my DB. However, if I delete an association in my mapping, since it's no longer in the mapping, when I run SchemaExport, it will not delete the foreign key for the deleted association. This means that it then fails to drop the table associated with that foreign key. Which further means that it can't recreate the table and I get a "There is already an object named Foo in the database" exception. Is there any way to brute delete the table via Schema Export?
The cleanest way is to do SchemaExport.Drop with the old nhibernate configuration, then create with the new one.
Alternatively you could drop and recreate the database itself, here's an example which does this at file level for SQL Server Express: http://nicholas.piasecki.name/blog/2010/01/integration-testing-with-sql-server-express-2008-nhibernate-and-mstest/