本文整理汇总了Python中django.db.migrations.state.ProjectState.from_apps方法的典型用法代码示例。如果您正苦于以下问题:Python ProjectState.from_apps方法的具体用法?Python ProjectState.from_apps怎么用?Python ProjectState.from_apps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.db.migrations.state.ProjectState
的用法示例。
在下文中一共展示了ProjectState.from_apps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_total_deconstruct
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def test_total_deconstruct(self):
loader = MigrationLoader(None, load=True, ignore_no_migrations=True)
loader.disk_migrations = {t: v for t, v in loader.disk_migrations.items() if t[0] != 'testapp'}
app_labels = {"testapp"}
questioner = NonInteractiveMigrationQuestioner(specified_apps=app_labels, dry_run=True)
autodetector = MigrationAutodetector(
loader.project_state(),
ProjectState.from_apps(apps),
questioner,
)
# Detect changes
changes = autodetector.changes(
graph=loader.graph,
trim_to_apps=app_labels or None,
convert_apps=app_labels or None,
migration_name="my_fake_migration_for_test_deconstruct",
)
self.assertGreater(len(changes), 0)
for app_label, app_migrations in changes.items():
for migration in app_migrations:
# Describe the migration
writer = MigrationWriter(migration)
migration_string = writer.as_string()
self.assertNotEqual(migration_string, "")
示例2: handle
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [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")
示例3: patched_project_state
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def patched_project_state():
"""Patches the standard Django :see:ProjectState.from_apps for the duration
of the context.
The patch intercepts the `from_apps` function to control
how model state is creatd. We want to use our custom
model state classes for certain types of models.
We have to do this because there is no way in Django
to extend the project state otherwise.
"""
from_apps_module_path = "django.db.migrations.state"
from_apps_class_path = f"{from_apps_module_path}.ProjectState"
from_apps_path = f"{from_apps_class_path}.from_apps"
with mock.patch(from_apps_path, new=project_state_from_apps):
yield
示例4: add_field
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def add_field(field, filters: List[str]):
"""Adds the specified field to a model.
Arguments:
field:
The field to add to a model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model()
state = migrations.state.ProjectState.from_apps(apps)
apply_migration([migrations.CreateModel(model.__name__, fields=[])], state)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AddField(model.__name__, "title", field)], state
)
yield calls
示例5: run
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def run(self, *args, **kwargs):
changed = set()
log.info("Checking DB migrations")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError:
log.critical("Unable to check migrations, cannot connect to database")
sys.exit(1)
autodetector = MigrationAutodetector(
executor.loader.project_state(), ProjectState.from_apps(apps)
)
changed.update(autodetector.changes(graph=executor.loader.graph).keys())
if changed:
log.critical(
"Apps with model changes but no corresponding "
f"migration file: {list(changed)}"
)
sys.exit(1)
else:
log.info("All migration files present")
示例6: test_for_missing_migrations
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def test_for_missing_migrations(self):
"""Checks if there're models changes which aren't reflected in migrations."""
migrations_loader = MigrationExecutor(connection).loader
migrations_detector = MigrationAutodetector(
from_state=migrations_loader.project_state(),
to_state=ProjectState.from_apps(apps)
)
if migrations_detector.changes(graph=migrations_loader.graph):
self.fail(
'Your models have changes that are not yet reflected '
'in a migration. You should add them now.'
)
示例7: test__migrations
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def test__migrations(self):
app_labels = set(app.label for app in apps.get_app_configs()
if app.name.startswith('wagtail.'))
for app_label in app_labels:
apps.get_app_config(app_label.split('.')[-1])
loader = MigrationLoader(None, ignore_no_migrations=True)
conflicts = dict(
(app_label, conflict)
for app_label, conflict in loader.detect_conflicts().items()
if app_label in app_labels
)
if conflicts:
name_str = "; ".join("%s in %s" % (", ".join(names), app)
for app, names in conflicts.items())
self.fail("Conflicting migrations detected (%s)." % name_str)
autodetector = MigrationAutodetector(
loader.project_state(),
ProjectState.from_apps(apps),
MigrationQuestioner(specified_apps=app_labels, dry_run=True),
)
changes = autodetector.changes(
graph=loader.graph,
trim_to_apps=app_labels or None,
convert_apps=app_labels or None,
)
if changes:
migrations = '\n'.join((
' {migration}\n{changes}'.format(
migration=migration,
changes='\n'.join(' {0}'.format(operation.describe())
for operation in migration.operations))
for (_, migrations) in changes.items()
for migration in migrations))
self.fail('Model changes with no migrations detected:\n%s' % migrations)
示例8: test_for_missing_migrations
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def test_for_missing_migrations(self):
"""Checks if there're models changes which aren't reflected in migrations."""
migrations_loader = MigrationExecutor(connection).loader
migrations_detector = MigrationAutodetector(
from_state=migrations_loader.project_state(),
to_state=ProjectState.from_apps(apps),
)
if migrations_detector.changes(graph=migrations_loader.graph):
self.fail(
"Your models have changes that are not yet reflected "
"in a migration. You should add them now."
)
示例9: handle
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def handle(self, *args, **kwargs):
changed = set()
ignore_list = ['authtools'] # dependencies that we don't care about migrations for (usually for testing only)
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())
for ignore in ignore_list:
if ignore in changed:
changed.remove(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")
示例10: test_for_missing_migrations
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def test_for_missing_migrations(self):
"""Checks if there're models changes which aren't reflected in migrations."""
current_models_state = ProjectState.from_apps(apps)
# skip tracking changes for TestModel
current_models_state.remove_model("tests", "testmodel")
migrations_loader = MigrationExecutor(connection).loader
migrations_detector = MigrationAutodetector(
from_state=migrations_loader.project_state(), to_state=current_models_state
)
if migrations_detector.changes(graph=migrations_loader.graph):
self.fail(
"Your models have changes that are not yet reflected "
"in a migration. You should add them now."
)
示例11: migrate
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def migrate(self, ops, state=None):
class Migration(migrations.Migration):
operations = ops
migration = Migration('name', 'tests')
inject_trigger_operations([(migration, False)])
with connection.schema_editor() as schema_editor:
return migration.apply(state or ProjectState.from_apps(self.apps), schema_editor)
示例12: apply_migration
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def apply_migration(operations, state=None, backwards: bool = False):
"""Executes the specified migration operations using the specified schema
editor.
Arguments:
operations:
The migration operations to execute.
state:
The state state to use during the
migrations.
backwards:
Whether to apply the operations
in reverse (backwards).
"""
state = state or migrations.state.ProjectState.from_apps(apps)
class Migration(migrations.Migration):
pass
Migration.operations = operations
migration = Migration("migration", "tests")
executor = MigrationExecutor(connection)
if not backwards:
executor.apply_migration(state, migration)
else:
executor.unapply_migration(state, migration)
return migration
示例13: make_migration
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def make_migration(app_label="tests", from_state=None, to_state=None):
"""Generates migrations based on the specified app's state."""
app_labels = [app_label]
loader = MigrationLoader(None, ignore_no_migrations=True)
loader.check_consistent_history(connection)
questioner = NonInteractiveMigrationQuestioner(
specified_apps=app_labels, dry_run=False
)
autodetector = MigrationAutodetector(
from_state or loader.project_state(),
to_state or ProjectState.from_apps(apps),
questioner,
)
changes = autodetector.changes(
graph=loader.graph,
trim_to_apps=app_labels or None,
convert_apps=app_labels or None,
migration_name="test",
)
changes_for_app = changes.get(app_label)
if not changes_for_app or len(changes_for_app) == 0:
return None
return changes_for_app[0]
示例14: alter_db_table
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
def alter_db_table(field, filters: List[str]):
"""Creates a model with the specified field and then renames the database
table.
Arguments:
field:
The field to include into the
model.
filters:
List of strings to filter
SQL statements on.
"""
model = define_fake_model()
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AlterModelTable(model.__name__, "NewTableName")], state
)
yield calls
示例15: alter_field
# 需要导入模块: from django.db.migrations.state import ProjectState [as 别名]
# 或者: from django.db.migrations.state.ProjectState import from_apps [as 别名]
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})
state = migrations.state.ProjectState.from_apps(apps)
apply_migration(
[
migrations.CreateModel(
model.__name__, fields=[("title", old_field.clone())]
)
],
state,
)
with filtered_schema_editor(*filters) as calls:
apply_migration(
[migrations.AlterField(model.__name__, "title", new_field)], state
)
yield calls