阻止Django SQLite数据库在推送到Heroku时被覆盖 [英] Prevent Django SQLite db from being overwritten while pushing to Heroku
问题描述
场景:
我有一个数据库文件 my_db
哪个是远程的现在,当推送到heroku时,我只是 git添加
> git commit
只有具有更改而不是整个的文件项目。我的问题在于,它以某种方式仍然用本地数据覆盖远程数据库。
有没有办法防止这种情况?
Heroku 不提供持久性文件系统。
我曾经使用过的Heroku应用程序使用PostgreSQL作为数据库,所以这不是一个问题。但是SQLite只是一个位于某个目录的文件,所以每次部署数据库都将丢失。
最简单的解决方案可能是从SQLite迁移到PostgreSQL,这在Heroku(和Django)中得到很好的支持,每次部署时都不会丢失数据。
如果您坚定地致力于SQLite,您可能还会有其他选项:
- 在每次推送之前,先将数据库文件备份回来,然后将其还原。
- 将您的生产数据库添加到您的Git存储库,因此它将与您的应用程序一起部署(请注意,我不推荐这个)。
- 许多用户使用Amazon S3存储其他类型的资产。您可能可以使用类似的过程与您的数据库,虽然我怀疑将有一些非常重要的安全风险这样做。
I've got a Django application that is storing a large amount of data in it's models. The problem is, whenever I deploy to Heroku even if it's a small change, the remote database with the correct data gets overwritten with the local database of dummy data.
Scenario:
I have a db file my_db
which is remote. Now, when pushing to heroku, I just git add
> git commit
only the files with the changes rather than the whole project. My problem lies in the fact that, it somehow still overwrites remote database with local data.
Is there a way to prevent this?
Heroku does not provide a persistent filesystem.
Most Heroku applications that I have worked on use PostgreSQL for their database, so this isn't a problem. But SQLite is just a file sitting in a directory somewhere, so every time you deploy your database will be lost.
The easiest solution is probably to migrate from SQLite to PostgreSQL, which is very well supported on Heroku (and in Django) and will not lose data every time you deploy.
If you're firmly committed to SQLite you may have some other options:
- Back your database file up before each push and restore it after your push.
- Add your production database to your Git repository, so it will be deployed along with your application (note that I don't recommend this).
- Many users use Amazon S3 to store other types of assets. You may be able to use a similar procedure with your database, though I suspect there will be some very significant security risks doing so.
这篇关于阻止Django SQLite数据库在推送到Heroku时被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!