简体   繁体   English

在自动构建之前部署/运行数据库更改

[英]Deploy/run database changes before automatic build

I am using SQL Server and Visual studio. 我正在使用SQL Server和Visual Studio。 I have an automatic build via CCNET. 我通过CCNET自动构建。 When I commit my code in SVN the automatic build runs and all unit tests runs with mocked/stubbed data. 当我在SVN中提交代码时,自动构建运行并且所有单元测试都使用模拟/存根数据运行。

The only thing that not goes automatic now is running the database changes/scripts, I do that by hand. 现在唯一不自动的是运行数据库更改/脚本,我手动完成。 Anybody got tips how this can be autometed? 任何人都有提示如何自动化?

Self evidently you need to automate running of your database change scripts - the challenge there being how do you decide what to run. 显然,你需要自动运行数据库更改脚本 - 这就是你如何决定运行什么的挑战。 The answer there is that you need to store the schema version in the database (or in the database metadata) and choose what scripts to run accordingly - whether that be to bring a database up from scratch or to apply appropriate changes. 答案是,您需要将模式版本存储在数据库(或数据库元数据)中,并选择要相应运行的脚本 - 无论是从头开始使用数据库还是应用适当的更改。

My solution is to put my database schema maintenance entirely in code, I think this is the best version of the writeup I've done so far: 我的解决方案是将我的数据库架构维护完全放在代码中,我认为这是我迄今为止所做的最好的写入版本:

How to create "embedded" SQL 2008 database file if it doesn't exist? 如果不存在,如何创建“嵌入式”SQL 2008数据库文件?

Given code smart enough to do upgrade the schema either in-line in your application or in its own console app you should be able to integrate automating schema changes into your CI build scripts. 如果代码足够智能,可以在应用程序中或在自己的控制台应用程序中内联升级架构,那么您应该能够将自动架构更改集成到CI构建脚本中。

I've been chasing around this a bit further since I wrote the above - it turns out that what I'm doing is not dissimilar to Ruby Migrations (I believe something similar also features in Subsonic) excepting that I don't currently have any "down" capability to reverse a schema change (an interesting idea I have concerns with having potentially destructive code "live"). 自从我编写上述内容以来,我一直在追逐这一点 - 事实证明,我所做的与Ruby Migrations(我相信在Subsonic中也有类似功能)并没有什么不同,除了我目前没有“向下”能够扭转架构变化(一个有趣的想法,我担心潜在的破坏性代码“直播”)。 There is an open source .NET project Migrator.net that looks useful, but I'm not sure how actively its being maintained. 有一个看起来很有用的开源.NET项目Migrator.net ,但我不确定它的维护有多积极。


Addendum 附录

Since I wrote this entity framework code first has appeared and they're starting to make headway with migrations - so that would now be worth a look. 自从我写这个实体框架代码以来,它首先出现了,并且它们开始在迁移方面取得进展 - 所以现在值得一看。 However I don't think its quite there yet and like several other options I've seen its not simple. 但是我不认为它已经存在,并且像我看到的其他几个选项并不简单。 I need to package mine up and make it available! 我需要把我打包并让它可用!

There are many ways you could tackle this one. 有很多方法可以解决这个问题。 An approach I have used in a number of companies I have documented here , basically it's source code to handle the task of automating your database updates and some explanations of the hows and whys. 我在这里记录的许多公司中使用过的方法,基本上它是处理自动化数据库更新任务的源代码,以及对hows和whys的一些解释。

I'd also recommend checking out Data Dude (just Google it), I haven't used it but my understanding is that it can be used to automate db updates (could be wrong). 我还建议查看Data Dude(只是谷歌),我没有使用它,但我的理解是它可以用于自动化db更新(可能是错误的)。

I would say that I don't recommend using a database compare tool to generate scripts...without extreme care being taken. 我会说我不建议使用数据库比较工具来生成脚本......不需要特别小心。 Problem is usually you'd compare your dev db (where your changes are) with say the build box db, but your db may contain (and likely will) other changes you have made for 'work in progress' bug fixes you may be working on. 问题通常是您将dev db(您的更改所在位置)与构建框db进行比较,但您的数据库可能包含(并且可能会)您为“正在进行的工作”所做的其他更改错误修复您可能正在工作上。

Anyway like I said, there are many ways to do it, investigate a few options and pick the ones that best suits the style of you and your team. 无论如何,就像我说的那样,有很多方法可以做,调查一些选项并选择最适合你和你的团队风格的选项。

Happy automating :-) 快乐自动化:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM