1515from .cloud import DatafoldAPI , TCloudApiDataDiff , TCloudApiOrgMeta , get_or_create_data_source
1616from .dbt_parser import DbtParser , PROJECT_FILE , TDatadiffConfig
1717from .diff_tables import DiffResultWrapper
18- from .format import jsonify , jsonify_exception
18+ from .format import jsonify , jsonify_error
1919from .tracking import (
2020 bool_ask_for_email ,
2121 create_email_signup_event_json ,
@@ -129,10 +129,20 @@ def dbt_diff(
129129 else :
130130 _local_diff (diff_vars , json_output )
131131 else :
132- rich .print (
133- _diff_output_base ("." .join (diff_vars .dev_path ), "." .join (diff_vars .prod_path ))
134- + "Skipped due to unknown primary key. Add uniqueness tests, meta, or tags.\n "
135- )
132+ if json_output :
133+ print (json .dumps (
134+ jsonify_error (
135+ table1 = diff_vars .prod_path ,
136+ table2 = diff_vars .dev_path ,
137+ dbt_model = diff_vars .dbt_model ,
138+ error = "No primary key found. Add uniqueness tests, meta, or tags." ,
139+ )
140+ ))
141+ else :
142+ rich .print (
143+ _diff_output_base ("." .join (diff_vars .dev_path ), "." .join (diff_vars .prod_path ))
144+ + "Skipped due to unknown primary key. Add uniqueness tests, meta, or tags.\n "
145+ )
136146
137147 # wait for all threads
138148 if diff_threads :
@@ -223,11 +233,12 @@ def _local_diff(diff_vars: TDiffVars, json_output: bool = False) -> None:
223233 dev_qualified_str = "." .join (diff_vars .dev_path )
224234 prod_qualified_str = "." .join (diff_vars .prod_path )
225235 diff_output_str = _diff_output_base (dev_qualified_str , prod_qualified_str )
226-
227- table1 = connect_to_table (diff_vars .connection , dev_qualified_str , tuple (diff_vars .primary_keys ), diff_vars .threads )
228- table2 = connect_to_table (
236+
237+ table1 = connect_to_table (
229238 diff_vars .connection , prod_qualified_str , tuple (diff_vars .primary_keys ), diff_vars .threads
230239 )
240+ table2 = connect_to_table (diff_vars .connection , dev_qualified_str , tuple (diff_vars .primary_keys ), diff_vars .threads )
241+
231242
232243 table1_columns = table1 .get_schema ()
233244 try :
@@ -242,11 +253,11 @@ def _local_diff(diff_vars: TDiffVars, json_output: bool = False) -> None:
242253 table1_column_names = set (table1_columns .keys ())
243254 table2_column_names = set (table2_columns .keys ())
244255 column_set = table1_column_names .intersection (table2_column_names )
245- columns_added = table1_column_names .difference (table2_column_names )
246- columns_removed = table2_column_names .difference (table1_column_names )
256+ columns_added = table2_column_names .difference (table1_column_names )
257+ columns_removed = table1_column_names .difference (table2_column_names )
247258 # col type is i = 1 in tuple
248259 columns_type_changed = {
249- k for k , v in table1_columns .items () if k in table2_columns and v [1 ] != table2_columns [k ][1 ]
260+ k for k , v in table2_columns .items () if k in table1_columns and v [1 ] != table1_columns [k ][1 ]
250261 }
251262
252263 if columns_added :
@@ -283,11 +294,11 @@ def _local_diff(diff_vars: TDiffVars, json_output: bool = False) -> None:
283294 try :
284295 list (diff )
285296 except Exception as e :
286- print (json .dumps (jsonify_exception (
297+ print (json .dumps (jsonify_error (
287298 list (table1 .table_path ),
288299 list (table2 .table_path ),
289300 diff_vars .dbt_model ,
290- e
301+ str ( e )
291302 )), flush = True )
292303 return
293304
0 commit comments