@@ -89,8 +89,12 @@ def _file_content(model: SqlaTable) -> str:
8989
9090 @staticmethod
9191 def _export (
92- model : SqlaTable , export_related : bool = True
92+ model : SqlaTable , export_related : bool = True , seen : set [ str ] | None = None
9393 ) -> Iterator [tuple [str , Callable [[], str ]]]:
94+ # Initialize seen set if not provided
95+ if seen is None :
96+ seen = set ()
97+
9498 yield (
9599 ExportDatasetsCommand ._file_name (model ),
96100 lambda : ExportDatasetsCommand ._file_content (model ),
@@ -103,32 +107,41 @@ def _export(
103107 )
104108 file_path = f"databases/{ db_file_name } .yaml"
105109
106- payload = model .database .export_to_dict (
107- recursive = False ,
108- include_parent_ref = False ,
109- include_defaults = True ,
110- export_uuids = True ,
111- )
112- # TODO (betodealmeida): move this logic to export_to_dict once this
113- # becomes the default export endpoint
114- if payload .get ("extra" ):
115- try :
116- payload ["extra" ] = json .loads (payload ["extra" ])
117- except json .JSONDecodeError :
118- logger .info ("Unable to decode `extra` field: %s" , payload ["extra" ])
119-
120- if ssh_tunnel := model .database .ssh_tunnel :
121- ssh_tunnel_payload = ssh_tunnel .export_to_dict (
110+ # Only yield the database file if not already seen. This is
111+ # critical to fix the issue where databases were being
112+ # duplicated and potentially overwritten when charts from
113+ # different databases were exported.
114+ if file_path not in seen :
115+ payload = model .database .export_to_dict (
122116 recursive = False ,
123117 include_parent_ref = False ,
124118 include_defaults = True ,
125- export_uuids = False ,
119+ export_uuids = True ,
126120 )
127- payload ["ssh_tunnel" ] = mask_password_info (ssh_tunnel_payload )
128-
129- payload ["version" ] = EXPORT_VERSION
121+ # TODO (betodealmeida): move this logic to export_to_dict once this
122+ # becomes the default export endpoint
123+ if payload .get ("extra" ):
124+ try :
125+ payload ["extra" ] = json .loads (payload ["extra" ])
126+ except json .JSONDecodeError :
127+ logger .info (
128+ "Unable to decode `extra` field: %s" , payload ["extra" ]
129+ )
130130
131- yield (
132- file_path ,
133- lambda : yaml .safe_dump (payload , sort_keys = False , allow_unicode = True ),
134- )
131+ if ssh_tunnel := model .database .ssh_tunnel :
132+ ssh_tunnel_payload = ssh_tunnel .export_to_dict (
133+ recursive = False ,
134+ include_parent_ref = False ,
135+ include_defaults = True ,
136+ export_uuids = False ,
137+ )
138+ payload ["ssh_tunnel" ] = mask_password_info (ssh_tunnel_payload )
139+
140+ payload ["version" ] = EXPORT_VERSION
141+
142+ yield (
143+ file_path ,
144+ lambda : yaml .safe_dump (
145+ payload , sort_keys = False , allow_unicode = True
146+ ),
147+ )
0 commit comments