基于网络上的阅读,堆栈溢出,主要是 这些文章 关于DB版本的编码恐怖片链接的DB版本,我曾努力编写针对8年历史的PHP MySQL网站的数据库的计划。

Database Version Control plan
- Create a db as the "Master Database"
- Create a table db_version (id, script_name, version_number, author, comment, date_ran)   
- Create baseline script for schema+core data that creates this db from scratch, run this on Master Db
- Create a "test data" script to load any db with working data
- Modifications to the master db are ONLY to be made through the db versioning process
- Ensure everyone developing against the Master Db has a local db created by the baseline script
- Procedures for commiting and updating from the Master Db
    - Master Db Commit
        - Perform a schema diff between your local db and the master db
        - Perform a data diff on core data between your local db and master db
        - If there are changes in either or both cases, combine these changes into an update script
        - Collect the data to be added to a new row in db_version table, and add an insert for this into the script
            - new version number = latest master db version number +1
            - author
            - comment
        - The script must be named as changeScript_V.sql where V is the latest master db version +1
        - Run the script against the master db
        - If the script executed succesfully, add it to the svn repository
        - Add the new db_version record to your local db_version table      
    - Update from Master Db
        - Update your local svn checkout to have all the latest change scripts available
        - compares your local db_version table to the master db_version table to determine which change scripts to run
        - Run the required change scripts in order against your local db, which will also update your local db_version table

我的第一个问题是,这听起来正确吗?
我的第二个问题是,每天要做一次不止一次的提交过程似乎有些复杂。有没有办法可靠地自动化它?还是我不应该经常进行数据库更改以使其重要吗?

有帮助吗?

解决方案

看着您的建议,似乎并不是可行的也不是实用的。我在一家公司工作,我们在每个数据库(非常复杂的系统)中使用了超过1k的表,这一切都很好:

  • 有一个负责DB的人(让他称他为dbperson) - 每个脚本/DB更改都必须通过他。这将避免任何不必要的更改,并避免一些问题的“忽略”(例如,如果某人移动索引以更好地进行查询,那么HI可能会破坏其他人的工作,也许有人会创建一个完全多余且不必要的桌子, ETC...)。这将保持DB清洁和高效。即使对于一个人(或他的代理人)来说,这似乎太多了,实际上并非如此 - DB通常很少发生变化。
  • 每个脚本必须通过dbperson通过验证
  • 批准脚本后,DBPerson分配了一个数字,并将其放入“ Update”文件夹/SVN(...)中,并带有适当的编号(如您所建议的,例如增量数字)。
  • 接下来,如果您有一些连续的集成,则脚本将拾取并更新数据库(如果您没有连续集成,请手动进行)。
  • 请勿将整个数据库脚本存储在脚本中。代替存储实际数据库。如果您有解决方案的分支 - 每个分支都有其自己的数据库,或者始终可以将每个分支的更新脚本分开,以便您可以回滚/转发到另一个分支。但是,我真的建议为每个分支提供一个单独的DB。
  • 请始终使用一个数据库,始终具有默认数据(完整) - 对于单元测试,回归测试等的需求,每当您进行测试时,请在此数据库的副本上进行操作。您甚至可以在每晚使用主数据库(当然适当)对测试数据库进行清理。

在这样的环境中,您将拥有多个版本的数据库:

  • 开发人员数据库(本地) - 开发人员用来测试他的工作的数据库。他总是可以从主或测试主复制。
  • 主数据库 - 具有所有默认值的一个,如果您对新客户进行重新部署,则可能是半空的。
  • 测试主数据库 - 填充测试数据的主数据库。您在这里运行的所有脚本也在这里运行。
  • 测试在进行数据库中 - 从测试主复制并用于测试 - 在进行任何新测试之前会被覆盖。
  • 如果您有分支(每个客户端的类似数据库),则与上面的分支相同。

您肯定需要对此进行修改以符合您的情况,但是无论如何,我认为在可维护性,合并,更新等方面,保留整个数据库的创建脚本的文本版本是错误的...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top