From 624ada2873913afef2a070614a799cf1a2da4c0c Mon Sep 17 00:00:00 2001 From: Jan Prochazka Date: Sun, 12 Sep 2021 17:33:51 +0200 Subject: [PATCH] alter table --- packages/tools/src/alterPlan.ts | 11 +++-- packages/tools/src/nameTools.ts | 28 ++++++++++++- packages/tools/src/schemaEditorTools.ts | 40 ++++++++++++++++++- .../ForeignKeyObjectListControl.svelte | 2 + .../web/src/elements/ObjectListControl.svelte | 12 ++++-- .../src/tableeditor/ColumnEditorModal.svelte | 2 +- .../ColumnsConstraintEditorModal.svelte | 7 +++- .../src/tableeditor/IndexEditorModal.svelte | 28 ++++++++++++- .../web/src/tableeditor/TableEditor.svelte | 23 +++++++++++ .../web/src/tabs/TableStructureTab.svelte | 4 +- 10 files changed, 141 insertions(+), 16 deletions(-) diff --git a/packages/tools/src/alterPlan.ts b/packages/tools/src/alterPlan.ts index a3a0a738..3e0a64c3 100644 --- a/packages/tools/src/alterPlan.ts +++ b/packages/tools/src/alterPlan.ts @@ -248,10 +248,13 @@ export class AlterPlan { if (op.operationType == 'dropTable') { return [ - ...(op.oldObject.dependencies || []).map(oldObject => ({ - operationType: 'dropConstraint', - oldObject, - })), + ...(op.oldObject.dependencies || []).map(oldObject => { + const opRes: AlterOperation = { + operationType: 'dropConstraint', + oldObject, + }; + return opRes; + }), op, ]; } diff --git a/packages/tools/src/nameTools.ts b/packages/tools/src/nameTools.ts index 3cdb52b0..f5b5fabd 100644 --- a/packages/tools/src/nameTools.ts +++ b/packages/tools/src/nameTools.ts @@ -1,4 +1,5 @@ -import { ColumnInfo, DatabaseInfo, DatabaseInfoObjects, TableInfo } from 'dbgate-types'; +import _ from 'lodash'; +import { ColumnInfo, ColumnReference, DatabaseInfo, DatabaseInfoObjects, TableInfo } from 'dbgate-types'; export function fullNameFromString(name) { const m = name.match(/\[([^\]]+)\]\.\[([^\]]+)\]/); @@ -68,3 +69,28 @@ export function makeUniqueColumnNames(res: ColumnInfo[]) { usedNames.add(res[i].columnName); } } + +function columnsConstraintName(prefix: string, table: TableInfo, columns: ColumnReference[]) { + return `${prefix}_${table.pureName}_${columns.map(x => x.columnName.replace(' ', '_')).join('_')}`; +} + +export function fillConstraintNames(table: TableInfo) { + if (!table) return table; + const res = _.cloneDeep(table); + if (res.primaryKey && !res.primaryKey.constraintName) { + res.primaryKey.constraintName = `PK_${res.pureName}`; + } + for (const fk of res.foreignKeys) { + if (fk.constraintName) continue; + fk.constraintName = columnsConstraintName('FK', res, fk.columns); + } + for (const ix of res.indexes) { + if (ix.constraintName) continue; + ix.constraintName = columnsConstraintName('IX', res, ix.columns); + } + for (const uq of res.uniques) { + if (uq.constraintName) continue; + uq.constraintName = columnsConstraintName('UQ', res, uq.columns); + } + return res; +} diff --git a/packages/tools/src/schemaEditorTools.ts b/packages/tools/src/schemaEditorTools.ts index 236f251e..a73a12f7 100644 --- a/packages/tools/src/schemaEditorTools.ts +++ b/packages/tools/src/schemaEditorTools.ts @@ -1,6 +1,14 @@ import uuidv1 from 'uuid/v1'; import _omit from 'lodash/omit'; -import { ColumnInfo, ConstraintInfo, ForeignKeyInfo, PrimaryKeyInfo, TableInfo } from 'dbgate-types'; +import { + ColumnInfo, + ConstraintInfo, + ForeignKeyInfo, + IndexInfo, + PrimaryKeyInfo, + TableInfo, + UniqueInfo, +} from 'dbgate-types'; export interface EditorColumnInfo extends ColumnInfo { isPrimaryKey?: boolean; @@ -8,7 +16,7 @@ export interface EditorColumnInfo extends ColumnInfo { export function fillEditorColumnInfo(column: ColumnInfo, table: TableInfo): EditorColumnInfo { return { - isPrimaryKey: !!(table?.primaryKey && table.primaryKey.columns.find(x => x.columnName == column.columnName)), + isPrimaryKey: !!table?.primaryKey?.columns?.find(x => x.columnName == column.columnName), dataType: column ? undefined : 'int', ...column, }; @@ -118,6 +126,26 @@ export function editorAddConstraint(table: TableInfo, constraint: ConstraintInfo ]; } + if (constraint.constraintType == 'index') { + res.indexes = [ + ...(res.indexes || []), + { + pairingId: uuidv1(), + ...constraint, + } as IndexInfo, + ]; + } + + if (constraint.constraintType == 'unique') { + res.uniques = [ + ...(res.uniques || []), + { + pairingId: uuidv1(), + ...constraint, + } as UniqueInfo, + ]; + } + return res; } @@ -139,6 +167,14 @@ export function editorModifyConstraint(table: TableInfo, constraint: ConstraintI ); } + if (constraint.constraintType == 'index') { + res.indexes = table.indexes.map(fk => (fk.pairingId == constraint.pairingId ? { ...fk, ...constraint } : fk)); + } + + if (constraint.constraintType == 'unique') { + res.uniques = table.uniques.map(fk => (fk.pairingId == constraint.pairingId ? { ...fk, ...constraint } : fk)); + } + return res; } diff --git a/packages/web/src/elements/ForeignKeyObjectListControl.svelte b/packages/web/src/elements/ForeignKeyObjectListControl.svelte index 704c0aa7..e934c645 100644 --- a/packages/web/src/elements/ForeignKeyObjectListControl.svelte +++ b/packages/web/src/elements/ForeignKeyObjectListControl.svelte @@ -10,11 +10,13 @@ export let title; export let clickable; export let onRemove = null; + export let onAddNew = null; + import FontIcon from '../icons/FontIcon.svelte'; + + import Link from './Link.svelte'; + import TableControl from './TableControl.svelte'; export let title; @@ -6,13 +10,16 @@ export let columns; export let showIfEmpty = false; export let clickable; - + export let onAddNew; {#if collection?.length > 0 || showIfEmpty}
- {title} + {title} + {#if onAddNew} + Add new + {/if}
diff --git a/packages/web/src/tableeditor/ColumnEditorModal.svelte b/packages/web/src/tableeditor/ColumnEditorModal.svelte index 7464ee1b..bad74949 100644 --- a/packages/web/src/tableeditor/ColumnEditorModal.svelte +++ b/packages/web/src/tableeditor/ColumnEditorModal.svelte @@ -22,7 +22,7 @@ export let onAddNext; - + {columnInfo ? 'Edit column' : `Add column ${(tableInfo?.columns || []).length + 1}`}
Column {index + 1}
-
+
{#key column.columnName} {/key}
+ {#if $$slots.column} +
+ (columns = changeFunc(columns))} {index} /> +
+ {/if}
+ import SelectField from '../forms/SelectField.svelte'; + import ColumnsConstraintEditorModal from './ColumnsConstraintEditorModal.svelte'; export let constraintInfo; export let setTableInfo; export let tableInfo; - +> + + { + setColumns(columns => + columns.map((col, i) => + i == index + ? { + ...col, + isDescending: e.detail == 'desc', + } + : col + ) + ); + }} + /> + + diff --git a/packages/web/src/tableeditor/TableEditor.svelte b/packages/web/src/tableeditor/TableEditor.svelte index 943d37e2..c7f11ae2 100644 --- a/packages/web/src/tableeditor/TableEditor.svelte +++ b/packages/web/src/tableeditor/TableEditor.svelte @@ -33,6 +33,17 @@ testEnabled: () => getCurrentEditor()?.writable(), onClick: () => getCurrentEditor().addForeignKey(), }); + + registerCommand({ + id: 'tableEditor.addINdex', + category: 'Table editor', + name: 'Add index', + icon: 'icon add-key', + toolbar: true, + isRelatedToTab: true, + testEnabled: () => getCurrentEditor()?.writable(), + onClick: () => getCurrentEditor().addIndex(), + });