doableware / djongo

Django and MongoDB database connector
https://www.djongomapper.com
GNU Affero General Public License v3.0
1.89k stars 355 forks source link

ALTER_QUERY::FIXED:: Also rename __schema__ document for renamed collection #688

Open frikilax opened 10 months ago

frikilax commented 10 months ago

Description

When doing a migration including RenameModel() operations, collections are correctly reamed, but not their corresponding schema document, resulting in out-of-sync database information for fields and auto-incremental primary IDs

How to reproduce

  1. Create a first migration creating a Model, with at least a primary id, and apply it:
    ...
        migrations.CreateModel(
              name='mymodel',
              fields=[
                  ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
              ],
          ),
    ...
  2. Check in MongoDB that the model has a corresponding schema document:
    db.getCollection('__schema__').find({name: {$regex: "mymodel"}})
    { "_id" : ObjectId("65952a1101314dfd724e8d92"), "name" : "test_mymodel", "auto" : { "field_names" : [ "id" ], "seq" : 0 }, "fields" : { "id" : { "type_code" : "int" } } }
  3. Create a second migration to rename the model, and apply it:
    ...
        migrations.RenameModel(
            old_name="mymodel",
            new_name="mynewmodel",
        )
    ...
  4. Check MongoDB's schema collection again, and see that the document "name" value wasn't updated:
    db.getCollection('__schema__').find({name: {$regex: "mymodel"}})
    { "_id" : ObjectId("65952a1101314dfd724e8d92"), "name" : "test_mymodel", "auto" : { "field_names" : [ "id" ], "seq" : 0 }, "fields" : { "id" : { "type_code" : "int" } } }

Implications

After launching the 2 migrations, schema document is not synced with the latest Model name, so new Model instances are created without the incremental ID value

ELDiablO59152 commented 10 months ago

Good catch