Skip to content

Commit

Permalink
more conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed Dec 7, 2022
1 parent dc87a8e commit 9d19015
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 67 deletions.
4 changes: 2 additions & 2 deletions core/dbt/adapters/base/relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Policy,
Path,
)
from dbt.exceptions import InternalException, ApproximateMatch
from dbt.exceptions import ApproximateMatch, InternalException, MultipleDatabasesNotAllowed
from dbt.node_types import NodeType
from dbt.utils import filter_null_values, deep_merge, classproperty

Expand Down Expand Up @@ -437,7 +437,7 @@ def flatten(self, allow_multiple_databases: bool = False):
if not allow_multiple_databases:
seen = {r.database.lower() for r in self if r.database}
if len(seen) > 1:
dbt.exceptions.raise_compiler_error(str(seen))
raise MultipleDatabasesNotAllowed(seen)

for information_schema_name, schema in self.search():
path = {"database": information_schema_name.database, "schema": schema}
Expand Down
7 changes: 2 additions & 5 deletions core/dbt/adapters/sql/impl.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import agate
from typing import Any, Optional, Tuple, Type, List

import dbt.clients.agate_helper
from dbt.contracts.connection import Connection
import dbt.exceptions
from dbt.exceptions import RelationTypeNull
from dbt.adapters.base import BaseAdapter, available
from dbt.adapters.cache import _make_ref_key_msg
from dbt.adapters.sql import SQLConnectionManager
Expand Down Expand Up @@ -132,9 +131,7 @@ def alter_column_type(self, relation, column_name, new_column_type) -> None:

def drop_relation(self, relation):
if relation.type is None:
dbt.exceptions.raise_compiler_error(
"Tried to drop relation {}, but its type is null.".format(relation)
)
raise RelationTypeNull(relation)

self.cache_dropped(relation)
self.execute_macro(DROP_RELATION_MACRO_NAME, kwargs={"relation": relation})
Expand Down
20 changes: 7 additions & 13 deletions core/dbt/config/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
from dbt.dataclass_schema import ValidationError
from dbt.exceptions import (
DbtProjectError,
NonUniquePackageName,
RuntimeException,
raise_compiler_error,
UninstalledPackagesFound,
validator_error_message,
)
from dbt.events.functions import warn_or_error
Expand Down Expand Up @@ -352,22 +353,15 @@ def load_dependencies(self, base_only=False) -> Mapping[str, "RuntimeConfig"]:
count_packages_specified = len(self.packages.packages) # type: ignore
count_packages_installed = len(tuple(self._get_project_directories()))
if count_packages_specified > count_packages_installed:
raise_compiler_error(
f"dbt found {count_packages_specified} package(s) "
f"specified in packages.yml, but only "
f"{count_packages_installed} package(s) installed "
f'in {self.packages_install_path}. Run "dbt deps" to '
f"install package dependencies."
raise UninstalledPackagesFound(
count_packages_specified,
count_packages_installed,
self.packages_install_path,
)
project_paths = itertools.chain(internal_packages, self._get_project_directories())
for project_name, project in self.load_projects(project_paths):
if project_name in all_projects:
raise_compiler_error(
f"dbt found more than one package with the name "
f'"{project_name}" included in this project. Package '
f"names must be unique in a project. Please rename "
f"one of these packages."
)
raise NonUniquePackageName(project_name)
all_projects[project_name] = project
self.dependencies = all_projects
return self.dependencies
Expand Down
8 changes: 2 additions & 6 deletions core/dbt/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from dbt.config.renderer import DbtProjectYamlRenderer, ProfileRenderer
from dbt.events.functions import fire_event
from dbt.events.types import InvalidVarsYAML
from dbt.exceptions import ValidationException, raise_compiler_error
from dbt.exceptions import ValidationException, VarsArgNotYamlDict


def parse_cli_vars(var_string: str) -> Dict[str, Any]:
Expand All @@ -19,11 +19,7 @@ def parse_cli_vars(var_string: str) -> Dict[str, Any]:
if var_type is dict:
return cli_vars
else:
type_name = var_type.__name__
raise_compiler_error(
"The --vars argument must be a YAML dictionary, but was "
"of type '{}'".format(type_name)
)
raise VarsArgNotYamlDict(var_type)
except ValidationException:
fire_event(InvalidVarsYAML())
raise
Expand Down
8 changes: 2 additions & 6 deletions core/dbt/context/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
DisallowSecretEnvVar,
EnvVarMissing,
MacroReturn,
raise_compiler_error,
RequiredVarNotFound,
)
from dbt.events.functions import fire_event, get_invocation_id
from dbt.events.types import JinjaLogInfo, JinjaLogDebug
Expand Down Expand Up @@ -128,7 +128,6 @@ def __new__(mcls, name, bases, dct):


