SQL Server 数据库架构版本控制和更新
对于我的应用程序,我必须支持更新方案,并且数据库可能会受到影响。
我希望能够从旧版本更新到最新版本,而无需安装中间版本。例如,假设我有版本 A(最旧的版本)、B(中间版本)和 C(新版本)。我希望能够将版本 A 直接更新到版本 C。对于应用程序文件,这很简单,我只需用新文件替换旧文件即可。然而,对于数据库,我不希望生成 SQL 脚本来将数据库架构从 A 直接更改为 C,而是我想首先应用一个脚本将架构从 A 更改为 B,从 B 更改为 C。
我如何存储SQL Server 数据库的数据库版本?我可以设置任何特殊属性,而不是实现版本表吗?在我的代码(.NET)中,我想读取数据库版本,并相应地以正确的顺序执行更新 SQL 脚本。
我将同时使用 SQL Server 2005 和 SQL Server 2008。
For my application I have to support update scenarios and the database might be affected.
I want to be able to update from an old version to the newest without installing intermediate versions. E.g. Suppose I have version A (the oldest), B (intermediate) and C (new version). I want to be able to update version A straight to version C. For the application files this is simple, I just replace the old with the new ones. However for the database I do not wish to generate a SQL Script to change the database schema from A straight to C, instead I want first apply a script to change the schema from A to B and from B to C.
How can I store the database version for a SQL Server database? Is there any special property which I can set, instead of implementing a version table? In my code (.NET) I want to read the database version and accordingly to execute the update SQL scripts in the proper order.
I will use both SQL Server 2005 and SQL Server 2008.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以添加一个注册当前版本的表。然后运行所有升级脚本,但脚本的编写方式应确保如果当前版本高于脚本预期的版本,则脚本不会执行。
这样,数据库模式的当前版本是什么就无关紧要了。脚本将仅对架构进行尚未进行的更改。
在这里您可以找到一些可以显着减少编写升级脚本的工作量的存储过程:如何维护SQL Server 数据库架构版本
You can add a table which registers current version. Then run all upgrade scripts but scripts should be written in such way that they do not execute if current version is higher than version expected by the script.
In that way, it is irrelevant what is the current version of the database schema. Scripts will make only changes that were not already made to the schema.
Here you can find some stored procedures that significantly reduce effort writing upgrade scripts: How to Maintain SQL Server Database Schema Version
我更喜欢使用纯 SQL 查询进行 SQL 架构迁移,而且我首先不喜欢 EF 代码,但如果您没有数据库版本控制,这并不容易,如果您使用 .net,则有一个免费的 NuGet 包将控制您的数据库版本,并且它可以使用普通的 SQL 查询。
https://www.nuget.org/packages/SQLMigrationByQuery
I prefer use plain SQL query for SQL schema migration and i'm not a fan of EF code first but it's not easy if you don't have a database version control, If you use .net there is a NuGet Package which is free and will control your database version and it work with plain SQL query.
https://www.nuget.org/packages/SQLMigrationByQuery
我使用数据库扩展属性,请参阅版本控制和您的数据库:
...
I use database extended properties, see Version Control and your Database:
...
插入一个版本表,它简单,有效,而且在过去,嗯,十多年来,它对我来说一直是一种享受。
我写这个是为了回答另一个问题 这里
Stick a version table in, its simple, its effective, and for the past, erm, well its more than 10 years it has worked a treat for me.
I wrote this up in response to another question here
这取决于您使用的 SQL Server 版本。
我不认为数据库版本是数据库的内置属性,但是您可以使用扩展属性。
就我个人而言,我会创建自己的版本表,因为这将允许您将更多元数据与其关联。
This depends on your version of SQL Server you're using.
I don't believe a database version is a built in property of a database, however you can probably use an extended property.
Personally, I'd create my own version table, as this will allow you to associate more metadata with it.