mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-17 23:45:59 +00:00
SYNC: restore table fixes & backup table tests
This commit is contained in:
committed by
Diflow
parent
ef23f0d18e
commit
ec06a7d861
@@ -44,6 +44,7 @@
|
||||
<svelte:fragment slot="footer">
|
||||
<FormSubmit
|
||||
value={_t('datagrid.closeTabs.close', { defaultMessage: 'Close tabs' })}
|
||||
data-testid="CloseTabModal_buttonConfirm"
|
||||
on:click={() => {
|
||||
closeCurrentModal();
|
||||
onConfirm();
|
||||
@@ -52,6 +53,7 @@
|
||||
<FormStyledButton
|
||||
type="button"
|
||||
value={_t('common.cancel', { defaultMessage: 'Cancel' })}
|
||||
data-testid="CloseTabModal_buttonCancel"
|
||||
on:click={() => {
|
||||
closeCurrentModal();
|
||||
onCancel();
|
||||
|
||||
@@ -13,6 +13,10 @@ export function createTableRestoreScript(backupTable: TableInfo, originalTable:
|
||||
);
|
||||
const valueColumns = _.difference(bothColumns, keyColumns);
|
||||
|
||||
if (keyColumns.length === 0) {
|
||||
throw new Error('Cannot create restore script: no key columns found');
|
||||
}
|
||||
|
||||
function makeColumnCond(colName: string, operator: '=' | '<>' | '<' | '>' | '<=' | '>=' = '='): Condition {
|
||||
return {
|
||||
conditionType: 'binary',
|
||||
@@ -30,6 +34,30 @@ export function createTableRestoreScript(backupTable: TableInfo, originalTable:
|
||||
};
|
||||
}
|
||||
|
||||
function makeNullNotNullCond(colName: string, isCond1: boolean): Condition {
|
||||
return {
|
||||
conditionType: 'and',
|
||||
conditions: [
|
||||
{
|
||||
conditionType: 'isNull',
|
||||
expr: {
|
||||
exprType: 'column',
|
||||
columnName: colName,
|
||||
source: isCond1 ? { name: originalTable } : { alias: 'bak' },
|
||||
},
|
||||
},
|
||||
{
|
||||
conditionType: 'isNotNull',
|
||||
expr: {
|
||||
exprType: 'column',
|
||||
columnName: colName,
|
||||
source: isCond1 ? { alias: 'bak' } : { name: originalTable },
|
||||
},
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
function putTitle(title: string) {
|
||||
dmp.putRaw('\n\n');
|
||||
dmp.comment(`******************** ${title} ********************`);
|
||||
@@ -45,50 +73,56 @@ export function createTableRestoreScript(backupTable: TableInfo, originalTable:
|
||||
dmp.comment(`Follows UPDATE, DELETE, INSERT statements to restore data`);
|
||||
dmp.putRaw('\n');
|
||||
|
||||
const update: Update = {
|
||||
commandType: 'update',
|
||||
from: { name: originalTable },
|
||||
fields: valueColumns.map(colName => ({
|
||||
exprType: 'select',
|
||||
select: {
|
||||
commandType: 'select',
|
||||
from: { name: backupTable, alias: 'bak' },
|
||||
columns: [
|
||||
{
|
||||
exprType: 'column',
|
||||
columnName: colName,
|
||||
source: { alias: 'bak' },
|
||||
},
|
||||
],
|
||||
where: {
|
||||
conditionType: 'and',
|
||||
conditions: keyColumns.map(colName => makeColumnCond(colName)),
|
||||
},
|
||||
},
|
||||
targetColumn: colName,
|
||||
})),
|
||||
where: {
|
||||
conditionType: 'exists',
|
||||
subQuery: {
|
||||
commandType: 'select',
|
||||
from: { name: backupTable, alias: 'bak' },
|
||||
selectAll: true,
|
||||
where: {
|
||||
conditionType: 'and',
|
||||
conditions: [
|
||||
...keyColumns.map(keyColName => makeColumnCond(keyColName)),
|
||||
if (valueColumns.length > 0) {
|
||||
const update: Update = {
|
||||
commandType: 'update',
|
||||
from: { name: originalTable },
|
||||
fields: valueColumns.map(colName => ({
|
||||
exprType: 'select',
|
||||
select: {
|
||||
commandType: 'select',
|
||||
from: { name: backupTable, alias: 'bak' },
|
||||
columns: [
|
||||
{
|
||||
conditionType: 'or',
|
||||
conditions: valueColumns.map(colName => makeColumnCond(colName, '<>')),
|
||||
exprType: 'column',
|
||||
columnName: colName,
|
||||
source: { alias: 'bak' },
|
||||
},
|
||||
],
|
||||
where: {
|
||||
conditionType: 'and',
|
||||
conditions: keyColumns.map(colName => makeColumnCond(colName)),
|
||||
},
|
||||
},
|
||||
targetColumn: colName,
|
||||
})),
|
||||
where: {
|
||||
conditionType: 'exists',
|
||||
subQuery: {
|
||||
commandType: 'select',
|
||||
from: { name: backupTable, alias: 'bak' },
|
||||
selectAll: true,
|
||||
where: {
|
||||
conditionType: 'and',
|
||||
conditions: [
|
||||
...keyColumns.map(keyColName => makeColumnCond(keyColName)),
|
||||
{
|
||||
conditionType: 'or',
|
||||
conditions: valueColumns.flatMap(colName => [
|
||||
makeColumnCond(colName, '<>'),
|
||||
makeNullNotNullCond(colName, true),
|
||||
makeNullNotNullCond(colName, false),
|
||||
]),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
putTitle('UPDATE');
|
||||
dumpSqlUpdate(dmp, update);
|
||||
dmp.endCommand();
|
||||
};
|
||||
putTitle('UPDATE');
|
||||
dumpSqlUpdate(dmp, update);
|
||||
dmp.endCommand();
|
||||
}
|
||||
|
||||
const delcmd: Delete = {
|
||||
commandType: 'delete',
|
||||
@@ -127,6 +161,14 @@ export function createTableRestoreScript(backupTable: TableInfo, originalTable:
|
||||
};
|
||||
|
||||
putTitle('INSERT');
|
||||
|
||||
const autoinc = originalTable.columns.find(x => x.autoIncrement);
|
||||
if (autoinc) {
|
||||
dmp.allowIdentityInsert(originalTable, true);
|
||||
}
|
||||
dumpSqlInsert(dmp, insert);
|
||||
dmp.endCommand();
|
||||
if (autoinc) {
|
||||
dmp.allowIdentityInsert(originalTable, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user