@uncutsql/core 0.2.0 → 0.3.0

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.
package/dist/index.d.cts CHANGED
@@ -25,6 +25,13 @@ export interface DatabaseColumn {
25
25
  export interface DatabaseIndex {
26
26
  readonly key: string;
27
27
  readonly name: string;
28
+ /**
29
+ * Namespaced keys retain vendor-specific index features without teaching core their meaning.
30
+ * Absent and `{}` are the same value; writers omit the field when it would be empty.
31
+ */
32
+ readonly options?: {
33
+ readonly [key: string]: SchemaValue;
34
+ };
28
35
  readonly terms: readonly SchemaValue[];
29
36
  readonly unique: boolean;
30
37
  readonly where: SchemaValue;
@@ -44,6 +51,13 @@ export interface DatabaseView {
44
51
  readonly columns?: readonly string[];
45
52
  readonly identity: TableIdentity;
46
53
  readonly name: string;
54
+ /**
55
+ * Namespaced keys retain vendor-specific view features without teaching core their meaning.
56
+ * Absent and `{}` are the same value; writers omit the field when it would be empty.
57
+ */
58
+ readonly options?: {
59
+ readonly [key: string]: SchemaValue;
60
+ };
47
61
  readonly query: SchemaValue;
48
62
  }
49
63
  export interface DatabaseTrigger {
@@ -54,6 +68,40 @@ export interface DatabaseTrigger {
54
68
  /** Normalized identity of the table or view that owns this trigger. */
55
69
  readonly target: TableIdentity;
56
70
  }
71
+ export interface SequenceOwner {
72
+ /** A column key of `table`. */
73
+ readonly column: string;
74
+ readonly table: TableIdentity;
75
+ }
76
+ export interface DatabaseSequence {
77
+ /**
78
+ * Dialect-normalized data type and parameters. Core compares it with `schemaValueEqual`
79
+ * and never interprets its keys.
80
+ */
81
+ readonly definition: {
82
+ readonly [key: string]: SchemaValue;
83
+ };
84
+ readonly identity: TableIdentity;
85
+ readonly name: string;
86
+ /** The column whose removal drops the sequence. */
87
+ readonly owner?: SequenceOwner;
88
+ }
89
+ export interface DatabaseFunction {
90
+ /**
91
+ * Dialect-normalized language, arguments, result, attributes and body. Core compares it with
92
+ * `schemaValueEqual` and never interprets its keys.
93
+ */
94
+ readonly definition: {
95
+ readonly [key: string]: SchemaValue;
96
+ };
97
+ readonly identity: TableIdentity;
98
+ readonly name: string;
99
+ /**
100
+ * Dialect-normalized types of the arguments that identify the function among overloads, in
101
+ * order. Together with `identity` it is the function's key.
102
+ */
103
+ readonly signature: readonly SchemaValue[];
104
+ }
57
105
  /** A container of named objects one level below an optional catalog. Keys are already normalized by the dialect. */
58
106
  export interface NamespaceIdentity {
59
107
  readonly catalog?: string;
@@ -67,14 +115,41 @@ export interface DatabaseNamespace {
67
115
  readonly [key: string]: SchemaValue;
68
116
  };
69
117
  }
118
+ export interface DatabaseExternals {
119
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
120
+ readonly functions?: readonly DatabaseFunction[];
121
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
122
+ readonly tables?: readonly DatabaseTable[];
123
+ }
70
124
  export interface DatabaseSchema {
125
+ /**
126
+ * Objects outside the managed scope that the model references. Diff, procedures and lowering
127
+ * never create, alter or drop them. Absent and `{}` are the same value.
128
+ */
129
+ readonly externals?: DatabaseExternals;
130
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
131
+ readonly functions?: readonly DatabaseFunction[];
71
132
  /** Absent means namespaces are not declared and no namespace check runs. */
72
133
  readonly namespaces?: readonly DatabaseNamespace[];
134
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
135
+ readonly sequences?: readonly DatabaseSequence[];
73
136
  readonly tables: readonly DatabaseTable[];
74
137
  readonly triggers?: readonly DatabaseTrigger[];
75
138
  readonly views?: readonly DatabaseView[];
76
139
  }
77
140
  export declare const tableKey: (table: TableIdentity) => string;
141
+ export declare const functionKey: (fn: Pick<DatabaseFunction, "identity" | "signature">) => string;
142
+ /** `schema.name(type, …)` for diagnostics; signature entries that are not strings print as JSON. */
143
+ export declare const functionLabel: (fn: Pick<DatabaseFunction, "identity" | "signature">) => string;
144
+ export interface ExternalObject {
145
+ /** Set when the external is a function. */
146
+ readonly function?: DatabaseFunction;
147
+ readonly identity: TableIdentity;
148
+ /** `External table schema.name` or `External function schema.name(type, …)`. */
149
+ readonly label: string;
150
+ }
151
+ /** The external objects of the models, tables before functions, each listed once by its key. */
152
+ export declare const externalObjects: (...databases: readonly DatabaseSchema[]) => readonly ExternalObject[];
78
153
  export declare const namespaceKey: (namespace: NamespaceIdentity) => string;
79
154
  /** The namespace that contains an object, or undefined when its identity has no schema level. */
80
155
  export declare const namespaceOf: (identity: TableIdentity) => NamespaceIdentity | undefined;
@@ -176,6 +251,22 @@ export type TriggerChange = {
176
251
  readonly after: DatabaseTrigger;
177
252
  readonly before: DatabaseTrigger;
178
253
  };
254
+ export type SequenceChange = {
255
+ readonly _tag: "SequenceAdded" | "SequenceRemoved";
256
+ readonly sequence: DatabaseSequence;
257
+ } | {
258
+ readonly _tag: "SequenceChanged" | "SequenceRenamed";
259
+ readonly after: DatabaseSequence;
260
+ readonly before: DatabaseSequence;
261
+ };
262
+ export type FunctionChange = {
263
+ readonly _tag: "FunctionAdded" | "FunctionRemoved";
264
+ readonly function: DatabaseFunction;
265
+ } | {
266
+ readonly _tag: "FunctionChanged";
267
+ readonly after: DatabaseFunction;
268
+ readonly before: DatabaseFunction;
269
+ };
179
270
  /** A namespace change lists the namespace only; objects inside it keep their own changes. */
180
271
  export type NamespaceChange = {
181
272
  readonly _tag: "NamespaceAdded" | "NamespaceRemoved";
@@ -191,11 +282,28 @@ export interface DropGuard {
191
282
  readonly table: TableIdentity;
192
283
  }
193
284
  export interface DatabaseDiff {
194
- readonly changes: readonly (NamespaceChange | TableChange | ViewChange | TriggerChange)[];
285
+ readonly changes: readonly (NamespaceChange | TableChange | ViewChange | TriggerChange | SequenceChange | FunctionChange)[];
195
286
  readonly dropGuards?: readonly DropGuard[];
196
287
  }
197
288
  export declare const validateDatabase: (database: DatabaseSchema, side: DiffDiagnostic["side"]) => readonly DiffDiagnostic[];
289
+ /**
290
+ * Checks an explicit list of managed schemas against the namespaces of a model built from schema
291
+ * files. Names are compared as catalog names, case-sensitively. The shape of the list (non-empty,
292
+ * unique, including `public`) is the caller's to validate.
293
+ */
294
+ export declare const checkManagedScope: (model: DatabaseSchema, managedSchemas: readonly string[]) => readonly DiffDiagnostic[];
198
295
  export declare const compareTable: (before: DatabaseTable, after: DatabaseTable) => readonly TableAlteration[];
296
+ /** Table and column renames of a migration, keyed by the table's key before the rename. */
297
+ export interface SequenceRenames {
298
+ readonly columns: ReadonlyMap<string, ReadonlyMap<string, string>>;
299
+ readonly tables: ReadonlyMap<string, TableIdentity>;
300
+ }
301
+ /** Collect the table and column renames that a list of changes carries. */
302
+ export declare const sequenceRenames: (changes: DatabaseDiff["changes"]) => SequenceRenames;
303
+ /** Function changes. A changed signature is another key: a removal and an addition. */
304
+ export declare const diffFunctions: (before: DatabaseSchema, after: DatabaseSchema) => readonly FunctionChange[];
305
+ /** Sequence changes. `renames` are the table and column renames the migration applies. */
306
+ export declare const diffSequences: (before: DatabaseSchema, after: DatabaseSchema, renames?: SequenceRenames) => readonly SequenceChange[];
199
307
  /** Compare normalized relational models without parsing SQL or consulting a dialect. */
200
308
  export declare const diffDatabases: (before: DatabaseSchema, after: DatabaseSchema) => Effect.Effect<DatabaseDiff, SchemaDiffError>;
201
309
  interface TableProcedure {
@@ -211,6 +319,20 @@ export type MigrationProcedure = {
211
319
  readonly _tag: "AlterNamespace";
212
320
  readonly after: DatabaseNamespace;
213
321
  readonly before: DatabaseNamespace;
322
+ } | {
323
+ readonly _tag: "CreateFunction" | "DropFunction";
324
+ readonly function: DatabaseFunction;
325
+ } | {
326
+ readonly _tag: "ReplaceFunction";
327
+ readonly after: DatabaseFunction;
328
+ readonly before: DatabaseFunction;
329
+ } | {
330
+ readonly _tag: "CreateSequence" | "DropSequence";
331
+ readonly sequence: DatabaseSequence;
332
+ } | {
333
+ readonly _tag: "AlterSequence" | "RenameSequence";
334
+ readonly after: DatabaseSequence;
335
+ readonly before: DatabaseSequence;
214
336
  } | {
215
337
  readonly _tag: "CreateTrigger" | "DropTrigger";
216
338
  readonly trigger: DatabaseTrigger;
@@ -419,6 +541,11 @@ interface Dialect<Ast, PhysicalPlan, E extends Error = Error, R = never, Semanti
419
541
  * `false`, a migration with guards is refused before lowering.
420
542
  */
421
543
  readonly supportsDropGuards?: boolean;
544
+ /**
545
+ * When `true`, a project may list the schemas it manages (ADR 0024 Decision 1). When absent or
546
+ * `false`, the caller refuses such a list by name.
547
+ */
548
+ readonly supportsManagedSchemas?: boolean;
422
549
  readonly toDatabaseSchema: (ast: Ast, side?: DiffDiagnostic["side"]) => Effect.Effect<DatabaseSchema, E, R>;
423
550
  }
424
551
  declare const SchemaModelError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
@@ -540,7 +667,7 @@ export interface SourceSpan {
540
667
  readonly end: number;
541
668
  readonly start: number;
542
669
  }
543
- export type SchemaSymbolKind = "column" | "index" | "namespace" | "table" | "trigger" | "view";
670
+ export type SchemaSymbolKind = "column" | "function" | "index" | "namespace" | "table" | "trigger" | "view";
544
671
  export interface SchemaSymbol {
545
672
  readonly children: readonly SchemaSymbol[];
546
673
  /** Short context shown next to the name, such as the written namespace. */
@@ -556,25 +683,98 @@ export interface SchemaDiagnostic extends SourceSpan {
556
683
  /** A declaration that references can target. `key` is opaque and normalized by the dialect. */
557
684
  export interface SchemaDeclaration {
558
685
  readonly key: string;
686
+ /** What is declared. A declaration without `kind` is never a completion candidate. */
687
+ readonly kind?: SchemaSymbolKind;
688
+ /** Opaque, dialect-normalized key of the declared name alone. Its presence makes the declaration renamable. */
689
+ readonly nameKey?: string;
690
+ /** Tables only: SQL text that denotes the table from any namespace, such as `app.orders`. */
691
+ readonly qualifiedText?: string;
692
+ /** Opaque key of the scope in which the name is unique: a namespace for a table, the table key for a column. */
693
+ readonly scope?: string;
559
694
  readonly selection: SourceSpan;
695
+ /** SQL text that denotes the name alone, quoted when the dialect requires it. */
696
+ readonly text?: string;
560
697
  }
561
698
  export interface SchemaReference extends SourceSpan {
562
699
  readonly key: string;
700
+ /** The name token alone, when the span also covers a qualifier (`public.users`). */
701
+ readonly selection?: SourceSpan;
702
+ }
703
+ /** Where an unresolved identifier token was found. */
704
+ export type SchemaNameContext = "check" | "default" | "generated" | "index" | "query" | "table" | "trigger" | "view";
705
+ /** An identifier token the service does not resolve. `nameKey` is built like `SchemaDeclaration.nameKey`. */
706
+ export interface SchemaNameUse extends SourceSpan {
707
+ readonly context: SchemaNameContext;
708
+ readonly nameKey: string;
563
709
  }
564
710
  export interface SchemaAnalysis {
565
711
  readonly declarations: readonly SchemaDeclaration[];
566
712
  readonly diagnostics: readonly SchemaDiagnostic[];
713
+ /** Uses that rename edits but no other feature reads (Decision 2). */
714
+ readonly occurrences?: readonly SchemaReference[];
567
715
  readonly references: readonly SchemaReference[];
568
716
  readonly symbols: readonly SchemaSymbol[];
717
+ /** Identifier tokens in constructs the service does not resolve (Decision 5). */
718
+ readonly unresolvedNames?: readonly SchemaNameUse[];
719
+ }
720
+ /** A validated new name: its key and the SQL text written at every edited site. */
721
+ export interface SchemaName {
722
+ readonly nameKey: string;
723
+ readonly text: string;
724
+ }
725
+ export type SchemaCompletionKind = "column" | "table";
726
+ /** What a completion request expects at the cursor. Keys are opaque, as everywhere else. */
727
+ export interface SchemaCompletion {
728
+ /** Declarations of the probed statement, with offsets in `source`, which no analysis holds yet. */
729
+ readonly declarations?: readonly SchemaDeclaration[];
730
+ readonly expected: SchemaCompletionKind;
731
+ /** Table sites only: tables of other namespaces are candidates too, inserted with `qualifiedText`. */
732
+ readonly qualify?: boolean;
733
+ /** The table key for a column; for a table, the namespace key an unqualified name resolves in. */
734
+ readonly scope: string;
735
+ /** The typed prefix the inserted text replaces; empty (`start === end`) when nothing is typed. */
736
+ readonly span: SourceSpan;
737
+ }
738
+ export interface SchemaCandidate {
739
+ readonly key: string;
740
+ readonly kind: SchemaCompletionKind;
741
+ /** Index in `analyses` of the first analysis that declares `key`. */
742
+ readonly source: number;
743
+ /** The SQL text inserted for the candidate. */
744
+ readonly text: string;
569
745
  }
570
746
  export interface LanguageService {
571
747
  /** Analyzes one document. Synchronous and total: it never throws, and a parse failure is a diagnostic. */
572
748
  readonly analyze: (source: string) => SchemaAnalysis;
749
+ /** The completion context at `offset`. Pure, synchronous and total; `undefined` where no site is known. */
750
+ readonly complete?: (source: string, offset: number) => SchemaCompletion | undefined;
751
+ /** Reads a new table or column name. Pure, synchronous and total; `undefined` when no identifier denotes it. */
752
+ readonly name?: (input: string) => SchemaName | undefined;
573
753
  }
754
+ /**
755
+ * Selects the completion candidates for `completion` from the analyses, in analysis then declaration order.
756
+ * A declaration is a candidate when its `kind` is `expected` and it has `text`, and either its `scope` is the
757
+ * completion's scope (inserting `text`), or the completion sets `qualify`, the declaration is a table with
758
+ * `qualifiedText` and another scope (inserting `qualifiedText`). One candidate per key: the first one wins.
759
+ */
760
+ export declare const completionCandidates: (completion: SchemaCompletion, analyses: readonly Pick<SchemaAnalysis, "declarations">[]) => SchemaCandidate[];
574
761
  /** Returns the first reference whose half-open span contains `offset`. */
575
762
  export declare const referenceAt: (analysis: SchemaAnalysis, offset: number) => SchemaReference | undefined;
763
+ /** Returns the first declaration whose half-open selection contains `offset`. */
764
+ export declare const declarationAt: (analysis: SchemaAnalysis, offset: number) => SchemaDeclaration | undefined;
765
+ /** Returns the spans of the references whose key equals `key`, then the declaration selections with that key when `includeDeclaration` is set. */
766
+ export declare const referencesTo: (key: string, analysis: SchemaAnalysis, includeDeclaration: boolean) => SourceSpan[];
576
767
  /** Returns the selections of the declarations whose key equals the reference key, in declaration order. */
577
768
  export declare const definitions: (reference: SchemaReference, analysis: SchemaAnalysis) => SourceSpan[];
769
+ /** The span that rename replaces: `selection` when present, else the reference span. */
770
+ export declare const nameSpan: (reference: SchemaReference) => SourceSpan;
771
+ /** The declaration, reference or occurrence whose name span contains `offset`. */
772
+ export declare const renameSiteAt: (analysis: SchemaAnalysis, offset: number) => {
773
+ readonly key: string;
774
+ readonly span: SourceSpan;
775
+ } | undefined;
776
+ /** The name spans of every declaration, reference and occurrence with `key`, in document order. */
777
+ export declare const renameSpans: (key: string, analysis: SchemaAnalysis) => SourceSpan[];
578
778
  /** Stable application contract identity, independent of the plugin package version. */
579
779
  interface ApplicationContract {
580
780
  readonly id: string;
package/dist/index.d.ts CHANGED
@@ -25,6 +25,13 @@ export interface DatabaseColumn {
25
25
  export interface DatabaseIndex {
26
26
  readonly key: string;
27
27
  readonly name: string;
28
+ /**
29
+ * Namespaced keys retain vendor-specific index features without teaching core their meaning.
30
+ * Absent and `{}` are the same value; writers omit the field when it would be empty.
31
+ */
32
+ readonly options?: {
33
+ readonly [key: string]: SchemaValue;
34
+ };
28
35
  readonly terms: readonly SchemaValue[];
29
36
  readonly unique: boolean;
30
37
  readonly where: SchemaValue;
@@ -44,6 +51,13 @@ export interface DatabaseView {
44
51
  readonly columns?: readonly string[];
45
52
  readonly identity: TableIdentity;
46
53
  readonly name: string;
54
+ /**
55
+ * Namespaced keys retain vendor-specific view features without teaching core their meaning.
56
+ * Absent and `{}` are the same value; writers omit the field when it would be empty.
57
+ */
58
+ readonly options?: {
59
+ readonly [key: string]: SchemaValue;
60
+ };
47
61
  readonly query: SchemaValue;
48
62
  }
49
63
  export interface DatabaseTrigger {
@@ -54,6 +68,40 @@ export interface DatabaseTrigger {
54
68
  /** Normalized identity of the table or view that owns this trigger. */
55
69
  readonly target: TableIdentity;
56
70
  }
71
+ export interface SequenceOwner {
72
+ /** A column key of `table`. */
73
+ readonly column: string;
74
+ readonly table: TableIdentity;
75
+ }
76
+ export interface DatabaseSequence {
77
+ /**
78
+ * Dialect-normalized data type and parameters. Core compares it with `schemaValueEqual`
79
+ * and never interprets its keys.
80
+ */
81
+ readonly definition: {
82
+ readonly [key: string]: SchemaValue;
83
+ };
84
+ readonly identity: TableIdentity;
85
+ readonly name: string;
86
+ /** The column whose removal drops the sequence. */
87
+ readonly owner?: SequenceOwner;
88
+ }
89
+ export interface DatabaseFunction {
90
+ /**
91
+ * Dialect-normalized language, arguments, result, attributes and body. Core compares it with
92
+ * `schemaValueEqual` and never interprets its keys.
93
+ */
94
+ readonly definition: {
95
+ readonly [key: string]: SchemaValue;
96
+ };
97
+ readonly identity: TableIdentity;
98
+ readonly name: string;
99
+ /**
100
+ * Dialect-normalized types of the arguments that identify the function among overloads, in
101
+ * order. Together with `identity` it is the function's key.
102
+ */
103
+ readonly signature: readonly SchemaValue[];
104
+ }
57
105
  /** A container of named objects one level below an optional catalog. Keys are already normalized by the dialect. */
58
106
  export interface NamespaceIdentity {
59
107
  readonly catalog?: string;
@@ -67,14 +115,41 @@ export interface DatabaseNamespace {
67
115
  readonly [key: string]: SchemaValue;
68
116
  };
69
117
  }
118
+ export interface DatabaseExternals {
119
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
120
+ readonly functions?: readonly DatabaseFunction[];
121
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
122
+ readonly tables?: readonly DatabaseTable[];
123
+ }
70
124
  export interface DatabaseSchema {
125
+ /**
126
+ * Objects outside the managed scope that the model references. Diff, procedures and lowering
127
+ * never create, alter or drop them. Absent and `{}` are the same value.
128
+ */
129
+ readonly externals?: DatabaseExternals;
130
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
131
+ readonly functions?: readonly DatabaseFunction[];
71
132
  /** Absent means namespaces are not declared and no namespace check runs. */
72
133
  readonly namespaces?: readonly DatabaseNamespace[];
134
+ /** Absent and `[]` are the same value; writers omit the field when it would be empty. */
135
+ readonly sequences?: readonly DatabaseSequence[];
73
136
  readonly tables: readonly DatabaseTable[];
74
137
  readonly triggers?: readonly DatabaseTrigger[];
75
138
  readonly views?: readonly DatabaseView[];
76
139
  }
77
140
  export declare const tableKey: (table: TableIdentity) => string;
141
+ export declare const functionKey: (fn: Pick<DatabaseFunction, "identity" | "signature">) => string;
142
+ /** `schema.name(type, …)` for diagnostics; signature entries that are not strings print as JSON. */
143
+ export declare const functionLabel: (fn: Pick<DatabaseFunction, "identity" | "signature">) => string;
144
+ export interface ExternalObject {
145
+ /** Set when the external is a function. */
146
+ readonly function?: DatabaseFunction;
147
+ readonly identity: TableIdentity;
148
+ /** `External table schema.name` or `External function schema.name(type, …)`. */
149
+ readonly label: string;
150
+ }
151
+ /** The external objects of the models, tables before functions, each listed once by its key. */
152
+ export declare const externalObjects: (...databases: readonly DatabaseSchema[]) => readonly ExternalObject[];
78
153
  export declare const namespaceKey: (namespace: NamespaceIdentity) => string;
79
154
  /** The namespace that contains an object, or undefined when its identity has no schema level. */
80
155
  export declare const namespaceOf: (identity: TableIdentity) => NamespaceIdentity | undefined;
@@ -176,6 +251,22 @@ export type TriggerChange = {
176
251
  readonly after: DatabaseTrigger;
177
252
  readonly before: DatabaseTrigger;
178
253
  };
254
+ export type SequenceChange = {
255
+ readonly _tag: "SequenceAdded" | "SequenceRemoved";
256
+ readonly sequence: DatabaseSequence;
257
+ } | {
258
+ readonly _tag: "SequenceChanged" | "SequenceRenamed";
259
+ readonly after: DatabaseSequence;
260
+ readonly before: DatabaseSequence;
261
+ };
262
+ export type FunctionChange = {
263
+ readonly _tag: "FunctionAdded" | "FunctionRemoved";
264
+ readonly function: DatabaseFunction;
265
+ } | {
266
+ readonly _tag: "FunctionChanged";
267
+ readonly after: DatabaseFunction;
268
+ readonly before: DatabaseFunction;
269
+ };
179
270
  /** A namespace change lists the namespace only; objects inside it keep their own changes. */
180
271
  export type NamespaceChange = {
181
272
  readonly _tag: "NamespaceAdded" | "NamespaceRemoved";
@@ -191,11 +282,28 @@ export interface DropGuard {
191
282
  readonly table: TableIdentity;
192
283
  }
193
284
  export interface DatabaseDiff {
194
- readonly changes: readonly (NamespaceChange | TableChange | ViewChange | TriggerChange)[];
285
+ readonly changes: readonly (NamespaceChange | TableChange | ViewChange | TriggerChange | SequenceChange | FunctionChange)[];
195
286
  readonly dropGuards?: readonly DropGuard[];
196
287
  }
197
288
  export declare const validateDatabase: (database: DatabaseSchema, side: DiffDiagnostic["side"]) => readonly DiffDiagnostic[];
289
+ /**
290
+ * Checks an explicit list of managed schemas against the namespaces of a model built from schema
291
+ * files. Names are compared as catalog names, case-sensitively. The shape of the list (non-empty,
292
+ * unique, including `public`) is the caller's to validate.
293
+ */
294
+ export declare const checkManagedScope: (model: DatabaseSchema, managedSchemas: readonly string[]) => readonly DiffDiagnostic[];
198
295
  export declare const compareTable: (before: DatabaseTable, after: DatabaseTable) => readonly TableAlteration[];
296
+ /** Table and column renames of a migration, keyed by the table's key before the rename. */
297
+ export interface SequenceRenames {
298
+ readonly columns: ReadonlyMap<string, ReadonlyMap<string, string>>;
299
+ readonly tables: ReadonlyMap<string, TableIdentity>;
300
+ }
301
+ /** Collect the table and column renames that a list of changes carries. */
302
+ export declare const sequenceRenames: (changes: DatabaseDiff["changes"]) => SequenceRenames;
303
+ /** Function changes. A changed signature is another key: a removal and an addition. */
304
+ export declare const diffFunctions: (before: DatabaseSchema, after: DatabaseSchema) => readonly FunctionChange[];
305
+ /** Sequence changes. `renames` are the table and column renames the migration applies. */
306
+ export declare const diffSequences: (before: DatabaseSchema, after: DatabaseSchema, renames?: SequenceRenames) => readonly SequenceChange[];
199
307
  /** Compare normalized relational models without parsing SQL or consulting a dialect. */
200
308
  export declare const diffDatabases: (before: DatabaseSchema, after: DatabaseSchema) => Effect.Effect<DatabaseDiff, SchemaDiffError>;
201
309
  interface TableProcedure {
@@ -211,6 +319,20 @@ export type MigrationProcedure = {
211
319
  readonly _tag: "AlterNamespace";
212
320
  readonly after: DatabaseNamespace;
213
321
  readonly before: DatabaseNamespace;
322
+ } | {
323
+ readonly _tag: "CreateFunction" | "DropFunction";
324
+ readonly function: DatabaseFunction;
325
+ } | {
326
+ readonly _tag: "ReplaceFunction";
327
+ readonly after: DatabaseFunction;
328
+ readonly before: DatabaseFunction;
329
+ } | {
330
+ readonly _tag: "CreateSequence" | "DropSequence";
331
+ readonly sequence: DatabaseSequence;
332
+ } | {
333
+ readonly _tag: "AlterSequence" | "RenameSequence";
334
+ readonly after: DatabaseSequence;
335
+ readonly before: DatabaseSequence;
214
336
  } | {
215
337
  readonly _tag: "CreateTrigger" | "DropTrigger";
216
338
  readonly trigger: DatabaseTrigger;
@@ -419,6 +541,11 @@ interface Dialect<Ast, PhysicalPlan, E extends Error = Error, R = never, Semanti
419
541
  * `false`, a migration with guards is refused before lowering.
420
542
  */
421
543
  readonly supportsDropGuards?: boolean;
544
+ /**
545
+ * When `true`, a project may list the schemas it manages (ADR 0024 Decision 1). When absent or
546
+ * `false`, the caller refuses such a list by name.
547
+ */
548
+ readonly supportsManagedSchemas?: boolean;
422
549
  readonly toDatabaseSchema: (ast: Ast, side?: DiffDiagnostic["side"]) => Effect.Effect<DatabaseSchema, E, R>;
423
550
  }
424
551
  declare const SchemaModelError_base: new <A extends Record<string, any> = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & {
@@ -540,7 +667,7 @@ export interface SourceSpan {
540
667
  readonly end: number;
541
668
  readonly start: number;
542
669
  }
543
- export type SchemaSymbolKind = "column" | "index" | "namespace" | "table" | "trigger" | "view";
670
+ export type SchemaSymbolKind = "column" | "function" | "index" | "namespace" | "table" | "trigger" | "view";
544
671
  export interface SchemaSymbol {
545
672
  readonly children: readonly SchemaSymbol[];
546
673
  /** Short context shown next to the name, such as the written namespace. */
@@ -556,25 +683,98 @@ export interface SchemaDiagnostic extends SourceSpan {
556
683
  /** A declaration that references can target. `key` is opaque and normalized by the dialect. */
557
684
  export interface SchemaDeclaration {
558
685
  readonly key: string;
686
+ /** What is declared. A declaration without `kind` is never a completion candidate. */
687
+ readonly kind?: SchemaSymbolKind;
688
+ /** Opaque, dialect-normalized key of the declared name alone. Its presence makes the declaration renamable. */
689
+ readonly nameKey?: string;
690
+ /** Tables only: SQL text that denotes the table from any namespace, such as `app.orders`. */
691
+ readonly qualifiedText?: string;
692
+ /** Opaque key of the scope in which the name is unique: a namespace for a table, the table key for a column. */
693
+ readonly scope?: string;
559
694
  readonly selection: SourceSpan;
695
+ /** SQL text that denotes the name alone, quoted when the dialect requires it. */
696
+ readonly text?: string;
560
697
  }
561
698
  export interface SchemaReference extends SourceSpan {
562
699
  readonly key: string;
700
+ /** The name token alone, when the span also covers a qualifier (`public.users`). */
701
+ readonly selection?: SourceSpan;
702
+ }
703
+ /** Where an unresolved identifier token was found. */
704
+ export type SchemaNameContext = "check" | "default" | "generated" | "index" | "query" | "table" | "trigger" | "view";
705
+ /** An identifier token the service does not resolve. `nameKey` is built like `SchemaDeclaration.nameKey`. */
706
+ export interface SchemaNameUse extends SourceSpan {
707
+ readonly context: SchemaNameContext;
708
+ readonly nameKey: string;
563
709
  }
564
710
  export interface SchemaAnalysis {
565
711
  readonly declarations: readonly SchemaDeclaration[];
566
712
  readonly diagnostics: readonly SchemaDiagnostic[];
713
+ /** Uses that rename edits but no other feature reads (Decision 2). */
714
+ readonly occurrences?: readonly SchemaReference[];
567
715
  readonly references: readonly SchemaReference[];
568
716
  readonly symbols: readonly SchemaSymbol[];
717
+ /** Identifier tokens in constructs the service does not resolve (Decision 5). */
718
+ readonly unresolvedNames?: readonly SchemaNameUse[];
719
+ }
720
+ /** A validated new name: its key and the SQL text written at every edited site. */
721
+ export interface SchemaName {
722
+ readonly nameKey: string;
723
+ readonly text: string;
724
+ }
725
+ export type SchemaCompletionKind = "column" | "table";
726
+ /** What a completion request expects at the cursor. Keys are opaque, as everywhere else. */
727
+ export interface SchemaCompletion {
728
+ /** Declarations of the probed statement, with offsets in `source`, which no analysis holds yet. */
729
+ readonly declarations?: readonly SchemaDeclaration[];
730
+ readonly expected: SchemaCompletionKind;
731
+ /** Table sites only: tables of other namespaces are candidates too, inserted with `qualifiedText`. */
732
+ readonly qualify?: boolean;
733
+ /** The table key for a column; for a table, the namespace key an unqualified name resolves in. */
734
+ readonly scope: string;
735
+ /** The typed prefix the inserted text replaces; empty (`start === end`) when nothing is typed. */
736
+ readonly span: SourceSpan;
737
+ }
738
+ export interface SchemaCandidate {
739
+ readonly key: string;
740
+ readonly kind: SchemaCompletionKind;
741
+ /** Index in `analyses` of the first analysis that declares `key`. */
742
+ readonly source: number;
743
+ /** The SQL text inserted for the candidate. */
744
+ readonly text: string;
569
745
  }
570
746
  export interface LanguageService {
571
747
  /** Analyzes one document. Synchronous and total: it never throws, and a parse failure is a diagnostic. */
572
748
  readonly analyze: (source: string) => SchemaAnalysis;
749
+ /** The completion context at `offset`. Pure, synchronous and total; `undefined` where no site is known. */
750
+ readonly complete?: (source: string, offset: number) => SchemaCompletion | undefined;
751
+ /** Reads a new table or column name. Pure, synchronous and total; `undefined` when no identifier denotes it. */
752
+ readonly name?: (input: string) => SchemaName | undefined;
573
753
  }
754
+ /**
755
+ * Selects the completion candidates for `completion` from the analyses, in analysis then declaration order.
756
+ * A declaration is a candidate when its `kind` is `expected` and it has `text`, and either its `scope` is the
757
+ * completion's scope (inserting `text`), or the completion sets `qualify`, the declaration is a table with
758
+ * `qualifiedText` and another scope (inserting `qualifiedText`). One candidate per key: the first one wins.
759
+ */
760
+ export declare const completionCandidates: (completion: SchemaCompletion, analyses: readonly Pick<SchemaAnalysis, "declarations">[]) => SchemaCandidate[];
574
761
  /** Returns the first reference whose half-open span contains `offset`. */
575
762
  export declare const referenceAt: (analysis: SchemaAnalysis, offset: number) => SchemaReference | undefined;
763
+ /** Returns the first declaration whose half-open selection contains `offset`. */
764
+ export declare const declarationAt: (analysis: SchemaAnalysis, offset: number) => SchemaDeclaration | undefined;
765
+ /** Returns the spans of the references whose key equals `key`, then the declaration selections with that key when `includeDeclaration` is set. */
766
+ export declare const referencesTo: (key: string, analysis: SchemaAnalysis, includeDeclaration: boolean) => SourceSpan[];
576
767
  /** Returns the selections of the declarations whose key equals the reference key, in declaration order. */
577
768
  export declare const definitions: (reference: SchemaReference, analysis: SchemaAnalysis) => SourceSpan[];
769
+ /** The span that rename replaces: `selection` when present, else the reference span. */
770
+ export declare const nameSpan: (reference: SchemaReference) => SourceSpan;
771
+ /** The declaration, reference or occurrence whose name span contains `offset`. */
772
+ export declare const renameSiteAt: (analysis: SchemaAnalysis, offset: number) => {
773
+ readonly key: string;
774
+ readonly span: SourceSpan;
775
+ } | undefined;
776
+ /** The name spans of every declaration, reference and occurrence with `key`, in document order. */
777
+ export declare const renameSpans: (key: string, analysis: SchemaAnalysis) => SourceSpan[];
578
778
  /** Stable application contract identity, independent of the plugin package version. */
579
779
  interface ApplicationContract {
580
780
  readonly id: string;