使用Git来跟踪mysql模式 - 一些问题 [英] Using Git to track mysql schema - some questions

查看:124
本文介绍了使用Git来跟踪mysql模式 - 一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是建议?



我可以问一些关于如何跟踪mysql模式版本的git命令例子吗?



我们应该使用另一个那么我们通常在我们的应用程序根目录中使用的那个存储库?



我应该使用一种叫做hook的东西吗?

更新:

我们浏览到.git数据库所在的项目根目录。


$ b 2)我们创建一个名为hooks的子文件夹。



3)我们在名为db-commit的文件中加入类似内容:

 #!/ bin / sh 
mysqldump -u DBUSER -pDBPASSWORD DATABASE --no-data = true> SQLVersionControl / vc.sql
git add SQLVersionControl / vc.sql
exit 0

现在我们可以:

4) git commit -m



这个提交将包含一个已经在提交之前运行的mysql模式转储。



上面的源代码在这里:
http://edmondscommerce.github.io/git/using-git-to-track-db-schema-changes-with-git-hook.html



如果这是可以接受的方式,我可以请耐心的人逐行评论,并尽可能详细地说明这里发生了什么:

 #!/ bin / sh 
mysqldump -u DBUSER -pDBPASSWORD DATABASE --no-data = true> SQLVersionControl / vc.sql
git add SQLVersionControl / vc.sql
exit 0

非常感谢。

解决方案

假设您已经有了一个git仓库,可以在shell脚本中执行以下操作: p>

 #!/ bin / bash -e 
#-e表示任何命令失败时退出
DBHOST = dbhost .yourdomain.com
DBUSER = dbuser
DBPASS = dbpass#以更安全的方式执行此操作
DBNAME = dbname
GITREPO = / path / to / git / repo
cd $ GITREPO
mysqldump -h $ DBHOST -u $ DBUSER -p $ DBPASS -d $ DBNAME> $ GITREPO / schema.sql#-d标志表示无数据
git add schema.sql
git commit -m$ DBNAME模式版本$(`date`)
git push#假设你有一个远程推送到

然后从cron每天启动这个脚本工作或你有什么。



编辑:通过在$ gitdir / hooks / pre -commit(名字很重要)中放置脚本,该脚本将在每次提交之前执行。这样,每次提交都会捕获数据库模式的状态,这是有道理的。如果您每次提交时都自动运行这个sql脚本,那么您会丢掉数据库,这是没有意义的。

 # !/ bin / sh 

这一行指定它是一个shell脚本。

  mysqldump -u DBUSER -pDBPASSWORD DATABASE --no-data = true> SQLVersionControl / vc.sql 

这与我在上面的回答中一样;

  git add SQLVersionControl / vc.sql 

这会将SQL文件添加到对存储库进行的每次提交中。

  exit 0 

这会成功退出脚本。这可能是危险的。如果 mysqldump git add 失败,您可能会吹走您想要保留的内容。


If this is recommended ?

Can I ask some git command examples about how to track versions of mysql schema?

Should we use another repository other then the one we normally use on our application root ?

Should I use something called hook ?

Update:

1) We navigate onto our project root where .git database resides.

2) We create a sub folder called hooks.

3) We put something like this inside a file called db-commit:

   #!/bin/sh
   mysqldump -u DBUSER -pDBPASSWORD  DATABASE --no-data=true> SQLVersionControl/vc.sql
   git add SQLVersionControl/vc.sql
   exit 0

Now we can:

4) git commit -m

This commit will include a mysql schema dump that has been run just before the commit.

The source of the above is here: http://edmondscommerce.github.io/git/using-git-to-track-db-schema-changes-with-git-hook.html

If this is an acceptable way of doing it, can I please ask someone with patience to comment line by line and with as much detail as possible, what is happening here:

#!/bin/sh
mysqldump -u DBUSER -pDBPASSWORD  DATABASE --no-data=true> SQLVersionControl/vc.sql
git add SQLVersionControl/vc.sql
exit 0

Thanks a lot.

解决方案

Assuming you have a git repo already, do the following in a shell script or whatever:

#!/bin/bash -e
# -e means exit if any command fails
DBHOST=dbhost.yourdomain.com
DBUSER=dbuser
DBPASS=dbpass # do this in a more secure fashion
DBNAME=dbname
GITREPO=/path/to/git/repo
cd $GITREPO
mysqldump -h $DBHOST -u $DBUSER -p$DBPASS -d $DBNAME > $GITREPO/schema.sql # the -d flag means "no data"
git add schema.sql
git commit -m "$DBNAME schema version $(`date`)"
git push # assuming you have a remote to push to

Then start this script on a daily basis from a cron job or what have you.

EDIT: By placing a script in $gitdir/hooks/pre-commit (the name is important), the script will be executed before every commit. This way the state of the DB schema is captured for each commit, which makes sense. If you automatically run this sql script every time you commit, you will blow away your database, which does not make sense.

#!/bin/sh

This line specifies that it's a shell script.

mysqldump -u DBUSER -pDBPASSWORD  DATABASE --no-data=true> SQLVersionControl/vc.sql

This is the same as in my answer above; taking the DDL only from the database and storing it in a file.

git add SQLVersionControl/vc.sql

This adds the SQL file to every commit made to your repository.

exit 0

This exits the script with success. This is possibly dangerous. If mysqldump or git add fails, you may blow away something you wanted to keep.

这篇关于使用Git来跟踪mysql模式 - 一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