schema update in database analyser

This commit is contained in:
SPRINX0\prochazka
2024-09-18 15:37:34 +02:00
parent 327d43096f
commit 5ab686b721
9 changed files with 101 additions and 22 deletions

View File

@@ -5,6 +5,7 @@ import _pick from 'lodash/pick';
import _compact from 'lodash/compact';
import { getLogger } from './getLogger';
import { type Logger } from 'pinomin';
import stableStringify from 'json-stable-stringify';
const logger = getLogger('dbAnalyser');
@@ -70,7 +71,10 @@ export class DatabaseAnalyser {
async fullAnalysis() {
const res = this.addEngineField(await this._runAnalysis());
// console.log('FULL ANALYSIS', res);
return res;
return {
...res,
schemas: await this.readSchemaList(),
};
}
async singleObjectAnalysis(name, typeField) {
@@ -87,6 +91,10 @@ export class DatabaseAnalyser {
return obj;
}
async readSchemaList() {
return undefined;
}
async incrementalAnalysis(structure) {
this.structure = structure;
@@ -99,20 +107,29 @@ export class DatabaseAnalyser {
const structureModifications = modifications.filter(x => x.action != 'setTableRowCounts');
const setTableRowCounts = modifications.find(x => x.action == 'setTableRowCounts');
let structureWithRowCounts = null;
let structureUpdated = null;
if (setTableRowCounts) {
const newStructure = mergeTableRowCounts(structure, setTableRowCounts.rowCounts);
if (areDifferentRowCounts(structure, newStructure)) {
structureWithRowCounts = newStructure;
structureUpdated = newStructure;
}
}
const schemas = await this.readSchemaList();
const areSchemasDifferent = stableStringify(schemas) != stableStringify(this.structure.schemas);
if (areSchemasDifferent) {
structureUpdated = {
...structureUpdated,
schemas,
};
}
if (structureModifications.length == 0) {
return structureWithRowCounts ? this.addEngineField(structureWithRowCounts) : null;
return structureUpdated ? this.addEngineField(structureUpdated) : null;
}
this.modifications = structureModifications;
if (structureWithRowCounts) this.structure = structureWithRowCounts;
if (structureUpdated) this.structure = structureUpdated;
logger.info({ modifications: this.modifications }, 'DB modifications detected:');
return this.addEngineField(this.mergeAnalyseResult(await this._runAnalysis()));
}

View File

@@ -214,6 +214,14 @@ export class SqlDumper implements AlterProcessor {
this.putCmd('^drop ^database %i', name);
}
createSchema(name: string) {
this.putCmd('^create ^schema %i', name);
}
dropSchema(name: string) {
this.putCmd('^drop ^schema %i', name);
}
specialColumnOptions(column) {}
selectScopeIdentity(table: TableInfo) {}

View File

@@ -84,7 +84,17 @@ export const driverBase = {
}
},
async operation(pool, operation, options: RunScriptOptions) {
throw new Error('Operation not defined in target driver');
const { type } = operation;
switch (type) {
case 'createSchema':
await runCommandOnDriver(pool, this, dmp => dmp.createSchema(operation.schemaName));
break;
case 'dropSchema':
await runCommandOnDriver(pool, this, dmp => dmp.dropSchema(operation.schemaName));
break;
default:
throw new Error(`Operation type ${type} not supported`);
}
},
getNewObjectTemplates() {
if (this.databaseEngineTypes.includes('sql')) {
@@ -180,5 +190,5 @@ export const driverBase = {
adaptTableInfo(table) {
return table;
}
},
};

View File

@@ -5,6 +5,10 @@
import _ from 'lodash';
import FontIcon from '../icons/FontIcon.svelte';
import { DatabaseInfo } from 'dbgate-types';
import { showModal } from '../modals/modalTools';
import ConfirmModal from '../modals/ConfirmModal.svelte';
import { runOperationOnDatabase } from '../modals/ConfirmSqlModal.svelte';
import InputTextModal from '../modals/InputTextModal.svelte';
export let dbinfo: DatabaseInfo;
export let selectedSchema;
@@ -13,6 +17,9 @@
export let onApplySelectedSchema;
export let valueStorageKey;
export let conid;
export let database;
let appliedSchema;
$: {
@@ -45,8 +52,33 @@
);
$: countBySchema = computeCountBySchema(objectList ?? []);
function handleAddNewSchema() {
// runCommand('add-schema', { conid: dbinfo.conid, database: dbinfo.database });
function handleCreateSchema() {
showModal(InputTextModal, {
header: 'Create schema',
value: 'newschema',
label: 'Schema name',
onConfirm: async name => {
const dbid = { conid, database };
await runOperationOnDatabase(dbid, {
type: 'createSchema',
schemaName: name,
});
selectedSchema = name;
},
});
}
function handleDropSchema() {
showModal(ConfirmModal, {
message: `Really drop schema ${appliedSchema}?`,
onConfirm: async () => {
const dbid = { conid, database };
runOperationOnDatabase(dbid, {
type: 'dropSchema',
schemaName: appliedSchema,
});
selectedSchema = null;
},
});
}
$: selectedSchema = localStorage.getItem(valueStorageKey ?? '');
@@ -81,10 +113,10 @@
<FontIcon icon="icon close" />
</InlineButton>
{/if}
<InlineButton on:click={handleAddNewSchema} title="Add new schema" square>
<InlineButton on:click={handleCreateSchema} title="Add new schema" square>
<FontIcon icon="icon plus-thick" />
</InlineButton>
<InlineButton on:click={handleAddNewSchema} title="Delete schema" square>
<InlineButton on:click={handleDropSchema} title="Delete schema" square disabled={!appliedSchema}>
<FontIcon icon="icon minus-thick" />
</InlineButton>
</div>

View File

@@ -151,6 +151,8 @@
appliedSelectedSchema = x;
}}
valueStorageKey={`sql-object-list-schema-${conid}-${database}`}
{conid}
{database}
/>
<WidgetsInnerContainer>