Testing django application with several legacy databases

前端 未结 1 1247
一整个雨季
一整个雨季 2021-01-13 03:07

I have django application with 5 legacy databases. Almost all models are set with the meta attribute managed=False. Since managed=False is set, mig

相关标签:
1条回答
  • 2021-01-13 03:48

    Here is how I solved my problem for now.

    Migrations that are created with managed=False option look like this:

    # migrations/0001_initial.py
    migrations.CreateModel(
                name='MyModel',
                fields=[
                    ('field_id', models.IntegerField(primary_key=True, serialize=False)),
                    ('slug', models.CharField(max_length=20, unique=True)),
                    ('name', models.CharField(max_length=64)),
                ],
                options={
                    'db_table': 'MyModel',
                    'managed': False,
                },
            ),
    

    One needs to comment out 'managed': False to allow migrations to be applied. In order not to mess with actual migrations, I have created folder test_migrations and copied there my migrations with 'managed': False commented out:

    # test_migrations/0001_initial.py
    migrations.CreateModel(
                name='MyModel',
                fields=[
                    ('field_id', models.IntegerField(primary_key=True, serialize=False)),
                    ('slug', models.CharField(max_length=20, unique=True)),
                    ('name', models.CharField(max_length=64)),
                ],
                options={
                    'db_table': 'MyModel',
                    # 'managed': False,
                },
            ),
    

    Then we need to refer to these migrations during test run. To do that, I have created settings file test.py and put there necessary references. Like this:

    from web_services.settings.dev import *
    
    
    MIGRATION_MODULES = {
        'myapp': 'web_services.apps.myapp.test_migrations',
    }
    

    And when running tests, you need to refer to that settings:

    python manage.py test --settings=web_services.settings.test

    0 讨论(0)
提交回复
热议问题