SYNC: fixed connection for scripts

This commit is contained in:
SPRINX0\prochazka
2025-06-23 13:28:37 +02:00
committed by Diflow
parent a648f1ee67
commit edf1632cab
2 changed files with 26 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
<script lang="ts" context="module">
import { filterName, getConnectionLabel } from 'dbgate-tools';
import { filterName, getConnectionLabel, getSqlFrontMatter } from 'dbgate-tools';
import yaml from 'js-yaml';
interface FileTypeHandler {
icon: string;
@@ -9,6 +10,7 @@
currentConnection: boolean;
extension: string;
label: string;
switchDatabaseOnOpen?: (data: any) => Promise<boolean>;
}
const sql: FileTypeHandler = {
@@ -19,6 +21,21 @@
currentConnection: true,
extension: 'sql',
label: 'SQL file',
switchDatabaseOnOpen: async data => {
const frontMatter = getSqlFrontMatter(data, yaml);
if (frontMatter?.connectionId) {
const connection = await getConnectionInfo({ conid: frontMatter.connectionId });
// console.log('Switching database to', frontMatter.databaseName, 'on connection', connection);
if (connection && frontMatter.databaseName) {
currentDatabase.set({
connection,
name: frontMatter.databaseName,
});
return true;
}
}
return false;
},
};
const shell: FileTypeHandler = {
@@ -161,6 +178,7 @@
import AppObjectCore from './AppObjectCore.svelte';
import { isProApp } from '../utility/proTools';
import { saveFileToDisk } from '../utility/exportFileTools';
import { getConnectionInfo } from '../utility/metadataLoaders';
export let data;
@@ -281,6 +299,10 @@
let tooltip = undefined;
const connProps: any = {};
if (handler.switchDatabaseOnOpen) {
await handler.switchDatabaseOnOpen(dataContent);
}
if (handler.currentConnection) {
const connection = _.get($currentDatabase, 'connection') || {};
const database = _.get($currentDatabase, 'name');

View File

@@ -616,16 +616,13 @@
export function toggleFixedConnection() {
const frontMatter = getSqlFrontMatter($editorValue, yaml);
const currentDatabase = getCurrentDatabase();
setEditorData(
setSqlFrontMatter(
$editorValue,
frontMatter?.connectionId &&
frontMatter?.connectionId == currentDatabase?.connection?._id &&
frontMatter?.databaseName == currentDatabase?.name
frontMatter?.connectionId && frontMatter?.connectionId == conid && frontMatter?.databaseName == database
? { ...frontMatter, connectionId: undefined, databaseName: undefined }
: currentDatabase?.connection?._id
? { ...frontMatter, connectionId: currentDatabase.connection._id, databaseName: currentDatabase.name }
: conid
? { ...frontMatter, connectionId: conid, databaseName: database }
: { ...frontMatter, connectionId: undefined, databaseName: undefined },
yaml
)