Example #1
0
    def test_add_migration_targets(self):
        """Testing MigrationList.add_migration_targets"""
        migration_list = MigrationList()
        migration_list.add_migration_targets([
            ('tests', '0001_initial'),
            ('tests', '0002_stuff'),
        ])

        self.assertEqual(
            migration_list._by_id,
            {
                ('tests', '0001_initial'): {
                    'app_label': 'tests',
                    'name': '0001_initial',
                    'migration': None,
                    'recorded_migration': None,
                },
                ('tests', '0002_stuff'): {
                    'app_label': 'tests',
                    'name': '0002_stuff',
                    'migration': None,
                    'recorded_migration': None,
                },
            })
        self.assertEqual(
            migration_list._by_app_label,
            {
                'tests': [
                    {
                        'app_label': 'tests',
                        'name': '0001_initial',
                        'migration': None,
                        'recorded_migration': None,
                    },
                    {
                        'app_label': 'tests',
                        'name': '0002_stuff',
                        'migration': None,
                        'recorded_migration': None,
                    },
                ],
            })
Example #2
0
    def record_applied_migrations(self,
                                  migration_targets,
                                  database=DEFAULT_DB_ALIAS):
        """Record applied migrations in the database.

        This is a convenience around creating a migration list and then
        recording it in the database.

        Args:
            migration_targets (list of tuple):
                The list of migration targets to store. Each item is a tuple
                in ``(app_label, label)`` form.

            database (unicode, optional):
                The name of the database to save these on.
        """
        assert supports_migrations

        migration_list = MigrationList()
        migration_list.add_migration_targets(migration_targets)

        record_applied_migrations(connection=connections[database],
                                  migrations=migration_list)
    def _add_migrations(self, graph, migrations_info, leaf_migration_targets,
                        mark_applied=[]):
        """Add migrations to a graph.

        This is a utility for simplifying the additions of a list of
        migrations to a graph, handling the creation of the Django migration
        objects, the formulation of a migration plan, and the recording of
        applied migrations.

        Args:
            graph (django_evolution.utils.graph.EvolutionGraph):
                The graph to add migrations to.

            migrations_info (list of tuple):
                The list of info on migrations to add. Each tuple contains:

                1. The app label
                2. The migration name
                3. The migration class

            leaf_migration_targets (list of tuple):
                The list of final migration targets to migrate to.

            mark_applied (list of tuple, optional):
                The list of migration targets to mark as applied.

        Returns:
            list of tuple:
            The migration plan generated from the migrations.
        """
        migration_list = MigrationList()

        for app_label, name, migration_cls in migrations_info:
            migration_list.add_migration_info(
                app_label=app_label,
                name=name,
                migration=migration_cls(name, app_label))

        connection = connections[DEFAULT_DB_ALIAS]

        if mark_applied:
            mark_applied_list = MigrationList()
            mark_applied_list.add_migration_targets(mark_applied)

            record_applied_migrations(connection, mark_applied_list)
        else:
            mark_applied_list = None

        migration_executor = MigrationExecutor(
            connection=connection,
            custom_migrations=migration_list)
        migration_loader = MigrationLoader(
            connection=connection,
            custom_migrations=migration_list)

        migration_plan = \
            migration_executor.migration_plan(leaf_migration_targets)
        migration_loader.build_graph()

        graph.add_migration_plan(migration_plan=migration_plan,
                                 migration_graph=migration_loader.graph)

        if mark_applied_list:
            graph.mark_migrations_applied(mark_applied_list)

        return migration_plan