FluentMigrator迁移成功,但数据库没有变化

发布于 2024-10-02 23:24:35 字数 2203 浏览 5 评论 0原文

我一定错过了一些非常基本的东西。

我正在开发一个遗留项目,并且我正在尝试将 FluentMigrator 加入其中,因为我有一些有趣的数据库 我认为即将发生的变化和数据迁移将会产生很大的影响 使用此工具更容易。

对于初始迁移,我只想将数据库提升到 当前的生产版本,按原样。为了简化初始迁移, 我编写了 SQL Server 2008 数据库的脚本,然后执行迁移 作为一系列 SQL 命令的脚本化命令。

为了测试它,我创建了一个完全空的数据库,并尝试运行它 从命令行使用以下命令:

> migrate -a "C:\My\Project\Path\bin\debug\Rds.MyProjName.DBMigrations.dll" 
-db SqlServer2008 -conn "Data Source=.\SQLEXPRESS2008;Initial Catalog=myNewDbName;
Integrated Security=SSPI" -version=20100901000000

指定的版本是第一个迁移类的时间戳 迁移属性。

在命令行中,一切似乎都运行良好 - 整个 脚本放大,结尾为:

-- CreateProductionDbCircaSep2010: migrated

但是,当我查看数据库时,它仍然是空的。 里面绝对什么也没有。我的 Up 方法如下所示:(

public override void Up()
{
    var cmds = LoadEmbeddedResources
        .GetEmbeddedResource("scripted_db_2010-09-01.sql")
        .AsString()
        .ParseCommands();

    foreach (var c in cmds) {
        Execute.Sql(c);
    }

    CreateReferenceData();
}

仅供参考,我正在解析脚本而不是按原样运行它,因为我 开始使用 Migrator.Net,然后发现它已经死了,而这个 已经设置好了。)

有人可以帮我吗?看起来几乎没有交易 提交,或一些命令行选项让迁移进行干- run 已打开,但我没有看到它...

编辑:我尝试过的其他操作

为了确认连接字符串正在使用我期望的数据库,我重命名了数据库并正如预期的那样,之后出现登录错误。

为了进一步测试,我缩短了脚本的长度,并尝试使用

public override void Up()
{
    Execute.Script(@"..\Resources\test.sql"); 
}

而不是逐个命令来执行它,但我得到了相同的结果。这是该测试的输出(注意,我编辑了路径等):

C:\My\Project\Path\FluentMigrator.Net\ >migrate -a "C:\My\Project\Path\bin\debug
\My.Project.DBMigrations.dll" -db SqlServer2008 -conn "Data Source=.\SQLEXPRESS2
008;Initial Catalog=myNewDbName;Integrated Security=SSPI" -version=2010090100000
0
Using Database SqlServer2008 and Connection String Data Source=.\SQLEXPRESS2008;
Initial Catalog=myNewDbName;Integrated Security=SSPI
-- VersionMigration: migrating ===============================================

-- CreateTable VersionInfo
-- VersionMigration: migrated
-- CreateProductionDbCircaSep2010: migrating =================================

-- ExecuteSqlScript C:\My\Project\Path\FluentMigrator.Net\..\Resources\test.sql
-- CreateProductionDbCircaSep2010: migrated

然而数据库中没有表 - 甚至没有预期的 VersionInfo 表。

I must be missing something pretty basic.

I'm working on a legacy project, and I'm trying to bring
FluentMigrator into the mix cause I've got some interesting database
changes and data migrations coming up that I think will be made much
easier by using this tool.

For the initial migration, I just want to bring the database up to the
current production version, as-is. To simplify the initial migration,
I scripted out my SQL Server 2008 database, and the migration executes
the scripted commands as a series of SQL commands.

To test it out, I create an entirely empty database, and try to run it
from command line using this:

> migrate -a "C:\My\Project\Path\bin\debug\Rds.MyProjName.DBMigrations.dll" 
-db SqlServer2008 -conn "Data Source=.\SQLEXPRESS2008;Initial Catalog=myNewDbName;
Integrated Security=SSPI" -version=20100901000000

The version specified is the timestamp on the first migration class's
Migration attribute.

At the command line, everything appears to run fine - the the entire
script zooms by, and it ends with:

-- CreateProductionDbCircaSep2010: migrated

However, when I take a look at the database, it is still empty.
Absolutely nothing is in there. My Up method looks like this:

