@uncutsql/core 0.7.0 → 0.9.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.ts CHANGED
@@ -354,6 +354,18 @@ export type TableAlteration = {
354
354
  readonly [key: string]: SchemaValue;
355
355
  };
356
356
  };
357
+ /** A column whose values a table change carries over, renamed or not. */
358
+ export interface ColumnPair {
359
+ readonly after: DatabaseColumn;
360
+ readonly before: DatabaseColumn;
361
+ readonly renamed: boolean;
362
+ }
363
+ /**
364
+ * Every column a table change carries over: changed, renamed and unchanged ones. A table option
365
+ * can change how a column with an unchanged declaration stores values (SQLite `STRICT`), so the
366
+ * dialect classifies every pair, not only declaration changes (ADR 0042).
367
+ */
368
+ export declare const pairedColumns: (before: DatabaseTable, after: DatabaseTable, changes: readonly TableAlteration[]) => ColumnPair[];
357
369
  interface TableChanges {
358
370
  readonly after: DatabaseTable;
359
371
  readonly before: DatabaseTable;
@@ -449,9 +461,24 @@ export interface DropGuard {
449
461
  readonly column?: string;
450
462
  readonly table: TableIdentity;
451
463
  }
464
+ export interface TypeChangeAnswer {
465
+ readonly classification: "may_lose" | "may_fail" | "both";
466
+ /** Source column name. */
467
+ readonly column: string;
468
+ readonly failure?: {
469
+ readonly action: "accept_failure";
470
+ } | {
471
+ readonly fallback: {
472
+ readonly literal: SchemaValue;
473
+ };
474
+ };
475
+ readonly loss?: "accept_loss" | "guard";
476
+ readonly table: TableIdentity;
477
+ }
452
478
  export interface DatabaseDiff {
453
479
  readonly changes: readonly (NamespaceChange | EnumChange | ExtensionChange | TableChange | ViewChange | TriggerChange | SequenceChange | FunctionChange | CommentChange)[];
454
480
  readonly dropGuards?: readonly DropGuard[];
481
+ readonly typeChanges?: readonly TypeChangeAnswer[];
455
482
  }
456
483
  export declare const validateDatabase: (database: DatabaseSchema, side: DiffDiagnostic["side"]) => readonly DiffDiagnostic[];
457
484
  /**
@@ -581,16 +608,23 @@ export type MigrationProcedure = {
581
608
  export interface DatabaseMigration {
582
609
  readonly dropGuards?: readonly DropGuard[];
583
610
  readonly procedures: readonly MigrationProcedure[];
611
+ readonly typeChanges?: readonly TypeChangeAnswer[];
584
612
  }
585
613
  /** Low-level logical lowering of caller-approved intent. Resolve migration decisions first in user-facing workflows. */
586
614
  export declare const planDatabaseMigration: (diff: DatabaseDiff) => DatabaseMigration;
587
615
  /** Reverse reviewed intent, retaining rename pairs and guarding newly added data. */
588
616
  export declare const planDownMigration: (up: DatabaseMigration) => DatabaseMigration | string;
617
+ export declare function hashMigrationPair(up: string | Uint8Array, down: string | Uint8Array): string;
589
618
  /** A normalized column key scoped to its qualified relation. */
590
619
  interface ColumnIdentity {
591
620
  readonly key: DatabaseColumn["key"];
592
621
  readonly table: TableIdentity;
593
622
  }
623
+ type SemanticSubject = {
624
+ readonly table: TableIdentity;
625
+ } | {
626
+ readonly column: ColumnIdentity;
627
+ };
594
628
  /** A condition describes a dependency on database behavior, such as a default expression's result. */
595
629
  type SemanticFact<T> = {
596
630
  readonly status: "known";
@@ -785,6 +819,30 @@ interface ExternalUse {
785
819
  readonly externals: DatabaseExternals;
786
820
  readonly statement: number;
787
821
  }
822
+ type TypeChangeClassification = "lossless" | "may_lose" | "may_fail" | "both";
823
+ type TypeChangeLiteral = SchemaValue;
824
+ interface TypeChangeSide {
825
+ readonly column: DatabaseColumn;
826
+ readonly schema: DatabaseSchema;
827
+ readonly table: DatabaseTable;
828
+ }
829
+ interface TypeChange {
830
+ readonly after: TypeChangeSide;
831
+ readonly before: TypeChangeSide;
832
+ /** Accepted enum and label renames before this conversion. Absent means no rename. */
833
+ readonly enumRenames?: EnumRenames;
834
+ }
835
+ /** Pure, synchronous operations that describe the dialect's value conversion. */
836
+ interface TypeChangeSupport {
837
+ /** Returns undefined when there is no conversion to classify. */
838
+ readonly classify: (change: TypeChange) => TypeChangeClassification | undefined;
839
+ readonly displayTypes: (change: TypeChange) => {
840
+ source: string;
841
+ target: string;
842
+ };
843
+ /** Returns undefined when the literal fits, or the reason it is refused. */
844
+ readonly validateFallback: (change: TypeChange, literal: TypeChangeLiteral) => string | undefined;
845
+ }
788
846
  /** Dialect capabilities. The caller owns comparison and logical planning. */
789
847
  interface Dialect<Ast, PhysicalPlan, E extends Error = Error, R = never, SemanticError extends Error = E, SemanticServices = R> {
790
848
  /** Combine independently parsed schema documents in source order. */
@@ -845,6 +903,8 @@ interface Dialect<Ast, PhysicalPlan, E extends Error = Error, R = never, Semanti
845
903
  readonly externals?: DatabaseExternals;
846
904
  readonly onExternalUse?: (use: ExternalUse) => void;
847
905
  }) => Effect.Effect<DatabaseSchema, E, R>;
906
+ /** Classifies type changes and validates fallback literals for migration decisions. */
907
+ readonly typeChange?: TypeChangeSupport;
848
908
  }
