-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathedit-database-schema.component.ts
More file actions
444 lines (415 loc) · 14.7 KB
/
edit-database-schema.component.ts
File metadata and controls
444 lines (415 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import { TextFieldModule } from '@angular/cdk/text-field';
import { CommonModule } from '@angular/common';
import { Component, computed, EventEmitter, Input, inject, OnInit, Output, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatIconModule } from '@angular/material/icon';
import { MatInputModule } from '@angular/material/input';
import { MatTooltipModule } from '@angular/material/tooltip';
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
import { Parser } from 'node-sql-parser';
import { SchemaChangeResponse, TableSchemaService } from 'src/app/services/table-schema.service';
import { SchemaDiagramViewerComponent } from './schema-diagram-viewer/schema-diagram-viewer.component';
interface ParsedColumn {
name: string;
type: string;
pk: boolean;
fk: boolean;
referenceTable?: string;
notNull: boolean;
unique: boolean;
defaultValue?: string;
}
interface ParsedChange {
id: string;
action: string;
icon: string;
tone: 'add' | 'edit' | 'remove';
targetTableName: string;
aiSummary?: string;
columns: ParsedColumn[];
forwardSql: string;
}
interface ChatMessage {
role: 'user' | 'ai' | 'error' | 'diagram';
text: string;
diagramSource?: string;
changes?: SchemaChangeResponse[];
parsedChanges?: ParsedChange[];
batchId?: string;
}
@Component({
selector: 'app-edit-database-schema',
templateUrl: './edit-database-schema.component.html',
styleUrls: ['./edit-database-schema.component.css'],
imports: [
CommonModule,
FormsModule,
MatButtonModule,
MatIconModule,
MatFormFieldModule,
MatInputModule,
MatTooltipModule,
TextFieldModule,
RouterModule,
SchemaDiagramViewerComponent,
],
})
export class EditDatabaseSchemaComponent implements OnInit {
@Input() connectionID: string;
@Input() showClose: boolean = false;
@Output() schemaApplied = new EventEmitter<void>();
@Output() closeEditor = new EventEmitter<void>();
private _tableSchema = inject(TableSchemaService);
private _route = inject(ActivatedRoute);
private _router = inject(Router);
protected isRoutedPage = signal(false);
protected messages = signal<ChatMessage[]>([]);
protected userPrompt = signal('');
protected submitting = signal(false);
protected applying = signal(false);
protected applied = signal(false);
protected initialDiagramLoading = signal(false);
private _threadId: string | undefined;
protected pendingBatch = computed(() => {
const msgs = this.messages();
for (let i = msgs.length - 1; i >= 0; i--) {
if (msgs[i].batchId && msgs[i].changes?.length) return msgs[i];
}
return null;
});
protected currentDiagram = computed(() => {
const msgs = this.messages();
for (let i = msgs.length - 1; i >= 0; i--) {
if (msgs[i].role === 'diagram' && msgs[i].diagramSource) return msgs[i];
}
return null;
});
protected chatMessages = computed(() => this.messages().filter((m) => m.role !== 'diagram'));
ngOnInit(): void {
if (!this.connectionID) {
const id = this._route.snapshot.paramMap.get('connection-id');
if (id) {
this.connectionID = id;
this.isRoutedPage.set(true);
this.showClose = false;
} else {
this._router.navigate(['/connections-list']);
return;
}
}
if (this.showClose || this.isRoutedPage()) {
this._loadDiagram('Current Database Structure');
}
}
async onSubmit() {
const prompt = this.userPrompt().trim();
if (!prompt || this.submitting()) return;
this.messages.update((msgs) => [...msgs, { role: 'user', text: prompt }]);
this.userPrompt.set('');
this.submitting.set(true);
try {
const result = await this._tableSchema.generateSchemaChange(this.connectionID, prompt, this._threadId);
if (result.threadId) {
this._threadId = result.threadId;
}
if (result && result.changes.length > 0) {
this.applied.set(false);
let previewSource = '';
try {
const preview = await this._tableSchema.previewDiagram(
this.connectionID,
result.changes.map((c) => c.forwardSql),
);
previewSource = preview.diagram ?? '';
} catch {
// preview is supplementary — don't block the change list on a diagram failure
}
const parsedChanges = result.changes.map((c) => this._parseChange(c));
this.messages.update((msgs) => {
const next: ChatMessage[] = [
...msgs,
{
role: 'ai',
text: `I've generated ${result.changes.length} change(s) for your database. Review them below and approve or reject.`,
changes: result.changes,
parsedChanges,
batchId: result.batchId,
},
];
if (previewSource) {
next.push({
role: 'diagram',
text: 'Schema Preview',
diagramSource: '```mermaid\n' + previewSource + '\n```',
});
}
return next;
});
} else {
this.messages.update((msgs) => [
...msgs,
{
role: 'ai',
text: 'I could not generate any schema changes for that prompt. Could you describe your database in more detail?',
},
]);
}
} catch (err: unknown) {
const error = err as { error?: { message?: string }; message?: string };
this.messages.update((msgs) => [
...msgs,
{
role: 'error',
text: error?.error?.message || error?.message || 'Failed to generate schema changes.',
},
]);
} finally {
this.submitting.set(false);
}
}
async onApprove() {
const batch = this.pendingBatch();
if (!batch?.batchId || this.applying()) return;
this.applying.set(true);
try {
const result = await this._tableSchema.approveBatch(batch.batchId, true);
if (result) {
const failed = result.changes.filter((c) => c.status === 'failed');
if (failed.length > 0) {
this.messages.update((msgs) => [
...msgs,
{
role: 'error',
text: `${failed.length} change(s) failed: ${failed.map((c) => c.executionError).join('; ')}`,
},
]);
} else {
this.applied.set(true);
this.messages.update((msgs) =>
msgs
.map((m) => (m === batch ? { ...m, batchId: undefined } : m))
.concat({
role: 'ai',
text: 'All changes applied successfully! Your tables have been created.',
}),
);
this._loadDiagram('Updated Database Structure');
}
}
} catch (err: unknown) {
const error = err as { error?: { message?: string }; message?: string };
this.messages.update((msgs) => [
...msgs,
{
role: 'error',
text: error?.error?.message || error?.message || 'Failed to apply schema changes.',
},
]);
} finally {
this.applying.set(false);
}
}
async onReject() {
const batch = this.pendingBatch();
if (!batch?.batchId) return;
await this._tableSchema.rejectBatch(batch.batchId);
this.messages.update((msgs) =>
msgs
.filter((m) => !(m.role === 'diagram' && m.text === 'Schema Preview'))
.map((m) => (m === batch ? { ...m, batchId: undefined } : m))
.concat({
role: 'ai',
text: 'Changes rejected. Feel free to describe what you need differently.',
}),
);
}
onOpenTables() {
this.schemaApplied.emit();
}
onKeydown(event: KeyboardEvent) {
if (event.key === 'Enter' && !event.shiftKey) {
event.preventDefault();
this.onSubmit();
}
}
private async _loadDiagram(label: string) {
this.initialDiagramLoading.set(true);
try {
const diagram = await this._tableSchema.fetchDiagram(this.connectionID);
const source = diagram?.diagram ?? '';
if (this._mermaidHasEntities(source)) {
this.messages.update((msgs) => [
...msgs,
{
role: 'diagram' as const,
text: label,
diagramSource: '```mermaid\n' + source + '\n```',
},
]);
}
} catch {
// Diagram is supplementary - don't show error if it fails
} finally {
this.initialDiagramLoading.set(false);
}
}
private _mermaidHasEntities(source: string): boolean {
if (!source) return false;
for (const line of source.split('\n')) {
if (/^\s*[A-Za-z_][\w-]*\s*\{/.test(line)) return true;
}
return false;
}
private _extractColumnName(value: unknown): string | null {
if (!value) return null;
const inner = (value as { column?: unknown }).column ?? value;
if (typeof inner === 'string') return inner;
const expr = (inner as { expr?: { value?: unknown } })?.expr;
if (expr && typeof expr.value === 'string') return expr.value;
return null;
}
private _extractReferenceTable(value: unknown): string | null {
if (!value) return null;
const inner = (value as { reference_definition?: unknown }).reference_definition ?? value;
const tables = (inner as { table?: unknown }).table;
const first = Array.isArray(tables) ? tables[0] : tables;
const name = (first as { table?: unknown })?.table;
return typeof name === 'string' ? name : null;
}
private _changeMeta(changeType: string): { action: string; icon: string; tone: 'add' | 'edit' | 'remove' } {
const map: Record<string, { action: string; icon: string; tone: 'add' | 'edit' | 'remove' }> = {
CREATE_TABLE: { action: 'Create table', icon: 'add_box', tone: 'add' },
DROP_TABLE: { action: 'Drop table', icon: 'delete_outline', tone: 'remove' },
ADD_COLUMN: { action: 'Add column', icon: 'add', tone: 'add' },
DROP_COLUMN: { action: 'Drop column', icon: 'remove', tone: 'remove' },
ALTER_COLUMN: { action: 'Modify column', icon: 'edit', tone: 'edit' },
ADD_INDEX: { action: 'Add index', icon: 'bookmark_add', tone: 'add' },
DROP_INDEX: { action: 'Drop index', icon: 'bookmark_remove', tone: 'remove' },
ADD_FOREIGN_KEY: { action: 'Add foreign key', icon: 'link', tone: 'add' },
DROP_FOREIGN_KEY: { action: 'Drop foreign key', icon: 'link_off', tone: 'remove' },
ADD_PRIMARY_KEY: { action: 'Add primary key', icon: 'vpn_key', tone: 'add' },
DROP_PRIMARY_KEY: { action: 'Drop primary key', icon: 'key_off', tone: 'remove' },
MONGO_CREATE_COLLECTION: { action: 'Create collection', icon: 'add_box', tone: 'add' },
MONGO_DROP_COLLECTION: { action: 'Drop collection', icon: 'delete_outline', tone: 'remove' },
MONGO_SET_VALIDATOR: { action: 'Set validator', icon: 'rule', tone: 'edit' },
MONGO_CREATE_INDEX: { action: 'Create index', icon: 'bookmark_add', tone: 'add' },
MONGO_DROP_INDEX: { action: 'Drop index', icon: 'bookmark_remove', tone: 'remove' },
DYNAMODB_CREATE_TABLE: { action: 'Create table', icon: 'add_box', tone: 'add' },
DYNAMODB_DROP_TABLE: { action: 'Drop table', icon: 'delete_outline', tone: 'remove' },
DYNAMODB_UPDATE_TABLE: { action: 'Update table', icon: 'edit', tone: 'edit' },
ELASTICSEARCH_CREATE_INDEX: { action: 'Create index', icon: 'add_box', tone: 'add' },
ELASTICSEARCH_DELETE_INDEX: { action: 'Delete index', icon: 'delete_outline', tone: 'remove' },
ELASTICSEARCH_UPDATE_MAPPING: { action: 'Update mapping', icon: 'edit', tone: 'edit' },
ROLLBACK: { action: 'Rollback', icon: 'undo', tone: 'edit' },
};
const upper = (changeType ?? '').toUpperCase();
return (
map[upper] ?? {
action: upper
.replace(/_/g, ' ')
.toLowerCase()
.replace(/\b\w/, (c) => c.toUpperCase()),
icon: 'code',
tone: 'edit',
}
);
}
private _parseChange(change: SchemaChangeResponse): ParsedChange {
const meta = this._changeMeta(change.changeType);
const parsed: ParsedChange = {
id: change.id,
action: meta.action,
icon: meta.icon,
tone: meta.tone,
targetTableName: change.targetTableName,
aiSummary: change.aiSummary,
columns: [],
forwardSql: change.forwardSql,
};
if ((change.changeType ?? '').toUpperCase() === 'CREATE_TABLE' && change.forwardSql) {
parsed.columns = this._parseCreateTableColumns(change.forwardSql);
}
return parsed;
}
private _parseCreateTableColumns(sql: string): ParsedColumn[] {
const parser = new Parser();
let ast: unknown;
try {
ast = parser.astify(sql, { database: 'PostgresQL' });
} catch {
try {
ast = parser.astify(sql, { database: 'MySQL' });
} catch {
return [];
}
}
const nodes = Array.isArray(ast) ? ast : [ast];
const columns: ParsedColumn[] = [];
const colIndex = new Map<string, number>();
for (const node of nodes) {
const root = node as Record<string, unknown> | null;
if (!root || root['type'] !== 'create' || root['keyword'] !== 'table') continue;
const defs = (root['create_definitions'] as unknown[]) ?? [];
for (const rawDef of defs) {
const def = rawDef as Record<string, unknown>;
if (def['resource'] === 'column') {
const name = this._extractColumnName(def['column']);
if (!name) continue;
const dataType = (def['definition'] as { dataType?: string } | undefined)?.dataType ?? '';
const primary = def['primary'] as string | undefined;
const nullable = (def['nullable'] as { value?: string } | undefined)?.value;
const ref = this._extractReferenceTable(def['reference_definition']);
colIndex.set(name, columns.length);
columns.push({
name,
type: dataType.toLowerCase(),
pk: primary === 'primary key' || primary === 'key',
fk: !!ref,
referenceTable: ref ?? undefined,
notNull: nullable === 'not null',
unique: !!def['unique'],
defaultValue: this._extractDefault(def['default_val']),
});
} else if (def['resource'] === 'constraint') {
const ctype = (def['constraint_type'] as string | undefined)?.toLowerCase();
const colRefs = (def['definition'] as unknown[]) ?? [];
if (ctype === 'primary key') {
for (const c of colRefs) {
const colName = this._extractColumnName(c);
if (!colName) continue;
const i = colIndex.get(colName);
if (i !== undefined) columns[i].pk = true;
}
} else if (ctype === 'foreign key') {
const refTable = this._extractReferenceTable(def['reference_definition']);
for (const c of colRefs) {
const colName = this._extractColumnName(c);
if (!colName) continue;
const i = colIndex.get(colName);
if (i !== undefined) {
columns[i].fk = true;
if (refTable) columns[i].referenceTable = refTable;
}
}
}
}
}
}
return columns;
}
private _extractDefault(value: unknown): string | undefined {
if (!value || typeof value !== 'object') return undefined;
const inner = (value as { value?: { type?: string; value?: unknown } }).value;
if (!inner) return undefined;
if (typeof inner === 'string' || typeof inner === 'number' || typeof inner === 'boolean') return String(inner);
const v = (inner as { value?: unknown }).value;
if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') return String(v);
if ((inner as { type?: string }).type === 'function') {
const name = (inner as { name?: { name?: { value?: string }[] } }).name?.name?.[0]?.value ?? '';
return name ? `${name.toUpperCase()}()` : undefined;
}
return undefined;
}
}