Beispiel #1
0
def verify_node_uuid_uniqueness(apps, schema_editor):
    """Check whether the database contains nodes with duplicate UUIDS.

    Note that we have to redefine this method from aiida.manage.database.integrity.verify_node_uuid_uniqueness
    because the migrations.RunPython command that will invoke this function, will pass two arguments and therefore
    this wrapper needs to have a different function signature.

    :raises: IntegrityError if database contains nodes with duplicate UUIDS.
    """
    from aiida.manage.database.integrity.duplicate_uuid import verify_uuid_uniqueness
    verify_uuid_uniqueness(table='db_dbnode')
def _verify_uuid_uniqueness(apps, schema_editor):
    """Check whether the respective tables contain rows with duplicate UUIDS.

    Note that we have to redefine this method from aiida.manage.database.integrity
    because the migrations.RunPython command that will invoke this function, will pass two arguments and therefore
    this wrapper needs to have a different function signature.

    :raises: IntegrityError if database contains rows with duplicate UUIDS.
    """
    # pylint: disable=unused-argument
    from aiida.manage.database.integrity.duplicate_uuid import verify_uuid_uniqueness

    for table in tables:
        verify_uuid_uniqueness(table=table)
Beispiel #3
0
    def setUpBeforeMigration(self):
        from aiida.common.utils import get_new_uuid
        self.file_name = 'test.temp'
        self.file_content = '#!/bin/bash\n\necho test run\n'

        self.nodes_boolean = []
        self.nodes_integer = []
        self.n_bool_duplicates = 2
        self.n_int_duplicates = 4

        node_bool = self.DbNode(type='data.bool.Bool.',
                                user_id=self.default_user.id,
                                uuid=get_new_uuid())
        node_bool.save()

        node_int = self.DbNode(type='data.int.Int.',
                               user_id=self.default_user.id,
                               uuid=get_new_uuid())
        node_int.save()

        self.nodes_boolean.append(node_bool)
        self.nodes_integer.append(node_int)

        for _ in range(self.n_bool_duplicates):
            node = self.DbNode(type='data.bool.Bool.',
                               user_id=self.default_user.id,
                               uuid=node_bool.uuid)
            node.save()
            utils.put_object_from_string(node.uuid, self.file_name,
                                         self.file_content)
            self.nodes_boolean.append(node)

        for _ in range(self.n_int_duplicates):
            node = self.DbNode(type='data.int.Int.',
                               user_id=self.default_user.id,
                               uuid=node_int.uuid)
            node.save()
            utils.put_object_from_string(node.uuid, self.file_name,
                                         self.file_content)
            self.nodes_integer.append(node)

        # Verify that there are duplicate UUIDs by checking that the following function raises
        with self.assertRaises(IntegrityError):
            verify_uuid_uniqueness(table='db_dbnode')

        # Now run the function responsible for solving duplicate UUIDs which would also be called by the user
        # through the `verdi database integrity detect-duplicate-uuid` command
        deduplicate_uuids(table='db_dbnode', dry_run=False)
    def test_deduplicated_uuids(self):
        """Verify that after the migration, all expected nodes are still there with unique UUIDs."""
        # If the duplicate UUIDs were successfully fixed, the following should not raise.
        verify_uuid_uniqueness(table='db_dbnode')

        # Reload the nodes by PK and check that all UUIDs are now unique
        nodes_boolean = [self.load_node(node.pk) for node in self.nodes_boolean]
        uuids_boolean = [node.uuid for node in nodes_boolean]
        self.assertEqual(len(set(uuids_boolean)), len(nodes_boolean))

        nodes_integer = [self.load_node(node.pk) for node in self.nodes_integer]
        uuids_integer = [node.uuid for node in nodes_integer]
        self.assertEqual(len(set(uuids_integer)), len(nodes_integer))

        for node in nodes_boolean:
            self.assertEqual(utils.get_object_from_repository(node.uuid, self.file_name), self.file_content)