在 MySQL 数据库上使用版本控制 (Git)

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

我是一名 WordPress 设计师/开发人员,他越来越多地参与使用版本控制,尤其是 Git,尽管我确实在某些项目中使用了 SVN.我目前正在将 Beanstalk 用于我的远程存储库.

I am a WordPress Designer/Developer, who is getting more and more heavily involved with using version control, notably Git, though I do use SVN for some projects. I am currently using Beanstalk for my remote repo.

将所有 WordPress 文件添加到我的 repo 没有问题,如果我想知道我可以 .gitignore wp-config 文件,但由于我是目前唯一的开发人员,而且这些项目是封闭源代码,真的没什么意义.

Adding all of the WordPress files to my repo is no problem, if I wanted to I know I could .gitignore the wp-config file, but since I'm the only developer, currently, and these projects are closed source, it really makes little sense.

WordPress 与任何 CMS 一样严重依赖数据库来保留文本内容和许多设置,具体取决于我使用的特定插件/主题配置.我想知道在数据库上使用版本控制的最佳方式是什么,如果可能的话.我想我可以做一个 SQL 转储,虽然我的 MySQL 服务器在 Windows 上运行(读作:我不知道如何做),然后将 SQL 转储添加到我的存储库中.但是当我推送一些东西时,就会带来巨大的安全威胁.

WordPress relies heavily on the database, as any CMS does, to keep textual content, and many settings depending on the specific plugin/theme configuration I'm using. I'm wondering what the best way of using version control on the database would be, if it's even possible. I guess I could do a SQL dump, though my MySQL server is running on Windows (read as: I don't know how to do it), and then add the SQL dump to my repository. But when I push something live, that poses huge security threats.

是否有公认的做法?

推荐答案

您可以在 git 存储库中备份您的数据库.当然,如果您将数据以二进制形式放入 git 中,您将失去 git 使用差异(更改)有效存储数据的所有能力.因此,第一个最佳实践是:以文本序列化格式存储数据.

You can backup your database within a git repository. Of course, if you place the data into git in a binary form, you will lose all of git's ability to efficiently store the data using diffs (changes). So the number one best practice is this: store the data in a text serialised format.

mysqldump 是一个合适的程序来帮助你做到这一点.虽然它并不完美.如果有任何事情扰乱了项目的序列化顺序(例如,由于创建新表等),那么人为中断将进入差异.这会降低存储效率.您可以编写一个自定义序列化程序来仅序列化更改——但是您正在做 git 已经擅长的艰苦工作.只需使用 sql 转储.

mysqldump is a suitable program to help you do this. It isn't perfect though. If anything disturbs the serialisation order of items (eg. as a result of creating new tables, etc.) then artificial breaks will enter into the diff. That will decrease the efficiency of storage. You could write a custom serialiser to serialise changes only -- but then you are doing the hard work that git is already good at. Just use the sql dump.

话虽如此,您想要做的并不是开发人员谈论将数据库放入 git 时通常的意思.例如,如果您阅读@eggyal 发布的链接(link到codinghorror)你会看到git中实际放置的是生成初始数据库所需的脚本.可能还有其他脚本,例如用干净状态填充数据库数据或用测试数据填充数据库数据的脚本.所有这些 sql 脚本都是文本文件,并且与您从 mysqldump 获得的 sql 转储格式几乎相同.因此,您没有理由不能对日常数据也这样做.

That being said, what you are wanting to do isn't what devs normally mean when they talk about putting the database in git. For instance, if you read the link posted by @eggyal (link to codinghorror) you will see that what is actually placed in git are the scripts needed to generate the initial database. There may be additional scripts, like those to populate the database data with a clean state, or to populate it with testing data. All such sql scripts are text files, and pretty much the same format as the sql dump you would get from mysqldump. So there's no reason you can't do it that way with your day-to-day data as well.

相关文章