mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-02 10:13:57 +00:00
fixed importing mysql dump #702
This commit is contained in:
@@ -16,7 +16,7 @@ class ImportStream extends stream.Transform {
|
|||||||
}
|
}
|
||||||
async _transform(chunk, encoding, cb) {
|
async _transform(chunk, encoding, cb) {
|
||||||
try {
|
try {
|
||||||
await this.driver.script(this.pool, chunk);
|
await this.driver.script(this.pool, chunk, { queryOptions: { importSqlDump: true } });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.emit('error', err.message);
|
this.emit('error', err.message);
|
||||||
}
|
}
|
||||||
@@ -47,7 +47,9 @@ async function importDatabase({ connection = undefined, systemConnection = undef
|
|||||||
const pool = systemConnection || (await connectUtility(driver, connection, 'write'));
|
const pool = systemConnection || (await connectUtility(driver, connection, 'write'));
|
||||||
logger.info(`Connected.`);
|
logger.info(`Connected.`);
|
||||||
|
|
||||||
|
logger.info(`Input file: ${inputFile}`);
|
||||||
const downloadedFile = await download(inputFile);
|
const downloadedFile = await download(inputFile);
|
||||||
|
logger.info(`Downloaded file: ${downloadedFile}`);
|
||||||
|
|
||||||
const fileStream = fs.createReadStream(downloadedFile, 'utf-8');
|
const fileStream = fs.createReadStream(downloadedFile, 'utf-8');
|
||||||
const splittedStream = splitQueryStream(fileStream, driver.getQuerySplitterOptions('script'));
|
const splittedStream = splitQueryStream(fileStream, driver.getQuerySplitterOptions('script'));
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ async function runScript(func) {
|
|||||||
await func();
|
await func();
|
||||||
process.exit(0);
|
process.exit(0);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error({ err }, `Error running script: ${err.message}`);
|
logger.error({ err }, `Error running script: ${err.message || err}`);
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ export const driverBase = {
|
|||||||
}
|
}
|
||||||
for (const sqlItem of splitQuery(sql, this.getQuerySplitterOptions('script'))) {
|
for (const sqlItem of splitQuery(sql, this.getQuerySplitterOptions('script'))) {
|
||||||
try {
|
try {
|
||||||
await this.query(pool, sqlItem, { discardResult: true });
|
await this.query(pool, sqlItem, { discardResult: true, ...options?.queryOptions });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (options?.useTransaction && this.supportsTransactions) {
|
if (options?.useTransaction && this.supportsTransactions) {
|
||||||
runCommandOnDriver(pool, this, dmp => dmp.rollbackTransaction());
|
runCommandOnDriver(pool, this, dmp => dmp.rollbackTransaction());
|
||||||
|
|||||||
2
packages/types/engines.d.ts
vendored
2
packages/types/engines.d.ts
vendored
@@ -25,10 +25,12 @@ export interface StreamOptions {
|
|||||||
|
|
||||||
export interface RunScriptOptions {
|
export interface RunScriptOptions {
|
||||||
useTransaction: boolean;
|
useTransaction: boolean;
|
||||||
|
queryOptions?: QueryOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueryOptions {
|
export interface QueryOptions {
|
||||||
discardResult?: boolean;
|
discardResult?: boolean;
|
||||||
|
importSqlDump?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface WriteTableOptions {
|
export interface WriteTableOptions {
|
||||||
|
|||||||
@@ -60,7 +60,7 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
async close(dbhan) {
|
async close(dbhan) {
|
||||||
return dbhan.client.close();
|
return dbhan.client.close();
|
||||||
},
|
},
|
||||||
query(dbhan, sql) {
|
query(dbhan, sql, options) {
|
||||||
if (sql == null) {
|
if (sql == null) {
|
||||||
return {
|
return {
|
||||||
rows: [],
|
rows: [],
|
||||||
@@ -68,6 +68,14 @@ const drivers = driverBases.map(driverBase => ({
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (options?.importSqlDump && sql.trim().startsWith('/*!') && sql.includes('character_set_client')) {
|
||||||
|
// skip this in SQL dumps
|
||||||
|
return {
|
||||||
|
rows: [],
|
||||||
|
columns: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
dbhan.client.query(sql, function (error, results, fields) {
|
dbhan.client.query(sql, function (error, results, fields) {
|
||||||
if (error) reject(error);
|
if (error) reject(error);
|
||||||
|
|||||||
Reference in New Issue
Block a user