将 MySQL 数据库置于版本控制之下?

2022-01-04 00:00:00 database mysql version-control

我目前在我的 PHP 项目中使用 SVN.我想我也应该让我的数据库处于版本控制之下,但最好的方法是什么?我是否只是在我的 SVN 项目中创建一个 db 文件夹,将 SQL 更改粘贴到名为 from_1.0_to_2.0.sql 的文件中并提交?

I'm currently using SVN for my PHP projects. I was thinking I should get my database under version control too, but what's the best way to do that? Do I just make a db-folder in my project in SVN, paste SQL changes into a file called from_1.0_to_2.0.sql and commit?

推荐答案

无论何时对数据库进行更改,都应将这些更改保存在迁移中,以便稍后在更新的同时在其他服务器上运行你的代码.但你基本上有正确的想法.您可以编写一些工具使其更加自动化;例如版本每个文件,然后在您的数据库中创建一个类似于 migration_version 的表,其中将包含数据库的当前版本.然后,您可以创建一个迁移脚本,该脚本将运行更新数据库所需的所有迁移.

Whenever you make changes to your database, you should save those changes in a migration, that you can then later on run on other servers at the same time you update your code. But you basically got the right idea. You can write some tools to make it more automated; for example version each file, and then create a table like migration_version in your database, which will contain the current version of the database. You can then create a migrate script that will run all the migrations required to get the database up-to-date.

请注意,如果您想要双向数据库版本控制(以便您也可以恢复到以前的数据库版本),您还需要为每个版本编写所需的查询.

Note that if you want bi-directional db versioning (so that you can revert back to previous db version too), you need to write the required queries for that too for each version.

还有一些工具可以帮助您编写迁移,例如 MySQLdiff

There are also some tools that can aid you in writing the migrations, such as MySQLdiff

相关文章