python类AlterField()的实例源码

test_migrations.py 文件源码 项目:django-boardinghouse 作者: schinckel 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_add_check_constraint(self):
        project_state = self.set_up_test_model()
        operation = migrations.AlterField(
            model_name='pony',
            name='pink',
            field=models.PositiveIntegerField(default=3)
        )
        new_state = project_state.clone()
        operation.state_forwards('tests', new_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'check')

        with connection.schema_editor() as editor:
            operation.database_forwards('tests', editor, project_state, new_state)

        self.assertConstraint('tests_pony', ['pink'], 'check')

        with connection.schema_editor() as editor:
            operation.database_backwards('tests', editor, new_state, project_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'check')
test_migrations.py 文件源码 项目:django-boardinghouse 作者: schinckel 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_add_unique_constraint(self):
        project_state = self.set_up_test_model()
        operation = migrations.AlterField(
            model_name='pony',
            name='pink',
            field=models.IntegerField(unique=True, default=3)
        )
        new_state = project_state.clone()
        operation.state_forwards('tests', new_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'unique')

        with connection.schema_editor() as editor:
            operation.database_forwards('tests', editor, project_state, new_state)

        self.assertConstraint('tests_pony', ['pink'], 'unique')

        with connection.schema_editor() as editor:
            operation.database_backwards('tests', editor, new_state, project_state)

        self.assertNoConstraint('tests_pony', ['pink'], 'unique')
test_hstore_autodetect.py 文件源码 项目:django-postgres-extra 作者: SectorLabs 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def test_uniqueness():
    """Tests whether changes in the `uniqueness`
    option are properly detected by the auto detector."""

    before = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField())
        ])
    ]
    after = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField(uniqueness=['en']))
        ])
    ]

    changes = detect_changes(before, after)

    assert_autodetector(changes, [
        migrations.AlterField(
            'Model1',
            'title',
            HStoreField(uniqueness=['en'])
        )
    ])
test_hstore_autodetect.py 文件源码 项目:django-postgres-extra 作者: SectorLabs 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_required():
    """Tests whether changes in the `required`
    option are properly detected by the auto detector."""

    before = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField())
        ])
    ]
    after = [
        migrations.state.ModelState('tests', 'Model1', [
            ('title', HStoreField(required=['en']))
        ])
    ]

    changes = detect_changes(before, after)

    assert_autodetector(changes, [
        migrations.AlterField(
            'Model1',
            'title',
            HStoreField(required=['en'])
        )
    ])
test_migrations.py 文件源码 项目:django-boardinghouse 作者: schinckel 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_alter_field(self):
        project_state = self.set_up_test_model()
        operation = migrations.AlterField('Pony', 'pink', models.IntegerField(null=True))
        new_state = project_state.clone()
        operation.state_forwards('tests', new_state)
        self.assertColumnNotNull('tests_pony', 'pink')
        with connection.schema_editor() as editor:
            operation.database_forwards('tests', editor, project_state, new_state)
        self.assertColumnNull('tests_pony', 'pink')
        with connection.schema_editor() as editor:
            operation.database_backwards('tests', editor, new_state, project_state)
        self.assertColumnNotNull('tests_pony', 'pink')
migrations.py 文件源码 项目:django-postgres-extra 作者: SectorLabs 项目源码 文件源码 阅读 44 收藏 0 点赞 0 评论 0
def alter_field(old_field, new_field, filters: List[str]):
    """Alters a field from one state to the other.

    Arguments:
        old_field:
            The field before altering it.

        new_field:
            The field after altering it.

        filters:
            List of strings to filter
            SQL statements on.
    """

    model = define_fake_model({'title': old_field})
    project = migrations.state.ProjectState.from_apps(apps)

    with connection.schema_editor() as schema_editor:
        execute_migration(schema_editor, [
            migrations.CreateModel(
                model.__name__,
                fields=[
                    ('title', old_field.clone())
                ]
            )
        ], project)

    with filtered_schema_editor(*filters) as (schema_editor, calls):
        execute_migration(schema_editor, [
            migrations.AlterField(
                model.__name__,
                'title',
                new_field
            )
        ], project)

    yield calls
optimizer.py 文件源码 项目:django-wechat-api 作者: crazy-canux 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def reduce_alter_field_rename_field(self, operation, other, in_between):
        if (operation.model_name_lower == other.model_name_lower and
                operation.name_lower == other.old_name_lower):
            return [
                other,
                migrations.AlterField(
                    model_name=operation.model_name,
                    name=other.new_name,
                    field=operation.field,
                ),
            ]


问题


面经


文章

微信
公众号

扫码关注公众号