def _make_key(relation) -> _ReferenceKey: """Make _ReferenceKeys with lowercase values for the cache so we don't have to keep track of quoting """ # databases and schemas can both be None return _ReferenceKey(lowercase(relation.database), lowercase(relation.schema), lowercase(relation.identifier))
def _list_relations_in_schema( self, database: Optional[str], schema: Optional[str]) -> List[_CachedRelation]: """Get the relations in a schema. Callers should hold the lock.""" key = (lowercase(database), lowercase(schema)) to_remove: List[_CachedRelation] = [] for cachekey, relation in self.relations.items(): if (cachekey.database, cachekey.schema) == key: to_remove.append(relation) return to_remove
def add_schema( self, database: Optional[str], schema: Optional[str], ) -> None: """Add a schema to the set of known schemas (case-insensitive) :param database: The database name to add. :param schema: The schema name to add. """ self.schemas.add((lowercase(database), lowercase(schema)))
def __contains__(self, schema_id: Tuple[Optional[str], str]): """A schema is 'in' the relations cache if it is in the set of cached schemas. :param schema_id: The db name and schema name to look up. """ db, schema = schema_id return (lowercase(db), schema.lower()) in self.schemas
def get_relations(self, database: Optional[str], schema: Optional[str]) -> List[Any]: """Case-insensitively yield all relations matching the given schema. :param str schema: The case-insensitive schema name to list from. :return List[BaseRelation]: The list of relations with the given schema """ database = lowercase(database) schema = lowercase(schema) with self.lock: results = [ r.inner for r in self.relations.values() if (lowercase(r.schema) == schema and lowercase(r.database) == database) ] if None in results: dbt.exceptions.raise_cache_inconsistent( 'in get_relations, a None relation was found in the cache!') return results
def drop_schema( self, database: Optional[str], schema: Optional[str], ) -> None: """Drop the given schema and remove it from the set of known schemas. Then remove all its contents (and their dependents, etc) as well. """ key = (lowercase(database), lowercase(schema)) if key not in self.schemas: return # avoid iterating over self.relations while removing things by # collecting the list first. with self.lock: to_remove = self._list_relations_in_schema(database, schema) self._remove_all(to_remove) # handle a drop_schema race by using discard() over remove() self.schemas.discard(key)
def _build_run_result(self, node, start_time, error, status, timing_info, skip=False, failed=None): execution_time = time.time() - start_time thread_id = threading.current_thread().name status = utils.lowercase(status) return PartialResult( node=node, status=status, error=error, execution_time=execution_time, thread_id=thread_id, timing=timing_info, )
def identifier(self) -> Optional[str]: return lowercase(self.inner.identifier)
def schema(self) -> Optional[str]: return lowercase(self.inner.schema)
def database(self) -> Optional[str]: return lowercase(self.inner.database)
def update_schemas(self, schemas: Iterable[Tuple[Optional[str], str]]): """Add multiple schemas to the set of known schemas (case-insensitive) :param schemas: An iterable of the schema names to add. """ self.schemas.update((lowercase(d), s.lower()) for (d, s) in schemas)
def key(self) -> CatalogKey: return CatalogKey( lowercase(self.metadata.database), self.metadata.schema.lower(), self.metadata.name.lower(), )