@@ -104,7 +104,7 @@ export type MetabaseAppState = MetabaseAppStateSQLEditor | MetabaseAppStateDashb
104104
105105// no need to fetch fields since we don't want that in limited entities
106106
107- export async function convertDOMtoStateSQLQueryV2 ( pageType : MetabasePageType ) : Promise < MetabaseAppStateSQLEditorV2 > {
107+ export async function convertDOMtoStateSQLQueryV2 ( pageType : MetabasePageType , currentDBId : number ) : Promise < MetabaseAppStateSQLEditorV2 > {
108108 const [ metabaseUrl , currentCardRaw , outputMarkdown , parameterValues ] = await Promise . all ( [
109109 RPCs . queryURL ( ) ,
110110 getCurrentCard ( ) as Promise < Card > ,
@@ -115,8 +115,8 @@ export async function convertDOMtoStateSQLQueryV2(pageType: MetabasePageType) :
115115 const metabaseOrigin = new URL ( metabaseUrl ) . origin ;
116116 const isEmbedded = getParsedIframeInfo ( ) . isEmbedded
117117 const sqlQuery = get ( currentCard , 'dataset_query.native.query' , '' ) || ''
118- const limitedEntities = await getLimitedEntities ( sqlQuery ) ;
119- const dbId = await getSelectedDbId ( ) ;
118+ const dbId = currentDBId
119+ const limitedEntities = await getLimitedEntities ( sqlQuery , currentDBId ) ;
120120 const selectedDatabaseInfo = dbId ? await getDatabaseInfo ( dbId ) : undefined ;
121121 const sqlErrorMessage = await getSqlErrorMessage ( ) ;
122122 const appStateType = pageType === 'sql' ? MetabaseAppStateType . SQLEditor : MetabaseAppStateType . RandomPage ;
@@ -153,27 +153,27 @@ export async function convertDOMtoStateSQLQueryV2(pageType: MetabasePageType) :
153153 }
154154}
155155
156- export async function convertDOMtoStateSQLQuery ( ) {
156+ export async function convertDOMtoStateSQLQuery ( currentDBId : number ) {
157157 // CAUTION: This one does not update when changed via ui for some reason
158158 // const dbId = _.get(hashMetadata, 'dataset_query.database');
159159 const fullUrl = await RPCs . queryURL ( ) ;
160160 const url = new URL ( fullUrl ) . origin ;
161161 const availableDatabases = ( await getDatabases ( ) ) ?. data ?. map ( ( { name } ) => name ) ;
162- const dbId = await getSelectedDbId ( ) ;
162+ const dbId = currentDBId ;
163163 const selectedDatabaseInfo = dbId ? await getDatabaseInfo ( dbId ) : undefined ;
164164 const defaultSchema = selectedDatabaseInfo ?. default_schema ;
165- let sqlQuery = await getCurrentQuery ( )
166- const appSettings = RPCs . getAppSettings ( )
167- const cache = RPCs . getCache ( )
168- const sqlTables = getTablesFromSqlRegex ( sqlQuery )
165+ let sqlQuery = await getCurrentQuery ( ) ;
166+ const appSettings = RPCs . getAppSettings ( ) ;
167+ const cache = RPCs . getCache ( ) ;
168+ const sqlTables = getTablesFromSqlRegex ( sqlQuery ) ;
169169 if ( defaultSchema ) {
170170 sqlTables . forEach ( ( table ) => {
171171 if ( table . schema === undefined || table . schema === '' ) {
172172 table . schema = defaultSchema
173173 }
174174 } )
175175 }
176- let relevantTablesWithFields = await getTablesWithFields ( appSettings . tableDiff , appSettings . drMode , false , sqlTables , [ ] )
176+ let relevantTablesWithFields = await getTablesWithFields ( appSettings . tableDiff , appSettings . drMode , false , sqlTables , [ ] , dbId ) ;
177177 // add defaultSchema back to relevantTablesWithFields. kind of hacky but whatever
178178 relevantTablesWithFields = relevantTablesWithFields . map ( table => {
179179 if ( table . schema === undefined || table . schema === '' ) {
@@ -241,13 +241,13 @@ export async function convertDOMtoStateSQLQuery() {
241241 return metabaseAppStateSQLEditor ;
242242}
243243
244- export async function convertDOMtoStateMBQLQuery ( ) {
245- return await getMBQLAppState ( ) as MetabaseAppStateMBQLEditor ;
244+ export async function convertDOMtoStateMBQLQuery ( currentDBId : number ) : Promise < MetabaseAppStateMBQLEditor > {
245+ return await getMBQLAppState ( currentDBId ) as MetabaseAppStateMBQLEditor ;
246246}
247247
248248// check if on dashboard page
249- export async function convertDOMtoStateDashboard ( ) : Promise < MetabaseAppStateDashboard > {
250- const dashboardInfo = await getDashboardAppState ( ) ;
249+ export async function convertDOMtoStateDashboard ( currentDBId : number ) : Promise < MetabaseAppStateDashboard > {
250+ const dashboardInfo = await getDashboardAppState ( currentDBId ) ;
251251 return dashboardInfo as MetabaseAppStateDashboard ;
252252} ;
253253
@@ -272,21 +272,21 @@ export async function semanticQueryState() {
272272 return metabaseSemanticQueryAppState ;
273273}
274274
275- export async function convertDOMtoState ( ) {
275+ export async function convertDOMtoState ( currentDBId : number ) {
276276 const url = await queryURL ( ) ;
277277 const qlType = await getQLType ( ) ;
278278 const metabasePageType = determineMetabasePageType ( { } , url , qlType ) ;
279279 if ( metabasePageType === 'dashboard' ) {
280- return await convertDOMtoStateDashboard ( ) ;
280+ return await convertDOMtoStateDashboard ( currentDBId ) ;
281281 }
282282 if ( metabasePageType === 'mbql' ) {
283- return await convertDOMtoStateMBQLQuery ( ) ;
283+ return await convertDOMtoStateMBQLQuery ( currentDBId ) ;
284284 }
285285 const appSettings = RPCs . getAppSettings ( )
286286 if ( appSettings . useV2States && appSettings . analystMode ) {
287- return await convertDOMtoStateSQLQueryV2 ( metabasePageType ) ;
287+ return await convertDOMtoStateSQLQueryV2 ( metabasePageType , currentDBId ) ;
288288 }
289- return await convertDOMtoStateSQLQuery ( ) ;
289+ return await convertDOMtoStateSQLQuery ( currentDBId ) ;
290290}
291291
292292async function getSqlVariables ( ) {
0 commit comments