849
909
  /** An external of `after` that `before` lacks (`new`) or declares differently (`changed`). */
850
910
  type ExternalChange = {
@@ -888,18 +948,21 @@ export declare const decodeDatabaseSchema: (value: unknown) => Effect.Effect<Dat
888
948
  interface MigrationSource {
889
949
  /** A root baseline records the original prefix tip as the logical checkpoint. */
890
950
  readonly baselineCheckpoint?: string;
891
- readonly down?: {
951
+ readonly down: {
892
952
  /** What the down SQL relies on (ADR 0038 Decision 2). */
893
953
  readonly externals?: DatabaseExternals;
954
+ /** The complete down file text. */
894
955
  readonly sql: string;
895
956
  } | {
896
957
  readonly reason: string;
958
+ readonly text: string;
897
959
  };
898
960
  /** What the up SQL relies on (ADR 0038 Decision 2). */
899
961
  readonly externals?: DatabaseExternals;
900
962
  readonly id: number;
963
+ /** The SHA-256 digest of the migration pair. */
901
964
  readonly sha256: string;
902
- /** The whole migration file, including the CLI's `-- ` header lines. */
965
+ /** The complete up file text. */
903
966
  readonly sql: string;
904
967
  }
905
968
  interface MigrationCheckpoint {
@@ -962,7 +1025,11 @@ interface ExecutorCapabilities<E extends Error = Error, R = never> {
962
1025
  * Recheck the database position under the write transaction before executing. */
963
1026
  checkpoint?: MigrationCheckpoint, options?: ExecutionOptions) => Effect.Effect<ExecutionResult, E | V, R | RV>;
964
1027
  readonly checkpoint: (target: string, options?: ExecutionOptions) => Effect.Effect<MigrationCheckpoint, E, R>;
965
- /** Replay a verified prefix on an empty database and render its state as one SQL migration. */
1028
+ /**
1029
+ * Replay a verified prefix on an empty database and render its state as one SQL migration.
1030
+ * Returns the baseline up SQL only, without a chain header or down record; the caller writes
1031
+ * the down file.
1032
+ */
966
1033
  readonly compact?: (migrations: readonly MigrationSource[], options?: ReplayOptions) => Effect.Effect<string, E, R>;
967
1034
  /** Equal to the `Dialect.name` that produced the lock. Core and the CLI compare it only for equality. */
968
1035
  readonly dialect: string;
@@ -996,7 +1063,7 @@ interface MigrationRun {
996
1063
  readonly offset: number;
997
1064
  }
998
1065
  /** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
999
- export declare const migrationRun: (direction: "up" | "down", migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined, sha256: (sql: string) => string) => MigrationRun | undefined;
1066
+ export declare const migrationRun: (direction: "up" | "down", migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined) => MigrationRun | undefined;
1000
1067
  /** Whether the history tip read under the write transaction is a matching prefix of the run. */
1001
1068
  export declare const continuesRun: (run: MigrationRun, migrations: readonly MigrationSource[], checkpoint: MigrationCheckpoint | undefined, tip: MigrationCheckpoint) => boolean;
1002
1069
  /** The file to execute after the tip, or `undefined` when the run is complete. */
@@ -1026,13 +1093,40 @@ export interface EnumMigrationDecision extends DecisionQuestion {
1026
1093
  readonly enum: TableIdentity;
1027
1094
  readonly value?: string;
1028
1095
  }
1029
- export type MigrationDecision = TableMigrationDecision | EnumMigrationDecision;
1096
+ export interface TypeChangeDecision {
1097
+ /** Type-change questions have no rename candidates. */
1098
+ readonly candidates?: never;
1099
+ readonly column: string;
1100
+ readonly id: string;
1101
+ readonly kind: "type_change_loss" | "type_change_failure";
1102
+ readonly question: string;
1103
+ readonly resolution: {
1104
+ readonly action: "pending";
1105
+ } | {
1106
+ readonly action: "accept_loss";
1107
+ } | {
1108
+ readonly action: "guard";
1109
+ } | {
1110
+ readonly action: "accept_failure";
1111
+ } | {
1112
+ readonly action?: never;
1113
+ readonly fallback: {
1114
+ readonly literal: TypeChangeLiteral;
1115
+ };
1116
+ };
1117
+ readonly table: TableIdentity;
1118
+ }
1119
+ type RemovalDecision = TableMigrationDecision | EnumMigrationDecision;
1120
+ export type MigrationDecision = RemovalDecision | TypeChangeDecision;
1030
1121
  export interface MigrationDecisions {
1031
1122
  readonly after: DatabaseSchema;
1032
1123
  readonly before: DatabaseSchema;
1033
1124
  readonly decisions: readonly MigrationDecision[];
1034
1125
  readonly version: 1;
1035
1126
  }
1127
+ type RemovalDecisions = Omit<MigrationDecisions, "decisions"> & {
1128
+ readonly decisions: readonly RemovalDecision[];
1129
+ };
1036
1130
  declare const MigrationDecisionError_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 & {
1037
1131
  readonly _tag: "MigrationDecisionError";
1038
1132
  } & Readonly<A>;
@@ -1040,21 +1134,15 @@ export declare class MigrationDecisionError extends MigrationDecisionError_base<
1040
1134
  readonly message: string;
1041
1135
  readonly pending: MigrationDecisions;
1042
1136
  }> {}
1043
- export declare const prepareMigrationDecisions: (before: DatabaseSchema, after: DatabaseSchema) => Effect.Effect<{
1044
- after: DatabaseSchema;
1045
- before: DatabaseSchema;
1046
- decisions: readonly MigrationDecision[];
1047
- version: 1;
1048
- }, SchemaDiffError, never>;
1137
+ export declare function prepareMigrationDecisions(before: DatabaseSchema, after: DatabaseSchema): Effect.Effect<RemovalDecisions, SchemaDiffError>;
1138
+ export declare function prepareMigrationDecisions(before: DatabaseSchema, after: DatabaseSchema, typeChange?: TypeChangeSupport): Effect.Effect<MigrationDecisions, SchemaDiffError>;
1139
+ /** Rebuild the column pair and accepted enum renames to validate a proposed fallback. */
1140
+ export declare const validateTypeChangeFallback: (document: MigrationDecisions, decision: TypeChangeDecision, literal: TypeChangeLiteral, typeChange: TypeChangeSupport) => string | undefined;
1049
1141
  /** Refresh the questions and snapshots, carrying forward applicable reviewed choices. */
1050
- export declare const mergeMigrationDecisions: (fresh: MigrationDecisions, previous: unknown) => Effect.Effect<{
1051
- decisions: MigrationDecision[];
1052
- after: DatabaseSchema;
1053
- before: DatabaseSchema;
1054
- version: 1;
1055
- }, SchemaDiffError | SchemaModelError | MigrationDecisionError, never>;
1142
+ export declare function mergeMigrationDecisions(fresh: MigrationDecisions, previous: unknown): Effect.Effect<RemovalDecisions, MigrationDecisionError | SchemaDiffError | SchemaModelError>;
1143
+ export declare function mergeMigrationDecisions(fresh: MigrationDecisions, previous: unknown, typeChange?: TypeChangeSupport): Effect.Effect<MigrationDecisions, MigrationDecisionError | SchemaDiffError | SchemaModelError>;
1056
1144
  /** Validate untrusted JSON and resolve intent against fresh comparison results. */
1057
- export declare const resolveMigrationDecisions: (before: DatabaseSchema, after: DatabaseSchema, supplied?: unknown) => Effect.Effect<{
1145
+ export declare const resolveMigrationDecisions: (before: DatabaseSchema, after: DatabaseSchema, supplied?: unknown, typeChange?: TypeChangeSupport) => Effect.Effect<{
1058
1146
  decisions: MigrationDecisions;
1059
1147
  diff: DatabaseDiff;
1060
1148
  }, SchemaDiffError | MigrationDecisionError, never>;
@@ -1088,6 +1176,12 @@ export interface SchemaDeclaration {
1088
1176
  /** Opaque key of the scope in which the name is unique: a namespace for a table, the table key for a column. */
1089
1177
  readonly scope?: string;
1090
1178
  readonly selection: SourceSpan;
1179
+ /**
1180
+ * Set by the language service on table and column declarations that reach the end of the document.
1181
+ * It equals the identity `toDatabaseSchema` gives the object. Absent for dropped, renamed-away or
1182
+ * unconverted objects, and when the service omits subjects (only mapper hovers are unavailable).
1183
+ */
1184
+ readonly subject?: SemanticSubject;
1091
1185
  /** SQL text that denotes the name alone, quoted when the dialect requires it. */
1092
1186
  readonly text?: string;
1093
1187
  }
@@ -1103,7 +1197,9 @@ export interface SchemaNameUse extends SourceSpan {
1103
1197
  readonly context: SchemaNameContext;
1104
1198
  readonly nameKey: string;
1105
1199
  }
1106
- export interface SchemaAnalysis {
1200
+ export interface SchemaAnalysis<Ast = unknown> {
1201
+ /** The document's parse result, equal to what the same module's `Dialect.parse` succeeds with. Absent when parsing failed. */
1202
+ readonly ast?: Ast;
1107
1203
  readonly declarations: readonly SchemaDeclaration[];
1108
1204
  readonly diagnostics: readonly SchemaDiagnostic[];
1109
1205
  /** Uses that rename edits but no other feature reads (Decision 2). */
@@ -1139,13 +1235,13 @@ export interface SchemaCandidate {
1139
1235
  /** The SQL text inserted for the candidate. */
1140
1236
  readonly text: string;
1141
1237
  }
1142
- export interface LanguageService {
1238
+ export interface LanguageService<Ast = unknown> {
1143
1239
  /**
1144
1240
  * Analyzes one document. Synchronous and total: it never throws. Each diagnostic is either a
1145
1241
  * parse failure or a statement whose precondition fails (it cannot apply). Either kind means the
1146
1242
  * document has errors, which suspends project checks.
1147
1243
  */
1148
- readonly analyze: (source: string) => SchemaAnalysis;
1244
+ readonly analyze: (source: string) => SchemaAnalysis<Ast>;
1149
1245
  /** The completion context at `offset`. Pure, synchronous and total; `undefined` where no site is known. */
1150
1246
  readonly complete?: (source: string, offset: number) => SchemaCompletion | undefined;
1151
1247
  /** Reads a new table or column name. Pure, synchronous and total; `undefined` when no identifier denotes it. */
@@ -1225,10 +1321,26 @@ interface ApplicationMapper<Input, Output, Options, E = never, R = never> {
1225
1321
  interface Generator<Schema, Options, E = never, R = never> {
1226
1322
  readonly generate: (schema: Schema, options: Options) => Effect.Effect<GenerationResult, E, R>;
1227
1323
  }
1324
+ /** A mapper's own rendering of one mapped table or column, for editor hovers. */
1325
+ interface MapperSubjectDescription {
1326
+ /** Named types the texts use, such as a brand declaration. */
1327
+ readonly definitions?: readonly string[];
1328
+ /** Code block language, such as `typescript` or `python`. */
1329
+ readonly language: string;
1330
+ /** Why the subject has these types, such as `override accounts.email` or `runtime profile bigint`. */
1331
+ readonly notes?: readonly string[];
1332
+ /** Labelled type texts in display order, such as select, insert and update. */
1333
+ readonly types: readonly {
1334
+ readonly label: string;
1335
+ readonly text: string;
1336
+ }[];
1337
+ }
1228
1338
  /** Programmatic plugin contract. A dynamically exported mapper supplies its own services. */
1229
1339
  interface MapperPlugin<Input, Output, Options, E = never, R = never> extends ApplicationMapper<Input, Output, Options, E, R> {
1230
1340
  readonly decodeInput: (value: unknown) => Effect.Effect<Input, E, R>;
1231
1341
  readonly decodeOptions: (value: unknown) => Effect.Effect<Options, E, R>;
1342
+ /** Optional: describes a mapped subject. Core never reads the mapped output itself. */
1343
+ readonly describe?: (schema: Output, subject: SemanticSubject) => MapperSubjectDescription | undefined;
1232
1344
  readonly descriptor: MapperDescriptor;
1233
1345
  }
1234
1346
  /** Programmatic plugin contract. A dynamically exported generator supplies its own services. */
@@ -1271,6 +1383,8 @@ export declare const validateApplicationCompatibility: (output: ApplicationContr
1271
1383
  /** Validate the text artifact and diagnostic envelopes before handing results to a consumer. */
1272
1384
  export declare const decodeGenerationResult: (value: unknown) => Effect.Effect<GenerationResult, CodegenError>;
1273
1385
  interface DynamicMapperPlugin {
1386
+ /** Synchronous and optional. Throws a CodegenError when the plugin fails or returns an invalid value. */
1387
+ readonly describe?: (schema: unknown, subject: SemanticSubject) => MapperSubjectDescription | undefined;
1274
1388
  readonly descriptor: MapperDescriptor;
1275
1389
  readonly map: (schema: unknown, options: unknown) => Effect.Effect<MappingResult<unknown>, CodegenError>;
1276
1390
  }
@@ -1282,4 +1396,37 @@ interface DynamicGeneratorPlugin {
1282
1396
  export declare const decodeMapperPlugin: (value: unknown) => Effect.Effect<DynamicMapperPlugin, CodegenError>;
1283
1397
  /** Validate the generator value selected from a module by the consumer. */
1284
1398
  export declare const decodeGeneratorPlugin: (value: unknown) => Effect.Effect<DynamicGeneratorPlugin, CodegenError>;
1285
- export type { ApplicationContract, ApplicationMapper, CodegenDiagnostic, ColumnIdentity, Dialect, DynamicGeneratorPlugin, DynamicMapperPlugin, ExecutionOptions, ExecutionResult, ExecutorCapabilities, ExternalChange, ExternalUse, ExtractOmission, ExtractOptions, ExtractResult, ExtractedPart, GeneratedFile, GenerationResult, Generator, GeneratorDescriptor, GeneratorPlugin, MapperDescriptor, MapperPlugin, MappingResult, MigrationCheckpoint, MigrationExecutor, MigrationRun, MigrationSource, ReplayOptions, SemanticColumn, SemanticColumnType, SemanticDatabaseSchema, SemanticDiagnostic, SemanticDiagnosticSubject, SemanticElement, SemanticFact, SemanticForeignKey, SemanticForeignKeyTarget, SemanticInsertPolicy, SemanticKey, SemanticRelation, SemanticSchemaResolution, SemanticStructField, SemanticValueDomain, SemanticWritePolicy, StatementEdit };
1399
+ interface MappingFailure {
1400
+ readonly error: CodegenError;
1401
+ }
1402
+ interface MappedProfile {
1403
+ readonly mapper: DynamicMapperPlugin;
1404
+ readonly name: string;
1405
+ readonly result: MappingResult<unknown> | MappingFailure;
1406
+ }
1407
+ /** Map every profile in order, retaining errors and diagnostics for the caller. */
1408
+ export declare const mapProfiles: (schema: SemanticDatabaseSchema, profiles: readonly {
1409
+ readonly mapper: DynamicMapperPlugin;
1410
+ readonly name: string;
1411
+ readonly options: unknown;
1412
+ }[]) => Effect.Effect<readonly MappedProfile[]>;
1413
+ declare const GenerationFailure_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 & {
1414
+ readonly _tag: "GenerationFailure";
1415
+ } & Readonly<A>;
1416
+ declare class GenerationFailure extends GenerationFailure_base<{
1417
+ readonly error: CodegenError;
1418
+ readonly generator: string;
1419
+ readonly stage: "compatibility" | "generation";
1420
+ }> {}
1421
+ /** Check the entire batch's contracts before invoking any generator. */
1422
+ export declare const generate: (mapped: MappedProfile & {
1423
+ readonly result: MappingResult<unknown>;
1424
+ }, generators: readonly {
1425
+ readonly generator: DynamicGeneratorPlugin;
1426
+ readonly name: string;
1427
+ readonly options: unknown;
1428
+ }[]) => Effect.Effect<readonly {
1429
+ readonly name: string;
1430
+ readonly result: GenerationResult;
1431
+ }[], GenerationFailure>;
1432
+ export type { ApplicationContract, ApplicationMapper, CodegenDiagnostic, ColumnIdentity, Dialect, DynamicGeneratorPlugin, DynamicMapperPlugin, ExecutionOptions, ExecutionResult, ExecutorCapabilities, ExternalChange, ExternalUse, ExtractOmission, ExtractOptions, ExtractResult, ExtractedPart, GeneratedFile, GenerationFailure, GenerationResult, Generator, GeneratorDescriptor, GeneratorPlugin, MappedProfile, MapperDescriptor, MapperPlugin, MapperSubjectDescription, MappingFailure, MappingResult, MigrationCheckpoint, MigrationExecutor, MigrationRun, MigrationSource, ReplayOptions, SemanticColumn, SemanticColumnType, SemanticDatabaseSchema, SemanticDiagnostic, SemanticDiagnosticSubject, SemanticElement, SemanticFact, SemanticForeignKey, SemanticForeignKeyTarget, SemanticInsertPolicy, SemanticKey, SemanticRelation, SemanticSchemaResolution, SemanticStructField, SemanticSubject, SemanticValueDomain, SemanticWritePolicy, StatementEdit, TypeChange, TypeChangeClassification, TypeChangeLiteral, TypeChangeSupport };