Пример #1
0
def get_trigger_tables():
    """Determines which tables need to have triggers set on them.

    Returns a dictionary of table names (key) with a dictionary (value) that
    provides additional information about a table:

        * list of primary keys for each table.
        * whether it's an entity table
    """
    tables = collections.OrderedDict(
    )  # mapping of table names to their models and their "kind" (direct or not)
    for _, entity in SCHEMA.items():
        # Entity table itself
        mapped_class = class_mapper(entity.model)
        tables[mapped_class.mapped_table.name] = {
            "model": entity.model,
            "is_direct": True,
            "has_gid": mapped_class.has_property('gid'),
        }
        # Tables that contain the referenced column
        # TODO(roman): maybe come up with a better description above
        for path in unique_split_paths(
            [path for field in entity.fields for path in field.paths]):
            model = last_model_in_path(entity.model, path)
            if model is not None:
                table_name = class_mapper(model).mapped_table.name
                if table_name not in tables:
                    tables[table_name] = {
                        "model": model,
                        "is_direct": False,
                    }

    return tables
Пример #2
0
def get_trigger_tables():
    """Determines which tables need to have triggers set on them.

    Returns a dictionary of table names (key) with a dictionary (value) that
    provides additional information about a table:

        * list of primary keys for each table.
        * whether it's an entity table
    """
    tables = collections.OrderedDict()  # mapping of table names to their models and their "kind" (direct or not)
    for _, entity in SCHEMA.items():
        # Entity table itself
        mapped_class = class_mapper(entity.model)
        tables[mapped_class.mapped_table.name] = {
            "model": entity.model,
            "is_direct": True,
            "has_gid": mapped_class.has_property('gid'),
        }
        # Tables that contain the referenced column
        # TODO(roman): maybe come up with a better description above
        for path in unique_split_paths([path for field in entity.fields
                                             for path in field.paths if field.trigger]):
            model = last_model_in_path(entity.model, path)
            if model is not None:
                table_name = class_mapper(model).mapped_table.name
                if table_name not in tables:
                    tables[table_name] = {
                        "model": model,
                        "is_direct": False,
                    }

    return tables
Пример #3
0
    def test_index_by_fk_1(self):
        columns = {'id': '1', 'area': '2', 'type': '3'}
        parsed_message = Message(1, 'area_alias', columns, 'delete')
        handler.SCHEMA = SCHEMA
        self.handler = handler.Handler(SCHEMA.keys())
        for entity_type, entity in SCHEMA.items():
            self.handler.cores[entity_type] = mock.Mock()

        self.handler._index_by_fk(parsed_message)
        calls = self.handler.db_session().execute.call_args_list
        self.assertEqual(len(calls), 6)
        actual_queries = [str(call[0][0]) for call in calls]
        expected_queries = [
            'SELECT place_1.id AS place_1_id \n'
            'FROM musicbrainz.place AS place_1 JOIN musicbrainz.area ON musicbrainz.area.id = place_1.area \n'
            'WHERE musicbrainz.area.id = :id_1',
            'SELECT label_1.id AS label_1_id \n'
            'FROM musicbrainz.label AS label_1 JOIN musicbrainz.area ON musicbrainz.area.id = label_1.area \n'
            'WHERE musicbrainz.area.id = :id_1',
            'SELECT artist_1.id AS artist_1_id \n'
            'FROM musicbrainz.artist AS artist_1 JOIN musicbrainz.area ON musicbrainz.area.id = artist_1.end_area \n'
            'WHERE musicbrainz.area.id = :id_1',
            'SELECT artist_1.id AS artist_1_id \n'
            'FROM musicbrainz.artist AS artist_1 JOIN musicbrainz.area ON musicbrainz.area.id = artist_1.area \n'
            'WHERE musicbrainz.area.id = :id_1',
            'SELECT artist_1.id AS artist_1_id \n'
            'FROM musicbrainz.artist AS artist_1 JOIN musicbrainz.area ON musicbrainz.area.id = artist_1.begin_area \n'
            'WHERE musicbrainz.area.id = :id_1',
            'SELECT musicbrainz.area.id AS musicbrainz_area_id \n'
            'FROM musicbrainz.area \n'
            'WHERE musicbrainz.area.id = :id_1'
        ]

        self.assertEqual(expected_queries, actual_queries)
Пример #4
0
 def test_index_by_fk_3(self):
     columns = {'release_group': 1}
     parsed_message = Message(1, 'release', columns, 'delete')
     handler.SCHEMA = SCHEMA
     self.handler = handler.Handler()
     for entity_type, entity in SCHEMA.items():
         self.handler.cores[entity_type] = mock.Mock()
         entity.build_entity_query = mock.MagicMock()
     self.handler._index_by_fk(parsed_message)
     calls = self.handler.db_session().execute.call_args_list
     self.assertEqual(len(calls), 1)
     actual_queries = [str(call[0][0]) for call in calls]
     expected_queries = [
         'SELECT musicbrainz.release_group.id AS musicbrainz_release_group_id \n'
         'FROM musicbrainz.release_group \n'
         'WHERE musicbrainz.release_group.id = :id_1']
     self.assertEqual(expected_queries, actual_queries)
Пример #5
0
    def setUp(self):
        super(HandlerTest, self).setUp()
        handler.SCHEMA = {self.entity_type: None}

        solr_version_check_patcher = mock.patch(
            "sir.amqp.handler.solr_version_check")
        self.addCleanup(solr_version_check_patcher.stop)
        solr_version_check_patcher.start()

        self.handler = handler.Handler(handler.SCHEMA.keys())
        self.channel = self.handler.channel = mock.MagicMock()
        self.handler.connection = mock.MagicMock()

        self.handler.cores[self.entity_type] = mock.Mock()

        for entity_type, entity in SCHEMA.items():
            patcher = mock.patch.object(entity, 'build_entity_query')
            patcher.start()
            self.addCleanup(patcher.stop)