def _install_baseline(app, latest_version, using, verbosity): """Install baselines for an app. This goes through the entire evolution sequence for an app and records each evolution as being applied, creating a baseline for any apps that are newly-added whose models have just been created (or existed prior to using Django Evolution). Args: app (module): The app models module. latest_version (django_evolution.models.Version): The latest version, which the evolutions will be associated with. using (str): The database being updated. verbosity (int): The verbosity used to control output. """ app_label = get_app_label(app) sequence = get_evolution_sequence(app) if sequence and verbosity > 0: print('Evolutions in %s baseline: %s' % (app_label, ', '.join(sequence))) for evo_label in sequence: evolution = Evolution(app_label=app_label, label=evo_label, version=latest_version) evolution.save(using=using)
def test_with_builtin(self): """Testing get_evolution_sequence with built-in evolutions""" self.assertEqual( get_evolution_sequence(get_app('contenttypes')), [ 'contenttypes_unique_together_baseline', 'contenttypes_move_to_migrations', ])
def test_with_project_deprecated_setting(self): """Testing get_evolution_sequence with project-provided evolutions and deprecated settings.CUSTOM_EVOLUTIONS """ custom_evolutions = { 'django_evolution.tests.migrations_app': 'django_evolution.tests.evolutions_app.evolutions', } with self.settings(CUSTOM_EVOLUTIONS=custom_evolutions): self.assertEqual(get_evolution_sequence(get_app('migrations_app')), ['first_evolution', 'second_evolution'])
def test_with_project(self): """Testing get_evolution_sequence with project-provided evolutions""" new_settings = { 'CUSTOM_EVOLUTIONS': { 'django_evolution.tests.migrations_app': 'django_evolution.tests.evolutions_app.evolutions', }, } with self.settings(DJANGO_EVOLUTION=new_settings): self.assertEqual(get_evolution_sequence(get_app('migrations_app')), ['first_evolution', 'second_evolution'])
def _on_app_models_updated(app, using=DEFAULT_DB_ALIAS, **kwargs): """Handler for when an app's models were updated. This is called in response to a syncdb or migrate operation for an app. The very first time this is called for Django Evolution's app, this will set up the current project version to contain the full database signature, and to populate the list of evolutions with all currently-registered ones. This is only done when we're not actively evolving the database. That means it will only be called if we're running unit tests or in reaction to some other process that emits the signals (such as the flush management command). Args: app (module): The app models module that was updated. using (str, optional): The database being updated. **kwargs (dict): Additional keyword arguments provided by the signal handler for the syncdb or migrate operation. """ global _django_evolution_app if _django_evolution_app is None: _django_evolution_app = get_app('django_evolution') if (_evolve_lock > 0 or app is not _django_evolution_app or Version.objects.using(using).exists()): return evolver = Evolver(database_name=using) version = evolver.version version.signature = evolver.target_project_sig version.save(using=using) evolutions = [] for app in get_apps(): app_label = get_app_label(app) evolutions += [ Evolution(app_label=app_label, label=evolution_label, version=version) for evolution_label in get_evolution_sequence(app) ] Evolution.objects.using(using).bulk_create(evolutions)
def test_with_not_found(self): """Testing get_evolution_sequence with evolutions not found""" self.assertEqual(get_evolution_sequence(get_app('migrations_app')), [])
def test_with_app(self): """Testing get_evolution_sequence with app-provided evolutions""" self.assertEqual(get_evolution_sequence(get_app('evolutions_app')), ['first_evolution', 'second_evolution'])