本文整理汇总了Python中django.db.migrations.Migration方法的典型用法代码示例。如果您正苦于以下问题:Python migrations.Migration方法的具体用法?Python migrations.Migration怎么用?Python migrations.Migration使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.migrations
的用法示例。
在下文中一共展示了migrations.Migration方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_fake_model
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def get_fake_model(fields=None, model_base=LocalizedModel, meta_options={}):
"""Creates a fake model to use during unit tests."""
model = define_fake_model(fields, model_base, meta_options)
class TestProject:
def clone(self, *_args, **_kwargs):
return self
@property
def apps(self):
return self
class TestMigration(migrations.Migration):
operations = [HStoreExtension()]
with connection.schema_editor() as schema_editor:
migration_executor = MigrationExecutor(schema_editor.connection)
migration_executor.apply_migration(
TestProject(), TestMigration("eh", "postgres_extra")
)
schema_editor.create_model(model)
return model
示例2: test_custom_operation
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_custom_operation(self):
migration = type(str("Migration"), (migrations.Migration,), {
"operations": [
custom_migration_operations.operations.TestOperation(),
custom_migration_operations.operations.CreateModel(),
migrations.CreateModel("MyModel", (), {}, (models.Model,)),
custom_migration_operations.more_operations.TestOperation()
],
"dependencies": []
})
writer = MigrationWriter(migration)
output = writer.as_string()
result = self.safe_exec(output)
self.assertIn("custom_migration_operations", result)
self.assertNotEqual(
result['custom_migration_operations'].operations.TestOperation,
result['custom_migration_operations'].more_operations.TestOperation
)
示例3: test_sorted_imports
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_sorted_imports(self):
"""
#24155 - Tests ordering of imports.
"""
migration = type(str("Migration"), (migrations.Migration,), {
"operations": [
migrations.AddField("mymodel", "myfield", models.DateTimeField(
default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc),
)),
]
})
writer = MigrationWriter(migration)
output = writer.as_string()
self.assertIn(
"import datetime\n"
"from django.db import migrations, models\n"
"from django.utils.timezone import utc\n",
output
)
示例4: test_custom_operation
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_custom_operation(self):
migration = type("Migration", (migrations.Migration,), {
"operations": [
custom_migration_operations.operations.TestOperation(),
custom_migration_operations.operations.CreateModel(),
migrations.CreateModel("MyModel", (), {}, (models.Model,)),
custom_migration_operations.more_operations.TestOperation()
],
"dependencies": []
})
writer = MigrationWriter(migration)
output = writer.as_string()
result = self.safe_exec(output)
self.assertIn("custom_migration_operations", result)
self.assertNotEqual(
result['custom_migration_operations'].operations.TestOperation,
result['custom_migration_operations'].more_operations.TestOperation
)
示例5: test_sorted_imports
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_sorted_imports(self):
"""
#24155 - Tests ordering of imports.
"""
migration = type("Migration", (migrations.Migration,), {
"operations": [
migrations.AddField("mymodel", "myfield", models.DateTimeField(
default=datetime.datetime(2012, 1, 1, 1, 1, tzinfo=utc),
)),
]
})
writer = MigrationWriter(migration)
output = writer.as_string()
self.assertIn(
"import datetime\n"
"from django.db import migrations, models\n"
"from django.utils.timezone import utc\n",
output
)
示例6: test_migration_file_header_comments
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_migration_file_header_comments(self):
"""
Test comments at top of file.
"""
migration = type("Migration", (migrations.Migration,), {
"operations": []
})
dt = datetime.datetime(2015, 7, 31, 4, 40, 0, 0, tzinfo=utc)
with mock.patch('django.db.migrations.writer.now', lambda: dt):
for include_header in (True, False):
with self.subTest(include_header=include_header):
writer = MigrationWriter(migration, include_header)
output = writer.as_string()
self.assertEqual(
include_header,
output.startswith(
"# Generated by Django %s on 2015-07-31 04:40\n\n" % get_version()
)
)
if not include_header:
# Make sure the output starts with something that's not
# a comment or indentation or blank line
self.assertRegex(output.splitlines(keepends=True)[0], r"^[^#\s]+")
示例7: get_fake_model
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def get_fake_model(fields=None, model_base=models.Model, options=None):
fields = fields if fields else {}
options = options if options else {}
"""Create fake model to use during unit tests."""
model = define_fake_model(fields, model_base, options)
class TestProject:
def clone(self, *_args, **_kwargs):
return self
class TestMigration(migrations.Migration):
operations = [HStoreExtension()]
with connection.schema_editor() as schema_editor:
migration_executor = MigrationExecutor(schema_editor.connection)
migration_executor.apply_migration(
TestProject(), TestMigration("caluma_extra", "caluma_core")
)
schema_editor.create_model(model)
return model
示例8: test_has_errors
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_has_errors(self):
linter = MigrationLinter(database="mysql")
self.assertFalse(linter.has_errors)
m = Migration("0001_create_table", "app_add_not_null_column")
linter.lint_migration(m)
self.assertFalse(linter.has_errors)
m = Migration("0002_add_new_not_null_field", "app_add_not_null_column")
linter.lint_migration(m)
self.assertTrue(linter.has_errors)
m = Migration("0001_create_table", "app_add_not_null_column")
linter.lint_migration(m)
self.assertTrue(linter.has_errors)
示例9: test_exclude_migration_tests
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def test_exclude_migration_tests(self):
m = Migration("0002_add_new_not_null_field", "app_add_not_null_column")
linter = MigrationLinter(exclude_migration_tests=[], database="mysql")
linter.lint_migration(m)
self.assertTrue(linter.has_errors)
linter = MigrationLinter(exclude_migration_tests=["NOT_NULL"], database="mysql")
linter.lint_migration(m)
self.assertFalse(linter.has_errors)
示例10: _filter_predicate
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def _filter_predicate(target: MigrationTarget, migration: Migration) -> bool:
# when ``None`` passed as migration name then initial migration from
# target's app will be chosen and handled properly in ``_get_index``
# so in final all target app migrations will be excluded from plan
index = 2 - (target[1] is None)
return (migration.app_label, migration.name)[:index] == target[:index]
示例11: backwards
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def backwards(apps, schema_editor):
"""
Migration nullified after squash of papers
"""
pass
示例12: fix_duplicate_attachments
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def fix_duplicate_attachments(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
"""Migration 0041 had a bug, where if multiple messages referenced the
same attachment, rather than creating a single attachment object
for all of them, we would incorrectly create one for each message.
This results in exceptions looking up the Attachment object
corresponding to a file that was used in multiple messages that
predate migration 0041.
This migration fixes this by removing the duplicates, moving their
messages onto a single canonical Attachment object (per path_id).
"""
Attachment = apps.get_model('zerver', 'Attachment')
# Loop through all groups of Attachment objects with the same `path_id`
for group in Attachment.objects.values('path_id').annotate(Count('id')).order_by().filter(id__count__gt=1):
# Sort by the minimum message ID, to find the first attachment
attachments = sorted(list(Attachment.objects.filter(path_id=group['path_id']).order_by("id")),
key = lambda x: min(x.messages.all().values_list('id')[0]))
surviving = attachments[0]
to_cleanup = attachments[1:]
for a in to_cleanup:
# For each duplicate attachment, we transfer its messages
# to the canonical attachment object for that path, and
# then delete the original attachment.
for msg in a.messages.all():
surviving.messages.add(msg)
surviving.is_realm_public = surviving.is_realm_public or a.is_realm_public
surviving.save()
a.delete()
示例13: migrate_daily_recurrences
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def migrate_daily_recurrences(apps, schema_editor):
"""
Migration to convert weekly recurrences into new complex recurrences
"""
Schedule = apps.get_model("schedules", "Schedule")
for schedule in Schedule.objects.all():
schedule.recurrences = recurrence.Recurrence(rrules=[recurrence.Rule(recurrence.WEEKLY, byday=(int(schedule.day), ))])
schedule.save()
示例14: rollback_base_playbook_name
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def rollback_base_playbook_name(apps, schema_editor):
"""
On rollback, warn that all custom playbook names will be lost.
The defaults, based on the openedx_release, can be recalculated using the
function above when the migration is performed.
"""
print('\nReverting this migration causes data loss...')
print('All customized playbook_names set will be lost!')
# Migration ###################################################################
示例15: populate_data_type_tags
# 需要导入模块: from django.db import migrations [as 别名]
# 或者: from django.db.migrations import Migration [as 别名]
def populate_data_type_tags(apps, schema_editor):
# Go through all of the Ingest models and convert the data_type string into an array of tags
update = 'UPDATE ingest SET data_type_tags = string_to_array(data_type,\',\') WHERE data_type <> \'\''
with connection.cursor() as cursor:
cursor.execute(update)
count = cursor.rowcount
if count:
print('%d entries updated with data type tags' % count)
print ('Migration finished.')