关于在生产数据库上看似混淆不清的迁移技术的问题

huangapple go评论95阅读模式
英文:

Question about migration techniques on a production db that seem confusing

问题

I use C#, .NET 和 EF Core/SQL for the data layer.

I've read a book on EF Core and one on TSQL and now I'm confused about some aspects of the migration techniques on a production system.

My questions are:

  • I've read about the fact that you can just use script migration -idempotent to get a new/the applicable migration script, taking into account what migration was the last one in the EFMigration_History table. In the book it says you NEED a tool such as DbUp or RedGate's flyaway in order to run this script. Can't you just use MSSMS to do it? Or the VS SQL Server Explorer's New Query?
  • There was also a technique, considered an easy one, where you would create a console app within your solution, this console app would just have context.Database.Migrate in it so that it could run whenever you call it and apply the new migration to a stopped app or a running app with some other tweaks, which is very confusing because maybe I'm not understanding this at all, but when the code is live you can't call/run a solution's project and have an impact on the production system right? And if so do you have an idea what this technique really is?
  • Do you recommend using SQL scripts to build migrations? The use of a SQL database comparison tool to produce migration form the current database schema to the desired database schema, it seems like you need good knowledge of SQL but I'm not too bad.

I'd like to know what you recommend to understand what's my best bet at not breaking my production system by planning ahead of time and making sure I understand what to do before I have to re do everything.

英文:

I use C#, .NET and EF Core/SQL for the data layer.

I've read a book on EF Core and one on TSQL and now I'm confused about some aspects of the migration techniques on a production system.

My questions are:

  • I've read about the fact that you can just use script migration -idempotent to get a new/the applicable migration script, taking into account what migration was the last one in the EFMigration_History table. In the book it says you NEED a tool such as DbUp or RedGate's flyaway in order to run this script. Can't you just use MSSMS to do it ? Or the VS SQL Server Explorer's New Query ?
  • There was also a technique, considered an easy one, where you would create a console app within your solution, this console app would just have context.Database.Migrate in it so that it could run whenever you call it and apply the new migration to a stopped app or a running app with some other tweaks, which is very confusing because maybe I'm not understanding this at all, but when the code is live you can't call/run a solution's project and have an impact on the production system right ? And if so do you have an idea what this technique really is ?
  • Do you recommend using SQL scripts to build migrations ? The use of a SQL database comparison tool to produce migration form the current database schema to the desired database schema, it seems like you need good knowledge of SQL but I'm not too bad.

I'd like to know what you recommend to understand what's my best bet at not breaking my production system by planning ahead of time and making sure I understand what to do before I have to re do everything.

答案1

得分: 1

这是运行迁移的代码,它将输出成功和出错的迁移。你可以在此基础上进行扩展。

protected virtual void RunMigrations(String connectionString)
{
    Console.WriteLine($"\n ---- 正在运行迁移 [{connectionString}]");

    var dbContext = GetDbContext(connectionString);

    var pendingMigrations = dbContext.Database.GetPendingMigrations();
    var latestMigration = pendingMigrations.OrderByDescending(t => t).FirstOrDefault();

    try
    {
        if (pendingMigrations.Any())
        {
            Console.WriteLine("运行迁移 \n - {0}", String.Join("\n - ", pendingMigrations));

            dbContext.Database.Migrate();

            Console.WriteLine("迁移完成...........................");
        }
        else
        {
            Console.WriteLine("没有待处理的迁移。");
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        var successfulMigrations = pendingMigrations.Intersect(dbContext.Database.GetAppliedMigrations());
        var unsuccessfulMigrations = pendingMigrations.Except(successfulMigrations);

        if (unsuccessfulMigrations.Any())
        {
            Console.WriteLine("无法应用以下迁移 \n - {0}", String.Join("\n -", unsuccessfulMigrations));
        }
    }
    Console.WriteLine("--------------------------------------------------------------------------------");
}
英文:

Here is the code to run the migrations, which will output the Migrations run with success and errors. You should be able to build on top of it.

protected virtual void RunMigrations(String connectionString)
        {
            Console.WriteLine($"\n ---- Running migrations for [{connectionString}]");

            var dbContext = GetDbContext(connectionString);

            var pendingMigrations = dbContext.Database.GetPendingMigrations();
            var latestMigration = pendingMigrations.OrderByDescending(t => t).FirstOrDefault();

            try
            {
                if (pendingMigrations.Any())
                {
                    Console.WriteLine("Running migrations \n - {0}", String.Join("\n - ", pendingMigrations));

                    dbContext.Database.Migrate();

                    Console.WriteLine("Migrations completed...........................");
                }
                else
                {
                    Console.WriteLine("No pending migrations.");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                var successfulMigrations = pendingMigrations.Intersect(dbContext.Database.GetAppliedMigrations());
                var unsuccessfulMigrations = pendingMigrations.Except(successfulMigrations);

                if (unsuccessfulMigrations.Any())
                {
                    Console.WriteLine("Failed to apply the following migrations \n - {0}", String.Join("\n -", unsuccessfulMigrations));
                }
            }
            Console.WriteLine("--------------------------------------------------------------------------------");
        }

huangapple
  • 本文由 发表于 2023年6月5日 06:02:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402572.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定