迁移 Django 装置?
我有一个 Django 应用程序。我有包含测试数据的 .json 固定文件,以及使用数据确认应用程序正常工作的单元测试。我还使用 South 来迁移我的数据库。
在进行了几次数据库迁移后,我的固定装置已过时,因为数据库已迁移,例如添加了新的数据库列,而固定装置数据没有该列,因为它是在数据库更改之前捕获的。
当我迁移数据库时,推进我的赛程的最佳方法是什么?
I have a Django application. I have .json fixture files containing test data, with unit tests that use the data to confirm the application is working properly. I also use South to migrate my database.
After doing a few database migrations, my fixtures are out of date, because the database has migrated, adding a new database column, for example, and the fixture data doesn't have that column, since it was captured before the database changed.
What's the best way to move my fixtures forward as I migrate my database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我使用的过程:
将代码回滚到最初创建夹具的修订版。例如:
svn up -r12345
。清空数据库,然后使用
manage.pysyncdb --noinput --migrate创建它
使用
manage.py loaddata my_fixture.json
加载fixture使用
svn up
将代码前滚到现在使用
迁移数据库>manage.py migrate
使用
manage.py dumpdata --indent=2 myapp > 转储数据my_fixture.json
请注意,选择过去的修订版本时需要小心回滚到.就我而言,我最近需要进行一些修复,因此我实际上必须选择目录来回滚到特定修订。虽然很乏味,但比手动编辑 9,000 行 JSON 文件要好。
另外,在步骤 6 中,请确保转储正确的应用程序集。
将来,当我编写迁移时,我可以再次执行这些步骤以使所有装置保持最新。
Here's the process I used:
Roll back the code to the revision that created the fixture in the first place. For example:
svn up -r12345
.Empty the database, then create it with
manage.py syncdb --noinput --migrate
Load the fixture with
manage.py loaddata my_fixture.json
Roll the code forward to now, with
svn up
Migrate the database with
manage.py migrate
Dump the data with
manage.py dumpdata --indent=2 myapp >my_fixture.json
Note that you need to be careful when choosing the past revision to roll back to. In my case, I had some recent fixes that needed to be in place, so I actually had to pick and choose directories to roll back to specific revisions. Tedious, but better than hand-editing a 9,000-line JSON file.
Also, in step 6, be sure to dump the correct set of applications.
In the future, as I write migrations, I can do these steps again to keep all the fixtures up-to-date.
为什么不能简单地从数据库创建一个新的
.json
文件。当我需要创建一个新的装置时,我就是这么做的。Why can't you simply create a fresh
.json
file from your db. This is what I do when I need to create a new fixture.太晚了。
迁移数据库时,您需要
loaddata
和dumpdata
。一旦它停止工作,那就太晚了。
一个可能的后备方案是编写一个简短的脚本将 JSON 装置加载到内存中,
然后“手动”构建数据库对象。
有了这些线索,您也许就可以使用当前的架构和遗留设备构建数据库。
It's too late.
As you migrate your database you need to
loaddata
anddumpdata
.One it stops working, it's too late.
A possible fallback is to write a short script to load the JSON fixtures into memory,
and then "manually" build database objects.
With something along those lines, you might be able to construct a database using your current schema and legacy fixtures.