mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-03 00:13:57 +00:00
imp exp - transfer from one DB to another
This commit is contained in:
@@ -115,7 +115,7 @@ function Menu({ data, makeAppObj, setOpenedTabs, showModal }) {
|
|||||||
sourceConnectionId: data.conid,
|
sourceConnectionId: data.conid,
|
||||||
sourceDatabaseName: data.database,
|
sourceDatabaseName: data.database,
|
||||||
sourceSchemaName: data.schemaName,
|
sourceSchemaName: data.schemaName,
|
||||||
sourceTables: [data.pureName],
|
sourceList: [data.pureName],
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -605,7 +605,7 @@ export default function DataGridCore(props) {
|
|||||||
sourceConnectionId: conid,
|
sourceConnectionId: conid,
|
||||||
sourceDatabaseName: database,
|
sourceDatabaseName: database,
|
||||||
sourceSchemaName: display.baseTable && display.baseTable.schemaName,
|
sourceSchemaName: display.baseTable && display.baseTable.schemaName,
|
||||||
sourceTables: display.baseTable ? [display.baseTable.pureName] : [],
|
sourceList: display.baseTable ? [display.baseTable.pureName] : [],
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
));
|
));
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ function SourceTargetConfig({
|
|||||||
connectionIdField,
|
connectionIdField,
|
||||||
databaseNameField,
|
databaseNameField,
|
||||||
schemaNameField,
|
schemaNameField,
|
||||||
tablesField,
|
tablesField = undefined,
|
||||||
}) {
|
}) {
|
||||||
const types = [
|
const types = [
|
||||||
{ value: 'database', label: 'Database', directions: ['source', 'target'] },
|
{ value: 'database', label: 'Database', directions: ['source', 'target'] },
|
||||||
@@ -97,7 +97,7 @@ function SourceTargetConfig({
|
|||||||
<FormDatabaseSelect conidName={connectionIdField} name={databaseNameField} />
|
<FormDatabaseSelect conidName={connectionIdField} name={databaseNameField} />
|
||||||
<Label>Schema</Label>
|
<Label>Schema</Label>
|
||||||
<FormSchemaSelect conidName={connectionIdField} databaseName={databaseNameField} name={schemaNameField} />
|
<FormSchemaSelect conidName={connectionIdField} databaseName={databaseNameField} name={schemaNameField} />
|
||||||
{direction == 'source' && (
|
{tablesField && (
|
||||||
<>
|
<>
|
||||||
<Label>Tables/views</Label>
|
<Label>Tables/views</Label>
|
||||||
<FormTablesSelect
|
<FormTablesSelect
|
||||||
@@ -117,7 +117,7 @@ function SourceTargetConfig({
|
|||||||
export default function ImportExportConfigurator() {
|
export default function ImportExportConfigurator() {
|
||||||
const { values, setFieldValue } = useFormikContext();
|
const { values, setFieldValue } = useFormikContext();
|
||||||
const targetDbinfo = useDatabaseInfo({ conid: values.targetConnectionId, database: values.targetDatabaseName });
|
const targetDbinfo = useDatabaseInfo({ conid: values.targetConnectionId, database: values.targetDatabaseName });
|
||||||
const sources = values.sourceTables;
|
const { sourceList } = values;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container>
|
<Container>
|
||||||
@@ -128,7 +128,7 @@ export default function ImportExportConfigurator() {
|
|||||||
connectionIdField="sourceConnectionId"
|
connectionIdField="sourceConnectionId"
|
||||||
databaseNameField="sourceDatabaseName"
|
databaseNameField="sourceDatabaseName"
|
||||||
schemaNameField="sourceSchemaName"
|
schemaNameField="sourceSchemaName"
|
||||||
tablesField="sourceTables"
|
tablesField="sourceList"
|
||||||
/>
|
/>
|
||||||
<SourceTargetConfig
|
<SourceTargetConfig
|
||||||
direction="target"
|
direction="target"
|
||||||
@@ -136,10 +136,9 @@ export default function ImportExportConfigurator() {
|
|||||||
connectionIdField="targetConnectionId"
|
connectionIdField="targetConnectionId"
|
||||||
databaseNameField="targetDatabaseName"
|
databaseNameField="targetDatabaseName"
|
||||||
schemaNameField="targetSchemaName"
|
schemaNameField="targetSchemaName"
|
||||||
tablesField="targetTables"
|
|
||||||
/>
|
/>
|
||||||
</Wrapper>
|
</Wrapper>
|
||||||
<TableControl rows={sources || []}>
|
<TableControl rows={sourceList || []}>
|
||||||
<TableColumn fieldName="source" header="Source" formatter={(row) => row} />
|
<TableColumn fieldName="source" header="Source" formatter={(row) => row} />
|
||||||
<TableColumn
|
<TableColumn
|
||||||
fieldName="action"
|
fieldName="action"
|
||||||
|
|||||||
@@ -6,35 +6,116 @@ import engines from '@dbgate/engines';
|
|||||||
import { findObjectLike } from '@dbgate/datalib';
|
import { findObjectLike } from '@dbgate/datalib';
|
||||||
import { quoteFullName, fullNameFromString } from '@dbgate/datalib';
|
import { quoteFullName, fullNameFromString } from '@dbgate/datalib';
|
||||||
|
|
||||||
|
export function getTargetName(source, values) {
|
||||||
|
const key = `targetName_${source}`;
|
||||||
|
if (values[key]) return values[key];
|
||||||
|
if (values.targetStorageType == 'csv') return `${source}.csv`;
|
||||||
|
if (values.targetStorageType == 'jsonl') return `${source}.jsonl`;
|
||||||
|
return source;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getConnection(storageType, conid, database) {
|
||||||
|
if (storageType == 'database') {
|
||||||
|
const conn = await getConnectionInfo({ conid });
|
||||||
|
const driver = engines(conn);
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
..._.pick(conn, ['server', 'engine', 'user', 'password', 'port']),
|
||||||
|
database,
|
||||||
|
},
|
||||||
|
driver,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
return [null, null];
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSourceExpr(sourceName, values, sourceConnection, sourceDriver) {
|
||||||
|
if (values.sourceStorageType == 'database') {
|
||||||
|
const fullName = { schemaName: values.sourceSchemaName, pureName: sourceName };
|
||||||
|
return [
|
||||||
|
'queryReader',
|
||||||
|
{
|
||||||
|
connection: sourceConnection,
|
||||||
|
// @ts-ignore
|
||||||
|
sql: `select * from ${quoteFullName(sourceDriver.dialect, fullName)}`,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFlagsFroAction(action) {
|
||||||
|
switch (action) {
|
||||||
|
case 'dropCreateTable':
|
||||||
|
return {
|
||||||
|
createIfNotExists: true,
|
||||||
|
dropIfExists: true,
|
||||||
|
};
|
||||||
|
case 'truncate':
|
||||||
|
return {
|
||||||
|
createIfNotExists: true,
|
||||||
|
truncate: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
createIfNotExists: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTargetExpr(sourceName, values, targetConnection, targetDriver) {
|
||||||
|
if (values.targetStorageType == 'csv') {
|
||||||
|
return [
|
||||||
|
'csvWriter',
|
||||||
|
{
|
||||||
|
fileName: getTargetName(sourceName, values),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if (values.targetStorageType == 'jsonl') {
|
||||||
|
return [
|
||||||
|
'jsonLinesWriter',
|
||||||
|
{
|
||||||
|
fileName: getTargetName(sourceName, values),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
if (values.targetStorageType == 'database') {
|
||||||
|
return [
|
||||||
|
'tableWriter',
|
||||||
|
{
|
||||||
|
connection: targetConnection,
|
||||||
|
schemaName: values.targetSchemaName,
|
||||||
|
pureName: getTargetName(sourceName, values),
|
||||||
|
...getFlagsFroAction(values[`actionType_${sourceName}`]),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default async function createImpExpScript(values) {
|
export default async function createImpExpScript(values) {
|
||||||
const script = new ScriptWriter();
|
const script = new ScriptWriter();
|
||||||
if (values.sourceStorageType == 'database') {
|
|
||||||
const tables = getAsArray(values.sourceTables);
|
|
||||||
for (const table of tables) {
|
|
||||||
const sourceVar = script.allocVariable();
|
|
||||||
const connection = await getConnectionInfo({ conid: values.sourceConnectionId });
|
|
||||||
const driver = engines(connection);
|
|
||||||
|
|
||||||
const fullName = { schemaName: values.sourceSchemaName, pureName: table };
|
const [sourceConnection, sourceDriver] = await getConnection(
|
||||||
script.assign(sourceVar, 'queryReader', {
|
values.sourceStorageType,
|
||||||
connection: {
|
values.sourceConnectionId,
|
||||||
..._.pick(connection, ['server', 'engine', 'user', 'password', 'port']),
|
values.sourceDatabaseName
|
||||||
database: values.sourceDatabaseName,
|
);
|
||||||
},
|
const [targetConnection, targetDriver] = await getConnection(
|
||||||
sql: `select * from ${quoteFullName(driver.dialect, fullName)}`,
|
values.targetStorageType,
|
||||||
});
|
values.targetConnectionId,
|
||||||
|
values.targetDatabaseName
|
||||||
|
);
|
||||||
|
|
||||||
|
if (values.sourceStorageType == 'database') {
|
||||||
|
const sourceList = getAsArray(values.sourceList);
|
||||||
|
for (const sourceName of sourceList) {
|
||||||
|
const sourceVar = script.allocVariable();
|
||||||
|
// @ts-ignore
|
||||||
|
script.assign(sourceVar, ...getSourceExpr(sourceName, values, sourceConnection, sourceDriver));
|
||||||
|
|
||||||
const targetVar = script.allocVariable();
|
const targetVar = script.allocVariable();
|
||||||
if (values.targetStorageType == 'csv') {
|
// @ts-ignore
|
||||||
script.assign(targetVar, 'csvWriter', {
|
script.assign(targetVar, ...getTargetExpr(sourceName, values, targetConnection, targetDriver));
|
||||||
fileName: `${fullName.pureName}.csv`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (values.targetStorageType == 'jsonl') {
|
|
||||||
script.assign(targetVar, 'jsonLinesWriter', {
|
|
||||||
fileName: `${fullName.pureName}.jsonl`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
script.copyStream(sourceVar, targetVar);
|
script.copyStream(sourceVar, targetVar);
|
||||||
script.put();
|
script.put();
|
||||||
@@ -43,15 +124,7 @@ export default async function createImpExpScript(values) {
|
|||||||
return script.s;
|
return script.s;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getTargetName(source, values) {
|
export function getActionOptions(source, values, targetDbinfo) {
|
||||||
const key = `targetName_${source}`;
|
|
||||||
if (values[key]) return values[key];
|
|
||||||
if (values.targetStorageType == 'csv') return `${source}.csv`;
|
|
||||||
if (values.targetStorageType == 'jsonl') return `${source}.jsonl`;
|
|
||||||
return source;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getActionOptions(source, values,targetDbinfo) {
|
|
||||||
const res = [];
|
const res = [];
|
||||||
const targetName = getTargetName(source, values);
|
const targetName = getTargetName(source, values);
|
||||||
if (values.targetStorageType == 'database') {
|
if (values.targetStorageType == 'database') {
|
||||||
|
|||||||
Reference in New Issue
Block a user