ts-grm-migrate 0.1.0-alpha.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/LICENSE +21 -0
- package/LICENSES/Apache-2.0.txt +202 -0
- package/README.md +237 -0
- package/THIRD_PARTY_NOTICES.md +17 -0
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +21 -0
- package/dist/bin.mjs.map +1 -0
- package/dist/cli-C-KYDbI_.mjs +3573 -0
- package/dist/cli-C-KYDbI_.mjs.map +1 -0
- package/dist/index.d.mts +1046 -0
- package/dist/index.mjs +96 -0
- package/dist/index.mjs.map +1 -0
- package/docs/compatibility.md +22 -0
- package/docs/design.md +41 -0
- package/docs/releasing.md +11 -0
- package/package.json +89 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,1046 @@
|
|
|
1
|
+
import { MySqlDriver } from "@ts-grm/sql";
|
|
2
|
+
import { CascadeType, ScalarType, spi } from "@ts-grm/core";
|
|
3
|
+
//#region src/vendor/ts-grm.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* 方言映射能力 —— migrate 对上游 `Driver` 的最小结构化投影。
|
|
6
|
+
*
|
|
7
|
+
* 只声明 migrate 实际用到的部分(适配器取列类型名、DDL 生成器透传给
|
|
8
|
+
* `toCreationStatements`),因此不依赖上游未导出的 `Driver` 类型。
|
|
9
|
+
*/
|
|
10
|
+
interface SchemaDriver {
|
|
11
|
+
typeName(columnDef: ColumnDef): string;
|
|
12
|
+
}
|
|
13
|
+
/** 上游 TableDef 的结构镜像(见文件头注释) */
|
|
14
|
+
interface TableDef {
|
|
15
|
+
readonly entity: spi.Entity | undefined;
|
|
16
|
+
readonly prop: spi.EntityProp | undefined;
|
|
17
|
+
readonly name: string;
|
|
18
|
+
readonly columns: ReadonlyArray<ColumnDef>;
|
|
19
|
+
readonly constraints: ReadonlyArray<ConstraintDef>;
|
|
20
|
+
toCreationStatements(driver: SchemaDriver): ReadonlyArray<string>;
|
|
21
|
+
toDeletionStatements(driver: SchemaDriver): ReadonlyArray<string>;
|
|
22
|
+
}
|
|
23
|
+
/** 上游 ColumnDef 的结构镜像 */
|
|
24
|
+
interface ColumnDef {
|
|
25
|
+
readonly declaringTable: TableDef;
|
|
26
|
+
readonly prop: spi.EntityProp | undefined;
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly type: ScalarType<any>;
|
|
29
|
+
readonly nullable: boolean;
|
|
30
|
+
readonly length: number | undefined;
|
|
31
|
+
readonly precision: number | undefined;
|
|
32
|
+
readonly scale: number | undefined;
|
|
33
|
+
readonly when: ReadonlyArray<spi.Entity> | undefined;
|
|
34
|
+
}
|
|
35
|
+
/** 上游 ConstraintDef 的结构镜像(判别联合,保序:PRIMARY_KEY/INDEX → UNIQUE → CHECK → FOREIGN_KEY) */
|
|
36
|
+
type ConstraintDef = SimpleConstraintDef | ForeignKeyConstraintDef;
|
|
37
|
+
type SimpleConstraintDef = {
|
|
38
|
+
readonly kind: "PRIMARY_KEY" | "INDEX";
|
|
39
|
+
readonly columns: ReadonlyArray<ColumnDef>;
|
|
40
|
+
readonly implicit: "MIDDLE_TABLE" | undefined;
|
|
41
|
+
} | {
|
|
42
|
+
readonly kind: "UNIQUE";
|
|
43
|
+
readonly columns: ReadonlyArray<ColumnDef>;
|
|
44
|
+
readonly implicit: "ASSOCIATION" | "MIDDLE_ENTITY" | undefined;
|
|
45
|
+
} | {
|
|
46
|
+
readonly kind: "CHECK";
|
|
47
|
+
readonly column: ColumnDef;
|
|
48
|
+
readonly values: ReadonlyArray<string | number>;
|
|
49
|
+
readonly implicit: "POLYMORPHISM" | undefined;
|
|
50
|
+
};
|
|
51
|
+
type ForeignKeyConstraintDef = {
|
|
52
|
+
readonly kind: "FOREIGN_KEY";
|
|
53
|
+
readonly columns: ReadonlyArray<ColumnDef>;
|
|
54
|
+
readonly referencedColumns: ReadonlyArray<ColumnDef>;
|
|
55
|
+
readonly cascade: CascadeType;
|
|
56
|
+
readonly implicit: "INHERITANCE" | undefined;
|
|
57
|
+
};
|
|
58
|
+
//#endregion
|
|
59
|
+
//#region src/schema/model.d.ts
|
|
60
|
+
/** 数据库 schema(一个数据库连接的完整结构) */
|
|
61
|
+
interface Schema {
|
|
62
|
+
/** 表集合;表名使用数据库实际存储的名字(不折叠大小写) */
|
|
63
|
+
readonly tables: ReadonlyArray<Table>;
|
|
64
|
+
}
|
|
65
|
+
/** 表:继承原生 TableDef,去掉模型语义与建表方法,约束/索引为迁移扩展后的形态 */
|
|
66
|
+
interface Table extends Omit<TableDef, "entity" | "prop" | "columns" | "constraints" | "toCreationStatements" | "toDeletionStatements"> {
|
|
67
|
+
readonly columns: ReadonlyArray<Column>;
|
|
68
|
+
readonly constraints: ReadonlyArray<Constraint>;
|
|
69
|
+
/** migrate 扩展:索引。ts-grm 模型无此概念,来源:补充声明 / introspection */
|
|
70
|
+
readonly indexes: ReadonlyArray<Index>;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 列:继承原生 ColumnDef,去掉模型引用与 ScalarType 类型,type 用方言原生字符串。
|
|
74
|
+
* precision / scale 一并去掉:该信息已编码进方言类型字符串(如 "numeric(10,2)"),
|
|
75
|
+
* migrate 的 IR 不重复承载,避免两处表达同一事实而漂移。
|
|
76
|
+
*/
|
|
77
|
+
interface Column extends Omit<ColumnDef, "declaringTable" | "prop" | "type" | "when" | "precision" | "scale"> {
|
|
78
|
+
/** 方言原生类型,如 "bigint" / "text" / "varchar(50)" / "numeric(10,2)" */
|
|
79
|
+
readonly type: string;
|
|
80
|
+
/** 默认值表达式原文(introspection 读到什么存什么,如 "nextval('t_id_seq'::regclass)") */
|
|
81
|
+
readonly default: string | undefined;
|
|
82
|
+
/** 是否自增(identity / serial)——方言差异:PG 靠 default 或 attidentity,MySQL 靠 extra */
|
|
83
|
+
readonly autoIncrement: boolean;
|
|
84
|
+
/** 列在表中的序号(从 1 开始),方言重建表时需要,diff 时忽略 */
|
|
85
|
+
readonly ordinal: number;
|
|
86
|
+
readonly comment: string | undefined;
|
|
87
|
+
/** SQL Server named DEFAULT constraint, retained for ALTER/DROP COLUMN. */
|
|
88
|
+
readonly defaultConstraint?: string;
|
|
89
|
+
readonly collation?: string;
|
|
90
|
+
/** MySQL attributes retained when MODIFY restates a whole column definition. */
|
|
91
|
+
readonly mysql?: {
|
|
92
|
+
readonly charset?: string;
|
|
93
|
+
readonly collation?: string;
|
|
94
|
+
readonly onUpdate?: string;
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
type Constraint = PrimaryKeyConstraint | UniqueConstraint | ForeignKeyConstraint | CheckConstraint;
|
|
98
|
+
/** 主键:继承原生 PRIMARY_KEY 定义(注意:原生 kind 是 "PRIMARY_KEY" | "INDEX" 联合),列改为名字集合 */
|
|
99
|
+
interface PrimaryKeyConstraint extends Omit<Extract<ConstraintDef, {
|
|
100
|
+
readonly kind: "PRIMARY_KEY" | "INDEX";
|
|
101
|
+
}>, "kind" | "columns"> {
|
|
102
|
+
readonly kind: "PRIMARY_KEY";
|
|
103
|
+
readonly name: string | undefined;
|
|
104
|
+
readonly columns: ReadonlyArray<string>;
|
|
105
|
+
}
|
|
106
|
+
/** 唯一约束:继承原生 UNIQUE 定义,列改为名字集合 */
|
|
107
|
+
interface UniqueConstraint extends Omit<Extract<ConstraintDef, {
|
|
108
|
+
readonly kind: "UNIQUE";
|
|
109
|
+
}>, "columns"> {
|
|
110
|
+
readonly name: string | undefined;
|
|
111
|
+
readonly columns: ReadonlyArray<string>;
|
|
112
|
+
}
|
|
113
|
+
type OnDelete = "NO_ACTION" | "RESTRICT" | "CASCADE" | "SET_NULL" | "SET_DEFAULT";
|
|
114
|
+
/** 外键:继承原生 FOREIGN_KEY 定义(含 cascade / implicit),引用目标改为表名 + 列名 */
|
|
115
|
+
interface ForeignKeyConstraint extends Omit<Extract<ConstraintDef, {
|
|
116
|
+
readonly kind: "FOREIGN_KEY";
|
|
117
|
+
}>, "columns" | "referencedColumns"> {
|
|
118
|
+
readonly name: string | undefined;
|
|
119
|
+
/** 本表列 */
|
|
120
|
+
readonly columns: ReadonlyArray<string>;
|
|
121
|
+
/** 被引用表名 */
|
|
122
|
+
readonly referencedTable: string;
|
|
123
|
+
/** 被引用列 */
|
|
124
|
+
readonly referencedColumns: ReadonlyArray<string>;
|
|
125
|
+
/** 由原生 cascade 归一化的删除动作(适配器填充) */
|
|
126
|
+
readonly onDelete: OnDelete;
|
|
127
|
+
/** PG 特有:延迟约束(模型侧无来源,introspection 读 condeferrable) */
|
|
128
|
+
readonly deferrable: boolean;
|
|
129
|
+
}
|
|
130
|
+
/** 检查约束:继承原生 CHECK 定义(column+values 保留),expression 供 DDL 直接使用 */
|
|
131
|
+
interface CheckConstraint extends Omit<Extract<ConstraintDef, {
|
|
132
|
+
readonly kind: "CHECK";
|
|
133
|
+
}>, "column"> {
|
|
134
|
+
readonly name: string | undefined;
|
|
135
|
+
/** 条件表达式原文(如 `"TYPE" in ('Book', 'PaperBook')`) */
|
|
136
|
+
readonly expression: string;
|
|
137
|
+
/** Dialect-normalized comparison form; expression remains executable SQL. */
|
|
138
|
+
readonly comparisonExpression?: string;
|
|
139
|
+
}
|
|
140
|
+
interface Index {
|
|
141
|
+
/** Database-required supporting index, not an independently managed model index. */
|
|
142
|
+
readonly implicit?: boolean;
|
|
143
|
+
readonly name: string;
|
|
144
|
+
readonly columns: ReadonlyArray<string>;
|
|
145
|
+
readonly unique: boolean;
|
|
146
|
+
/** 部分索引(PG)的谓词表达式 */
|
|
147
|
+
readonly predicate: string | undefined;
|
|
148
|
+
}
|
|
149
|
+
/** 便捷构造:空 schema */
|
|
150
|
+
declare function emptySchema(): Schema;
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/dialect.d.ts
|
|
153
|
+
/**
|
|
154
|
+
* 方言注册表 —— 「有哪些方言、上游由谁提供、migrate 实现到哪一步」的唯一出处。
|
|
155
|
+
*
|
|
156
|
+
* 这里不含任何实现。各层实现按方言分文件(`introspector/`、`ddl/`、`executor/`),
|
|
157
|
+
* 未实现的分支由 runtime 在装配**之前**拒掉,并给出准确提示,而不是运行到一半才炸。
|
|
158
|
+
*
|
|
159
|
+
* 为什么集中成一张表:ts-grm 的**驱动型号比方言多** —— SQL Server 有 2012 变体、
|
|
160
|
+
* Oracle 有 12 变体,型号散落在配置/装配代码里很容易漏。将来补一个方言,
|
|
161
|
+
* 只需要在这里把 `implemented` 翻过来、并加上对应实现文件。
|
|
162
|
+
*
|
|
163
|
+
* 上游驱动清单核对自 `@ts-grm/sql` 的导出(packages/sql/src/index.ts)。
|
|
164
|
+
*/
|
|
165
|
+
/** migrate 的方言标识 */
|
|
166
|
+
type DialectName = "postgres" | "mysql" | "sqlite" | "mssql" | "oracle";
|
|
167
|
+
/** 单个方言的支持情况 */
|
|
168
|
+
interface DialectInfo {
|
|
169
|
+
readonly name: DialectName;
|
|
170
|
+
/**
|
|
171
|
+
* ts-grm 上游对应的驱动类名。多个表示版本变体。
|
|
172
|
+
* 拼写沿用上游(`Oracle12Drivier` 上游就是这个名字,不是笔误)。
|
|
173
|
+
*/
|
|
174
|
+
readonly tsGrmDrivers: ReadonlyArray<string>;
|
|
175
|
+
/**
|
|
176
|
+
* migrate 是否**端到端可用**(结构读取 + DDL 生成 + 语句执行都具备)。
|
|
177
|
+
*
|
|
178
|
+
* 五种方言均已接通;具体版本和能力边界见 README。
|
|
179
|
+
*/
|
|
180
|
+
readonly implemented: boolean;
|
|
181
|
+
}
|
|
182
|
+
declare const DIALECTS: ReadonlyArray<DialectInfo>;
|
|
183
|
+
/** 全部方言名(供配置校验与错误提示) */
|
|
184
|
+
declare const DIALECT_NAMES: ReadonlyArray<DialectName>;
|
|
185
|
+
/** 已端到端可用的方言名 */
|
|
186
|
+
declare const IMPLEMENTED_DIALECT_NAMES: ReadonlyArray<DialectName>;
|
|
187
|
+
/**
|
|
188
|
+
* 取某个方言的支持信息。
|
|
189
|
+
* 类型上入参已受限,但配置文件可能是无类型的 JS,所以这里仍做运行时兜底。
|
|
190
|
+
*/
|
|
191
|
+
declare function dialectInfo(name: string): DialectInfo;
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/schema/adapter.d.ts
|
|
194
|
+
/**
|
|
195
|
+
* TableDef[] → Schema。
|
|
196
|
+
*
|
|
197
|
+
* @param tableDefs createSchema() 的产物
|
|
198
|
+
* @param driver createSchema 所用 sqlClient 的 driver(提供方言类型映射)
|
|
199
|
+
*/
|
|
200
|
+
declare function tableDefsToSchema(tableDefs: ReadonlyArray<TableDef>, driver: SchemaDriver, options?: {
|
|
201
|
+
readonly dialect?: DialectName;
|
|
202
|
+
}): Schema;
|
|
203
|
+
//#endregion
|
|
204
|
+
//#region src/snapshot.d.ts
|
|
205
|
+
/** 快照文件格式版本 */
|
|
206
|
+
declare const SNAPSHOT_FORMAT_VERSION: 1;
|
|
207
|
+
interface SchemaSnapshot {
|
|
208
|
+
readonly formatVersion: typeof SNAPSHOT_FORMAT_VERSION;
|
|
209
|
+
readonly schema: Schema;
|
|
210
|
+
}
|
|
211
|
+
/** Schema → 快照 JSON 字符串(可读格式) */
|
|
212
|
+
declare function toSnapshot(schema: Schema): string;
|
|
213
|
+
/**
|
|
214
|
+
* 快照 JSON 字符串 → Schema。
|
|
215
|
+
* 外部输入路径(快照文件可能被手改/损坏/跨版本),先校验形状,失败抛出可读错误。
|
|
216
|
+
*/
|
|
217
|
+
declare function fromSnapshot(json: string): Schema;
|
|
218
|
+
/** Schema 形状校验(快照反序列化的安全网) */
|
|
219
|
+
declare function isSchema(value: unknown): value is Schema;
|
|
220
|
+
//#endregion
|
|
221
|
+
//#region src/diff/types.d.ts
|
|
222
|
+
/** 一次 diff 的完整结果 */
|
|
223
|
+
interface Diff {
|
|
224
|
+
/** 全部操作,按执行顺序 */
|
|
225
|
+
readonly changes: ReadonlyArray<Change>;
|
|
226
|
+
/** 破坏性操作列表(数据可能丢失),是 changes 的子集,供确认/警告 */
|
|
227
|
+
readonly destructive: ReadonlyArray<DestructiveChange>;
|
|
228
|
+
}
|
|
229
|
+
type Change = CreateTable | DropTable | AlterTable;
|
|
230
|
+
/** 单表级别的变更集合 */
|
|
231
|
+
interface AlterTable {
|
|
232
|
+
readonly kind: "ALTER_TABLE";
|
|
233
|
+
readonly table: string;
|
|
234
|
+
readonly columns: ReadonlyArray<ColumnChange>;
|
|
235
|
+
readonly constraints: ReadonlyArray<ConstraintChange>;
|
|
236
|
+
readonly indexes: ReadonlyArray<IndexChange>;
|
|
237
|
+
}
|
|
238
|
+
type ColumnChange = AddColumn | DropColumn | AlterColumn;
|
|
239
|
+
interface AddColumn {
|
|
240
|
+
readonly kind: "ADD_COLUMN";
|
|
241
|
+
readonly column: Column;
|
|
242
|
+
}
|
|
243
|
+
interface DropColumn {
|
|
244
|
+
readonly kind: "DROP_COLUMN";
|
|
245
|
+
readonly column: string;
|
|
246
|
+
}
|
|
247
|
+
interface AlterColumn {
|
|
248
|
+
readonly kind: "ALTER_COLUMN";
|
|
249
|
+
readonly column: string;
|
|
250
|
+
/** 类型变化(null = 不变) */
|
|
251
|
+
readonly type: string | undefined;
|
|
252
|
+
/** nullable 变化(undefined = 不变) */
|
|
253
|
+
readonly nullable: boolean | undefined;
|
|
254
|
+
/** 默认值变化:string = 设置;"" = 删除;undefined = 不变 */
|
|
255
|
+
readonly default: string | "" | undefined;
|
|
256
|
+
readonly autoIncrement: boolean | undefined;
|
|
257
|
+
}
|
|
258
|
+
type ConstraintChange = AddConstraint | DropConstraint;
|
|
259
|
+
interface AddConstraint {
|
|
260
|
+
readonly kind: "ADD_CONSTRAINT";
|
|
261
|
+
readonly constraint: Constraint;
|
|
262
|
+
}
|
|
263
|
+
interface DropConstraint {
|
|
264
|
+
readonly kind: "DROP_CONSTRAINT";
|
|
265
|
+
readonly constraint: Constraint;
|
|
266
|
+
}
|
|
267
|
+
type IndexChange = AddIndex | DropIndex;
|
|
268
|
+
interface AddIndex {
|
|
269
|
+
readonly kind: "ADD_INDEX";
|
|
270
|
+
readonly index: Index;
|
|
271
|
+
}
|
|
272
|
+
interface DropIndex {
|
|
273
|
+
readonly kind: "DROP_INDEX";
|
|
274
|
+
readonly index: Index;
|
|
275
|
+
}
|
|
276
|
+
interface CreateTable {
|
|
277
|
+
readonly kind: "CREATE_TABLE";
|
|
278
|
+
readonly table: Table;
|
|
279
|
+
}
|
|
280
|
+
interface DropTable {
|
|
281
|
+
readonly kind: "DROP_TABLE";
|
|
282
|
+
readonly table: string;
|
|
283
|
+
/**
|
|
284
|
+
* 该表**自身**的外键约束名(现状库里的实际名字)。
|
|
285
|
+
*
|
|
286
|
+
* DDL 生成时会把这些外键**全部**先摘掉,再删所有表(两个阶段,
|
|
287
|
+
* 不是按表逐个处理):否则被某个外键引用的表(同样要删)会因依赖
|
|
288
|
+
* 关系删不掉,PG 报 `cannot drop table ... because other objects depend
|
|
289
|
+
* on it`,整个迁移事务回滚(实测:book / tag / book_tag_mapping 三表,
|
|
290
|
+
* introspection 的字母序让 book 先于中间表被删即失败)。
|
|
291
|
+
* 摘完外键后各 `drop table` 再无依赖,与遍历顺序无关。
|
|
292
|
+
*
|
|
293
|
+
* 对应 prisma 把 `DropForeignKey` 作为独立步骤、排序后置于 `DropTable` 之前。
|
|
294
|
+
*/
|
|
295
|
+
readonly foreignKeyNames: ReadonlyArray<string>;
|
|
296
|
+
}
|
|
297
|
+
/** 可能丢失数据的操作子集(带表名上下文,便于 CLI 提示) */
|
|
298
|
+
type DestructiveChange = DropTable | {
|
|
299
|
+
readonly kind: "DROP_COLUMN";
|
|
300
|
+
readonly table: string;
|
|
301
|
+
readonly column: string;
|
|
302
|
+
} | {
|
|
303
|
+
readonly kind: "ALTER_COLUMN";
|
|
304
|
+
readonly table: string;
|
|
305
|
+
readonly column: string;
|
|
306
|
+
readonly type: string;
|
|
307
|
+
};
|
|
308
|
+
//#endregion
|
|
309
|
+
//#region src/differ.d.ts
|
|
310
|
+
interface Differ {
|
|
311
|
+
/** 计算从 `from` 演进到 `to` 所需的变更 */
|
|
312
|
+
diff(from: Schema, to: Schema): Diff;
|
|
313
|
+
}
|
|
314
|
+
declare class SchemaDiffer implements Differ {
|
|
315
|
+
diff(from: Schema, to: Schema): Diff;
|
|
316
|
+
/** 单表变更;无任何变化返回 null */
|
|
317
|
+
private _alterTable;
|
|
318
|
+
private _columnChanges;
|
|
319
|
+
/** 列变更;无变化返回 null */
|
|
320
|
+
private _alterColumn;
|
|
321
|
+
/** 约束:内容匹配(kind + 列集合 [+ 引用/表达式]),名字不算内容;DROP 先于 ADD 执行 */
|
|
322
|
+
private _constraintChanges;
|
|
323
|
+
/** 索引:内容匹配(唯一性 + 列集合 + 谓词),名字不算内容;DROP 先于 ADD 执行 */
|
|
324
|
+
private _indexChanges;
|
|
325
|
+
/** ALTER_TABLE 中的破坏性子集 */
|
|
326
|
+
private _destructiveOf;
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
329
|
+
//#region src/sql.d.ts
|
|
330
|
+
/**
|
|
331
|
+
* 数据库交互的基础类型。
|
|
332
|
+
*
|
|
333
|
+
* `SqlQueryable` 是最小的「执行 SQL 并拿到行」能力:pg 的 Pool / Client 天然满足,
|
|
334
|
+
* 测试可注入假实现。introspector 与 executor 都以它为公共底座。
|
|
335
|
+
*/
|
|
336
|
+
interface SqlQueryable {
|
|
337
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
338
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
339
|
+
}>;
|
|
340
|
+
}
|
|
341
|
+
//#endregion
|
|
342
|
+
//#region src/executor.d.ts
|
|
343
|
+
type MigrationCompletion = (connection: SqlQueryable) => Promise<void>;
|
|
344
|
+
interface SqlExecutor extends SqlQueryable {
|
|
345
|
+
/**
|
|
346
|
+
* 按序执行语句,失败时抛出带上下文的错误。
|
|
347
|
+
* 支持事务 DDL 的方言整体回滚;MySQL 等隐式提交方言必须明确报告可能部分生效。
|
|
348
|
+
* complete 必须使用同一连接调用,支持事务 DDL 时必须在 COMMIT 前完成。
|
|
349
|
+
*/
|
|
350
|
+
executeStatements(statements: ReadonlyArray<string>, complete?: MigrationCompletion): Promise<void>;
|
|
351
|
+
/**
|
|
352
|
+
* 获取迁移互斥锁(PG 用 advisory lock),阻止其他 migrate 实例并发执行;
|
|
353
|
+
* 返回解锁函数(幂等)。
|
|
354
|
+
*
|
|
355
|
+
* 与进程锁文件互补:进程锁保护本地项目(多终端),数据库锁保护同一数据库
|
|
356
|
+
* (多机部署时进程锁无效)。
|
|
357
|
+
*/
|
|
358
|
+
acquireMigrationLock(key: string): Promise<() => Promise<void>>;
|
|
359
|
+
}
|
|
360
|
+
//#endregion
|
|
361
|
+
//#region src/executor/postgres.d.ts
|
|
362
|
+
/** pg 的 Pool(只声明用到的部分) */
|
|
363
|
+
interface PgPoolLike {
|
|
364
|
+
connect(): Promise<PgClientLike>;
|
|
365
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
366
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
367
|
+
}>;
|
|
368
|
+
}
|
|
369
|
+
/** pg 的连接 */
|
|
370
|
+
interface PgClientLike {
|
|
371
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
372
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
373
|
+
}>;
|
|
374
|
+
release(): void;
|
|
375
|
+
}
|
|
376
|
+
declare class PostgresSqlExecutor implements SqlExecutor {
|
|
377
|
+
private readonly _pool;
|
|
378
|
+
constructor(_pool: PgPoolLike);
|
|
379
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
380
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
381
|
+
}>;
|
|
382
|
+
executeStatements(statements: ReadonlyArray<string>, complete?: MigrationCompletion): Promise<void>;
|
|
383
|
+
acquireMigrationLock(key: string): Promise<() => Promise<void>>;
|
|
384
|
+
}
|
|
385
|
+
//#endregion
|
|
386
|
+
//#region src/lock.d.ts
|
|
387
|
+
interface ProcessLock {
|
|
388
|
+
/** 锁文件路径 */
|
|
389
|
+
readonly path: string;
|
|
390
|
+
/** 释放锁(幂等;只删属于自己的锁) */
|
|
391
|
+
release(): Promise<void>;
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* 获取进程锁;已有活着的持有者时抛出可读错误。
|
|
395
|
+
*
|
|
396
|
+
* @param lockPath 锁文件路径(通常在项目根)
|
|
397
|
+
*/
|
|
398
|
+
declare function acquireProcessLock(lockPath: string): Promise<ProcessLock>;
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/introspector.d.ts
|
|
401
|
+
interface Introspector {
|
|
402
|
+
readonly dialect: Dialect;
|
|
403
|
+
/** 读取数据库的完整结构 */
|
|
404
|
+
introspect(): Promise<Schema>;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* 方言标识。
|
|
408
|
+
*
|
|
409
|
+
* 与 `src/dialect.ts` 的 `DialectName` 同义,保留 `Dialect` 这个名字是为了
|
|
410
|
+
* 贴合上游(ts-grm 用 `Driver`/`dialect` 的用词)以及各实现文件现有的写法。
|
|
411
|
+
*/
|
|
412
|
+
type Dialect = DialectName;
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region src/introspector/postgres.d.ts
|
|
415
|
+
interface PostgresIntrospectorOptions {
|
|
416
|
+
/** 数据库查询能力(通常传 pg 的 Pool / Client) */
|
|
417
|
+
readonly query: SqlQueryable;
|
|
418
|
+
/** 目标 schema 名,默认 "public" */
|
|
419
|
+
readonly schema?: string;
|
|
420
|
+
}
|
|
421
|
+
declare class PostgresIntrospector implements Introspector {
|
|
422
|
+
private readonly _options;
|
|
423
|
+
readonly dialect: Dialect;
|
|
424
|
+
constructor(_options: PostgresIntrospectorOptions);
|
|
425
|
+
introspect(): Promise<Schema>;
|
|
426
|
+
}
|
|
427
|
+
//#endregion
|
|
428
|
+
//#region src/ddl.d.ts
|
|
429
|
+
/** Both states from the same introspection/diff pass, needed by whole-column DDL. */
|
|
430
|
+
interface DdlContext {
|
|
431
|
+
readonly from: Schema;
|
|
432
|
+
readonly to: Schema;
|
|
433
|
+
}
|
|
434
|
+
interface DdlGenerator {
|
|
435
|
+
readonly dialect: Dialect;
|
|
436
|
+
/** 把 diff 渲染为按序执行的 SQL 语句数组 */
|
|
437
|
+
statements(diff: Diff, context?: DdlContext): ReadonlyArray<string>;
|
|
438
|
+
/**
|
|
439
|
+
* 把整个 schema 渲染为建表 SQL(用于重建表路径 / 影子库初始化)。
|
|
440
|
+
* 传入目标态 TableDef 映射时逐表复用 ts-grm 原生 toCreationStatements。
|
|
441
|
+
*/
|
|
442
|
+
createStatements(schema: Schema): ReadonlyArray<string>;
|
|
443
|
+
}
|
|
444
|
+
/** DDL 生成器构造选项(目前仅 SQLite 需要:重建表/建表复用 ts-grm 原生) */
|
|
445
|
+
interface DdlGeneratorOptions {
|
|
446
|
+
/** 方言类型映射(原生 toCreationStatements 需要;提供 tableDefs 时必填) */
|
|
447
|
+
driver?: SchemaDriver;
|
|
448
|
+
/** 表名 → 原生 TableDef(目标态模型产物;CREATE_TABLE / 重建表时条件复用原生) */
|
|
449
|
+
tableDefs?: ReadonlyMap<string, TableDef>;
|
|
450
|
+
}
|
|
451
|
+
//#endregion
|
|
452
|
+
//#region src/ddl/postgres.d.ts
|
|
453
|
+
declare class PostgresDdlGenerator implements DdlGenerator {
|
|
454
|
+
readonly dialect: Dialect;
|
|
455
|
+
statements(diff: Diff): ReadonlyArray<string>;
|
|
456
|
+
createStatements(schema: Schema): ReadonlyArray<string>;
|
|
457
|
+
private _alterTable;
|
|
458
|
+
private _columnChange;
|
|
459
|
+
private _constraintChange;
|
|
460
|
+
private _indexChange;
|
|
461
|
+
}
|
|
462
|
+
//#endregion
|
|
463
|
+
//#region src/ddl/sqlite.d.ts
|
|
464
|
+
declare class SqliteDdlGenerator implements DdlGenerator {
|
|
465
|
+
private readonly _options;
|
|
466
|
+
readonly dialect: Dialect;
|
|
467
|
+
constructor(_options?: DdlGeneratorOptions);
|
|
468
|
+
statements(diff: Diff): ReadonlyArray<string>;
|
|
469
|
+
createStatements(schema: Schema): ReadonlyArray<string>;
|
|
470
|
+
private _createTable;
|
|
471
|
+
private _alterTable;
|
|
472
|
+
/** 纯 ADD_COLUMN 原地执行(SQLite 对 not null / unique 有额外限制,注释提示) */
|
|
473
|
+
private _addColumn;
|
|
474
|
+
/**
|
|
475
|
+
* 重建表路径:SQLite 无法原地 drop column / 改类型 / 改约束,官方做法是
|
|
476
|
+
* 「建新表 → 搬数据 → 换名」。但正确的重建还要同时处理三件目前无法安全完成的事:
|
|
477
|
+
*
|
|
478
|
+
* 1. **外部外键要重定向**。引用该表的其他表,会因 `alter table rename` 被 SQLite
|
|
479
|
+
* 一并改成指向临时表,换名后需要逐个改回;
|
|
480
|
+
* 2. **索引与触发器会随旧表消失**,需按目标态重建 —— 而索引在模型侧无来源;
|
|
481
|
+
* 3. **可搬迁的列是目标与现状的交集**,需要现状信息,而 DDL 生成器只有目标态。
|
|
482
|
+
*
|
|
483
|
+
* 在这三件事设计清楚之前,这里**显式报错**,而不是生成会丢数据的语句 ——
|
|
484
|
+
* 后者只会在真实库里安静地把表清空。这是刻意的取舍,不是遗漏。
|
|
485
|
+
*/
|
|
486
|
+
private _rebuildTable;
|
|
487
|
+
private _indexChanges;
|
|
488
|
+
}
|
|
489
|
+
//#endregion
|
|
490
|
+
//#region src/store.d.ts
|
|
491
|
+
/** 磁盘上的一个迁移文件(`<id>.sql`) */
|
|
492
|
+
interface MigrationFile {
|
|
493
|
+
/** 唯一 id,即文件名去掉 `.sql`(如 "20260911120000_init") */
|
|
494
|
+
readonly id: string;
|
|
495
|
+
/** 迁移 SQL 全文(可含多条语句) */
|
|
496
|
+
readonly sql: string;
|
|
497
|
+
/** 内容校验和(对 sql 全文 sha256) */
|
|
498
|
+
readonly checksum: string;
|
|
499
|
+
/** 应用顺序(同 id,时间戳前缀保证字典序即时间序) */
|
|
500
|
+
readonly sortKey: string;
|
|
501
|
+
}
|
|
502
|
+
/** 历史表中的一条记录 */
|
|
503
|
+
interface AppliedMigration {
|
|
504
|
+
readonly id: string;
|
|
505
|
+
readonly checksum: string;
|
|
506
|
+
readonly appliedAt: Date;
|
|
507
|
+
readonly rolledBackAt: Date | undefined;
|
|
508
|
+
/** 执行未完成或失败时为 true;执行前持久化,成功提交后清除(migrate resolve 可恢复) */
|
|
509
|
+
readonly failed: boolean;
|
|
510
|
+
/** 失败摘要(一行,便于快速判断与增删查) */
|
|
511
|
+
readonly error: string | undefined;
|
|
512
|
+
/** 执行日志(可读详情,成功时为 undefined) */
|
|
513
|
+
readonly logs: string | undefined;
|
|
514
|
+
}
|
|
515
|
+
/** 磁盘迁移文件(读 + 写) */
|
|
516
|
+
interface MigrationFileStore {
|
|
517
|
+
/** 列出全部迁移文件,按 sortKey 升序 */
|
|
518
|
+
listFiles(): Promise<ReadonlyArray<MigrationFile>>;
|
|
519
|
+
/** 写入一个新迁移文件(dev 生成时用) */
|
|
520
|
+
write(file: MigrationFile): Promise<void>;
|
|
521
|
+
}
|
|
522
|
+
/** 数据库迁移历史(读 + 写) */
|
|
523
|
+
interface MigrationHistoryStore {
|
|
524
|
+
/**
|
|
525
|
+
* 历史表名。introspection 时必须从现状中排除它 —— 否则 diff 会把它当作
|
|
526
|
+
* 「目标态里没有的表」而生成 DROP,把记账表删掉(实测踩过这个坑)。
|
|
527
|
+
*/
|
|
528
|
+
readonly tableName: string;
|
|
529
|
+
/** 建表(幂等);首次使用前调用 */
|
|
530
|
+
ensureTable(): Promise<void>;
|
|
531
|
+
/** 已应用的迁移(含失败的),按应用时间升序 */
|
|
532
|
+
listApplied(): Promise<ReadonlyArray<AppliedMigration>>;
|
|
533
|
+
/** 记录成功;提供 connection 时必须使用该事务连接,不能转交连接池。 */
|
|
534
|
+
recordApplied(migration: MigrationFile, connection?: SqlQueryable): Promise<void>;
|
|
535
|
+
/** 标记失败(`error` 为摘要,`logs` 为可读详情);供 resolve / 重试 */
|
|
536
|
+
markFailed(id: string, error: string, logs?: string): Promise<void>;
|
|
537
|
+
/**
|
|
538
|
+
* 标记已回滚(`resolve --rolled-back`):记录保留,但它重新变成「待应用」。
|
|
539
|
+
* 不删记录 —— 什么时候回滚过、当时什么情况,都是有用的审计信息。
|
|
540
|
+
* 返回是否命中记录(false = 该迁移没有历史记录)。
|
|
541
|
+
*/
|
|
542
|
+
markRolledBack(id: string): Promise<boolean>;
|
|
543
|
+
}
|
|
544
|
+
/** 对迁移 SQL 全文计算校验和 */
|
|
545
|
+
declare function checksumOf(sql: string): string;
|
|
546
|
+
/**
|
|
547
|
+
* 基于目录的迁移文件存储。
|
|
548
|
+
*
|
|
549
|
+
* 格式:`<id>.sql`,文件全文交给执行器。PG / MySQL / SQL Server 执行批次,
|
|
550
|
+
* Oracle 执行器负责识别 SQL 语句边界。文件不要自行写 begin / commit:事务由执行器管理。
|
|
551
|
+
*/
|
|
552
|
+
declare class FileMigrationStore implements MigrationFileStore {
|
|
553
|
+
private readonly _dir;
|
|
554
|
+
constructor(_dir: string);
|
|
555
|
+
listFiles(): Promise<ReadonlyArray<MigrationFile>>;
|
|
556
|
+
write(file: MigrationFile): Promise<void>;
|
|
557
|
+
}
|
|
558
|
+
/** 默认历史表名(对应 prisma 的 `_prisma_migrations`) */
|
|
559
|
+
declare const DEFAULT_HISTORY_TABLE = "_migrations";
|
|
560
|
+
/**
|
|
561
|
+
* 基于数据库的迁移历史存储。
|
|
562
|
+
*
|
|
563
|
+
* 语句是标准 SQL,方言差异集中在三处:时间类型(PG `timestamptz` / SQLite `text`)、
|
|
564
|
+
* 当前时间表达式(`now()` / `current_timestamp`)、以及参数占位符(`$n` / `?`)。
|
|
565
|
+
* 另有 SQLite 不支持 `alter table ... add column if not exists`,改查 catalog 实现幂等。
|
|
566
|
+
*/
|
|
567
|
+
declare class DatabaseMigrationHistoryStore implements MigrationHistoryStore {
|
|
568
|
+
private readonly _options;
|
|
569
|
+
readonly tableName: string;
|
|
570
|
+
private readonly _table;
|
|
571
|
+
private readonly _dialect;
|
|
572
|
+
private readonly _server?;
|
|
573
|
+
constructor(_options: {
|
|
574
|
+
readonly executor: SqlExecutor;
|
|
575
|
+
readonly table?: string;
|
|
576
|
+
/** 方言,默认 postgres(用于时间类型、now() 与占位符) */
|
|
577
|
+
readonly dialect?: DialectName;
|
|
578
|
+
/** SQL Server defaults to dbo; Oracle requires an explicit schema. */
|
|
579
|
+
readonly schema?: string;
|
|
580
|
+
});
|
|
581
|
+
/** 当前时间表达式:PG 用 now(),SQLite 用 current_timestamp */
|
|
582
|
+
private get _now();
|
|
583
|
+
/** 参数占位符:PG 是 $n,SQLite 是 ?(位置绑定,顺序一致) */
|
|
584
|
+
private _ph;
|
|
585
|
+
ensureTable(): Promise<void>;
|
|
586
|
+
/**
|
|
587
|
+
* 幂等补列。
|
|
588
|
+
* PG 9.6+ 有 `add column if not exists`;SQLite 没有该语法,改查 catalog
|
|
589
|
+
* (`pragma_table_info` 表值函数,SQLite 3.16+)。
|
|
590
|
+
*/
|
|
591
|
+
private _addColumnIfMissing;
|
|
592
|
+
listApplied(): Promise<ReadonlyArray<AppliedMigration>>;
|
|
593
|
+
recordApplied(migration: MigrationFile, connection?: SqlQueryable): Promise<void>;
|
|
594
|
+
markFailed(id: string, error: string, logs?: string): Promise<void>;
|
|
595
|
+
markRolledBack(id: string): Promise<boolean>;
|
|
596
|
+
}
|
|
597
|
+
//#endregion
|
|
598
|
+
//#region src/drift.d.ts
|
|
599
|
+
interface SchemaDrift {
|
|
600
|
+
/** 涉及的表 */
|
|
601
|
+
readonly table: string;
|
|
602
|
+
/** 人类可读的说明,如「缺少列 EMAIL」 */
|
|
603
|
+
readonly summary: string;
|
|
604
|
+
/**
|
|
605
|
+
* 是否由**已知限制**导致(目前只有 CHECK 表达式:PG 会把它 deparse 成另一种写法,
|
|
606
|
+
* 与模型侧生成的 `col in (...)` 永远不相等)。这类差异无法通过迁移消除,
|
|
607
|
+
* 不应作为异常告警。
|
|
608
|
+
*/
|
|
609
|
+
readonly known: boolean;
|
|
610
|
+
}
|
|
611
|
+
/** 把一次 diff 渲染成对账报告(空数组 = 数据库与模型一致) */
|
|
612
|
+
declare function describeDiff(diff: Diff): ReadonlyArray<SchemaDrift>;
|
|
613
|
+
/** 过滤出真正的异常(排除已知限制造成的噪声) */
|
|
614
|
+
declare function abnormalDrift(drift: ReadonlyArray<SchemaDrift>): ReadonlyArray<SchemaDrift>;
|
|
615
|
+
//#endregion
|
|
616
|
+
//#region src/migrator.d.ts
|
|
617
|
+
interface MigratorOptions {
|
|
618
|
+
/** 磁盘迁移文件 */
|
|
619
|
+
readonly files: MigrationFileStore;
|
|
620
|
+
/** 数据库迁移历史 */
|
|
621
|
+
readonly history: MigrationHistoryStore;
|
|
622
|
+
/** 语句执行 + 数据库锁 */
|
|
623
|
+
readonly executor: SqlExecutor;
|
|
624
|
+
/** 现状读取(diff 的 from 侧) */
|
|
625
|
+
readonly introspector: Introspector;
|
|
626
|
+
/** diff → 方言 SQL */
|
|
627
|
+
readonly ddl: DdlGenerator;
|
|
628
|
+
/** 从模型推导的目标 schema(ts-grm 侧提供,diff 的 to 侧) */
|
|
629
|
+
targetSchema: () => Promise<Schema>;
|
|
630
|
+
/** 迁移文件目录(dev 生成新迁移时写入) */
|
|
631
|
+
readonly migrationsDir: string;
|
|
632
|
+
/** 进程锁文件路径(通常在项目根) */
|
|
633
|
+
readonly lockPath: string;
|
|
634
|
+
/** 数据库 advisory lock 的 key;默认使用固定资源名,由执行器限定数据库/schema */
|
|
635
|
+
readonly lockKey?: string;
|
|
636
|
+
/**
|
|
637
|
+
* 破坏性变更的确认钩子。返回 false 则中止(抛 `MigrationAbortedError`)。
|
|
638
|
+
* 不传 = 总是允许(程序化调用 / CI)。CLI 在这里做交互确认。
|
|
639
|
+
*/
|
|
640
|
+
readonly confirm?: (diff: Diff) => Promise<boolean>;
|
|
641
|
+
}
|
|
642
|
+
/** 使用者在确认破坏性变更时选择中止 */
|
|
643
|
+
declare class MigrationAbortedError extends Error {
|
|
644
|
+
constructor();
|
|
645
|
+
}
|
|
646
|
+
/** `deploy()` 结果 */
|
|
647
|
+
interface DeployResult {
|
|
648
|
+
/** 本次应用的迁移 id(按应用顺序) */
|
|
649
|
+
readonly applied: ReadonlyArray<string>;
|
|
650
|
+
/** 已应用过而跳过的数量 */
|
|
651
|
+
readonly skipped: number;
|
|
652
|
+
/** 应用后的对账结果(空 = 数据库已等于模型;详见 `src/drift.ts`) */
|
|
653
|
+
readonly drift: ReadonlyArray<SchemaDrift>;
|
|
654
|
+
}
|
|
655
|
+
/** `dev()` 结果 */
|
|
656
|
+
interface DevResult {
|
|
657
|
+
readonly diff: Diff;
|
|
658
|
+
/** 新生成的迁移 id;无变更时为 undefined */
|
|
659
|
+
readonly migrationId: string | undefined;
|
|
660
|
+
readonly applied: boolean;
|
|
661
|
+
readonly drift: ReadonlyArray<SchemaDrift>;
|
|
662
|
+
}
|
|
663
|
+
/** `push()` 结果 */
|
|
664
|
+
interface PushResult {
|
|
665
|
+
readonly diff: Diff;
|
|
666
|
+
readonly statements: ReadonlyArray<string>;
|
|
667
|
+
readonly drift: ReadonlyArray<SchemaDrift>;
|
|
668
|
+
}
|
|
669
|
+
/** `status()` 结果 */
|
|
670
|
+
interface MigrationStatus {
|
|
671
|
+
readonly applied: ReadonlyArray<{
|
|
672
|
+
readonly id: string;
|
|
673
|
+
readonly appliedAt: Date;
|
|
674
|
+
readonly failed: boolean;
|
|
675
|
+
}>;
|
|
676
|
+
readonly pending: ReadonlyArray<string>;
|
|
677
|
+
}
|
|
678
|
+
/** `resolve()` 的动作 */
|
|
679
|
+
type ResolveAction =
|
|
680
|
+
/** 标记为已应用(SQL 已经手工执行过) */
|
|
681
|
+
"applied" |
|
|
682
|
+
/** 清除失败记录(影响已回退,想重新应用) */
|
|
683
|
+
"rolled-back";
|
|
684
|
+
interface ResolveOptions {
|
|
685
|
+
readonly migration: string;
|
|
686
|
+
readonly action: ResolveAction;
|
|
687
|
+
}
|
|
688
|
+
declare class Migrator {
|
|
689
|
+
private readonly _options;
|
|
690
|
+
private readonly _differ;
|
|
691
|
+
constructor(_options: MigratorOptions);
|
|
692
|
+
/** 应用所有未应用的迁移(deploy 路径,无交互) */
|
|
693
|
+
deploy(): Promise<DeployResult>;
|
|
694
|
+
/**
|
|
695
|
+
* 生成并应用新迁移(dev 路径)。
|
|
696
|
+
* 与数据库无差异时不产生文件;破坏性变更的确认交给 CLI 层(`Diff.destructive`)。
|
|
697
|
+
* `name` 可省略,省略时迁移只用时间戳命名。
|
|
698
|
+
*/
|
|
699
|
+
dev(options: {
|
|
700
|
+
readonly name?: string;
|
|
701
|
+
}): Promise<DevResult>;
|
|
702
|
+
/** 无历史快速同步:diff 后直接应用,不写文件、不记历史(push 路径) */
|
|
703
|
+
push(): Promise<PushResult>;
|
|
704
|
+
/** 迁移状态(只读,不加锁,不改库) */
|
|
705
|
+
status(): Promise<MigrationStatus>;
|
|
706
|
+
/**
|
|
707
|
+
* 手工修正迁移状态 —— 失败后的恢复途径。
|
|
708
|
+
*
|
|
709
|
+
* - `applied`:记为已应用(适用于「这条 SQL 我已经手工执行过了」)
|
|
710
|
+
* - `rolled-back`:清除历史记录,让它重新变成待应用(适用于「影响已回退」)
|
|
711
|
+
*
|
|
712
|
+
* 没有这一步,一次失败的迁移会把 deploy / dev 永久卡住 —— `_assertNoFailed`
|
|
713
|
+
* 会一直拒绝,而使用者只能去手工改数据库表。
|
|
714
|
+
*/
|
|
715
|
+
resolve(options: ResolveOptions): Promise<void>;
|
|
716
|
+
/**
|
|
717
|
+
* 对账:不应用任何东西,只报告当前数据库与模型的差异。
|
|
718
|
+
* 迁移之后调它,可确认数据库真的变成了模型的样子。
|
|
719
|
+
*/
|
|
720
|
+
checkDrift(): Promise<ReadonlyArray<SchemaDrift>>;
|
|
721
|
+
/** 再 introspect 一次并与模型对比(空 = 一致) */
|
|
722
|
+
private _describeDrift;
|
|
723
|
+
/**
|
|
724
|
+
* 上次失败的迁移必须先处理:它意味着数据库可能处于半应用状态,
|
|
725
|
+
* 而且失败记录的 checksum 是空的(不能被当成正常应用的迁移做漂移比较)。
|
|
726
|
+
*/
|
|
727
|
+
private _assertNoFailed;
|
|
728
|
+
/** 有破坏性变更时询问;使用者拒绝则中止 */
|
|
729
|
+
private _confirmIfNeeded;
|
|
730
|
+
/** 包一层进程锁 + 数据库锁 */
|
|
731
|
+
private _withLocks;
|
|
732
|
+
private _applyOne;
|
|
733
|
+
/**
|
|
734
|
+
* 当前「已生效」的迁移记录:**排除被标记回滚的**。
|
|
735
|
+
* 被回滚的迁移重新算待应用(`resolve --rolled-back` 的语义),
|
|
736
|
+
* 但它那条记录会保留下来,作为「什么时候回滚过」的审计痕迹。
|
|
737
|
+
*/
|
|
738
|
+
private _effectiveApplied;
|
|
739
|
+
private _diffAgainstDatabase;
|
|
740
|
+
}
|
|
741
|
+
/**
|
|
742
|
+
* 迁移 id:UTC 毫秒时间戳 + 名字 slug(`20260911120000000_init`)。
|
|
743
|
+
* 时间戳前缀保证**字典序即时间序**,`sortKey` 直接复用它。
|
|
744
|
+
*/
|
|
745
|
+
declare function generateMigrationId(now: Date, name: string): string;
|
|
746
|
+
/** 语句数组 → 迁移文件内容(语句间空行分隔,统一以分号结尾) */
|
|
747
|
+
declare function toSqlFile(statements: ReadonlyArray<string>): string;
|
|
748
|
+
//#endregion
|
|
749
|
+
//#region src/config.d.ts
|
|
750
|
+
/**
|
|
751
|
+
* 数据库连接。
|
|
752
|
+
*
|
|
753
|
+
* - postgres:原样传给 pg 的 Pool(host / port / user / password / database / connectionString);
|
|
754
|
+
* - sqlite:用 `file` 指定库文件(相对项目根;`:memory:` 为内存库),其余字段忽略。
|
|
755
|
+
*/
|
|
756
|
+
interface DatabaseConfig {
|
|
757
|
+
/** SQL Server TLS options. Defaults: encrypt=true, trustServerCertificate=false. */
|
|
758
|
+
readonly encrypt?: boolean;
|
|
759
|
+
readonly trustServerCertificate?: boolean;
|
|
760
|
+
readonly host?: string;
|
|
761
|
+
readonly port?: number;
|
|
762
|
+
readonly database?: string;
|
|
763
|
+
readonly user?: string;
|
|
764
|
+
readonly password?: string;
|
|
765
|
+
readonly connectionString?: string;
|
|
766
|
+
/** sqlite 专用:库文件路径(相对项目根)或 ":memory:" */
|
|
767
|
+
readonly file?: string;
|
|
768
|
+
}
|
|
769
|
+
interface MigrateConfig {
|
|
770
|
+
/**
|
|
771
|
+
* 方言,默认 "postgres"。
|
|
772
|
+
*
|
|
773
|
+
* postgres / sqlite / mysql / mssql / oracle 均有实现,版本和功能边界见 README。
|
|
774
|
+
*
|
|
775
|
+
* sqlite 时用 `database.file` 指定库文件(相对项目根,或 ":memory:"),
|
|
776
|
+
* 且**不能**配 `schema`(SQLite 没有 schema 概念,配了会报错)。
|
|
777
|
+
*/
|
|
778
|
+
readonly dialect?: DialectName;
|
|
779
|
+
/** 数据库连接 */
|
|
780
|
+
readonly database: DatabaseConfig;
|
|
781
|
+
/**
|
|
782
|
+
* 模型文件或目录(相对项目根,必须以 `./` 或 `../` 开头 —— `EntityManager.of` 的要求)。
|
|
783
|
+
* 只用于触发模型注册;实际拿到的是全局注册表里的**全部** model。
|
|
784
|
+
*/
|
|
785
|
+
readonly models: ReadonlyArray<string>;
|
|
786
|
+
/** 迁移文件目录(相对项目根),默认 `./src/ts-grm` */
|
|
787
|
+
readonly migrationsDir?: string;
|
|
788
|
+
/** 目标 schema:PG 默认 public,SQL Server 默认 dbo,Oracle 默认登录用户 schema。 */
|
|
789
|
+
readonly schema?: string;
|
|
790
|
+
/** 进程锁文件(相对项目根),默认 `./.ts-grm-migrate.lock` */
|
|
791
|
+
readonly lockPath?: string;
|
|
792
|
+
}
|
|
793
|
+
/** 配置辅助函数:仅用于类型提示与自动补全 */
|
|
794
|
+
declare function defineConfig(config: MigrateConfig): MigrateConfig;
|
|
795
|
+
/** 配置文件名候选(按顺序尝试) */
|
|
796
|
+
declare const CONFIG_FILENAMES: ReadonlyArray<string>;
|
|
797
|
+
interface LoadedConfig {
|
|
798
|
+
readonly config: MigrateConfig;
|
|
799
|
+
/** 实际加载的配置文件绝对路径 */
|
|
800
|
+
readonly path: string;
|
|
801
|
+
}
|
|
802
|
+
/** 加载配置:显式路径优先,否则在 `cwd` 下按候选名查找 */
|
|
803
|
+
declare function loadConfig(cwd: string, explicitPath?: string): Promise<LoadedConfig>;
|
|
804
|
+
//#endregion
|
|
805
|
+
//#region src/runtime.d.ts
|
|
806
|
+
interface Runtime {
|
|
807
|
+
readonly migrator: Migrator;
|
|
808
|
+
/** 释放连接池等资源 */
|
|
809
|
+
close(): Promise<void>;
|
|
810
|
+
}
|
|
811
|
+
interface RuntimeOptions {
|
|
812
|
+
/**
|
|
813
|
+
* 破坏性变更确认钩子(CLI 传交互式确认)。
|
|
814
|
+
* 不传 = 总是允许(CI / 程序化调用,「我知道在做什么」的默认)。
|
|
815
|
+
*/
|
|
816
|
+
readonly confirm?: (diff: Diff) => Promise<boolean>;
|
|
817
|
+
}
|
|
818
|
+
/** 默认迁移目录 / 锁文件(相对项目根) */
|
|
819
|
+
declare const DEFAULT_MIGRATIONS_DIR = "./src/ts-grm";
|
|
820
|
+
declare const DEFAULT_LOCK_PATH = "./.ts-grm-migrate.lock";
|
|
821
|
+
/**
|
|
822
|
+
* 从配置组装运行时。
|
|
823
|
+
*
|
|
824
|
+
* `cwd` 是项目根:模型路径、迁移目录、锁文件都相对它解析。
|
|
825
|
+
*/
|
|
826
|
+
declare function createRuntime(config: MigrateConfig, cwd: string, options?: RuntimeOptions): Promise<Runtime>;
|
|
827
|
+
//#endregion
|
|
828
|
+
//#region src/cli/types.d.ts
|
|
829
|
+
/** `parseArgs` 结果 */
|
|
830
|
+
interface ParsedArgs {
|
|
831
|
+
/** 第一个位置参数(命令名) */
|
|
832
|
+
readonly command: string | undefined;
|
|
833
|
+
/** `--key value` / `--key=value` / `-h` 形式的选项 */
|
|
834
|
+
readonly flags: ReadonlyMap<string, string | true>;
|
|
835
|
+
}
|
|
836
|
+
/** `run()` 的可注入依赖(测试用) */
|
|
837
|
+
interface RunOptions {
|
|
838
|
+
/** 覆盖破坏性变更的确认逻辑 */
|
|
839
|
+
readonly confirm?: (diff: Diff) => Promise<boolean>;
|
|
840
|
+
readonly log?: (message: string) => void;
|
|
841
|
+
readonly errorLog?: (message: string) => void;
|
|
842
|
+
}
|
|
843
|
+
//#endregion
|
|
844
|
+
//#region src/cli.d.ts
|
|
845
|
+
declare function parseArgs(argv: ReadonlyArray<string>): ParsedArgs;
|
|
846
|
+
/**
|
|
847
|
+
* 执行一条命令,返回退出码。
|
|
848
|
+
*
|
|
849
|
+
* 不调用 `process.exit`,也不直接写 console(可注入),便于测试。
|
|
850
|
+
*/
|
|
851
|
+
declare function run(argv: ReadonlyArray<string>, cwd: string, options?: RunOptions): Promise<number>;
|
|
852
|
+
//#endregion
|
|
853
|
+
//#region src/ddl/mysql.d.ts
|
|
854
|
+
/**
|
|
855
|
+
* Phase ordering: drop dependent FKs/indexes/constraints, alter columns, add keys, add FKs.
|
|
856
|
+
* CREATE foreign keys are deferred so table order and cyclic references are harmless.
|
|
857
|
+
* @see https://github.com/prisma/prisma-engines/blob/main/schema-engine/connectors/sql-schema-connector/src/flavour/mysql/renderer.rs
|
|
858
|
+
*/
|
|
859
|
+
declare class MysqlDdlGenerator implements DdlGenerator {
|
|
860
|
+
readonly dialect: "mysql";
|
|
861
|
+
statements(diff: Diff, context?: DdlContext): ReadonlyArray<string>;
|
|
862
|
+
createStatements(schema: Schema): ReadonlyArray<string>;
|
|
863
|
+
}
|
|
864
|
+
//#endregion
|
|
865
|
+
//#region src/introspector/mysql.d.ts
|
|
866
|
+
/**
|
|
867
|
+
* MySQL 8.0.16+ / InnoDB catalog reader. Catalog names are bound, never interpolated.
|
|
868
|
+
* @see https://dev.mysql.com/doc/refman/8.4/en/information-schema.html
|
|
869
|
+
*/
|
|
870
|
+
declare class MysqlIntrospector implements Introspector {
|
|
871
|
+
private readonly _options;
|
|
872
|
+
readonly dialect: "mysql";
|
|
873
|
+
constructor(_options: {
|
|
874
|
+
readonly query: SqlQueryable;
|
|
875
|
+
});
|
|
876
|
+
introspect(): Promise<Schema>;
|
|
877
|
+
private _column;
|
|
878
|
+
private _constraints;
|
|
879
|
+
private _indexes;
|
|
880
|
+
}
|
|
881
|
+
//#endregion
|
|
882
|
+
//#region src/executor/mysql.d.ts
|
|
883
|
+
interface MysqlConnectionLike {
|
|
884
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<readonly [unknown, unknown]>;
|
|
885
|
+
execute(sql: string, params: ReadonlyArray<unknown>): Promise<readonly [unknown, unknown]>;
|
|
886
|
+
release(): void;
|
|
887
|
+
destroy(): void;
|
|
888
|
+
}
|
|
889
|
+
interface MysqlPoolLike {
|
|
890
|
+
getConnection(): Promise<MysqlConnectionLike>;
|
|
891
|
+
}
|
|
892
|
+
/**
|
|
893
|
+
* DDL implicitly commits in MySQL. Never report that a failed migration was rolled back.
|
|
894
|
+
* Keep the advisory lock and all migration work on the same session: loss of the session
|
|
895
|
+
* must abort the migration instead of continuing on another pooled connection.
|
|
896
|
+
* @see https://dev.mysql.com/doc/refman/8.4/en/implicit-commit.html
|
|
897
|
+
* @see https://dev.mysql.com/doc/refman/8.4/en/locking-functions.html
|
|
898
|
+
*/
|
|
899
|
+
declare class MysqlSqlExecutor implements SqlExecutor {
|
|
900
|
+
private readonly _pool;
|
|
901
|
+
private readonly _lockTimeout;
|
|
902
|
+
private _locked;
|
|
903
|
+
constructor(_pool: MysqlPoolLike, _lockTimeout?: number);
|
|
904
|
+
private _prepare;
|
|
905
|
+
private _withConnection;
|
|
906
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
907
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
908
|
+
}>;
|
|
909
|
+
executeStatements(statements: ReadonlyArray<string>, complete?: MigrationCompletion): Promise<void>;
|
|
910
|
+
acquireMigrationLock(_key: string): Promise<() => Promise<void>>;
|
|
911
|
+
}
|
|
912
|
+
//#endregion
|
|
913
|
+
//#region src/server/sql.d.ts
|
|
914
|
+
type ServerDialect = "mssql" | "oracle";
|
|
915
|
+
/** Names and namespaces are shared by introspection, history and DDL rendering. */
|
|
916
|
+
declare class ServerSql {
|
|
917
|
+
readonly dialect: ServerDialect;
|
|
918
|
+
readonly schema: string;
|
|
919
|
+
constructor(dialect: ServerDialect, schema: string);
|
|
920
|
+
identifier(name: string): string;
|
|
921
|
+
table(name: string): string;
|
|
922
|
+
param(index: number): string;
|
|
923
|
+
literal(value: string): string;
|
|
924
|
+
name(value: string): string;
|
|
925
|
+
}
|
|
926
|
+
//#endregion
|
|
927
|
+
//#region src/server/ddl.d.ts
|
|
928
|
+
/** Dependency ordering shared by SQL Server and Oracle; syntax stays dialect-specific. */
|
|
929
|
+
declare class ServerDdlGenerator implements DdlGenerator {
|
|
930
|
+
readonly dialect: ServerDialect;
|
|
931
|
+
readonly sql: ServerSql;
|
|
932
|
+
constructor(dialect: ServerDialect, schema: string);
|
|
933
|
+
statements(diff: Diff, context?: DdlContext): ReadonlyArray<string>;
|
|
934
|
+
createStatements(schema: Schema): ReadonlyArray<string>;
|
|
935
|
+
private column;
|
|
936
|
+
private createTable;
|
|
937
|
+
private constraint;
|
|
938
|
+
private index;
|
|
939
|
+
}
|
|
940
|
+
//#endregion
|
|
941
|
+
//#region src/ddl/sqlserver.d.ts
|
|
942
|
+
declare class SqlServerDdlGenerator extends ServerDdlGenerator {
|
|
943
|
+
constructor(schema?: string);
|
|
944
|
+
}
|
|
945
|
+
//#endregion
|
|
946
|
+
//#region src/introspector/sqlserver.d.ts
|
|
947
|
+
/** @see https://learn.microsoft.com/en-us/sql/relational-databases/system-catalog-views/catalog-views-transact-sql */
|
|
948
|
+
declare class SqlServerIntrospector implements Introspector {
|
|
949
|
+
private readonly _options;
|
|
950
|
+
readonly dialect: "mssql";
|
|
951
|
+
constructor(_options: {
|
|
952
|
+
readonly query: SqlQueryable;
|
|
953
|
+
readonly schema?: string;
|
|
954
|
+
});
|
|
955
|
+
introspect(): Promise<Schema>;
|
|
956
|
+
}
|
|
957
|
+
//#endregion
|
|
958
|
+
//#region src/executor/sqlserver.d.ts
|
|
959
|
+
interface SqlServerRequestLike {
|
|
960
|
+
input(name: string, value: unknown): SqlServerRequestLike;
|
|
961
|
+
batch(sql: string): Promise<{
|
|
962
|
+
readonly recordset?: ReadonlyArray<Record<string, unknown>>;
|
|
963
|
+
}>;
|
|
964
|
+
query(sql: string): Promise<{
|
|
965
|
+
readonly recordset?: ReadonlyArray<Record<string, unknown>>;
|
|
966
|
+
}>;
|
|
967
|
+
}
|
|
968
|
+
/** Request factory bound to one physical session, never to the unpinned pool. */
|
|
969
|
+
interface SqlServerSession {
|
|
970
|
+
request(): SqlServerRequestLike;
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* Transactional DDL and session-owned application locks use the same pinned connection.
|
|
974
|
+
* @see https://learn.microsoft.com/en-us/sql/relational-databases/system-stored-procedures/sp-getapplock-transact-sql
|
|
975
|
+
*/
|
|
976
|
+
declare class SqlServerSqlExecutor implements SqlExecutor {
|
|
977
|
+
private readonly _session;
|
|
978
|
+
private readonly _schema;
|
|
979
|
+
private readonly _lockTimeoutMs;
|
|
980
|
+
private _lockHeld;
|
|
981
|
+
constructor(_session: SqlServerSession, _schema?: string, _lockTimeoutMs?: number);
|
|
982
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
983
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
984
|
+
}>;
|
|
985
|
+
executeStatements(statements: ReadonlyArray<string>, complete?: MigrationCompletion): Promise<void>;
|
|
986
|
+
acquireMigrationLock(_key: string): Promise<() => Promise<void>>;
|
|
987
|
+
}
|
|
988
|
+
//#endregion
|
|
989
|
+
//#region src/ddl/oracle.d.ts
|
|
990
|
+
declare class OracleDdlGenerator extends ServerDdlGenerator {
|
|
991
|
+
constructor(schema: string);
|
|
992
|
+
}
|
|
993
|
+
//#endregion
|
|
994
|
+
//#region src/introspector/oracle.d.ts
|
|
995
|
+
/** @see https://docs.oracle.com/en/database/oracle/oracle-database/23/refrn/ALL_TAB_COLUMNS.html */
|
|
996
|
+
declare class OracleIntrospector implements Introspector {
|
|
997
|
+
private readonly options;
|
|
998
|
+
readonly dialect: "oracle";
|
|
999
|
+
constructor(options: {
|
|
1000
|
+
query: SqlQueryable;
|
|
1001
|
+
schema: string;
|
|
1002
|
+
});
|
|
1003
|
+
introspect(): Promise<Schema>;
|
|
1004
|
+
}
|
|
1005
|
+
//#endregion
|
|
1006
|
+
//#region src/executor/oracle.d.ts
|
|
1007
|
+
interface OracleSession {
|
|
1008
|
+
execute(sql: string, binds: Record<string, unknown>, options: Record<string, unknown>): Promise<{
|
|
1009
|
+
rows?: ReadonlyArray<Record<string, unknown>>;
|
|
1010
|
+
outBinds?: unknown;
|
|
1011
|
+
}>;
|
|
1012
|
+
}
|
|
1013
|
+
/** @see https://docs.oracle.com/en/database/oracle/oracle-database/23/arpls/DBMS_LOCK.html */
|
|
1014
|
+
declare class OracleSqlExecutor implements SqlExecutor {
|
|
1015
|
+
private readonly session;
|
|
1016
|
+
private readonly schema;
|
|
1017
|
+
private readonly lockTimeout;
|
|
1018
|
+
private locked;
|
|
1019
|
+
constructor(session: OracleSession, schema: string, lockTimeout?: number);
|
|
1020
|
+
query(sql: string, params?: ReadonlyArray<unknown>): Promise<{
|
|
1021
|
+
readonly rows: ReadonlyArray<Record<string, unknown>>;
|
|
1022
|
+
}>;
|
|
1023
|
+
executeStatements(statements: ReadonlyArray<string>, complete?: MigrationCompletion): Promise<void>;
|
|
1024
|
+
acquireMigrationLock(_key: string): Promise<() => Promise<void>>;
|
|
1025
|
+
}
|
|
1026
|
+
//#endregion
|
|
1027
|
+
//#region src/server/history.d.ts
|
|
1028
|
+
/** Named columns are deliberately quoted consistently on Oracle (case-sensitive catalog names). */
|
|
1029
|
+
declare class ServerMigrationHistoryStore implements MigrationHistoryStore {
|
|
1030
|
+
private readonly executor;
|
|
1031
|
+
private readonly sql;
|
|
1032
|
+
readonly tableName: string;
|
|
1033
|
+
constructor(executor: SqlExecutor, sql: ServerSql, tableName?: string);
|
|
1034
|
+
private get table();
|
|
1035
|
+
private get now();
|
|
1036
|
+
private column;
|
|
1037
|
+
ensureTable(): Promise<void>;
|
|
1038
|
+
listApplied(): Promise<ReadonlyArray<AppliedMigration>>;
|
|
1039
|
+
recordApplied(file: MigrationFile, connection?: SqlQueryable): Promise<void>;
|
|
1040
|
+
markFailed(id: string, error: string, logs?: string): Promise<void>;
|
|
1041
|
+
private write;
|
|
1042
|
+
markRolledBack(id: string): Promise<boolean>;
|
|
1043
|
+
}
|
|
1044
|
+
//#endregion
|
|
1045
|
+
export { type AlterTable, type AppliedMigration, CONFIG_FILENAMES, type Change, type CheckConstraint, type Column, type ColumnChange, type Constraint, type ConstraintChange, type CreateTable, DEFAULT_HISTORY_TABLE, DEFAULT_LOCK_PATH, DEFAULT_MIGRATIONS_DIR, DIALECTS, DIALECT_NAMES, type DatabaseConfig, DatabaseMigrationHistoryStore, type DdlContext, type DdlGenerator, type DdlGeneratorOptions, type DeployResult, type DestructiveChange, type DevResult, type Dialect, type DialectInfo, type DialectName, type Diff, type Differ, type DropTable, FileMigrationStore, type ForeignKeyConstraint, IMPLEMENTED_DIALECT_NAMES, type Index, type IndexChange, type Introspector, type LoadedConfig, type MigrateConfig, MigrationAbortedError, type MigrationFile, type MigrationFileStore, type MigrationHistoryStore, type MigrationStatus, Migrator, type MigratorOptions, type MysqlConnectionLike, MysqlDdlGenerator, MysqlIntrospector, type MysqlPoolLike, MysqlSqlExecutor, type OnDelete, OracleDdlGenerator, OracleIntrospector, type OracleSession, OracleSqlExecutor, type ParsedArgs, type PgClientLike, type PgPoolLike, PostgresDdlGenerator, PostgresIntrospector, type PostgresIntrospectorOptions, PostgresSqlExecutor, type PrimaryKeyConstraint, type ProcessLock, type PushResult, type ResolveAction, type ResolveOptions, type RunOptions, type Runtime, type RuntimeOptions, SNAPSHOT_FORMAT_VERSION, type Schema, SchemaDiffer, type SchemaDrift, type SchemaDriver, type SchemaSnapshot, ServerMigrationHistoryStore, ServerSql, type SqlExecutor, type SqlQueryable, SqlServerDdlGenerator, SqlServerIntrospector, type SqlServerRequestLike, type SqlServerSession, SqlServerSqlExecutor, SqliteDdlGenerator, type Table, type UniqueConstraint, abnormalDrift, acquireProcessLock, checksumOf, createRuntime, defineConfig, describeDiff, dialectInfo, emptySchema, fromSnapshot, generateMigrationId, isSchema, loadConfig, parseArgs, run, tableDefsToSchema, toSnapshot, toSqlFile };
|
|
1046
|
+
//# sourceMappingURL=index.d.mts.map
|