mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-21 04:46:02 +00:00
data duplicator works in simple case
This commit is contained in:
@@ -1,22 +1,54 @@
|
||||
<script lang="ts" context="module">
|
||||
const getCurrentEditor = () => getActiveComponent('DataDuplicatorTab');
|
||||
|
||||
registerCommand({
|
||||
id: 'dataDuplicator.run',
|
||||
category: 'Data duplicator',
|
||||
name: 'Run',
|
||||
toolbar: true,
|
||||
isRelatedToTab: true,
|
||||
icon: 'icon run',
|
||||
testEnabled: () => getCurrentEditor()?.canRun(),
|
||||
onClick: () => getCurrentEditor().run(),
|
||||
});
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import ro from 'date-fns/locale/ro';
|
||||
|
||||
import { ScriptWriter, ScriptWriterJson } from 'dbgate-tools';
|
||||
|
||||
import _ from 'lodash';
|
||||
import ToolStripCommandButton from '../buttons/ToolStripCommandButton.svelte';
|
||||
import ToolStripContainer from '../buttons/ToolStripContainer.svelte';
|
||||
import invalidateCommands from '../commands/invalidateCommands';
|
||||
import registerCommand from '../commands/registerCommand';
|
||||
import TableControl from '../elements/TableControl.svelte';
|
||||
import CheckboxField from '../forms/CheckboxField.svelte';
|
||||
import SelectField from '../forms/SelectField.svelte';
|
||||
import { extractShellConnection } from '../impexp/createImpExpScript';
|
||||
import useEditorData from '../query/useEditorData';
|
||||
import { currentArchive } from '../stores';
|
||||
import { useArchiveFiles, useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders';
|
||||
import TableStructureTab from './TableStructureTab.svelte';
|
||||
import { currentArchive, getCurrentConfig } from '../stores';
|
||||
import { apiCall, apiOff, apiOn } from '../utility/api';
|
||||
import { changeTab } from '../utility/common';
|
||||
import createActivator, { getActiveComponent } from '../utility/createActivator';
|
||||
import { useArchiveFiles, useArchiveFolders, useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders';
|
||||
import useEffect from '../utility/useEffect';
|
||||
|
||||
export let conid;
|
||||
export let database;
|
||||
export let tabid;
|
||||
|
||||
let busy = false;
|
||||
let runnerId = null;
|
||||
|
||||
export const activator = createActivator('DataDuplicatorTab', true);
|
||||
|
||||
$: connection = useConnectionInfo({ conid });
|
||||
$: dbinfo = useDatabaseInfo({ conid, database });
|
||||
|
||||
$: archiveFiles = useArchiveFiles({ folder: $currentArchive });
|
||||
$: archiveFolders = useArchiveFolders();
|
||||
$: archiveFiles = useArchiveFiles({ folder: $editorState?.value?.archiveFolder });
|
||||
|
||||
$: pairedNames = _.intersectionBy(
|
||||
$dbinfo?.tables?.map(x => x.pureName),
|
||||
@@ -24,6 +56,16 @@
|
||||
(x: string) => _.toUpper(x)
|
||||
);
|
||||
|
||||
$: {
|
||||
changeTab(tabid, tab => ({ ...tab, busy }));
|
||||
}
|
||||
|
||||
$: {
|
||||
busy;
|
||||
runnerId;
|
||||
invalidateCommands();
|
||||
}
|
||||
|
||||
const { editorState, editorValue, setEditorData } = useEditorData({
|
||||
tabid,
|
||||
onInitialData: value => {
|
||||
@@ -41,8 +83,58 @@
|
||||
}));
|
||||
}
|
||||
|
||||
function createScript(forceScript = false) {
|
||||
const config = getCurrentConfig();
|
||||
const script = config.allowShellScripting || forceScript ? new ScriptWriter() : new ScriptWriterJson();
|
||||
script.dataDuplicator({
|
||||
connection: extractShellConnection($connection, database),
|
||||
archive: $editorState.value.archiveFolder,
|
||||
items: tableRows
|
||||
.filter(x => x.isChecked)
|
||||
.map(row => ({
|
||||
name: row.name,
|
||||
operation: row.operation,
|
||||
matchColumns: _.compact([row.matchColumn1]),
|
||||
})),
|
||||
});
|
||||
return script.getScript();
|
||||
}
|
||||
|
||||
export function canRun() {
|
||||
return !!tableRows.find(x => x.isChecked) && !busy;
|
||||
}
|
||||
|
||||
export async function run() {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
const script = await createScript();
|
||||
let runid = runnerId;
|
||||
const resp = await apiCall('runners/start', { script });
|
||||
runid = resp.runid;
|
||||
runnerId = runid;
|
||||
}
|
||||
|
||||
$: effect = useEffect(() => registerRunnerDone(runnerId));
|
||||
|
||||
function registerRunnerDone(rid) {
|
||||
if (rid) {
|
||||
apiOn(`runner-done-${rid}`, handleRunnerDone);
|
||||
return () => {
|
||||
apiOff(`runner-done-${rid}`, handleRunnerDone);
|
||||
};
|
||||
} else {
|
||||
return () => {};
|
||||
}
|
||||
}
|
||||
|
||||
$: $effect;
|
||||
|
||||
const handleRunnerDone = () => {
|
||||
busy = false;
|
||||
};
|
||||
|
||||
// $: console.log('$archiveFiles', $archiveFiles);
|
||||
// $: console.log('$editorState', $editorState);
|
||||
$: console.log('$editorState', $editorState.value);
|
||||
|
||||
$: tableRows = pairedNames.map(name => {
|
||||
const item = $editorState?.value?.tables?.[name];
|
||||
@@ -59,63 +151,87 @@
|
||||
matchColumn1,
|
||||
};
|
||||
});
|
||||
|
||||
$: console.log('$archiveFolders', $archiveFolders);
|
||||
</script>
|
||||
|
||||
<div>
|
||||
<div class="bold m-2">Imported files</div>
|
||||
<ToolStripContainer>
|
||||
<div>
|
||||
<div class="bold m-2">Source archive</div>
|
||||
<SelectField
|
||||
isNative
|
||||
value={$editorState.value?.archiveFolder}
|
||||
on:change={e => {
|
||||
setEditorData(old => ({
|
||||
...old,
|
||||
archiveFolder: e.detail,
|
||||
}));
|
||||
}}
|
||||
options={$archiveFolders?.map(x => ({
|
||||
label: x.name,
|
||||
value: x.name,
|
||||
})) || []}
|
||||
/>
|
||||
|
||||
<TableControl
|
||||
rows={tableRows}
|
||||
columns={[
|
||||
{ header: '', fieldName: 'isChecked', slot: 1 },
|
||||
{ header: 'File=>Table', fieldName: 'name' },
|
||||
{ header: 'Operation', fieldName: 'operation', slot: 2 },
|
||||
{ header: 'Match column', fieldName: 'matchColumn1', slot: 3 },
|
||||
]}
|
||||
>
|
||||
<svelte:fragment slot="1" let:row>
|
||||
<CheckboxField
|
||||
checked={row.isChecked}
|
||||
on:change={e => {
|
||||
changeTable({ ...row, isChecked: e.target.checked });
|
||||
}}
|
||||
/>
|
||||
</svelte:fragment>
|
||||
<svelte:fragment slot="2" let:row>
|
||||
<SelectField
|
||||
isNative
|
||||
value={row.operation}
|
||||
on:change={e => {
|
||||
changeTable({ ...row, operation: e.detail });
|
||||
}}
|
||||
disabled={!row.isChecked}
|
||||
options={[
|
||||
{ label: 'Copy row', value: 'copy' },
|
||||
{ label: 'Lookup (find matching row)', value: 'lookup' },
|
||||
{ label: 'Insert if not exists', value: 'insertMissing' },
|
||||
]}
|
||||
/>
|
||||
</svelte:fragment>
|
||||
<svelte:fragment slot="3" let:row>
|
||||
{#if row.operation != 'copy'}
|
||||
<div class="bold m-2">Imported files</div>
|
||||
|
||||
<TableControl
|
||||
rows={tableRows}
|
||||
columns={[
|
||||
{ header: '', fieldName: 'isChecked', slot: 1 },
|
||||
{ header: 'File=>Table', fieldName: 'name' },
|
||||
{ header: 'Operation', fieldName: 'operation', slot: 2 },
|
||||
{ header: 'Match column', fieldName: 'matchColumn1', slot: 3 },
|
||||
]}
|
||||
>
|
||||
<svelte:fragment slot="1" let:row>
|
||||
<CheckboxField
|
||||
checked={row.isChecked}
|
||||
on:change={e => {
|
||||
changeTable({ ...row, isChecked: e.target.checked });
|
||||
}}
|
||||
/>
|
||||
</svelte:fragment>
|
||||
<svelte:fragment slot="2" let:row>
|
||||
<SelectField
|
||||
isNative
|
||||
value={row.matchColumn1}
|
||||
value={row.operation}
|
||||
on:change={e => {
|
||||
changeTable({ ...row, matchColumn1: e.detail });
|
||||
changeTable({ ...row, operation: e.detail });
|
||||
}}
|
||||
disabled={!row.isChecked}
|
||||
options={$dbinfo?.tables
|
||||
?.find(x => x.pureName?.toUpperCase() == row.name.toUpperCase())
|
||||
?.columns?.map(col => ({
|
||||
label: col.columnName,
|
||||
value: col.columnName,
|
||||
})) || []}
|
||||
options={[
|
||||
{ label: 'Copy row', value: 'copy' },
|
||||
{ label: 'Lookup (find matching row)', value: 'lookup' },
|
||||
{ label: 'Insert if not exists', value: 'insertMissing' },
|
||||
]}
|
||||
/>
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
</TableControl>
|
||||
</div>
|
||||
</svelte:fragment>
|
||||
<svelte:fragment slot="3" let:row>
|
||||
{#if row.operation != 'copy'}
|
||||
<SelectField
|
||||
isNative
|
||||
value={row.matchColumn1}
|
||||
on:change={e => {
|
||||
changeTable({ ...row, matchColumn1: e.detail });
|
||||
}}
|
||||
disabled={!row.isChecked}
|
||||
options={$dbinfo?.tables
|
||||
?.find(x => x.pureName?.toUpperCase() == row.name.toUpperCase())
|
||||
?.columns?.map(col => ({
|
||||
label: col.columnName,
|
||||
value: col.columnName,
|
||||
})) || []}
|
||||
/>
|
||||
{/if}
|
||||
</svelte:fragment>
|
||||
</TableControl>
|
||||
</div>
|
||||
<svelte:fragment slot="toolstrip">
|
||||
<ToolStripCommandButton command="dataDuplicator.run" />
|
||||
</svelte:fragment>
|
||||
</ToolStripContainer>
|
||||
|
||||
<!-- <div>
|
||||
{#each pairedNames as name}
|
||||
<div>{name}</div>
|
||||
|
||||
Reference in New Issue
Block a user