Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a way to check which migration(version) that currently is on a database

I am trying to figure out if there is a way to check which migration version that is the latest that has been running on a database. This to know the state of a certain database.

I read something about entity framework 6 is creating a table to hold track on it. I haven't tried it but I would like to know if theres anything similar to entity framework core.

like image 725
stian64 Avatar asked Nov 09 '16 11:11

stian64


People also ask

What about migrating an existing database?

Over time, a business may migrate from an existing database to save costs, enhance reliability, achieve scalability, or any other objective. This process of moving data from one place to another is known as database migration. Even though they are essential, data migration projects can be very complex.

What is a database migration tool?

A database migration tool allows firms to transfer data from one type of database to another, or from a database to another type of data repository such as a data warehouse or data lake, without having to rely on manual coding or overly complicated ETL tools.


2 Answers

It is possible to get a list of pending migrations in Entity Framework Core using the following code:

var migrationsAssembly = db.GetService<IMigrationsAssembly>();
var historyRepository = db.GetService<IHistoryRepository>();

var all = migrationsAssembly.Migrations.Keys;
var applied = historyRepository.GetAppliedMigrations().Select(r => r.MigrationId);
var pending = all.Except(applied);

See https://github.com/aspnet/EntityFramework/issues/6110#issuecomment-242220554.

You need to include a couple of using statements:

using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;

There is an open issue for a more formal API similar to the one you have in EF 6: https://github.com/aspnet/EntityFramework/issues/577

like image 146
Mike Brind Avatar answered Sep 30 '22 13:09

Mike Brind


It is possible to at least get the

migrations that are defined in the assembly but haven't been applied to the target database.

( source ) via DbMigrators Method getPendingMigrations().

If you want the actual version of a database, there actually is a migration history table __MigrationHistory containing a MigrationId column which should give you what you want. Here's an article showing how to work with it: https://msdn.microsoft.com/en-us/data/dn456841.aspx

like image 45
Dominik Avatar answered Sep 30 '22 14:09

Dominik