imp exp - transfer from one DB to another

This commit is contained in:
Jan Prochazka
2020-06-18 12:17:55 +02:00
parent 3f40996d2d
commit 967c5860c9
4 changed files with 113 additions and 41 deletions

View File

@@ -6,35 +6,116 @@ import engines from '@dbgate/engines';
import { findObjectLike } 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) {
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 };
script.assign(sourceVar, 'queryReader', {
connection: {
..._.pick(connection, ['server', 'engine', 'user', 'password', 'port']),
database: values.sourceDatabaseName,
},
sql: `select * from ${quoteFullName(driver.dialect, fullName)}`,
});
const [sourceConnection, sourceDriver] = await getConnection(
values.sourceStorageType,
values.sourceConnectionId,
values.sourceDatabaseName
);
const [targetConnection, targetDriver] = await getConnection(
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();
if (values.targetStorageType == 'csv') {
script.assign(targetVar, 'csvWriter', {
fileName: `${fullName.pureName}.csv`,
});
}
if (values.targetStorageType == 'jsonl') {
script.assign(targetVar, 'jsonLinesWriter', {
fileName: `${fullName.pureName}.jsonl`,
});
}
// @ts-ignore
script.assign(targetVar, ...getTargetExpr(sourceName, values, targetConnection, targetDriver));
script.copyStream(sourceVar, targetVar);
script.put();
@@ -43,15 +124,7 @@ export default async function createImpExpScript(values) {
return script.s;
}
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;
}
export function getActionOptions(source, values,targetDbinfo) {
export function getActionOptions(source, values, targetDbinfo) {
const res = [];
const targetName = getTargetName(source, values);
if (values.targetStorageType == 'database') {