简体   繁体   English

跟踪对数据库所做的更改

[英]Track changes made to a database

Background: 背景:

I have a MS SQL Server database and I want to track changes to it. 我有一个MS SQL Server数据库,我想跟踪它的更改。 For example if a column needed to be added or removed or a table needed to be dropped. 例如,如果需要添加或删除列或需要删除表。 Something similar to Version control for regular code. 与常规代码的版本控制类似。

The problem: 问题:

While looking around I saw that there were some tools that can be used: 环顾四周时,我发现可以使用一些工具:

  • RedGate SQL Source Control RedGate SQL源代码控制
  • Visual Studio Database project Visual Studio数据库项目

I am more interested in knowing if either of these tools will track changes to my database? 我对知道这两个工具中的任何一个是否可以跟踪对我的数据库的更改更感兴趣。 More specifically I have a TFS server that is the source control for my MVC code, can I use either of these with TFS? 更具体地说,我有一个TFS服务器,它是我的MVC代码的源代码控制,我可以将这两个服务器与TFS一起使用吗? Will it allow us to restore from older versions? 它将允许我们从旧版本还原吗? Will it allow multiple developers to work on the database simultaneously? 它将允许多个开发人员同时处理数据库吗?

For this type of work, ApexSQL Source Control shown to be all that you need. 对于此类工作,ApexSQL源代码控制显示为您所需要的。 With this SSMS add-in you can work directly on a database, and all of your changes will be tracked in real time . 使用此SSMS加载项,您可以直接在数据库上工作,并且所有更改都将实时跟踪

Yes, several developers can work in the same time on the same database. 是的,几个开发人员可以在同一数据库上同时工作。 When one developer works on a one or several objects, other developers can see which those objects are, and until the first one does not finish changing the others cannot change that object, they will not be allowed to. 当一个开发人员处理一个或多个对象时,其他开发人员可以看到那些对象,并且直到第一个对象完成更改之前,其他开发人员无法更改该对象,否则将不允许它们。 If by any case, object is changed wrong, previous version or any earlier version can be restored at any moment. 无论如何,如果对象更改错误,则可以随时还原以前的版本或任何早期版本。

This add-in has all necessary options and features to allow the developers to work without losing time for checking changes made against object, since the add-in does that for them. 该外接程序具有所有必要的选项和功能,以使开发人员可以在不浪费时间检查对对象所做的更改的情况下工作,因为该外接程序会为他们执行此操作。 And you can always see by whom, when and what that change is. 而且,您始终可以看到更改的对象,时间和对象。

Being in the database version control space for 5 years (as director of product management at DBmaestro ) and having worked as a DBA for over two decades, I can tell you the simple fact that you cannot treat the database objects as you treat your Java, C# or other files and save the changes in simple DDL scripts. 我已经在数据库版本控制领域工作了5年(担任DBmaestro产品管理总监 ),并且已经担任DBA超过20年了,我可以告诉你一个简单的事实,即当您处理Java时,您不能对待数据库对象, C#或其他文件,并将更改保存在简单的DDL脚本中。

