@ecubelabs/mysql-mcp 1.0.0-next.3 → 1.0.0-next.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,4 @@
1
+ import { normalizeGithubRepo } from './docs.js';
1
2
  const REMOTE_PREFIX = 'origin/';
2
3
  const BRANCH_MAX_LENGTH = 100;
3
4
  const SUGGESTED_BRANCH = {
@@ -45,21 +46,41 @@ export function normalizeBranchName(input) {
45
46
  }
46
47
  return branch;
47
48
  }
48
- export function missingBranchToolInstruction(branch) {
49
- return branch
50
- ? ''
51
- : ' No code branch is recorded for this database yet (response banners show no "code:"). ' +
52
- 'Before linking or reading documents, ask the user which branch matches this database ' +
53
- 'and record it with branch.';
49
+ export function formatCodeBranches(branches) {
50
+ return Object.keys(branches)
51
+ .sort()
52
+ .map((repo) => `${repo}@${branches[repo]}`)
53
+ .join(', ');
54
54
  }
55
- export function loadedBranch(value) {
56
- if (typeof value !== 'string') {
57
- return null;
58
- }
59
- try {
60
- return normalizeBranchName(value);
55
+ export function codeBranchToolInstruction(branches) {
56
+ const recorded = formatCodeBranches(branches);
57
+ return ((recorded ? ` Recorded code branches: ${recorded}.` : '') +
58
+ ' If this session is inside a GitHub repository whose branch is not recorded (response banners ' +
59
+ 'list recorded ones after "code:"), and you will link its documents or correlate its code with ' +
60
+ 'data, first ask the user which branch of that repository matches this database and record it ' +
61
+ 'with branch {repo, branch}.');
62
+ }
63
+ export function codeRevisionDescription(branches) {
64
+ const recorded = formatCodeBranches(branches);
65
+ return recorded
66
+ ? `\n\nCODE REVISION: this database matches ${recorded} (repository@branch). ` +
67
+ `Read entities, migrations, and queries from that branch of each repository when correlating code with data.`
68
+ : '';
69
+ }
70
+ export function loadedCodeBranches(value) {
71
+ const branches = {};
72
+ if (!value || typeof value !== 'object' || Array.isArray(value)) {
73
+ return branches;
61
74
  }
62
- catch {
63
- return null;
75
+ for (const [repo, branch] of Object.entries(value)) {
76
+ try {
77
+ if (normalizeGithubRepo(repo) !== repo || typeof branch !== 'string') {
78
+ continue;
79
+ }
80
+ branches[repo] = normalizeBranchName(branch);
81
+ }
82
+ catch {
83
+ }
64
84
  }
85
+ return branches;
65
86
  }
@@ -1,5 +1,5 @@
1
1
  import { executeQuery } from '../db/index.js';
2
- import { emptyTable, normalizeName } from './types.js';
2
+ import { emptyTable, normalizeName, SYSTEM_SCHEMAS } from './types.js';
3
3
  function chainAfter(pending, run) {
4
4
  if (!pending) {
5
5
  return run();
@@ -26,13 +26,11 @@ export class CatalogCollector {
26
26
  this.options = options;
27
27
  }
28
28
  inventoryNeedsRefresh() {
29
- return this.options.schemas.some((name) => {
30
- const state = this.store.schemaState(name);
31
- return !state || isExpired(state.scannedAt, this.options.ttlHours);
32
- });
29
+ const scannedAt = this.store.schemaScanTimes();
30
+ return scannedAt.length === 0 || scannedAt.some((value) => isExpired(value, this.options.ttlHours));
33
31
  }
34
32
  async collectInventory(force = false) {
35
- if (!this.store.isEnabled() || this.options.schemas.length === 0) {
33
+ if (!this.store.isEnabled()) {
36
34
  return;
37
35
  }
38
36
  if (!force && !this.inventoryNeedsRefresh()) {
@@ -51,42 +49,41 @@ export class CatalogCollector {
51
49
  return promise;
52
50
  }
53
51
  async runInventory() {
54
- const placeholders = this.options.schemas.map(() => '?').join(', ');
52
+ const placeholders = SYSTEM_SCHEMAS.map(() => '?').join(', ');
55
53
  const rows = await executeQuery(`SELECT
56
- table_schema AS \`table_schema\`,
57
- table_name AS \`table_name\`,
58
- table_comment AS \`table_comment\`,
59
- table_rows AS \`table_rows\`
60
- FROM information_schema.tables
61
- WHERE table_schema IN (${placeholders})
62
- ORDER BY table_schema, table_name`, [...this.options.schemas]);
63
- const declaredBySchema = new Map(this.options.schemas.map((name) => [normalizeName(name), name]));
54
+ s.schema_name AS \`table_schema\`,
55
+ t.table_name AS \`table_name\`,
56
+ t.table_comment AS \`table_comment\`,
57
+ t.table_rows AS \`table_rows\`
58
+ FROM information_schema.schemata s
59
+ LEFT JOIN information_schema.tables t ON t.table_schema = s.schema_name
60
+ WHERE s.schema_name NOT IN (${placeholders})
61
+ ORDER BY s.schema_name, t.table_name`, [...SYSTEM_SCHEMAS]);
64
62
  const rowsBySchema = new Map();
65
63
  for (const row of rows) {
66
- const declared = declaredBySchema.get(normalizeName(String(row.table_schema)));
67
- if (!declared) {
68
- continue;
69
- }
70
- const bucket = rowsBySchema.get(declared);
71
- if (bucket) {
64
+ const schemaName = String(row.table_schema);
65
+ const bucket = rowsBySchema.get(schemaName) ?? [];
66
+ if (row.table_name !== null && row.table_name !== undefined) {
72
67
  bucket.push(row);
73
68
  }
74
- else {
75
- rowsBySchema.set(declared, [row]);
76
- }
69
+ rowsBySchema.set(schemaName, bucket);
77
70
  }
78
71
  const now = new Date().toISOString();
72
+ let tableCount = 0;
79
73
  this.store.update((catalog) => {
80
- for (const schemaName of this.options.schemas) {
81
- const schema = catalog.schemas[schemaName];
82
- if (!schema) {
83
- continue;
84
- }
85
- const nextTables = Object.fromEntries((rowsBySchema.get(schemaName) ?? []).map((row) => {
86
- const existing = schema.tables[row.table_name] ?? emptyTable();
74
+ const previousByName = new Map(Object.keys(catalog.schemas).map((name) => [normalizeName(name), name]));
75
+ const schemas = {};
76
+ for (const [schemaName, schemaRows] of rowsBySchema) {
77
+ const previousName = catalog.schemas[schemaName]
78
+ ? schemaName
79
+ : previousByName.get(normalizeName(schemaName));
80
+ const previous = previousName ? catalog.schemas[previousName] : undefined;
81
+ const tables = Object.fromEntries(schemaRows.map((row) => {
82
+ const tableName = row.table_name;
83
+ const existing = previous?.tables[tableName] ?? emptyTable();
87
84
  const numericRows = Number(row.table_rows);
88
85
  return [
89
- row.table_name,
86
+ tableName,
90
87
  {
91
88
  ...existing,
92
89
  comment: row.table_comment ?? '',
@@ -94,16 +91,12 @@ export class CatalogCollector {
94
91
  },
95
92
  ];
96
93
  }));
97
- schema.tables = nextTables;
98
- schema.scannedAt = now;
94
+ tableCount += schemaRows.length;
95
+ schemas[schemaName] = { ...previous, scannedAt: now, tables };
99
96
  }
97
+ catalog.schemas = schemas;
100
98
  });
101
- for (const schemaName of this.options.schemas) {
102
- if (!rowsBySchema.has(schemaName)) {
103
- console.error(`[catalog] declared schema "${schemaName}" returned no tables; check MYSQL_APP_SCHEMAS and the account's grants.`);
104
- }
105
- }
106
- console.error(`[catalog] inventory scan complete: ${this.options.schemas.length} schemas, ${rows.length} tables`);
99
+ console.error(`[catalog] inventory scan complete: ${rowsBySchema.size} schemas, ${tableCount} tables`);
107
100
  }
108
101
  detailNeedsRefresh(schemaName, tableName) {
109
102
  const meta = this.store.tableMeta(schemaName, tableName);
@@ -16,7 +16,7 @@ export function normalizeGithubRepo(input) {
16
16
  return `${owner}/${name}`;
17
17
  }
18
18
  }
19
- throw new Error(`Document repo must be a GitHub repository as owner/name, https://github.com/owner/name, ` +
19
+ throw new Error(`repo must be a GitHub repository as owner/name, https://github.com/owner/name, ` +
20
20
  `or git@github.com:owner/name: "${input}".`);
21
21
  }
22
22
  export function normalizeDocumentPath(input) {
@@ -42,7 +42,7 @@ export class CatalogDocuments {
42
42
  constructor(store) {
43
43
  this.store = store;
44
44
  }
45
- link(links, guidance = null) {
45
+ link(links, guidanceFor = () => []) {
46
46
  const resolved = links.map((link) => ({
47
47
  ...link,
48
48
  doc: normalizeDocumentPath(link.doc),
@@ -68,13 +68,14 @@ export class CatalogDocuments {
68
68
  }
69
69
  }
70
70
  });
71
+ const guidance = guidanceFor(resolved);
71
72
  return JSON.stringify({
72
73
  linked: resolved.map((link) => ({
73
74
  table: `${link.schema}.${link.table}`,
74
75
  doc: link.doc,
75
76
  repo: link.repo,
76
77
  })),
77
- ...(guidance ? { guidance } : {}),
78
+ ...(guidance.length > 0 ? { guidance } : {}),
78
79
  }, null, 2);
79
80
  }
80
81
  unlink(schemaName, tableName) {
@@ -2,11 +2,12 @@ import { createHash } from 'crypto';
2
2
  import * as os from 'os';
3
3
  import * as path from 'path';
4
4
  import { CatalogCollector } from './collect.js';
5
- import { CatalogDocuments } from './docs.js';
6
- import { normalizeBranchName } from './branch.js';
7
- import { missingBranchNotice, renderDescribe, renderMap, renderToolDescriptionSuffix, searchCatalog, } from './render.js';
5
+ import { CatalogDocuments, normalizeGithubRepo } from './docs.js';
6
+ import { formatCodeBranches, normalizeBranchName } from './branch.js';
7
+ import { normalizeSchemaOwner } from './owner.js';
8
+ import { missingBranchNotice, missingOwnerNotice, renderDescribe, renderMap, renderToolDescriptionSuffix, searchCatalog, } from './render.js';
8
9
  import { CatalogStore } from './store.js';
9
- import { emptyUsage, normalizeName, pruneJoins } from './types.js';
10
+ import { emptyUsage, normalizeName, pruneJoins, SYSTEM_SCHEMAS } from './types.js';
10
11
  import { prepareCatalogQuery, resultColumnNames } from './usage.js';
11
12
  const UNKNOWN_REFERENCE_REFRESH_COOLDOWN_MS = 60_000;
12
13
  const UNKNOWN_REFERENCE_MEMORY_LIMIT = 256;
@@ -56,7 +57,6 @@ export class SchemaCatalog {
56
57
  this.options = options;
57
58
  this.store = new CatalogStore(options);
58
59
  this.collector = new CatalogCollector(this.store, {
59
- schemas: options.appSchemas.map((entry) => entry.schema),
60
60
  ttlHours: options.ttlHours,
61
61
  });
62
62
  this.documents = new CatalogDocuments(this.store);
@@ -64,34 +64,88 @@ export class SchemaCatalog {
64
64
  isEnabled() {
65
65
  return this.store.isEnabled();
66
66
  }
67
- codeBranch() {
68
- return this.isEnabled() ? this.store.codeBranch() : null;
67
+ codeBranches() {
68
+ return this.isEnabled() ? { ...this.store.codeBranches() } : {};
69
69
  }
70
- setCodeBranch(input) {
70
+ codeBranchSummary() {
71
+ return this.isEnabled() ? formatCodeBranches(this.store.codeBranches()) || null : null;
72
+ }
73
+ setCodeBranch(repoInput, branchInput) {
71
74
  if (!this.isEnabled()) {
72
75
  throw new Error('The schema catalog is disabled.');
73
76
  }
74
- const branch = normalizeBranchName(input);
75
- const previous = this.store.codeBranch();
77
+ const repo = normalizeGithubRepo(repoInput);
78
+ const branch = normalizeBranchName(branchInput);
79
+ const previous = this.store.codeBranches()[repo] ?? null;
76
80
  if (previous === branch) {
77
- return JSON.stringify({ branch, changed: false, status: `이미 ${branch} 브랜치가 기록돼 있습니다.` }, null, 2);
81
+ return JSON.stringify({ repo, branch, changed: false, status: `${repo}에는 이미 ${branch} 브랜치가 기록돼 있습니다.` }, null, 2);
78
82
  }
79
83
  this.store.update((catalog) => {
80
- catalog.codeBranch = branch;
84
+ catalog.codeBranches[repo] = branch;
81
85
  });
82
- if (this.store.codeBranch() !== branch) {
86
+ if (this.store.codeBranches()[repo] !== branch) {
83
87
  throw new Error('The catalog cache is disabled, so the branch was not recorded.');
84
88
  }
85
89
  return JSON.stringify({
90
+ repo,
86
91
  branch,
87
92
  ...(previous ? { previous } : {}),
88
93
  changed: true,
89
94
  status: previous
90
- ? `브랜치를 바꿨습니다: ${previous} → ${branch}`
91
- : `${branch} 브랜치를 기록했습니다. 다음 세션에도 이어 씁니다.`,
95
+ ? `${repo}의 브랜치를 바꿨습니다: ${previous} → ${branch}`
96
+ : `${repo}에 ${branch} 브랜치를 기록했습니다. 다음 세션에도 이어 씁니다.`,
92
97
  note: 'banner와 describe는 바로 이 값을 씁니다. mysql_query 툴 설명의 CODE REVISION은 서버를 다시 띄우면 바뀝니다.',
93
98
  }, null, 2);
94
99
  }
100
+ schemaOwners() {
101
+ return this.isEnabled() ? this.store.schemaOwners() : {};
102
+ }
103
+ async setSchemaOwner(target, repoInput, appInput) {
104
+ if (!this.isEnabled()) {
105
+ throw new Error('The schema catalog is disabled.');
106
+ }
107
+ const requested = target.replace(/`/g, '').trim();
108
+ const owner = normalizeSchemaOwner(repoInput, appInput);
109
+ let schema = this.store.schemaName(requested);
110
+ if (!schema) {
111
+ await this.collector.collectInventory(false);
112
+ schema = this.store.schemaName(requested);
113
+ }
114
+ if (!schema) {
115
+ throw new Error(`Unknown schema "${requested}". owner takes a schema name from mysql_catalog {action:"map"}, not schema.table.`);
116
+ }
117
+ const name = schema;
118
+ const previous = this.store.schemaOwners()[name] ?? null;
119
+ const label = `${owner.repo}의 ${owner.app}`;
120
+ if (previous && previous.repo === owner.repo && previous.app === owner.app) {
121
+ return JSON.stringify({ schema: name, ...owner, changed: false, status: `${name} schema의 owner는 이미 ${label}입니다.` }, null, 2);
122
+ }
123
+ let missing = false;
124
+ this.store.update((catalog) => {
125
+ const entry = catalog.schemas[name];
126
+ if (!entry) {
127
+ missing = true;
128
+ return;
129
+ }
130
+ entry.repo = owner.repo;
131
+ entry.app = owner.app;
132
+ });
133
+ if (missing) {
134
+ throw new Error(`${name} is no longer in the catalog. Run mysql_catalog {action:"map"} to see what remains.`);
135
+ }
136
+ if (!this.isEnabled()) {
137
+ throw new Error('The catalog cache is disabled, so the owner was not recorded.');
138
+ }
139
+ return JSON.stringify({
140
+ schema: name,
141
+ ...owner,
142
+ ...(previous ? { previous } : {}),
143
+ changed: true,
144
+ status: previous
145
+ ? `${name} schema의 owner를 바꿨습니다: ${previous.repo}의 ${previous.app} → ${label}`
146
+ : `${name} schema의 owner를 ${label}(으)로 기록했습니다. 다음 세션에도 이어 씁니다.`,
147
+ }, null, 2);
148
+ }
95
149
  startInventory() {
96
150
  if (!this.isEnabled() || this.closing) {
97
151
  return;
@@ -142,11 +196,10 @@ export class SchemaCatalog {
142
196
  }
143
197
  catalogKeyForReference(reference) {
144
198
  const requestedSchema = reference.schema ?? this.options.defaultSchema;
145
- if (!requestedSchema) {
199
+ if (!requestedSchema || SYSTEM_SCHEMAS.some((name) => sameName(name, requestedSchema))) {
146
200
  return null;
147
201
  }
148
- const declared = this.options.appSchemas.find((entry) => sameName(entry.schema, requestedSchema));
149
- return declared ? `${declared.schema}.${reference.table}`.toLowerCase() : null;
202
+ return `${requestedSchema}.${reference.table}`.toLowerCase();
150
203
  }
151
204
  resolveReferences(references) {
152
205
  return references
@@ -160,7 +213,7 @@ export class SchemaCatalog {
160
213
  resolved = this.resolveTable(input);
161
214
  }
162
215
  if (!resolved) {
163
- throw new Error(`Unknown or ambiguous table "${input}". Use a declared schema.table name.`);
216
+ throw new Error(`Unknown or ambiguous table "${input}". Use a qualified schema.table name.`);
164
217
  }
165
218
  return resolved;
166
219
  }
@@ -193,7 +246,7 @@ export class SchemaCatalog {
193
246
  if (!entry) {
194
247
  throw new Error(`Table disappeared during refresh: ${input}`);
195
248
  }
196
- return renderDescribe(`${resolved.schema}.${resolved.table}`, entry, this.store.codeBranch(), this.store.readJoins(resolved.schema, resolved.table), this.options.profile);
249
+ return renderDescribe(`${resolved.schema}.${resolved.table}`, entry, this.store.codeBranches(), this.store.readJoins(resolved.schema, resolved.table), this.options.profile, this.store.schemaOwners()[resolved.schema] ?? null);
197
250
  }
198
251
  async refresh(target) {
199
252
  if (!this.isEnabled()) {
@@ -209,15 +262,13 @@ export class SchemaCatalog {
209
262
  at,
210
263
  }, null, 2);
211
264
  }
212
- const declared = requested.includes('.')
213
- ? undefined
214
- : this.options.appSchemas.find((entry) => sameName(entry.schema, requested));
215
- if (declared) {
265
+ const schema = requested.includes('.') ? null : this.store.schemaName(requested);
266
+ if (schema) {
216
267
  await this.collector.collectInventory(true);
217
268
  return JSON.stringify({
218
269
  refreshed: 'inventory',
219
- requestedSchema: declared.schema,
220
- note: '인벤토리는 선언된 모든 스키마를 쿼리 한 번으로 함께 갱신합니다.',
270
+ requestedSchema: schema,
271
+ note: '인벤토리는 모든 스키마를 쿼리 한 번으로 함께 갱신합니다.',
221
272
  ...this.inventoryReport(),
222
273
  at,
223
274
  }, null, 2);
@@ -241,7 +292,7 @@ export class SchemaCatalog {
241
292
  }, null, 2);
242
293
  }
243
294
  throw new Error(`Unknown refresh target "${requested}". Use schema.table for one table's ` +
244
- 'columns, a declared schema name for the table inventory, or omit target ' +
295
+ 'columns, a schema name for the table inventory, or omit target ' +
245
296
  'for the inventory.');
246
297
  }
247
298
  inventoryReport() {
@@ -270,7 +321,25 @@ export class SchemaCatalog {
270
321
  repo: link.repo,
271
322
  });
272
323
  }
273
- return this.documents.link(resolvedLinks, this.store.codeBranch() ? null : missingBranchNotice(this.options.profile));
324
+ return this.documents.link(resolvedLinks, (linked) => {
325
+ const branchNotices = [...new Set(linked.map((link) => link.repo))]
326
+ .filter((repo) => !this.store.codeBranches()[repo])
327
+ .map((repo) => missingBranchNotice(this.options.profile, repo));
328
+ const owners = this.store.schemaOwners();
329
+ const docsBySchema = new Map();
330
+ for (const link of linked) {
331
+ if (owners[link.schema]) {
332
+ continue;
333
+ }
334
+ const docs = docsBySchema.get(link.schema) ?? [];
335
+ if (!docs.some((entry) => entry.repo === link.repo && entry.doc === link.doc)) {
336
+ docs.push({ repo: link.repo, doc: link.doc });
337
+ }
338
+ docsBySchema.set(link.schema, docs);
339
+ }
340
+ const ownerNotices = [...docsBySchema].map(([schema, docs]) => missingOwnerNotice(schema, docs));
341
+ return [...branchNotices, ...ownerNotices];
342
+ });
274
343
  }
275
344
  async unlink(input) {
276
345
  if (!this.isEnabled()) {
@@ -409,9 +478,9 @@ export class SchemaCatalog {
409
478
  return null;
410
479
  }
411
480
  return (`[카탈로그] ${[...unreviewed].join(', ')} 에 연결된 문서가 없습니다.\n` +
412
- `도메인 문서(model.md)를 찾으면 mysql_catalog {action:"link", links:[{table:"${first}", doc:"<저장소 기준 상대 경로>", repo:"<owner/name>"}]} 로 연결하고,\n` +
413
- `없으면 mysql_catalog {action:"unlink", table:"${first}"} 로 기록하세요.` +
414
- (this.store.codeBranch() ? '' : `\n${missingBranchNotice(this.options.profile)}`));
481
+ `도메인 문서(model.md)를 찾으면 mysql_catalog {action:"link", links:[{table:"${first}", doc:"<저장소 기준 상대 경로>", repo:"<owner/name>"}]} 로 연결하세요.\n` +
482
+ `문서 저장소 안의 세션에서 찾아봤는데 없을 때만 mysql_catalog {action:"unlink", table:"${first}"} 로 기록하세요. ` +
483
+ '그 저장소 밖의 세션이면 unlink하지 마세요.');
415
484
  }
416
485
  indexFacts(reference) {
417
486
  if (!this.isEnabled()) {
@@ -0,0 +1,35 @@
1
+ import { normalizeGithubRepo } from './docs.js';
2
+ const APP_MAX_LENGTH = 100;
3
+ export function normalizeAppName(input) {
4
+ const app = typeof input === 'string' ? input.trim() : '';
5
+ const reject = (reason) => {
6
+ throw new Error(`Invalid app name ${JSON.stringify(input)}: ${reason}.`);
7
+ };
8
+ if (!app) {
9
+ reject('it is empty');
10
+ }
11
+ if (app.length > APP_MAX_LENGTH) {
12
+ reject(`it is longer than ${APP_MAX_LENGTH} characters`);
13
+ }
14
+ if (/[\u0000- \u007f]/.test(app)) {
15
+ reject('it contains whitespace or a control character');
16
+ }
17
+ return app;
18
+ }
19
+ export function normalizeSchemaOwner(repo, app) {
20
+ return { repo: normalizeGithubRepo(repo), app: normalizeAppName(app) };
21
+ }
22
+ export function schemaOwnerOf(schema) {
23
+ return typeof schema.repo === 'string' && typeof schema.app === 'string'
24
+ ? { repo: schema.repo, app: schema.app }
25
+ : null;
26
+ }
27
+ export function loadedSchemaOwner(schema) {
28
+ try {
29
+ const owner = normalizeSchemaOwner(schema.repo, schema.app);
30
+ return owner.repo === schema.repo && owner.app === schema.app ? owner : {};
31
+ }
32
+ catch {
33
+ return {};
34
+ }
35
+ }
@@ -1,4 +1,5 @@
1
1
  import { suggestedBranch } from './branch.js';
2
+ import { schemaOwnerOf } from './owner.js';
2
3
  const UNLINKED_SAMPLE_LIMIT = 30;
3
4
  const HOT_TABLE_LIMIT = 10;
4
5
  function sampled(values) {
@@ -32,7 +33,6 @@ function rankTables(tables) {
32
33
  }
33
34
  function allTables(catalog) {
34
35
  return Object.entries(catalog.schemas).flatMap(([schema, entry]) => Object.entries(entry.tables).map(([table, tableEntry]) => ({
35
- app: entry.app,
36
36
  schema,
37
37
  table,
38
38
  entry: tableEntry,
@@ -64,15 +64,16 @@ export function renderToolDescriptionSuffix(catalog, budget) {
64
64
  }
65
65
  return HOT_TABLES_HEADING + listed + staticGuidance;
66
66
  }
67
+ function ownerField(owner) {
68
+ return owner ? { owner } : {};
69
+ }
67
70
  export function renderMap(catalog) {
68
71
  const schemas = Object.entries(catalog.schemas).map(([name, schema]) => ({
69
- app: schema.app,
70
72
  schema: name,
71
- description: schema.description ?? '',
73
+ ...ownerField(schemaOwnerOf(schema)),
72
74
  scannedAt: schema.scannedAt,
73
75
  tableCount: Object.keys(schema.tables).length,
74
76
  tables: rankTables(Object.entries(schema.tables).map(([table, entry]) => ({
75
- app: schema.app,
76
77
  schema: name,
77
78
  table,
78
79
  entry,
@@ -115,6 +116,7 @@ export function searchCatalog(catalog, query, limit) {
115
116
  }
116
117
  const results = [];
117
118
  for (const [schemaName, schema] of Object.entries(catalog.schemas)) {
119
+ const owner = ownerField(schemaOwnerOf(schema));
118
120
  for (const [tableName, table] of Object.entries(schema.tables)) {
119
121
  const qualified = `${schemaName}.${tableName}`;
120
122
  const matchedColumns = table.columns
@@ -127,6 +129,7 @@ export function searchCatalog(catalog, query, limit) {
127
129
  score += Math.min(9, Math.floor(Math.log2(table.usage.successCount + 1)));
128
130
  results.push({
129
131
  table: qualified,
132
+ ...owner,
130
133
  score,
131
134
  comment: table.comment,
132
135
  matchedColumns,
@@ -137,13 +140,30 @@ export function searchCatalog(catalog, query, limit) {
137
140
  }
138
141
  return results.sort((a, b) => b.score - a.score || a.table.localeCompare(b.table)).slice(0, limit);
139
142
  }
140
- export function missingBranchNotice(profile) {
143
+ function branchQuestion(profile, repo) {
141
144
  const suggestion = suggestedBranch(profile);
142
145
  const question = suggestion
143
- ? `${profile} profile은 보통 ${suggestion} 브랜치에 대응합니다. ${suggestion}이 맞는지 사용자에게 물으세요.`
144
- : `이 profile(${profile})이 어느 브랜치에 대응하는지 사용자에게 물으세요.`;
145
- return (`[카탈로그] 이 DB에 대응하는 코드 브랜치가 기록되지 않았습니다. ${question}\n` +
146
- '확인받은 값을 mysql_catalog {action:"branch", branch:"<브랜치 이름>"} 로 기록하세요.');
146
+ ? `${profile} profile은 보통 ${suggestion} 브랜치에 대응합니다. ${repo}의 ${suggestion}이 맞는지 사용자에게 물으세요.`
147
+ : `이 profile(${profile})이 ${repo}의 어느 브랜치에 대응하는지 사용자에게 물으세요.`;
148
+ return (`${question}\n` +
149
+ `확인받은 값을 mysql_catalog {action:"branch", repo:"${repo}", branch:"<브랜치 이름>"} 로 기록하세요.`);
150
+ }
151
+ export function missingBranchNotice(profile, repo) {
152
+ return (`[카탈로그] ${repo}에서 이 DB에 대응하는 코드 브랜치가 기록되지 않았습니다. ` + branchQuestion(profile, repo));
153
+ }
154
+ export function missingOwnerNotice(schema, linkedDocs) {
155
+ const docs = linkedDocs.map(({ repo, doc }) => `${repo}의 ${doc}`).join(', ');
156
+ return (`[카탈로그] ${schema} schema를 가진 저장소와 앱이 기록되지 않았습니다. ` +
157
+ `방금 연결한 문서는 ${docs}입니다.\n` +
158
+ `${schema} schema가 어느 저장소의 어느 앱 것인지 사용자에게 물으세요.\n` +
159
+ `확인받은 값을 mysql_catalog {action:"owner", target:"${schema}", repo:"<owner/name>", app:"<앱 이름>"} 로 기록하세요.`);
160
+ }
161
+ export function renderSchemasResource(profile, codeBranches, owners) {
162
+ return JSON.stringify({
163
+ profile,
164
+ ...(Object.keys(codeBranches).length > 0 ? { codeBranches } : {}),
165
+ schemas: owners,
166
+ }, null, 2);
147
167
  }
148
168
  function shellQuote(value) {
149
169
  if (/^[A-Za-z0-9_./:@+-]+$/.test(value)) {
@@ -151,7 +171,7 @@ function shellQuote(value) {
151
171
  }
152
172
  return `'${value.replace(/'/g, `'"'"'`)}'`;
153
173
  }
154
- export function renderDescribe(qualifiedName, table, codeBranch, observedJoins, profile) {
174
+ export function renderDescribe(qualifiedName, table, codeBranches, observedJoins, profile, owner = null) {
155
175
  let docs;
156
176
  const hasDocumentDecision = Object.prototype.hasOwnProperty.call(table.curated, 'doc');
157
177
  const documentLink = table.curated.doc;
@@ -159,14 +179,14 @@ export function renderDescribe(qualifiedName, table, codeBranch, observedJoins,
159
179
  docs =
160
180
  '연결 안 됨\n' +
161
181
  `도메인 문서(model.md)를 찾으면 mysql_catalog {action:"link", links:[{table:"${qualifiedName}", doc:"<저장소 기준 상대 경로>", repo:"<owner/name>"}]} 로 연결하세요.\n` +
162
- 'status 같은 코드 컬럼의 의미는 도메인 문서에만 있습니다.' +
163
- (codeBranch ? '' : `\n${missingBranchNotice(profile)}`);
182
+ 'status 같은 코드 컬럼의 의미는 도메인 문서에만 있습니다.';
164
183
  }
165
184
  else if (documentLink == null) {
166
185
  docs = '문서 없음 확인됨';
167
186
  }
168
187
  else {
169
188
  const { repo } = documentLink;
189
+ const codeBranch = Object.prototype.hasOwnProperty.call(codeBranches, repo) ? codeBranches[repo] : null;
170
190
  docs = {
171
191
  path: documentLink.path,
172
192
  repo,
@@ -178,13 +198,16 @@ export function renderDescribe(qualifiedName, table, codeBranch, observedJoins,
178
198
  .map(encodeURIComponent)
179
199
  .join('/')}?ref=${encodeURIComponent(codeBranch)}`)}`
180
200
  : null,
181
- guidance: '상태 코드·도메인 규칙은 이 문서를 먼저 읽어라. ' +
182
- 'command는 문서가 있는 저장소 안에서, remoteCommand는 어디서든(gh 인증 필요) 실행한다.' +
183
- (codeBranch ? '' : `\n${missingBranchNotice(profile)}`),
201
+ guidance: '상태 코드·도메인 규칙은 이 문서를 먼저 읽어라.' +
202
+ (codeBranch
203
+ ? ' command는 문서가 있는 저장소 안에서, remoteCommand는 어디서든(gh 인증 필요) 실행한다.'
204
+ : `\n${repo}의 브랜치가 기록되지 않아 읽기 명령이 없습니다. 이 문서를 읽어야 할 때만 ` +
205
+ `아래대로 하세요.\n${branchQuestion(profile, repo)}`),
184
206
  };
185
207
  }
186
208
  return JSON.stringify({
187
209
  table: qualifiedName,
210
+ ...ownerField(owner),
188
211
  ...table,
189
212
  ...(observedJoins.length > 0 ? { observedJoins } : {}),
190
213
  docs,