def check_migration_names(*args, **kwargs) -> List[CheckMessage]: """ Finds automatic names in available migrations. We use nested import here, because some versions of django fails otherwise. They do raise: ``django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.`` """ from django.db.migrations.loader import MigrationLoader # noqa: WPS433 loader = MigrationLoader(None, ignore_no_migrations=True) loader.load_disk() messages = [] ignores = _build_ignores() for app_label, migration_name in loader.disk_migrations.keys(): if _is_ignored(app_label, migration_name, ignores): continue if fnmatch(migration_name, '????_auto_*'): messages.append( Warning( 'Migration {0}.{1} has an automatic name.'.format( app_label, migration_name, ), hint=('Rename the migration to describe its contents, ' + "or if it's from a third party app, add to " + _SETTINGS_NAME), id='{0}.E001'.format(CHECK_NAME), ), ) return messages
def test_loading_package_without__file__(self): """ To support frozen environments, MigrationLoader loads migrations from regular packages with no __file__ attribute. """ test_module = import_module("migrations.test_migrations") loader = MigrationLoader(connection) # __file__ == __spec__.origin or the latter is None and former is # undefined. module_file = test_module.__file__ module_origin = test_module.__spec__.origin module_has_location = test_module.__spec__.has_location try: del test_module.__file__ test_module.__spec__.origin = None test_module.__spec__.has_location = False loader.load_disk() migrations = [ name for app, name in loader.disk_migrations if app == "migrations" ] self.assertCountEqual(migrations, ["0001_initial", "0002_second"]) finally: test_module.__file__ = module_file test_module.__spec__.origin = module_origin test_module.__spec__.has_location = module_has_location
def test_loading_namespace_package(self): """Migration directories without an __init__.py file are ignored.""" loader = MigrationLoader(connection) loader.load_disk() migrations = [ name for app, name in loader.disk_migrations if app == "migrations" ] self.assertEqual(migrations, [])
def test_ignore_files(self): """Files prefixed with underscore, tilde, or dot aren't loaded.""" loader = MigrationLoader(connection) loader.load_disk() migrations = [ name for app, name in loader.disk_migrations if app == "migrations" ] self.assertEqual(migrations, ["0001_initial"])
def test_load_module_file(self): migration_loader = MigrationLoader(connection) with override_settings( MIGRATION_MODULES={ "migrations": "migrations.faulty_migrations.file" }): migration_loader.load_disk()
def test_load_empty_dir(self): migration_loader = MigrationLoader(connection) with override_settings( MIGRATION_MODULES={ "migrations": "migrations.faulty_migrations.namespace" }): migration_loader.load_disk()
def _gather_all_migrations(): from django.db.migrations.loader import MigrationLoader migration_loader = MigrationLoader(connection=None, load=False) migration_loader.load_disk() # Prune Django apps for (app_label, _), migration in migration_loader.disk_migrations.items(): if app_label not in DJANGO_APPS_WITH_MIGRATIONS: yield migration
def test_load_import_error(self): migration_loader = MigrationLoader(connection) with override_settings( MIGRATION_MODULES={ "migrations": "migrations.faulty_migrations.import_error" }): with self.assertRaises(ImportError): migration_loader.load_disk()
def test_ignore_files(self): """Files prefixed with underscore, tilde, or dot aren't loaded.""" loader = MigrationLoader(connection) loader.load_disk() migrations = [name for app, name in loader.disk_migrations if app == 'migrations'] self.assertEqual(migrations, ['0001_initial'])
def test_load_empty_dir(self): migration_loader = MigrationLoader(connection) with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.namespace"}): migration_loader.load_disk()
def test_load_module_file(self): migration_loader = MigrationLoader(connection) with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.file"}): migration_loader.load_disk()
def test_load_import_error(self): migration_loader = MigrationLoader(connection) with override_settings(MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.import_error"}): with self.assertRaises(ImportError): migration_loader.load_disk()