recreate object warning

This commit is contained in:
Jan Prochazka 2021-09-16 10:48:46 +02:00
parent 3ca0810756
commit e715a95cc0
8 changed files with 75 additions and 19 deletions

View File

@ -30,7 +30,7 @@ async function testDatabaseDiff(conn, driver, mangle, createObject = null) {
mangle(structure2);
structure2 = extendDatabaseInfo(structure2);
const sql = getAlterDatabaseScript(structure1, structure2, {}, structure2, driver);
const { sql } = getAlterDatabaseScript(structure1, structure2, {}, structure2, driver);
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql);

View File

@ -46,7 +46,7 @@ async function testTableDiff(conn, driver, mangle) {
mangle(tget(structure2));
structure2 = extendDatabaseInfo(structure2);
const sql = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver);
const { sql } = getAlterTableScript(tget(structure1), tget(structure2), {}, structure2, driver);
console.log('RUNNING ALTER SQL', driver.engine, ':', sql);
await driver.script(conn, sql);

View File

@ -104,6 +104,12 @@ type AlterOperation =
| AlterOperation_RecreateTable;
export class AlterPlan {
recreates = {
tables: 0,
constraints: 0,
sqlObjects: 0,
};
public operations: AlterOperation[] = [];
constructor(public db: DatabaseInfo, public dialect: SqlDialect) {}
@ -209,6 +215,7 @@ export class AlterPlan {
table,
operations,
});
this.recreates.tables += 1;
}
run(processor: AlterProcessor) {
@ -270,6 +277,10 @@ export class AlterPlan {
return opRes;
}),
];
if (constraints.length > 0) {
this.recreates.constraints += 1;
}
return res;
}
@ -289,6 +300,7 @@ export class AlterPlan {
}
if (op.operationType == 'changeConstraint') {
this.recreates.constraints += 1;
const opDrop: AlterOperation = {
operationType: 'dropConstraint',
oldObject: op.oldObject,
@ -356,6 +368,7 @@ export class AlterPlan {
const table = this.db.tables.find(
x => x.pureName == op[objectField].pureName && x.schemaName == op[objectField].schemaName
);
this.recreates.tables += 1;
return [
{
operationType: 'recreateTable',

View File

@ -348,6 +348,7 @@ export function createAlterDatabasePlan(
} else {
if (newobj == null) plan.dropSqlObject(oldobj);
else if (newobj.createSql != oldobj.createSql) {
plan.recreates.sqlObjects += 1;
plan.dropSqlObject(oldobj);
plan.createSqlObject(newobj);
}
@ -372,13 +373,16 @@ export function getAlterTableScript(
opts: DbDiffOptions,
db: DatabaseInfo,
driver: EngineDriver
): string {
) {
const plan = createAlterTablePlan(oldTable, newTable, opts, db, driver);
const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp);
if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction();
return dmp.s;
return {
sql: dmp.s,
recreates: plan.recreates,
};
}
export function getAlterDatabaseScript(
@ -387,11 +391,14 @@ export function getAlterDatabaseScript(
opts: DbDiffOptions,
db: DatabaseInfo,
driver: EngineDriver
): string {
) {
const plan = createAlterDatabasePlan(oldDb, newDb, opts, db, driver);
const dmp = driver.createDumper();
if (!driver.dialect.disableExplicitTransaction) dmp.beginTransaction();
plan.run(dmp);
if (!driver.dialect.disableExplicitTransaction) dmp.commitTransaction();
return dmp.s;
return {
sql: dmp.s,
recreates: plan.recreates,
};
}

View File

@ -3,6 +3,8 @@
import { getFormContext } from './FormProviderCore.svelte';
import { createEventDispatcher } from 'svelte';
export let disabled;
const dispatch = createEventDispatcher();
const { submitActionRef } = getFormContext();
@ -13,8 +15,10 @@
}
submitActionRef.set(() => {
handleClick();
if (!disabled) {
handleClick();
}
});
</script>
<FormStyledButton type="submit" on:click={handleClick} {...$$props} />
<FormStyledButton type="submit" {disabled} on:click={handleClick} {...$$props} />

View File

@ -1,7 +1,11 @@
<script>
import _ from 'lodash';
import FormStyledButton from '../elements/FormStyledButton.svelte';
import FormCheckboxField from '../forms/FormCheckboxField.svelte';
import FormProvider from '../forms/FormProvider.svelte';
import FormSubmit from '../forms/FormSubmit.svelte';
import FormValues from '../forms/FormValues.svelte';
import FontIcon from '../icons/FontIcon.svelte';
import SqlEditor from '../query/SqlEditor.svelte';
import ModalBase from './ModalBase.svelte';
@ -10,6 +14,11 @@
export let sql;
export let onConfirm;
export let engine;
export let recreates;
$: isRecreated = _.sum(_.values(recreates || {})) > 0;
$: console.log('recreates', recreates);
</script>
<FormProvider>
@ -20,15 +29,32 @@
<SqlEditor {engine} value={sql} readOnly />
</div>
{#if isRecreated}
<div class="form-margin">
<div>
<FontIcon icon="img warn" /> This operation is not directly supported by SQL engine. DbGate can emulate it, but
please check the generated SQL script.
</div>
<FormCheckboxField
templateProps={{ noMargin: true }}
label="Allow recreate (don't use on production databases)"
name="allowRecreate"
/>
</div>
{/if}
<div slot="footer">
<FormSubmit
value="OK"
on:click={() => {
closeCurrentModal();
onConfirm();
}}
/>
<FormStyledButton type="button" value="Close" on:click={closeCurrentModal} />
<FormValues let:values>
<FormSubmit
value="OK"
disabled={isRecreated && !values.allowRecreate}
on:click={() => {
closeCurrentModal();
onConfirm();
}}
/>
<FormStyledButton type="button" value="Close" on:click={closeCurrentModal} />
</FormValues>
</div>
</ModalBase>
</FormProvider>
@ -39,4 +65,8 @@
height: 30vh;
width: 40vw;
}
.form-margin {
margin: var(--dim-large-form-margin);
}
</style>

View File

@ -102,7 +102,7 @@
function doSave(createTableName) {
const driver = findEngineDriver($connection, $extensions);
const sql = getAlterTableScript(
const { sql, recreates } = getAlterTableScript(
$editorValue.base,
fillConstraintNames($editorValue.current, driver.dialect),
{},
@ -112,6 +112,7 @@
showModal(ConfirmSqlModal, {
sql,
recreates,
onConfirm: () => {
handleConfirmSql(sql, createTableName);
},

View File

@ -1,4 +1,4 @@
import _ from 'lodash'
import _ from 'lodash';
import { findEngineDriver, generateDbPairingId, getAlterDatabaseScript } from 'dbgate-tools';
import InputTextModal from '../modals/InputTextModal.svelte';
import { showModal } from '../modals/modalTools';
@ -15,10 +15,11 @@ export async function alterDatabaseDialog(conid, database, updateFunc) {
const dbUpdated = _.cloneDeep(db);
updateFunc(dbUpdated);
const sql = getAlterDatabaseScript(db, dbUpdated, {}, db, driver);
const { sql, recreates } = getAlterDatabaseScript(db, dbUpdated, {}, db, driver);
showModal(ConfirmSqlModal, {
sql,
recreates,
onConfirm: async () => {
const resp = await axiosInstance.request({
url: 'database-connections/run-script',