如何测试数据库迁移?

发布于 2024-08-23 09:40:49 字数 722 浏览 8 评论 0原文

我正在使用 Migrator.NET 为应用程序编写数据库迁移。马克-安德烈·库诺耶写道:

就像您应用程序中的任何代码一样 必须测试您的迁移。涨跌代码。做你的一部分 持续构建过程并进行测试 在尽可能多的不同数据库上 尽可能的环境。

我该怎么做?假设我有创建表的 Up() 方法和删除同一个表的 Down() 方法,并且我正在使用 SQL Server。测试会是什么样子?我是否应该对系统表运行 SQL 查询(例如 select * from sys.columns)来检查表是否已创建以及它是否具有正确的结构?如果我们使用 NHibernate 怎么办?

编辑 我指的是 Rails ActiveRecord Migrations 意义上的迁移(基于 C# 代码以小步骤创建、修改和拆除数据库)。

编辑2 这里就是我的位置了解我们应该测试迁移。该博客文章实际上是从 Migrator 的 wiki 链接的。

I'm using Migrator.NET to write database migrations for the application. Marc-André Cournoyer wrote:

Like any code in your application you
must test your migrations. Ups and downs code. Do it part of your
continuous build process and test it
on as many different databases and
environment as you can.

How do I do that? Say I have the Up() method which creates a table and the Down() method which drops the same table and I'm using SQL Server. How would a test look like? Should I be running SQL query against the system tables, like select * from sys.columns, to check if the table was created and that it has the proper structure? What if we're using NHibernate?

EDIT
I mean migrations in the Rails ActiveRecord Migrations sense (creating, modifying and tearing down databases in small steps based on C# code).

EDIT 2
And here's where I read about that we should test migrations. The blog post is actually linked from Migrator's wiki.

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

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

发布评论

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

评论(6

甲如呢乙后呢 2024-08-30 09:40:49

你测试你的 DAL - 某种集成测试吗?

您需要的不仅仅是迁移脚本,还需要基线脚本。当您想要测试数据库升级时,您应该在测试/登台服务器上运行基线中的所有脚本以创建最新版本的数据库。然后根据最新的测试数据库测试您的 DAL。如果所有 DAL 测试都成功,那么您的迁移应该已经成功(否则您的 DAL 测试不够完整)。

这是一项运行成本昂贵的测试,但它几乎坚如磐石。我个人承认目前很多工作都是手动完成的;我们有一个内部迁移工具,可以应用所有脚本(包括基线),因此测试数据库设置和 DAL 测试是单独的步骤。但它确实有效。如果您想确保创建了一个表,没有比实际尝试向其中插入数据更好的方法了!

您可以尝试通过查看系统目录和 INFORMATION_SCHEMA 视图等来验证结果,但最终确保其实际工作的唯一方法是尝试 >使用新对象。仅仅因为对象在那里并不意味着它们具有功能。

Do you test your DAL - some sort of integration test?

You need more than a migration script, you also need a baseline script. When you want to test a database upgrade, you should run all the scripts from the baseline on a testing/staging server to create the newest version of the database. Then test your DAL against the up-to-date test database. If all the DAL tests succeed then your migration should have been successful (otherwise your DAL tests are not complete enough).

It's an expensive test to run, but it's pretty much rock solid. I'll personally admit to doing a lot of this manually at the moment; we have an in-house migration tool that will apply all scripts (including the baseline), so the test database setup and DAL tests are separate steps. It works though. If you want to make sure that a table was created, there's no better method than to actually try to insert data into it!

You can try to verify the results by looking at system catalogs and INFORMATION_SCHEMA views and so on, but ultimately the only way to be sure it's actually working is to try to use the new objects. Just because the objects are there doesn't mean that they're functional.

羞稚 2024-08-30 09:40:49

也许这篇文章可以帮助您:

http://www.benzzon .se/forum/uploads/benzzon/2006-03-27_134824_sp_CompareDB.txt

此脚本比较两个数据库。(结构和数据)

maybe this scrip can help you :

http://www.benzzon.se/forum/uploads/benzzon/2006-03-27_134824_sp_CompareDB.txt

this script compare two db.(structure and data)

无人问我粥可暖 2024-08-30 09:40:49

源代码控制用于获取当前代码库的快照。迁移是将数据库的更改从一个版本移动到下一个版本。因此,在未来的某个时刻,您可以使用旧数据库,应用迁移并使用最新的代码库。

我从未见过实际的迁移测试。我已经看到了测试的结果,他们已经抓住/提醒我运行最新的迁移。

describe User do
  it { should have_column :name, :type => :string }
  it { should validate_presence_of :name}       
end

于是有人改变了模型。添加测试来反映模型。添加迁移。然后提交源代码。
您获取最新的并运行测试。测试失败,因为数据库不对应。您记得运行迁移,然后重新运行测试。成功。

Source control is for taking a snapshot of your current code base. Migration is for moving changing your database from one version to the next. So that at some future point you can take an old database, apply migrations and work with the latest code base.

I've never seen the actual migrations tested. I have seen the results tested, and they have caught/reminded me to run the latest migrations.

describe User do
  it { should have_column :name, :type => :string }
  it { should validate_presence_of :name}       
end

So someone changes the model. Adds a test to reflect the model. Adds the migration. Then commits the source.
You grab the latest, run tests. Tests fail because the database doesn't correspond. You remember to run migrations, then rerun tests. Success.

不交电费瞎发啥光 2024-08-30 09:40:49

如果使用 NHibernate,请将迁移测试视为整体持久性测试策略的一部分,即,如果您可以创建并保存所有实体而没有任何错误,则您的数据库映射应该是正确的。

Treat migrations testing as part of your overall persistence testing strategy if using NHibernate, i.e. if you can create and save all of your entities without any errors, your database and your mappings should be correct.

嘦怹 2024-08-30 09:40:49

您可以对数据库系统对象进行比较,但您需要有一个比较目标 - 否则您如何知道是否通过或失败?

我认为您最好创建一组边缘情况 CRUD 操作测试用例来练习数据层中的实体或操作。如果其中任何一个失败,则数据库与所需内容不同步。即,如果字段 char(20) 的插入失败,因为数据库中只有 char(15)。然后可以进行数据库结构比较,看看是否关闭。

您可以通过仅关注最近更改的项目并假设已应用先前的更改来缩短此过程。

You COULD do a comparison of database system objects, but you would need to have a target against which to compare - otherwise how would you know if passed or failed?

I think you may be better off creating a set of edge case CRUD operation test cases that exercise the entities or operations in the data layer. If any of these fail, the database is not in sync with what is required. i.e. if the insert of an field char(20) fails because it is only char(15) in the database. Then the db structure comparison can be done to see what if off.

You may be able to short circuit this by focusing only on the recently changed items, and assuming prior changes have been applied.

二智少女猫性小仙女 2024-08-30 09:40:49

我也在寻找这个问题的答案。我认为这应该在集成环境而不是单元测试环境中进行测试:对于单元测试(DAL),我删除数据库并重新创建它。

但是,理想情况下,我希望有一个集成环境,我的数据库是从生产复制的,并且数据库迁移脚本以两种方式运行:
向上确保生产顺利升级,向下确保可以回滚。

I'm looking for an answer to this as well. I think this should be tested in an integration environment rather than a unit test one: For unit tests (DAL) I drop the database and re-create it.

However, ideally I'd like to have an integration environment were my DB is replicated from production and DB migration scripts run both ways:
Upwards to ensure a smooth upgrade of production and Downwards to ensure rollbacks are possible.

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