迁移 Django 装置?

发布于 2024-09-29 05:00:38 字数 201 浏览 9 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

榆西 2024-10-06 05:00:38

这是我使用的过程:

  1. 将代码回滚到最初创建夹具的修订版。例如:svn up -r12345

  2. 清空数据库,然后使用manage.pysyncdb --noinput --migrate创建它

  3. 使用manage.py loaddata my_fixture.json加载fixture

  4. 使用 svn up 将代码前滚到现在

  5. 使用 迁移数据库>manage.py migrate

  6. 使用 manage.py dumpdata --indent=2 myapp > 转储数据my_fixture.json

请注意,选择过去的修订版本时需要小心回滚到.就我而言,我最近需要进行一些修复,因此我实际上必须选择目录来回滚到特定修订。虽然很乏味,但比手动编辑 9,000 行 JSON 文件要好。

另外,在步骤 6 中,请确保转储正确的应用程序集。

将来,当我编写迁移时,我可以再次执行这些步骤以使所有装置保持最新。

Here's the process I used:

  1. Roll back the code to the revision that created the fixture in the first place. For example: svn up -r12345.

  2. Empty the database, then create it with manage.py syncdb --noinput --migrate

  3. Load the fixture with manage.py loaddata my_fixture.json

  4. Roll the code forward to now, with svn up

  5. Migrate the database with manage.py migrate

  6. 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.

鸵鸟症 2024-10-06 05:00:38

为什么不能简单地从数据库创建一个新的 .json 文件。当我需要创建一个新的装置时,我就是这么做的。

python manage.py dumpdata <your_app> auth > test_data.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.

python manage.py dumpdata <your_app> auth > test_data.json
在风中等你 2024-10-06 05:00:38

在迁移数据库时,推进我的赛程的最佳方式是什么?

太晚了。

迁移数据库时,您需要loaddatadumpdata

一旦它停止工作,那就太晚了。

一个可能的后备方案是编写一个简短的脚本将 JSON 装置加载到内存中,
然后“手动”构建数据库对象。

with open( "somefile.json", "r" ) as data:
    for obj in json.load( data ):
        if obj['model'] == 'someapp.somemodel':
            SomeNewModel.objects.create( 
                field = obj['fields']['element']
                ...
                )

有了这些线索,您也许就可以使用当前的架构和遗留设备构建数据库。

What's the best way to move my fixtures forward as I migrate my database?

It's too late.

As you migrate your database you need to loaddata and dumpdata.

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 open( "somefile.json", "r" ) as data:
    for obj in json.load( data ):
        if obj['model'] == 'someapp.somemodel':
            SomeNewModel.objects.create( 
                field = obj['fields']['element']
                ...
                )

With something along those lines, you might be able to construct a database using your current schema and legacy fixtures.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文