数据库开发的持续集成 - Sql Server数据库结构比较
本系列文章目录
数据库开发的持续集成 - Sql Server 部署升级工具
数据库开发的持续集成 - Sql Server数据库结构比较
数据库开发的持续集成 - 方法和流程
数据库开发的持续集成 - Liquibase的简介和应用
数据库的持续集成 - CruiseControl.Net的项目配置
上回说到了数据库开发的持续集成的总的意图,提供一个数据库部署和升级的工具,接下来说说如何进行数据库比较。
在我的开发中数据库比较工具主要需要两种形式: 桌面工具和MsBuid任务。
桌面工具推荐开源的DaBCoS3,基本够用,有朋友推荐直接用VS2005,但我比较喜欢小巧一点的东东,因为不仅仅在数据库开发的时候用。
MsBuild任务很重要,在持续集成的时候可用来验证升级脚本是否正常工作。在CC.Net中加入SqlDeply任务(MsBuild),对产品系统数据库的副本进行升级,然后使用MsBuild数据库比较任务比较升级后的结构与开发数据库是否相同。以此来验证升级脚本,相当于对升级脚本做自动测试(当然还有回归测试),可大大节约人力。
在网上搜寻一大圈,发现Red Gate提供的程序集不错,但要$,放弃。绕了一大圈,回到M$,PowerTools被选中。要先装Team Edition for Database。
数据结构比较的MsBuild任务如此例:
<Project DefaultTargets="SchemaCompare" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="SqlSchemaCompareTask" AssemblyName="Microsoft.VisualStudio.TeamSystem.Data.PowerTools.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<PropertyGroup>
<UpdateSql/>
</PropertyGroup>
<Target Name ="SchemaCompare">
<SqlSchemaCompareTask
SourceConnectionString="server=product;user id=sa;password=mypass"
SourceDatabaseName="Northwind"
TargetConnectionString="Data Source=.;Integrated Security=True;Pooling=False"
TargetDatabaseName="Northwind"
OutputPath= "."
OutputFileName = "update.sql"
ForceColumnOrder="true"
IgnoreExtendedProperties="true"
IgnoreStatistics="true"
IgnoreConstraintNames="true"
IgnoreQuotedIdentifiersAndAnsiNullSettings="true"
IgnoreTriggerOrder="true"
IgnoreUsers="true"
IgnoreWhiteSpace="true"
DoNotOutputCommentHeader="true"
NoTransactionalChangeScript="true"
SkipSETStatements="true"
ScriptCollationWhenDifferentFromDefault="true"
/>
<ReadLinesFromFile File="update.sql">
<Output TaskParameter="Lines" PropertyName="UpdateSql" />
</ReadLinesFromFile>
<!-- NOTE: If update.sql is emtpy, the two databases are same -->
<Message Text="$(UpdateSql)" Condition="'$(UpdateSql)' != ''" />
<Error Text="Soure database is different from the target" Condition="'$(UpdateSql)' != ''" />
</Target>
</Project>
注意:此例中SqlSchemaComapreTask的属性目前已优化,用以确保在数据库相同时产生的update.sql是空的,并以此来判断两个数据库是否相等。这个任务用来产生升级脚本的,这里只用于比较两个数据库是否结构相同。<UsingTask TaskName="SqlSchemaCompareTask" AssemblyName="Microsoft.VisualStudio.TeamSystem.Data.PowerTools.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<PropertyGroup>
<UpdateSql/>
</PropertyGroup>
<Target Name ="SchemaCompare">
<SqlSchemaCompareTask
SourceConnectionString="server=product;user id=sa;password=mypass"
SourceDatabaseName="Northwind"
TargetConnectionString="Data Source=.;Integrated Security=True;Pooling=False"
TargetDatabaseName="Northwind"
OutputPath= "."
OutputFileName = "update.sql"
ForceColumnOrder="true"
IgnoreExtendedProperties="true"
IgnoreStatistics="true"
IgnoreConstraintNames="true"
IgnoreQuotedIdentifiersAndAnsiNullSettings="true"
IgnoreTriggerOrder="true"
IgnoreUsers="true"
IgnoreWhiteSpace="true"
DoNotOutputCommentHeader="true"
NoTransactionalChangeScript="true"
SkipSETStatements="true"
ScriptCollationWhenDifferentFromDefault="true"
/>
<ReadLinesFromFile File="update.sql">
<Output TaskParameter="Lines" PropertyName="UpdateSql" />
</ReadLinesFromFile>
<!-- NOTE: If update.sql is emtpy, the two databases are same -->
<Message Text="$(UpdateSql)" Condition="'$(UpdateSql)' != ''" />
<Error Text="Soure database is different from the target" Condition="'$(UpdateSql)' != ''" />
</Target>
</Project>
数据库数据比较的MsBuild任务如此例:
<Project DefaultTargets="DataCompare" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="SqlDataCompareTask" AssemblyName="Microsoft.VisualStudio.TeamSystem.Data.PowerTools.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<Target Name ="DataCompare">
<SqlDataCompareTask
SourceConnectionString="server=product;user id=sa;password=mypass"
SourceDatabaseName="Northwind"
TargetConnectionString="Data Source=.;Integrated Security=True;Pooling=False"
TargetDatabaseName="Northwind"
OutputPath = "."
OutputFileName = "TestDataCompare.sql"
TrimTrailingSpaces="true"
DisableTriggers="true"
DisableKeys="false"
DoNotOutputCommentHeader="true"
DoNotUseTransactions="true" />
</Target>
</Project>
<UsingTask TaskName="SqlDataCompareTask" AssemblyName="Microsoft.VisualStudio.TeamSystem.Data.PowerTools.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<Target Name ="DataCompare">
<SqlDataCompareTask
SourceConnectionString="server=product;user id=sa;password=mypass"
SourceDatabaseName="Northwind"
TargetConnectionString="Data Source=.;Integrated Security=True;Pooling=False"
TargetDatabaseName="Northwind"
OutputPath = "."
OutputFileName = "TestDataCompare.sql"
TrimTrailingSpaces="true"
DisableTriggers="true"
DisableKeys="false"
DoNotOutputCommentHeader="true"
DoNotUseTransactions="true" />
</Target>
</Project>
Update 20060617
最终我选定了Java下的liquibase作为数据库比较、升级、部署的工具,已应用在数据库持续集成开发项目中(参见方法和流程),感觉不错,唯一缺憾就是暂时还不支持.Net