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/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ts-grm-migrate contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
# ts-grm-migrate
|
|
2
|
+
|
|
3
|
+
[ts-grm](https://github.com/babyfish-ct/ts-grm) 的数据库 schema 迁移工具 —— 像 Prisma Migrate 那样管理数据库版本,
|
|
4
|
+
但**模型就是你写的 ts-grm 代码**,不需要额外的 schema 文件,也没有代码生成步骤。
|
|
5
|
+
|
|
6
|
+
## 功能清单
|
|
7
|
+
|
|
8
|
+
- **增量迁移**:对比你的模型与数据库现状,生成可读的 SQL 迁移文件,并记录应用历史
|
|
9
|
+
- **按序部署**:把所有未应用的迁移依次应用到目标库(部署 / CI 场景)
|
|
10
|
+
- **快速同步**:快速执行迁移SQL,将数据库同步为model定义的形状
|
|
11
|
+
- **迁移后对账**:应用完再核对数据库与模型,不一致就指出「哪个库、哪张表、差在哪」
|
|
12
|
+
- **失败可恢复**:迁移中途失败会记入历史并阻止后续部署,用 `resolve` 修正状态后继续
|
|
13
|
+
|
|
14
|
+
## 状态与安装
|
|
15
|
+
|
|
16
|
+
**Alpha / 尚未正式发布。** 当前仅承诺下表列出的验证范围;不保证任意数据库结构或历史版本兼容。
|
|
17
|
+
运行环境与当前 ts-grm 工具链一致:Node `>=24.11.0`、ESM。
|
|
18
|
+
开发使用 Yarn 4.1.0、TypeScript 7、tsdown、Biome 与 Vitest 4。
|
|
19
|
+
|
|
20
|
+
先在本仓库构建安装包:
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
corepack yarn install --immutable
|
|
24
|
+
corepack yarn check
|
|
25
|
+
corepack yarn pack --out /tmp/ts-grm-migrate.tgz
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
再在使用者项目中安装(以下示例使用 SQLite):
|
|
29
|
+
|
|
30
|
+
```sh
|
|
31
|
+
npm install @ts-grm/core@0.0.13 @ts-grm/sql@0.0.13 better-sqlite3
|
|
32
|
+
npm install -D /tmp/ts-grm-migrate.tgz
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
- core/sql 是 peerDependencies,请使用**相同版本**;当前支持 `>=0.0.9 <0.0.14`。
|
|
36
|
+
这是已验证范围,不自动承诺未来版本;验证方法见 [兼容性](docs/compatibility.md)。
|
|
37
|
+
- 数据库驱动按需安装,完整列表见下方支持表。
|
|
38
|
+
- 安装后使用 `npx tgm`,或通过包管理器运行 `tgm` / `ts-grm-migrate`。
|
|
39
|
+
- npm 发布步骤见 [发布](docs/releasing.md)。
|
|
40
|
+
|
|
41
|
+
## 快速开始
|
|
42
|
+
|
|
43
|
+
> **前置:项目需是 ESM** —— 给 `package.json` 加上 `"type": "module"`。
|
|
44
|
+
> ts-grm 的模型注册表是**模块级单例**,migrate 必须与你的模型共用同一份 **ESM** 实例;
|
|
45
|
+
> 若模型是 CommonJS,它会把模型注册到另一份实例上,migrate 看不到任何模型。
|
|
46
|
+
|
|
47
|
+
在项目根建一个配置文件:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// ts-grm-migrate.config.ts
|
|
51
|
+
import { defineConfig } from "ts-grm-migrate";
|
|
52
|
+
|
|
53
|
+
export default defineConfig({
|
|
54
|
+
dialect: "postgres",
|
|
55
|
+
database: { host: "localhost", database: "app", user: "postgres" },
|
|
56
|
+
models: ["./src/models"], // 交给 ts-grm 加载你的模型(.ts 或编译后的 .js)
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
然后:
|
|
61
|
+
|
|
62
|
+
```sh
|
|
63
|
+
tgm dev -n init # 对比模型与数据库,生成并应用第一个迁移
|
|
64
|
+
tgm dev # 不写名字也行:迁移只用时间戳命名
|
|
65
|
+
tgm status # 看看应用了哪些、还剩哪些
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### 配置项
|
|
69
|
+
|
|
70
|
+
- **`dialect`**:数据库方言,可选 `postgres`、`sqlite`、`mysql`、`mssql`、`oracle`,默认 `postgres`。
|
|
71
|
+
它决定使用哪个数据库驱动,以及结构读取、SQL 生成和迁移执行的具体实现;各方言的驱动依赖与支持范围见下表。
|
|
72
|
+
- **`database`**(必填):数据库连接,字段含义随方言变化,见下文示例
|
|
73
|
+
- **`models`**(必填):模型文件或目录,相对项目根,必须写成 `./xxx` 或 `../xxx`。
|
|
74
|
+
**必须是 ESM** —— 项目声明 `"type": "module"`,或指向编译后的 ESM 产物(`.js`)
|
|
75
|
+
- `migrationsDir`:迁移文件目录,默认 `./src/ts-grm`
|
|
76
|
+
- `schema`:目标 schema,PostgreSQL 默认 `public`,SQL Server 默认 `dbo`,Oracle 默认登录用户
|
|
77
|
+
- `lockPath`:进程锁文件,默认 `./.ts-grm-migrate.lock`
|
|
78
|
+
|
|
79
|
+
配置文件在项目根自动查找(`.ts` / `.mts` / `.mjs` / `.js`),也可以用 `--config <path>` 指定。
|
|
80
|
+
`.ts` 的模块类型跟随项目:CommonJS 项目请用 `.mts`,或给 `package.json` 加 `"type": "module"`。
|
|
81
|
+
|
|
82
|
+
## 数据库支持与配置
|
|
83
|
+
|
|
84
|
+
| `dialect` | 驱动依赖 | 支持范围 |
|
|
85
|
+
| --- | --- | --- |
|
|
86
|
+
| `postgres`(默认) | `pg` | PostgreSQL,schema 默认 `public` |
|
|
87
|
+
| `sqlite` | `better-sqlite3` | 基础迁移;需要重建表的变更尚未实现 |
|
|
88
|
+
| `mysql` | `mysql2` | MySQL 8.0.16+ / InnoDB,`lower_case_table_names=0` |
|
|
89
|
+
| `mssql` | `mssql` | SQL Server 2016+,schema 默认 `dbo`;已验证 SQL Server 2022 |
|
|
90
|
+
| `oracle` | `oracledb` | Oracle 19c+,schema 默认登录用户;已验证 Oracle Free 23 |
|
|
91
|
+
|
|
92
|
+
SQL Server 示例:
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
export default defineConfig({
|
|
96
|
+
dialect: "mssql",
|
|
97
|
+
database: {
|
|
98
|
+
host: "localhost", port: 1433, database: "app", user: "app", password: process.env.DB_PASSWORD,
|
|
99
|
+
// 本地自签名测试证书可启用;生产环境默认校验证书。
|
|
100
|
+
trustServerCertificate: true,
|
|
101
|
+
},
|
|
102
|
+
schema: "dbo",
|
|
103
|
+
models: ["./src/models"],
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Oracle 示例(Thin mode,无需安装 Oracle Client):
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
export default defineConfig({
|
|
111
|
+
dialect: "oracle",
|
|
112
|
+
database: {
|
|
113
|
+
user: "APP", password: process.env.DB_PASSWORD,
|
|
114
|
+
connectionString: "localhost:1521/FREEPDB1",
|
|
115
|
+
},
|
|
116
|
+
models: ["./src/models"],
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Oracle `database.database` 表示 service name,也可用 `host` / `port` / `database` 代替
|
|
121
|
+
`connectionString`。Oracle schema 必须已存在;SQL Server 会创建尚不存在的目标 schema。
|
|
122
|
+
SQL Server 和 Oracle 的自动生成 DDL 与历史表均限定目标 schema,避免读写落在不同命名空间。
|
|
123
|
+
|
|
124
|
+
Oracle 用户需要 `CREATE SESSION`、`CREATE TABLE`、表空间配额;创建 identity 列还需要
|
|
125
|
+
`CREATE SEQUENCE`。数据库锁使用 `DBMS_LOCK`,需由 DBA 授权:
|
|
126
|
+
|
|
127
|
+
```sql
|
|
128
|
+
GRANT EXECUTE ON SYS.DBMS_LOCK TO APP;
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
MySQL / Oracle 的 DDL 会隐式提交,失败可能留下部分改动。`resolve --rolled-back` 仅修改历史状态,
|
|
132
|
+
**不会撤销 SQL**;应先手工恢复数据库再重试,或补完 SQL 后 `resolve --applied`。
|
|
133
|
+
PostgreSQL、SQLite、SQL Server 在同一事务内执行迁移 SQL 并记录成功,记账失败也会回滚。
|
|
134
|
+
所有方言都会在执行前持久化未完成记录;进程中断、连接丢失或记账失败后会阻止自动重放,
|
|
135
|
+
需检查数据库实际状态,再使用 `resolve`。未完成记录在 `status` 中显示为 failed。
|
|
136
|
+
`status` 仅将确认不存在的历史表视为空历史,连接和权限错误会向上传递;Oracle 跨 schema
|
|
137
|
+
的 ORA-00942 无法区分缺表与无权限,因此保留错误。
|
|
138
|
+
|
|
139
|
+
新迁移 ID 使用毫秒时间戳,并在本地锁内保证晚于已有生成时间戳;文件使用独占创建,
|
|
140
|
+
遇到已有同名文件会报错,不覆盖历史 SQL。PostgreSQL 数据库锁按数据库和当前 schema
|
|
141
|
+
限定固定资源名,不依赖 checkout 的本地路径。
|
|
142
|
+
|
|
143
|
+
当前边界:
|
|
144
|
+
|
|
145
|
+
- SQL Server / Oracle 支持普通表、列、主键、唯一约束、外键、CHECK 和普通索引;
|
|
146
|
+
修改 identity 策略、自动重命名及复杂表重建不支持。
|
|
147
|
+
- SQL Server 的计算/隐藏/稀疏列、temporal/memory-optimized 表、自定义聚集约束布局、
|
|
148
|
+
降序/INCLUDE/特殊索引会明确拒绝。
|
|
149
|
+
- Oracle 的虚拟/隐藏列、IOT/嵌套/临时表、特殊/表达式/降序索引、延迟约束会明确拒绝。
|
|
150
|
+
- 两者目前不支持跨 schema 外键及未启用/未验证的约束。CHECK 只做保守的格式归一化,
|
|
151
|
+
不承诺任意两种等价表达式都能匹配。
|
|
152
|
+
- Oracle 迁移文件支持以分号分隔的 SQL,正确保留字符串、标识符、注释中的分号;
|
|
153
|
+
PL/SQL、SQL*Plus 指令暂不支持,会在执行文件前拒绝。SQL Server 文件使用 SQL 批次,
|
|
154
|
+
不支持客户端的 `GO` 分隔符。
|
|
155
|
+
- MySQL 暂不支持 MariaDB、生成列、隐藏列及特殊索引。
|
|
156
|
+
|
|
157
|
+
### 容器集成测试
|
|
158
|
+
|
|
159
|
+
```sh
|
|
160
|
+
corepack yarn install
|
|
161
|
+
corepack yarn test:servers
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
脚本默认用 Podman 启动 SQL Server Developer 与 Oracle Free(`CONTAINER_RUNTIME=docker` 可切换 Docker),随机分配仅监听 `127.0.0.1` 的端口,
|
|
165
|
+
在独立 schema/用户中验证迁移、数据保留、锁、失败恢复与 CLI,完成后清理本次容器和数据卷。
|
|
166
|
+
MySQL 集成测试始终创建随机专属数据库并清理,不使用 `MYSQL_DATABASE`;测试账号需要建库权限。
|
|
167
|
+
可通过 `MSSQL_TEST_IMAGE` / `ORACLE_TEST_IMAGE` 指定镜像。首次拉取镜像需要网络及足够磁盘空间。
|
|
168
|
+
SQL Server Developer 的测试用途受其许可条款约束,脚本用 `ACCEPT_EULA=Y` 启动。
|
|
169
|
+
|
|
170
|
+
也可对专用测试实例设置 `MSSQL_HOST/PORT/USER/PASSWORD/DATABASE` 或
|
|
171
|
+
`ORACLE_HOST/PORT/PASSWORD/DATABASE`,运行 `tests/server-integration.test.ts`。
|
|
172
|
+
Oracle 测试使用 SYSTEM 创建临时用户,需要该账户拥有 `DBMS_LOCK` 的转授权权限;
|
|
173
|
+
这些权限仅供测试环境使用。未设置对应 `*_HOST` 时,集成测试会跳过。
|
|
174
|
+
|
|
175
|
+
## 命令
|
|
176
|
+
|
|
177
|
+
| 命令 | 用途 |
|
|
178
|
+
| --- | --- |
|
|
179
|
+
| `tgm dev [-n <名字>]` | 对比模型与数据库,生成并应用一个迁移(开发用;名字可省略,省略时只用时间戳命名) |
|
|
180
|
+
| `tgm deploy` | 应用所有未应用的迁移(部署 / CI 用,无交互) |
|
|
181
|
+
| `tgm push [--force]` | 直接同步成模型的样子,不写文件、不记历史 |
|
|
182
|
+
| `tgm status` | 查看已应用 / 待应用的迁移 |
|
|
183
|
+
| `tgm resolve --applied <id>` | 把迁移标记为已应用(SQL 已手工执行过) |
|
|
184
|
+
| `tgm resolve --rolled-back <id>` | 清除失败记录,让它重新待应用 |
|
|
185
|
+
|
|
186
|
+
选项:`--config <path>` 指定配置文件、`-n` / `--name <名字>` 给迁移命名、`--force` 破坏性变更不询问、`-h` 显示帮助。
|
|
187
|
+
|
|
188
|
+
## 行为约定
|
|
189
|
+
|
|
190
|
+
- **迁移文件是人可读的 SQL**:`<migrationsDir>/<时间戳>_<名字>.sql`,可以手工编辑。
|
|
191
|
+
但**已应用的迁移不能再改** —— 内容一旦变动,后续 `deploy` 会因 checksum 不匹配而拒绝继续
|
|
192
|
+
- **破坏性变更会先问**:删表 / 删列 / 改列类型默认交互确认;非交互环境(CI)需显式加 `--force`
|
|
193
|
+
- **迁移后自动对账**:`dev` / `deploy` / `push` 完成后会再读一次数据库与模型比对,
|
|
194
|
+
仍有差异就报出具体位置,例如:
|
|
195
|
+
|
|
196
|
+
```
|
|
197
|
+
⚠ 对账发现数据库与模型不一致(库 app,schema public):
|
|
198
|
+
- 表 AUTHOR:多出列 LEGACY
|
|
199
|
+
这通常意味着迁移未完整生效,或数据库被手工改动过。
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
- **并发防护**:同一项目上的多个 migrate 实例由进程锁文件挡住;多机部署时再由数据库的
|
|
203
|
+
advisory lock 兜底
|
|
204
|
+
- **`schema` 是全局生效的**:同时作用于结构读取与 DDL 执行(PostgreSQL `search_path`,SQL Server / Oracle 限定表名),
|
|
205
|
+
不会出现「读了 A schema、却往 B schema 写」的情况
|
|
206
|
+
|
|
207
|
+
## 为什么是 peer 依赖
|
|
208
|
+
|
|
209
|
+
migrate 是 ts-grm 的插件,必须与你项目里的 ts-grm **共用同一份实例**:ts-grm 的模型注册表是
|
|
210
|
+
模块级单例,若 migrate 自带一份 `@ts-grm/core`,它看到的是空注册表,拿不到你定义的任何模型。
|
|
211
|
+
|
|
212
|
+
因此 `@ts-grm/core` / `@ts-grm/sql` 声明为 peerDependencies,由你的项目提供。
|
|
213
|
+
|
|
214
|
+
## 开发与协作
|
|
215
|
+
|
|
216
|
+
```sh
|
|
217
|
+
corepack yarn check
|
|
218
|
+
corepack yarn test:compat 0.0.9 0.0.13
|
|
219
|
+
corepack yarn test:package 0.0.9
|
|
220
|
+
corepack yarn test:postgres-mysql
|
|
221
|
+
corepack yarn test:servers
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
GitHub CI 分别运行本地检查、历史依赖/安装包测试、真实数据库测试;本地缺少数据库环境变量时
|
|
225
|
+
相关测试会跳过,因此不能仅凭 `yarn test` 绿色判断全部数据库通过。
|
|
226
|
+
|
|
227
|
+
- [贡献指南](CONTRIBUTING.md)
|
|
228
|
+
- [安全报告](SECURITY.md)
|
|
229
|
+
- [变更记录](CHANGELOG.md)
|
|
230
|
+
- [设计](docs/design.md):代码分层、差分规则与失败恢复
|
|
231
|
+
- [兼容性](docs/compatibility.md):ts-grm 版本范围及复现命令
|
|
232
|
+
- [发布](docs/releasing.md):npm 发布前检查
|
|
233
|
+
|
|
234
|
+
## 许可证
|
|
235
|
+
|
|
236
|
+
原创代码采用 [MIT](LICENSE)。ts-grm 使用 Apache-2.0;本项目改写的上游结构类型保留其许可与署名,
|
|
237
|
+
见[第三方声明](THIRD_PARTY_NOTICES.md)。
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
本项目原创代码使用 MIT,见 [LICENSE](LICENSE)。依赖和适配部分保留其各自许可证。
|
|
4
|
+
|
|
5
|
+
## ts-grm
|
|
6
|
+
|
|
7
|
+
`src/vendor/ts-grm.ts` 中的 `TableDef`、`ColumnDef` 和约束类型声明改写自 ts-grm 的
|
|
8
|
+
`schema_def.ts`:保留迁移器需要的字段,调整类型名称和引用,并添加本项目的适配接口及实现。
|
|
9
|
+
上游文件署名为陈涛 (Chen Tao),上游包标记为 Apache-2.0。改写部分保留其许可与署名;
|
|
10
|
+
本项目其余原创代码使用 MIT。
|
|
11
|
+
|
|
12
|
+
- @see https://github.com/babyfish-ct/ts-grm/blob/fe78eb6c323bf335ff23650a414856eb36bfbce7/packages/sql/src/impl/schema_def.ts
|
|
13
|
+
- @see https://github.com/babyfish-ct/ts-grm/blob/fe78eb6c323bf335ff23650a414856eb36bfbce7/packages/sql/package.json
|
|
14
|
+
- [Apache-2.0 全文](LICENSES/Apache-2.0.txt)
|
|
15
|
+
|
|
16
|
+
发布包将 `@ts-grm/core` 和 `@ts-grm/sql` 保留为外部 peerDependencies,不嵌入上游实现。
|
|
17
|
+
其他数据库驱动也为外部依赖,其许可证由各自包提供。
|
package/dist/bin.d.mts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
package/dist/bin.mjs
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { n as run, v as MigrationAbortedError } from "./cli-C-KYDbI_.mjs";
|
|
3
|
+
//#region src/bin.ts
|
|
4
|
+
async function main() {
|
|
5
|
+
try {
|
|
6
|
+
process.exitCode = await run(process.argv.slice(2), process.cwd());
|
|
7
|
+
} catch (e) {
|
|
8
|
+
if (e instanceof MigrationAbortedError) {
|
|
9
|
+
console.log("已取消。");
|
|
10
|
+
process.exitCode = 0;
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
console.error(`错误:${e.message}`);
|
|
14
|
+
process.exitCode = 1;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
main();
|
|
18
|
+
//#endregion
|
|
19
|
+
export {};
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=bin.mjs.map
|
package/dist/bin.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bin.mjs","names":[],"sources":["../src/bin.ts"],"sourcesContent":["#!/usr/bin/env node\nimport { run } from \"./cli.js\";\nimport { MigrationAbortedError } from \"./migrator.js\";\n\nasync function main(): Promise<void> {\n try {\n process.exitCode = await run(process.argv.slice(2), process.cwd());\n } catch (e) {\n if (e instanceof MigrationAbortedError) {\n console.log(\"已取消。\");\n process.exitCode = 0;\n return;\n }\n console.error(`错误:${(e as Error).message}`);\n process.exitCode = 1;\n }\n}\n\n// Keep the executable separate from library exports; no config/import cycle.\nvoid main();\n"],"mappings":";;;AAIA,eAAe,OAAsB;CACnC,IAAI;EACF,QAAQ,WAAW,MAAM,IAAI,QAAQ,KAAK,MAAM,CAAC,GAAG,QAAQ,IAAI,CAAC;CACnE,SAAS,GAAG;EACV,IAAI,aAAa,uBAAuB;GACtC,QAAQ,IAAI,MAAM;GAClB,QAAQ,WAAW;GACnB;EACF;EACA,QAAQ,MAAM,MAAO,EAAY,SAAS;EAC1C,QAAQ,WAAW;CACrB;AACF;AAGK,KAAK"}
|