forked from dbt-checkpoint/dbt-checkpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_source_columns_have_desc.py
73 lines (59 loc) · 2.04 KB
/
check_source_columns_have_desc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import argparse
import os
import time
from pathlib import Path
from typing import Any, Dict, Optional, Sequence
from dbt_checkpoint.tracking import dbtCheckpointTracking
from dbt_checkpoint.utils import (
JsonOpenError,
add_default_args,
get_json,
get_source_schemas,
)
def check_column_desc(paths: Sequence[str]) -> Dict[str, Any]:
status_code = 0
ymls = [Path(path) for path in paths]
# if user added schema but did not rerun
schemas = get_source_schemas(ymls)
for schema in schemas:
missing_cols = {
col.get("name")
for col in schema.table_schema.get("columns", [])
if not col.get("description")
}
if missing_cols and all(missing_cols):
status_code = 1
result = "\n- ".join(list(missing_cols)) # pragma: no mutate
print(
f"{schema.source_name}.{schema.table_name}: "
f"following columns are missing description:\n- {result}",
)
return {"status_code": status_code}
def main(argv: Optional[Sequence[str]] = None) -> int:
parser = argparse.ArgumentParser()
add_default_args(parser)
args = parser.parse_args(argv)
try:
manifest = get_json(args.manifest)
except JsonOpenError as e:
print(f"Unable to load manifest file ({e})")
return 1
start_time = time.time()
hook_properties = check_column_desc(paths=args.filenames)
end_time = time.time()
script_args = vars(args)
tracker = dbtCheckpointTracking(script_args=script_args)
tracker.track_hook_event(
event_name="Hook Executed",
manifest=manifest,
event_properties={
"hook_name": os.path.basename(__file__),
"description": "Check the model has description",
"status": hook_properties.get("status_code"),
"execution_time": end_time - start_time,
"is_pytest": script_args.get("is_test"),
},
)
return hook_properties.get("status_code")
if __name__ == "__main__":
exit(main())