@@ -526,7 +526,7 @@ def run(self, invoked_as: str, args: List[str]) -> None:
526
526
"""
527
527
528
528
try :
529
- ds_id = self .get_datasource_id ()
529
+ ds_id : UUID = self .get_datasource_id ()
530
530
except ValueError :
531
531
# Tried to introspect a datasource whose kernel-side handle in the connections dict
532
532
# wasn't coercable back into a UUID. This could be the case for the pre-datasource
@@ -563,7 +563,6 @@ def delta() -> float:
563
563
with ThreadPoolExecutor (
564
564
max_workers = self .MAX_INTROSPECTION_THREADS
565
565
) as executor , RelationStructureMessager (ds_id ) as messenger :
566
-
567
566
future_to_relation = {
568
567
executor .submit (
569
568
self .fully_introspect , inspector , schema_name , relation_name , kind
@@ -622,9 +621,8 @@ def all_table_and_views(self, inspector) -> List[Tuple[str, str, str]]:
622
621
return results
623
622
624
623
def fully_introspect (
625
- self , inspector : ' SchemaStrippingInspector' , schema_name : str , relation_name : str , kind : str
624
+ self , inspector : SchemaStrippingInspector , schema_name : str , relation_name : str , kind : str
626
625
) -> RelationStructureDescription :
627
-
628
626
"""Drive introspecting into this single relation, making all the necessary Introspector API
629
627
calls to learn all of the relation's sub-structures.
630
628
@@ -676,19 +674,28 @@ def fully_introspect(
676
674
)
677
675
678
676
def introspect_foreign_keys (
679
- self , inspector , schema_name , relation_name
677
+ self , inspector : SchemaStrippingInspector , schema_name : str , relation_name : str
680
678
) -> List [ForeignKeysModel ]:
681
679
"""Introspect all foreign keys for a table, describing the results as a List[ForeignKeysModel]"""
682
680
683
681
fkeys : List [ForeignKeysModel ] = []
684
682
685
683
fkey_dicts = inspector .get_foreign_keys (relation_name , schema_name )
686
684
685
+ # Convert from SQLA foreign key dicts to our ForeignKeysModel. But beware,
686
+ # Snowflake driver reports FKs with None for the target table's schema
687
+ # (at least sometimes?) so in case then err on the referencing table's schema, because
688
+ # ForeignKeysModel shared between us and Gate hate None for referred_schema.
689
+
687
690
for fkey in sorted (fkey_dicts , key = lambda d : d ['name' ]):
688
691
fkeys .append (
689
692
ForeignKeysModel (
690
693
name = fkey ['name' ],
691
- referenced_schema = fkey ['referred_schema' ],
694
+ referenced_schema = (
695
+ fkey ['referred_schema' ]
696
+ if fkey ['referred_schema' ] is not None
697
+ else schema_name
698
+ ),
692
699
referenced_relation = fkey ['referred_table' ],
693
700
columns = fkey ['constrained_columns' ],
694
701
referenced_columns = fkey ['referred_columns' ],
@@ -740,7 +747,6 @@ def introspect_indexes(self, inspector, schema_name, relation_name) -> List[Inde
740
747
index_dicts = inspector .get_indexes (relation_name , schema_name )
741
748
742
749
for index_dict in sorted (index_dicts , key = lambda d : d ['name' ]):
743
-
744
750
indexes .append (
745
751
IndexModel (
746
752
name = index_dict ['name' ],
@@ -752,7 +758,7 @@ def introspect_indexes(self, inspector, schema_name, relation_name) -> List[Inde
752
758
return indexes
753
759
754
760
def introspect_primary_key (
755
- self , inspector , relation_name , schema_name
761
+ self , inspector : SchemaStrippingInspector , relation_name : str , schema_name : str
756
762
) -> Tuple [Optional [str ], List [str ]]:
757
763
"""Introspect the primary key of a table, returning the pkey name and list of columns in the primary key (if any).
758
764
@@ -770,9 +776,8 @@ def introspect_primary_key(
770
776
return None , []
771
777
772
778
def introspect_columns (
773
- self , inspector : ' SchemaStrippingInspector' , schema_name : str , relation_name : str
779
+ self , inspector : SchemaStrippingInspector , schema_name : str , relation_name : str
774
780
) -> List [ColumnModel ]:
775
-
776
781
column_dicts = inspector .get_columns (relation_name , schema = schema_name )
777
782
778
783
retlist = []
@@ -798,38 +803,6 @@ def introspect_columns(
798
803
799
804
return retlist
800
805
801
- def inform_gate_relation (
802
- self ,
803
- session : requests .Session ,
804
- datasource_id ,
805
- relation_description : RelationStructureDescription ,
806
- ):
807
- """POST this `relation_description` up to Gate for storage, relating to `datasource_id`"""
808
-
809
- resp = session .post (
810
- f"http://gate.default/api/v1/datasources/{ datasource_id } /schema/relation" ,
811
- json = relation_description .dict (),
812
- )
813
-
814
- if resp .status_code == 204 :
815
- print (
816
- f'Stored structure of { relation_description .schema_name } .{ relation_description .relation_name } '
817
- )
818
- else :
819
- print (
820
- f'Failed storing structure of { relation_description .schema_name } .{ relation_description .relation_name } : { resp .status_code } , { resp .text } '
821
- )
822
-
823
- def inform_gate_completed (
824
- self , session : requests .Session , datasource_id : UUID , started_at : datetime
825
- ):
826
- """Tell gate to forget about any structures known for this datasource older than when we
827
- started this introspection run."""
828
-
829
- session .delete (
830
- f"http://gate.default/api/v1/datasources/{ datasource_id } /schema/relations?older_than={ started_at .isoformat ()} "
831
- )
832
-
833
806
def get_datasource_id (self ) -> UUID :
834
807
"""Convert a noteable_magics.sql.connection.Connection's name to the original
835
808
UUID Gate knew it as.
@@ -894,7 +867,6 @@ def __exit__(self, exc_type, exc_value, exc_traceback):
894
867
self ._message_gate (completed_introspection = True )
895
868
896
869
def _message_gate (self , completed_introspection : bool = False ):
897
-
898
870
base_url = f"http://gate.default/api/v1/datasources/{ self ._datasource_id } /schema/relations"
899
871
900
872
if self ._relations :
0 commit comments