How to rollback a migration in Flyway Scala? - scala

I just made a migration file.sql to add a new table to the database, and I ran the flywayMigrate command sbt flywayMigrate. Now I realised I missed adding two columns to the same table. I have an option to write another migration, but I don't want to increase the number of migrations when I can rollback and change the migration I just made to create a table and add two more columns to it. Can you tell me how do I rollback some number of migrations and change them, and run sbt flywayMigrate again?

You don't necessarily have to re-run whole migration. There's a table which flyway uses for metadata and I think it is called schema_version. You can set the last applied migration id (there are some hashes to be set IIRC, but you will be able to figure them from error messages, if they don't match) and flyway would pick up from there.

As described in Flyway FAQ rollback scripts are not supported.
You have options to:
restore your database from backup
drop and recreate the database rerunning all the migration scripts
manually rollback the migration and remove its record from metadata table

Related

Flyway clean removes public/routines and can't migrate afterwards

When I perform flyway clean, it removes everything from my public database including all tables, but also all routines.
The problem is that when I perform my first migration, i'm using Postgres's routine gen_uuid. Consequently, the migration fails and i'm stuck in a loop.
Is that normal ?
I found the answer, turns out there were a strong coupling between migrations written by the previous team on 2 pg extensions, pgcrypto and unaccent that was NOT written as migrations by us but by another container in the stack.
The solution is to let Flyway manage the creation of these extensions.

Check for pending migrations in Entity Framework?

In Entity Framework 6 I'm using the Update-Database command to apply migrations. I've got three environments that I juggle (DEV, QA and PROD), and upgrade them using
Update-Database -ConnectionStringName DEV
But, now I'd like to know which migration my PROD environment is at, and which migrations willl be applied if I call Update-Database.
Is there a command for checking which migration is the latest one applied, and which will be applied if I run Update-Database?
To see which migrations have been applied to the database use the command Get-Migrations:
Get-Migrations -ConnectionStringName PROD
You can also check the contents of the table __MigrationsHistory in the right database. It contains info about all migrations applied to the database.
The next migration applied depends on the existing migration files in your project. A migration file name includes a prefix that is a timestamp, which specifies the time at which the migration file was generated (unless you used the -force parameter that might cause to reuse an existing migration file keeping its existing timestamp string). The migrations are applied according to that timestamp. So the alphabetical ordering of your migration files indicates the order in which they are applied.
A safe way to check which migration will be applied next is to run Update-Database with the -Script parameter, which generates the SQL script for the migration but does not run it. So you can see which migration would be applied if you run the real Update-Database.
Update: in Entity Framework Core this command is not available. You can still check __MigrationsHistory table contents. To generate the SQL script that would be executed without running it you can use the command Script-Migration.

EF Applies All Migrations, Not Just New One

I'm using EF 6 and when I run Update-Database it is trying to apply every migration since the beginning of the project, even though there is only migration that is new.
If I attempt to create an empty migration, I get an error that says I can't create a new migration because there are explicit migrations pending.
The only blog post I found with a similar issue suggested dropping the __MigrationHistory table and doing Update-Database -Script, stripping out all the actual DB changes but leaving in the Create __MigrationHistory and Migration Inserts. I didn't drop the table, but I did rename it, and this didn't solve the problem for me.
This question looks like a duplicate, but when I run the script I see that my context key has not changed, so that's not my problem.
What's weird is I added a column to my model, and then did an add-migration with no problem. Then I went to lunch (but without doing an update-database), then after lunch I decided to add another column to my model and when I did add-migration -force again this error started. I've completely rolled back my workspace (completely delete the workspace and removed all code from my machine), but the problem persists.
I am in a team environment, but we each use our own dev databases, and I've verified the connection string is correct in my Migrations App.Config file.
I ran SQL Profiler during the Update-Database -Script command, and it ran the following query 24 times before giving up (12 batches of 2) :
IF db_id(N'ISEPDBContexts.AdministratorDb') IS NOT NULL SELECT 1 ELSE SELECT Count(*) FROM sys.databases WHERE [name]=N'ISEPDBContexts.AdministratorDb'
Which is weird because that's the name of my connection string, not the name of the database.
Turns out the problem was that Package Manager Console wasn't looking at my config file, so a quick search and I found that you have to not only set the Default Project in PMC (which I was doing), but the solution has to have the same project selected as Startup Project. (Credit)

How to Manage EF Migrations Between Development and Production Databases?

Before anyone marks this as a duplicate, none of the questions similar to this addressed any of my concerns or answered any of my questions.
I am currently developing all the POCOs and data contexts in a library project, and running migrations from within this project. The database I'm updating is the development database.
What do I do if I want to create the current schema to a fresh, new database? I figure that all I have to do is to change the connection string in web.config and run Update-Database, correct?
While the live/production database is up and running, I want to add new columns and new tables to the schema, and test it out in development. So I switch back the connection string to the development database's connection string, and run Update-Database.
Going back and forth between two databases seems like I'll get conflicts between _MigrationHistory tables and the auto-generated migration scripts.
Is it safe to manually delete the _MigrationHistory tables in both databases, and/or delete the migration files in /Migrations (so I'll run Add-Migration again)? How do we manage this?
What do I do if I want to create the current schema to a fresh, new database?
- Yes, to create fresh database to the current migration level you simply modify the connection string to point to a database that does not yet exist and run update-database. It will run all the migrations in order.
As far as migrating to the Production database, I am running the update-database command with the -script switch to acquire the raw sql and then applying that script to the production database manually. This is helpful if you need to keep a record of sql commands run against the database as well. Additionally, you can generate the script explicitly from a specific migration to another specific migration via some of the other update-database switches.
Alternatively, you can create an Idempotent script that works from any migration by using the–SourceMigration $InitialDatabase switch and optionally specify an end migration with –TargetMigration
If you delete the _MigrationHistory tables you will have issues where the generated script will be trying to add columns that already exist and such.
You may find the following link helpful:
Microsoft Entity Framework Migrations
I would suggest having a separate trunk in your source code repository - one pointing to production and one to development to avoid risks of switching between the two in visual studio.
Me also had the same problem, even when using one and the same database - due to some merges in the repository, and the mix of automatic/manual migrations. For some reason the EF was not taking into account the target database, and calculating what scripts need to me executed, based on what is already in the database.
To fix this, I go to the [__MigrationHistory] table on the target database and get the latest migration name. This will help EF to determinate the state of the DB, and will execute just the scripts needed.
then the following script is run:
update-database -script -sourcemigration {latest migration name}
This creates update script that is specific to the target database (the connection string should be correct, as discussed in the other comments)
you can also use -force parameter if needed
this way you can update any database to latest version, no mater in what version you found it, if it has MigrationHistory table.
Hope this helps
My production and my developmental database went out of synch and it gave me endless problems. I solved it using a tool from Red-Gate to match up the databases. After using the tool, the databases were exactly the same but my migration was not working and I started to get odd errors i.e. trying to add tables/ columns that already existed etc. I solved that. I just deleted the migration folder on the local, recreated it, added the initial migration, updated the database and then matched the data of this migration file (local) to the one on the host (delete all the data in the migration file on the host, and add the same data that is on the local into the host). A more detailed explanation is at:
migration synch developmental and production databases

Entity Framework - Rebuild Database after deleting table or renaming field

I'm having lots of trouble trying to figure out how to rebuild my database.
I've deleted a table, then i ran this command update-Database -Verbose and it doesn't rebuild my database.
The same thing goes for when i have a table, then i change a column name inside my Model (C#) and then i want to rebuild my database so that the name shows up in the database, and nothing happens when i run the same command.
How can i rebuild my database? I'm sure there's a command or something, besides the update-Database -Verbose.
I'm using Visual Studio express 2012 for Web.
EDIT: Couldn't find a command able to rebuild my Database, though i did find a simple way to do what i wanted.
You can delete tables or rename columns, in your Models, and it will always rebuild your database. In the Solution explorer, you'll find an icon that says "Show All Files", press it and open the folder App_Data, there you'll see your database file, delete it and re-run your application, and it will build again the Database, with all the changes you've made to your Models in your code. So that part about changing catalog and .mdf names in the Web.config file is not needed.
Update-Database is part of the Entity Framework Migrations package, which allows you to script the changes to the database sequentially. The command won't do anything on it's own without migrations to process. If you are using Migrations, you need to use the Add-Migration myNewMigration command first, and verify the script that is generated makes sense.
If you are not using Migrations, you can use the much simpler database options such as DropCreateDatabaseIfModelChanges() against your context class.
Another thing you can try is running Update-Database -f to "force" the migration to run even if it's already present in the _MigrationHistory