class Var:
UndefinedVarError = "Required var '{}' not found in config:\nVars supplied to {} = {}"
_VAR_NOTSET = object()

def __init__(
Expand All @@ -153,10 +152,7 @@ def node_name(self):
return "<Configuration>"

def get_missing_var(self, var_name):
dct = {k: self._merged[k] for k in self._merged}
pretty_vars = json.dumps(dct, sort_keys=True, indent=4)
msg = self.UndefinedVarError.format(var_name, self.node_name, pretty_vars)
raise_compiler_error(msg, self._node)
raise RequiredVarNotFound(var_name, self._merged, self._node)

def has_var(self, var_name: str):
return var_name in self._merged
Expand Down
4 changes: 2 additions & 2 deletions core/dbt/context/macro_resolver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Dict, MutableMapping, Optional
from dbt.contracts.graph.parsed import ParsedMacro
from dbt.exceptions import DuplicateMacroName, raise_compiler_error
from dbt.exceptions import DuplicateMacroName, PackageNotFoundForMacro
from dbt.include.global_project import PROJECT_NAME as GLOBAL_PROJECT_NAME
from dbt.clients.jinja import MacroGenerator

Expand Down Expand Up @@ -187,7 +187,7 @@ def get_from_package(self, package_name: Optional[str], name: str) -> Optional[M
elif package_name in self.macro_resolver.packages:
macro = self.macro_resolver.packages[package_name].get(name)
else:
raise_compiler_error(f"Could not find package '{package_name}'")
raise PackageNotFoundForMacro(package_name)
if not macro:
return None
macro_func = MacroGenerator(macro, self.ctx, self.node, self.thread_ctx)
Expand Down
4 changes: 2 additions & 2 deletions core/dbt/context/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from dbt.clients.jinja import MacroGenerator, MacroStack
from dbt.contracts.graph.parsed import ParsedMacro
from dbt.include.global_project import PROJECT_NAME as GLOBAL_PROJECT_NAME
from dbt.exceptions import DuplicateMacroName, raise_compiler_error
from dbt.exceptions import DuplicateMacroName, PackageNotFoundForMacro


FlatNamespace = Dict[str, MacroGenerator]
Expand Down Expand Up @@ -75,7 +75,7 @@ def get_from_package(self, package_name: Optional[str], name: str) -> Optional[M
elif package_name in self.packages:
return self.packages[package_name].get(name)
else:
raise_compiler_error(f"Could not find package '{package_name}'")
raise PackageNotFoundForMacro(package_name)


# This class builds the MacroNamespace by adding macros to
Expand Down
32 changes: 4 additions & 28 deletions core/dbt/contracts/graph/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
from dbt.exceptions import (
CompilationException,
DuplicateResourceName,
raise_compiler_error,
DuplicateMacroInPackage,
DuplicateMaterializationName,
)
from dbt.helper_types import PathSet
from dbt.events.functions import fire_event
from dbt.events.types import MergedFromState
from dbt.node_types import NodeType
from dbt.ui import line_wrap_message
from dbt import flags
from dbt import tracking
import dbt.utils
Expand Down Expand Up @@ -403,12 +403,7 @@ def __eq__(self, other: object) -> bool:
return NotImplemented
equal = self.specificity == other.specificity and self.locality == other.locality
if equal:
raise_compiler_error(
"Found two materializations with the name {} (packages {} and "
"{}). dbt cannot resolve this ambiguity".format(
self.macro.name, self.macro.package_name, other.macro.package_name
)
)
raise DuplicateMaterializationName(self.macro, other)

return equal

Expand Down Expand Up @@ -1047,26 +1042,7 @@ def merge_from_artifact(
def add_macro(self, source_file: SourceFile, macro: ParsedMacro):
if macro.unique_id in self.macros:
# detect that the macro exists and emit an error
other_path = self.macros[macro.unique_id].original_file_path
# subtract 2 for the "Compilation Error" indent
# note that the line wrap eats newlines, so if you want newlines,
# this is the result :(
msg = line_wrap_message(
f"""\
dbt found two macros named "{macro.name}" in the project
"{macro.package_name}".
To fix this error, rename or remove one of the following
macros:
- {macro.original_file_path}
- {other_path}
""",
subtract=2,
)
raise_compiler_error(msg)
raise DuplicateMacroInPackage(macro=macro, macro_mapping=self.macros)

self.macros[macro.unique_id] = macro
source_file.macros.append(macro.unique_id)
Expand Down
Loading

0 comments on commit 9d19015

Please sign in to comment.