本文整理汇总了Python中django.db.migrations.executor.MigrationExecutor方法的典型用法代码示例。如果您正苦于以下问题:Python executor.MigrationExecutor方法的具体用法?Python executor.MigrationExecutor怎么用?Python executor.MigrationExecutor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.migrations.executor
的用法示例。
在下文中一共展示了executor.MigrationExecutor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: migrations
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def migrations(transactional_db):
"""
This fixture returns a helper object to test Django data migrations.
Based on: https://gist.github.com/bennylope/82a6088c02fefdd47e18f3c04ec167af
"""
class Migrator(object):
def migrate(self, app, to):
migration = [(app, to)]
executor = MigrationExecutor(connection)
executor.migrate(migration)
return executor.loader.project_state(migration).apps
def reset(self):
call_command("migrate", no_input=True)
return Migrator()
示例2: setUp
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def setUp(self):
assert (
self.migrate_from and self.migrate_to
), "TestCase '{}' must define migrate_from and migrate_to properties".format(
type(self).__name__
)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例3: check_migrations
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def check_migrations():
"""
Check the status of database migrations.
The koku API server is responsible for running all database migrations. This method
will return the state of the database and whether or not all migrations have been completed.
Hat tip to the Stack Overflow contributor: https://stackoverflow.com/a/31847406
Returns:
Boolean - True if database is available and migrations have completed. False otherwise.
"""
try:
connection = connections[DEFAULT_DB_ALIAS]
connection.prepare_database()
executor = MigrationExecutor(connection)
targets = executor.loader.graph.leaf_nodes()
return not executor.migration_plan(targets)
except OperationalError:
return False
示例4: check_migrations
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def check_migrations(self):
"""
Check the status of database migrations.
The koku API server is responsible for running all database migrations. This method
will return the state of the database and whether or not all migrations have been completed.
Hat tip to the Stack Overflow contributor: https://stackoverflow.com/a/31847406
Returns:
Boolean - True if database is available and migrations have completed. False otherwise.
"""
connection = connections[DEFAULT_DB_ALIAS]
connection.prepare_database()
executor = MigrationExecutor(connection)
targets = executor.loader.graph.leaf_nodes()
return not executor.migration_plan(targets)
示例5: setUp
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def setUp(self):
assert self.migrate_from and self.migrate_to, \
"TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__)
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例6: setUp
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def setUp(self) -> None:
assert self.migrate_from and self.migrate_to, \
f"TestCase '{type(self).__name__}' must define migrate_from and migrate_to properties"
migrate_from: List[Tuple[str, str]] = [(self.app, self.migrate_from)]
migrate_to: List[Tuple[str, str]] = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(migrate_from).apps
# Reverse to the original migration
executor.migrate(migrate_from)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(migrate_to)
self.apps = executor.loader.project_state(migrate_to).apps
示例7: handle
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def handle(self, *args, **options):
changed = set()
self.stdout.write("Checking...")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
sys.exit("Unable to check migrations: cannot connect to database\n")
autodetector = MigrationAutodetector(
executor.loader.project_state(), ProjectState.from_apps(apps),
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
changed -= set(options["ignore"])
if changed:
sys.exit(
"Apps with model changes but no corresponding migration file: %(changed)s\n"
% {"changed": list(changed)}
)
else:
sys.stdout.write("All migration files present\n")
示例8: setUp
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def setUp(self):
assert (
self.migrate_from and self.migrate_to
), "TestCase '{}' must define migrate_from and migrate_to properties".format(
type(self).__name__
)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
if self.migrate_fixtures:
self.load_fixtures(self.migrate_fixtures, apps=old_apps)
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例9: get_fake_model
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [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
示例10: setUp
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def setUp(self):
self.migrate_from = [self.migrate_from]
self.migrate_to = [self.migrate_to]
# Reverse to the original migration
executor = MigrationExecutor(connection)
executor.migrate(self.migrate_from)
old_apps = executor.loader.project_state(self.migrate_from).apps
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor.loader.build_graph()
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例11: test_apply_all_replaced_marks_replacement_as_applied
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def test_apply_all_replaced_marks_replacement_as_applied(self):
"""
Applying all replaced migrations marks replacement as applied (#24628).
"""
recorder = MigrationRecorder(connection)
# Place the database in a state where the replaced migrations are
# partially applied: 0001 is applied, 0002 is not.
recorder.record_applied("migrations", "0001_initial")
executor = MigrationExecutor(connection)
# Use fake because we don't actually have the first migration
# applied, so the second will fail. And there's no need to actually
# create/modify tables here, we're just testing the
# MigrationRecord, which works the same with or without fake.
executor.migrate([("migrations", "0002_second")], fake=True)
# Because we've now applied 0001 and 0002 both, their squashed
# replacement should be marked as applied.
self.assertIn(
("migrations", "0001_squashed_0002"),
recorder.applied_migrations(),
)
示例12: test_migrate_marks_replacement_applied_even_if_it_did_nothing
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def test_migrate_marks_replacement_applied_even_if_it_did_nothing(self):
"""
A new squash migration will be marked as applied even if all its
replaced migrations were previously already applied (#24628).
"""
recorder = MigrationRecorder(connection)
# Record all replaced migrations as applied
recorder.record_applied("migrations", "0001_initial")
recorder.record_applied("migrations", "0002_second")
executor = MigrationExecutor(connection)
executor.migrate([("migrations", "0001_squashed_0002")])
# Because 0001 and 0002 are both applied, even though this migrate run
# didn't apply anything new, their squashed replacement should be
# marked as applied.
self.assertIn(
("migrations", "0001_squashed_0002"),
recorder.applied_migrations(),
)
示例13: setUp
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def setUp(self):
assert self.migrate_from and self.migrate_to, \
"TestCase '{}' must define migrate_from and migrate_to properties".format(type(self).__name__)
self.migrate_from = [(self.app, self.migrate_from)]
self.migrate_to = [(self.app, self.migrate_to)]
executor = MigrationExecutor(connection)
old_apps = executor.loader.project_state(self.migrate_from).apps
# Reverse to the original migration
executor.migrate(self.migrate_from)
super(TestMigrations, self).setUp()
self.setUpBeforeMigration(old_apps)
# Run the migration to test
executor = MigrationExecutor(connection)
executor.loader.build_graph() # reload.
executor.migrate(self.migrate_to)
self.apps = executor.loader.project_state(self.migrate_to).apps
示例14: test_atomic_operation_in_non_atomic_migration
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def test_atomic_operation_in_non_atomic_migration(self):
"""
An atomic operation is properly rolled back inside a non-atomic
migration.
"""
executor = MigrationExecutor(connection)
with self.assertRaisesMessage(RuntimeError, "Abort migration"):
executor.migrate([("migrations", "0001_initial")])
migrations_apps = executor.loader.project_state(("migrations", "0001_initial")).apps
Editor = migrations_apps.get_model("migrations", "Editor")
self.assertFalse(Editor.objects.exists())
# Record previous migration as successful.
executor.migrate([("migrations", "0001_initial")], fake=True)
# Rebuild the graph to reflect the new DB state.
executor.loader.build_graph()
# Migrating backwards is also atomic.
with self.assertRaisesMessage(RuntimeError, "Abort migration"):
executor.migrate([("migrations", None)])
self.assertFalse(Editor.objects.exists())
示例15: test_unrelated_applied_migrations_mutate_state
# 需要导入模块: from django.db.migrations import executor [as 别名]
# 或者: from django.db.migrations.executor import MigrationExecutor [as 别名]
def test_unrelated_applied_migrations_mutate_state(self):
"""
#26647 - Unrelated applied migrations should be part of the final
state in both directions.
"""
executor = MigrationExecutor(connection)
executor.migrate([
('mutate_state_b', '0002_add_field'),
])
# Migrate forward.
executor.loader.build_graph()
state = executor.migrate([
('mutate_state_a', '0001_initial'),
])
self.assertIn('added', dict(state.models['mutate_state_b', 'b'].fields))
executor.loader.build_graph()
# Migrate backward.
state = executor.migrate([
('mutate_state_a', None),
])
self.assertIn('added', dict(state.models['mutate_state_b', 'b'].fields))
executor.migrate([
('mutate_state_b', None),
])