1

我在 Heroku 上托管一个网站,并使用 SQLite 数据库。

问题是我希望能够从存储库中提取数据库(主要用于备份),但是每当我提交和推送更改到存储库时,永远不应该更改数据库。这是因为我本地计算机上的数据库中可能包含完全不同(且不相关)的数据;这是一个测试数据库。

解决这个问题的最佳方法是什么?我曾尝试将我的数据库添加到.gitignore文件中,但这会导致数据库完全取消版本化,使我无法在需要时提取它。

4

3 回答 3

1

While git (just like most other version control systems) supports tracking binary files like databases, it only does it best for text files. In other words, you should never use version control system to track constantly changing binary database files (unless they are created once and almost never change).

One popular method to still track databases in git is to track text database dumps. For example, SQLite database could be dumped into *.sql file using sqlite3 utility (subcommand .dump). However, even when using dumps, it is only appropriate to track template databases which do not change very often, and create binary database from such dumps using scripts as part of standard deployment.

于 2013-07-24T10:15:00.683 回答
0

您对版本控制感兴趣的是您的数据库架构吗?但是确保您不对其中的数据进行版本控制?

我会从 git 中排除您的数据库(使用.gitignore 文件)。

如果您正在使用 ORM 和迁移(例如 Active Record),那么您的架构已经在您的代码中被跟踪并且可以重新创建。

但是,如果您不是,那么您可能想要获取数据库的副本,然后保存创建语句并对其进行版本控制。

Heroku 不推荐在生产环境中使用 SQLite,而是使用他们的 Postgres 系统。这使您可以对远程数据库执行许多任务。

如果您想从 Heroku 中提取实时数据库,Postgres 备份的说明可能会有所帮助。

于 2013-10-15T13:45:06.053 回答
0

您可以在本地存储库中添加一个pre-commit挂钩,这将取消暂存您不想推送的任何文件。

例如将以下内容添加到.git/hooks/pre-commit

git reset ./file/to/database.db

在处理您的代码(可能会修改您的数据库)时,您将在某个时候结束:

$ git status --porcelain
 M file/to/database.db
 M src/foo.cc
$ git add .
$ git commit -m "fixing BUG in foo.cc"
M  file/to/database.db
.
[master 12345] fixing BUG in foo.cc
 1 file changed, 1 deletion(-)
$ git status --porcelain
 M file/to/database.db

所以你永远不会意外地提交对你的database.db

于 2013-07-24T10:56:15.717 回答