There are many reasons and I'll name a few: 原因有很多,我仅举几例:

  • Files are stored locally on the developer's PC and the change s/he makes do not affect other developers. 文件存储在本地开发人员的PC上,所做的更改不会影响其他开发人员。 Likewise, the developer is not affected by changes made by her colleague. 同样,开发人员不受同事所做更改的影响。 In database this is (usually) not the case and developers share the same database environment, so any change that were committed to the database affect others. 在数据库中(通常)不是这种情况,开发人员共享同一数据库环境,因此提交给数据库的任何更改都会影响其他更改。
  • Publishing code changes is done using the Check-In / Submit Changes / etc. (depending on which source control tool you use). 使用签入/提交更改/等完成发布代码更改(取决于您使用的源代码管理工具)。 At that point, the code from the local directory of the developer is inserted into the source control repository. 此时,来自开发人员本地目录的代码将插入到源代码控制存储库中。 Developer who wants to get the latest code need to request it from the source control tool. 想要获取最新代码的开发人员需要从源代码控制工具中请求它。 In database the change already exists and impacts other data even if it was not checked-in into the repository. 在数据库中,即使没有将其检入到存储库中,更改也已经存在并且会影响其他数据。
  • During the file check-in, the source control tool performs a conflict check to see if the same file was modified and checked-in by another developer during the time you modified your local copy. 在文件签入期间,源代码管理工具将执行冲突检查,以查看在修改本地副本期间同一文件是否已被其他开发人员修改和签入。 Again there is no check for this in the database. 同样,在数据库中对此没有检查。 If you alter a procedure from your local PC and at the same time I modify the same procedure with code form my local PC then we override each other's changes. 如果您在本地PC上更改了某个过程,同时又用本地PC上的代码修改了同一过程,那么我们将覆盖彼此的更改。
  • The build process of code is done by getting the label / latest version of the code to an empty directory and then perform a build – compile. 通过将代码的标签/最新版本保存到一个空目录中,然后执行构建–编译来完成代码的构建过程。 The output are binaries in which we copy & replace the existing. 输出是二进制文件,我们在其中复制并替换现有文件。 We don't care what was before. 我们不在乎以前是什么。 In database we cannot recreate the database as we need to maintain the data! 在数据库中,我们无法重新创建数据库,因为我们需要维护数据! Also the deployment executes SQL scripts which were generated in the build process. 部署还将执行在生成过程中生成的SQL脚本。
  • When executing the SQL scripts (with the DDL, DCL, DML (for static content) commands) you assume the current structure of the environment match the structure when you create the scripts. 当执行SQL脚本(使用DDL,DCL,DML(用于静态内容)命令)时,您假定环境的当前结构与创建脚本时的结构相匹配。 If not, then your scripts can fail as you are trying to add new column which already exists. 如果不是这样,那么当您尝试添加已经存在的新列时,脚本可能会失败。
  • Treating SQL scripts as code and manually generating them will cause syntax errors, database dependencies errors, scripts that are not reusable which complicate the task of developing, maintaining, testing those scripts. 将SQL脚本视为代码并手动生成它们会导致语法错误,数据库依赖项错误,不可重用的脚本,这些使开发,维护,测试这些脚本的任务变得复杂。 In addition, those scripts may run on an environment which is different from the one you though it would run on. 此外,这些脚本可能会在与您运行的环境不同的环境中运行。
  • Sometimes the script in the version control repository does not match the structure of the object that was tested and then errors will happen in production! 有时,版本控制存储库中的脚本与测试对象的结构不匹配,从而在生产中会发生错误!

There are many more, but I think you got the picture. 还有很多,但我想您已经知道了。

What I found that works is the following: 我发现有效的方法如下:

  1. Use an enforced version control system that enforces check-out/check-in operations on the database objects. 使用强制版本控制系统,该系统对数据库对象执行检出/检入操作。 This will make sure the version control repository matches the code that was checked-in as it reads the metadata of the object in the check-in operation and not as a separated step done manually. 这将确保版本控制存储库在签入操作中读取对象的元数据时,与被签入的代码匹配,而不是作为手动完成的单独步骤。 This also allow several developers to work in parallel on the same database while preventing them to accidently override each other code. 这还允许多个开发人员在同一个数据库上并行工作,同时防止他们意外地覆盖彼此的代码。
  2. Use an impact analysis that utilize baselines as part of the comparison to identify conflicts and identify if a change (when comparing the object's structure between the source control repository and the database) is a real change that origin from development or a change that was origin from a different path and then it should be skipped, such as different branch or an emergency fix. 使用影响分析,该分析利用基线作为比较的一部分,以识别冲突并确定更改(在比较源代码管理存储库和数据库之间的对象结构时)是源自开发的真实更改还是源自开发的更改其他路径,然后应跳过该路径,例如其他分支或紧急修复。

An article I wrote on this was published here , you are welcome to read it. 我在此发表的一篇文章已发表在这里 ,欢迎您阅读。

If you're looking for a product that will track changes into TFS from your SQL Server automatically, I'd invite you take a look at our product, Sql Historian . 如果您正在寻找一种产品,该产品将自动从您的SQL Server跟踪到TFS中的更改,我邀请您来看看我们的产品Sql Historian It's different from most other SQL version control systems (including the ones you've listed) in that it does not require developers to perform a check-in ritual to synchronize version control with what's already committed to the db. 它与大多数其他SQL版本控制系统(包括您列出的版本)不同,因为它不需要开发人员执行签入仪式以将版本控制与已提交给数据库的内容同步。

However, features common with Sql Historian and the other two systems you mention are: working with TFS, the ability to view older versions of your db objects, and allowing multiple users on the db at the same time. 但是,Sql Historian和您提到的其他两个系统具有的共同功能是:使用TFS,查看数据库对象的旧版本的功能以及允许多个用户同时在数据库上使用。

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

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