unique constraint editor

This commit is contained in:
Jan Prochazka 2021-09-12 20:39:59 +02:00
parent 3760217ff0
commit af75858ce8
3 changed files with 68 additions and 2 deletions

View File

@ -190,7 +190,7 @@ export class SqlDumper implements AlterProcessor {
if (includeNullable) { if (includeNullable) {
this.put(column.notNull ? '^not ^null' : '^null'); this.put(column.notNull ? '^not ^null' : '^null');
} }
if (includeDefault && column.defaultValue != null) { if (includeDefault && column.defaultValue?.trim()) {
this.columnDefault(column); this.columnDefault(column);
} }
} }

View File

@ -35,7 +35,7 @@
}); });
registerCommand({ registerCommand({
id: 'tableEditor.addINdex', id: 'tableEditor.addIndex',
category: 'Table editor', category: 'Table editor',
name: 'Add index', name: 'Add index',
icon: 'icon add-key', icon: 'icon add-key',
@ -44,6 +44,17 @@
testEnabled: () => getCurrentEditor()?.writable(), testEnabled: () => getCurrentEditor()?.writable(),
onClick: () => getCurrentEditor().addIndex(), onClick: () => getCurrentEditor().addIndex(),
}); });
registerCommand({
id: 'tableEditor.addUnique',
category: 'Table editor',
name: 'Add unique',
icon: 'icon add-key',
toolbar: true,
isRelatedToTab: true,
testEnabled: () => getCurrentEditor()?.writable(),
onClick: () => getCurrentEditor().addUnique(),
});
</script> </script>
<script lang="ts"> <script lang="ts">
@ -69,6 +80,7 @@
import ForeignKeyEditorModal from './ForeignKeyEditorModal.svelte'; import ForeignKeyEditorModal from './ForeignKeyEditorModal.svelte';
import IndexEditorModal from './IndexEditorModal.svelte'; import IndexEditorModal from './IndexEditorModal.svelte';
import PrimaryKeyEditorModal from './PrimaryKeyEditorModal.svelte'; import PrimaryKeyEditorModal from './PrimaryKeyEditorModal.svelte';
import UniqueEditorModal from './UniqueEditorModal.svelte';
export const activator = createActivator('TableEditor', true); export const activator = createActivator('TableEditor', true);
@ -118,11 +130,20 @@
}); });
} }
export function addUnique() {
showModal(UniqueEditorModal, {
setTableInfo,
tableInfo,
dbInfo,
});
}
$: columns = tableInfo?.columns; $: columns = tableInfo?.columns;
$: primaryKey = tableInfo?.primaryKey; $: primaryKey = tableInfo?.primaryKey;
$: foreignKeys = tableInfo?.foreignKeys; $: foreignKeys = tableInfo?.foreignKeys;
$: dependencies = tableInfo?.dependencies; $: dependencies = tableInfo?.dependencies;
$: indexes = tableInfo?.indexes; $: indexes = tableInfo?.indexes;
$: uniques = tableInfo?.uniques;
$: { $: {
tableInfo; tableInfo;
@ -223,6 +244,34 @@
onAddNew={addIndex} onAddNew={addIndex}
title={`Indexes (${indexes?.length || 0})`} title={`Indexes (${indexes?.length || 0})`}
clickable={writable()} clickable={writable()}
on:clickrow={e => showModal(UniqueEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })}
columns={[
{
fieldName: 'columns',
header: 'Columns',
slot: 0,
},
writable()
? {
fieldName: 'actions',
sortable: true,
slot: 1,
}
: null,
]}
>
<svelte:fragment slot="name" let:row><ConstraintLabel {...row} /></svelte:fragment>
<svelte:fragment slot="0" let:row>{row?.columns.map(x => x.columnName).join(', ')}</svelte:fragment>
<svelte:fragment slot="1" let:row
><Link onClick={() => setTableInfo(tbl => editorDeleteConstraint(tbl, row))}>Remove</Link></svelte:fragment
>
</ObjectListControl>
<ObjectListControl
collection={uniques}
onAddNew={addUnique}
title={`Unique constraints (${uniques?.length || 0})`}
clickable={writable()}
on:clickrow={e => showModal(IndexEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })} on:clickrow={e => showModal(IndexEditorModal, { constraintInfo: e.detail, tableInfo, setTableInfo })}
columns={[ columns={[
{ {

View File

@ -0,0 +1,17 @@
<script lang="ts">
import ColumnsConstraintEditorModal from './ColumnsConstraintEditorModal.svelte';
export let constraintInfo;
export let setTableInfo;
export let tableInfo;
</script>
<ColumnsConstraintEditorModal
{...$$restProps}
constraintLabel="unique"
constraintType="unique"
{constraintInfo}
{setTableInfo}
{tableInfo}
/>