public override void Up()
{
    var cmds = LoadEmbeddedResources
        .GetEmbeddedResource("scripted_db_2010-09-01.sql")
        .AsString()
        .ParseCommands();

    foreach (var c in cmds) {
        Execute.Sql(c);
    }

    CreateReferenceData();
}

(FYI, I'm parsing the script instead of running it as-is because I
started by using Migrator.Net before discovering it was dead, and this
was already set up.)

Can anyone give me a hand? It almost appears like a transaction isn't
committing, or some command-line option to have the migration do a dry-
run is turned on, but I don't see it...

EDIT:Additional things I've tried

To confirm that the connection string was using the database I expect it to, I renamed the database and afterwards got a login error, as expected.

For further tests, I cut down the length of the script, and tried executing it using

public override void Up()
{
    Execute.Script(@"..\Resources\test.sql"); 
}

instead of command-by-command, but I get the same results. Here is the output on that test (note, I edited the pathes and such):

C:\My\Project\Path\FluentMigrator.Net\ >migrate -a "C:\My\Project\Path\bin\debug
\My.Project.DBMigrations.dll" -db SqlServer2008 -conn "Data Source=.\SQLEXPRESS2
008;Initial Catalog=myNewDbName;Integrated Security=SSPI" -version=2010090100000
0
Using Database SqlServer2008 and Connection String Data Source=.\SQLEXPRESS2008;
Initial Catalog=myNewDbName;Integrated Security=SSPI
-- VersionMigration: migrating ===============================================

-- CreateTable VersionInfo
-- VersionMigration: migrated
-- CreateProductionDbCircaSep2010: migrating =================================

-- ExecuteSqlScript C:\My\Project\Path\FluentMigrator.Net\..\Resources\test.sql
-- CreateProductionDbCircaSep2010: migrated

Yet no tables in the database - there isn't even the expected VersionInfo table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十年九夏 2024-10-09 23:24:35

经过对版本 0.8 和 1.0 的一些调查和测试后,这似乎是 FluentMigrator.Net 中与迁移运行程序的事务管理相关的错误 - 如果您使用特定版本号运行迁移,则永远不会调用事务的提交;如果不指定版本号也没关系。

编辑:

我已经向该项目提交了一个补丁,该补丁已被接受。这不再是问题。

After some investigation and testing with both version 0.8 and 1.0, it appears this is a bug in FluentMigrator.Net related to the migration runner's transaction management - if you run a migration with a specific version number, the transaction's commit never gets called; if you don't specify a version number, it's fine.

EDIT:

I have since submitted a patch to the project which was accepted. This is no longer a problem.

这听起来真的很愚蠢,但我在上面浪费了几个小时,所以它属于这里:

确保您的迁移类使用公共可见性。

如果没有这个,您的迁移将被完全忽略。

This sounds really stupid, but I wasted several hours on it, so it belongs here:

Make sure your migration classes use public visibility.

Without this, your migrations will be completely ignored.

浅唱ヾ落雨殇 2024-10-09 23:24:35

我发现如果你的 dbconnection 字符串为空,也会发生同样的情况。错误的数据库连接字符串:崩溃,未找到迁移标签:崩溃..但数据库字符串为空..除了查看并报告迁移有效之外,什么也不做。请注意。

I found that if your dbconnection string is null, this same will happen. Wrong db connstring:crash, no migration tags found: crash.. but null db string.. silently do nothing but look and report that migrations worked. Be warned.

韶华倾负 2024-10-09 23:24:35

老问题,但我会添加这个提示,因为这是谷歌引导我寻找问题的 stackoverflow 文章。也许它会对某人有所帮助。

您是否记得在擦除数据库时清除 VersionInfo 表?

如果您已删除数据库中的所有表,并期望该工具从头开始重新创建数据库,但它什么也没做,您可能忘记清除/删除 VersionInfo 表。然后,FluentMigrator 将查看 VersionInfo 表,找到已应用的所有内容,并正确地得出结论:绝对不执行任何操作。让我们说...咳咳...好朋友告诉我这个秘诀...

Old question, but I'll add this tip as this is the stackoverflow article google led me to searching for the problem. Maybe it will help someone.

Did you remember to clear the VersionInfo table when wiping the database?

If you have dropped all the tables in the database, and expect the tool to re-create the database from scratch, but it simply does nothing, you might have forgot to clear / drop the VersionInfo table. FluentMigrator will then look in the VersionInfo table, find the everything is applied, and correctly conclude to do absolutely nothing. Let's say a... ahem... good friend told me this tip...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文