@stonyx/orm 0.2.1-beta.83 → 0.2.1-beta.84
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/aggregates.d.ts +21 -0
- package/dist/aggregates.js +90 -0
- package/dist/attr.d.ts +2 -0
- package/dist/attr.js +22 -0
- package/dist/belongs-to.d.ts +11 -0
- package/dist/belongs-to.js +58 -0
- package/dist/cli.d.ts +22 -0
- package/dist/cli.js +148 -0
- package/dist/commands.d.ts +7 -0
- package/dist/commands.js +146 -0
- package/dist/db.d.ts +21 -0
- package/dist/db.js +174 -0
- package/dist/exports/db.d.ts +7 -0
- package/{src → dist}/exports/db.js +2 -4
- package/dist/has-many.d.ts +11 -0
- package/dist/has-many.js +57 -0
- package/dist/hooks.d.ts +47 -0
- package/dist/hooks.js +106 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.js +34 -0
- package/dist/main.d.ts +46 -0
- package/dist/main.js +178 -0
- package/dist/manage-record.d.ts +13 -0
- package/dist/manage-record.js +113 -0
- package/dist/meta-request.d.ts +6 -0
- package/dist/meta-request.js +52 -0
- package/dist/migrate.d.ts +2 -0
- package/dist/migrate.js +57 -0
- package/dist/model-property.d.ts +9 -0
- package/dist/model-property.js +29 -0
- package/dist/model.d.ts +15 -0
- package/dist/model.js +18 -0
- package/dist/mysql/connection.d.ts +14 -0
- package/dist/mysql/connection.js +24 -0
- package/dist/mysql/migration-generator.d.ts +45 -0
- package/dist/mysql/migration-generator.js +245 -0
- package/dist/mysql/migration-runner.d.ts +12 -0
- package/dist/mysql/migration-runner.js +83 -0
- package/dist/mysql/mysql-db.d.ts +100 -0
- package/dist/mysql/mysql-db.js +411 -0
- package/dist/mysql/query-builder.d.ts +10 -0
- package/dist/mysql/query-builder.js +44 -0
- package/dist/mysql/schema-introspector.d.ts +19 -0
- package/dist/mysql/schema-introspector.js +286 -0
- package/dist/mysql/type-map.d.ts +21 -0
- package/dist/mysql/type-map.js +36 -0
- package/dist/orm-request.d.ts +38 -0
- package/dist/orm-request.js +453 -0
- package/dist/plural-registry.d.ts +4 -0
- package/{src → dist}/plural-registry.js +3 -6
- package/dist/postgres/connection.d.ts +15 -0
- package/dist/postgres/connection.js +30 -0
- package/dist/postgres/migration-generator.d.ts +45 -0
- package/dist/postgres/migration-generator.js +257 -0
- package/dist/postgres/migration-runner.d.ts +10 -0
- package/dist/postgres/migration-runner.js +82 -0
- package/dist/postgres/postgres-db.d.ts +119 -0
- package/dist/postgres/postgres-db.js +473 -0
- package/dist/postgres/query-builder.d.ts +27 -0
- package/dist/postgres/query-builder.js +98 -0
- package/dist/postgres/schema-introspector.d.ts +29 -0
- package/dist/postgres/schema-introspector.js +309 -0
- package/dist/postgres/type-map.d.ts +23 -0
- package/dist/postgres/type-map.js +53 -0
- package/dist/record.d.ts +75 -0
- package/dist/record.js +115 -0
- package/dist/relationships.d.ts +10 -0
- package/dist/relationships.js +35 -0
- package/dist/serializer.d.ts +17 -0
- package/dist/serializer.js +130 -0
- package/dist/setup-rest-server.d.ts +1 -0
- package/dist/setup-rest-server.js +54 -0
- package/dist/standalone-db.d.ts +58 -0
- package/dist/standalone-db.js +142 -0
- package/dist/store.d.ts +62 -0
- package/dist/store.js +271 -0
- package/dist/timescale/query-builder.d.ts +41 -0
- package/dist/timescale/query-builder.js +87 -0
- package/dist/timescale/timescale-db.d.ts +44 -0
- package/dist/timescale/timescale-db.js +81 -0
- package/dist/transforms.d.ts +2 -0
- package/dist/transforms.js +17 -0
- package/dist/types/orm-types.d.ts +142 -0
- package/dist/types/orm-types.js +1 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +13 -0
- package/dist/view-resolver.d.ts +8 -0
- package/dist/view-resolver.js +165 -0
- package/dist/view.d.ts +11 -0
- package/dist/view.js +18 -0
- package/package.json +34 -11
- package/src/{aggregates.js → aggregates.ts} +27 -13
- package/src/{attr.js → attr.ts} +2 -2
- package/src/{belongs-to.js → belongs-to.ts} +36 -17
- package/src/{cli.js → cli.ts} +17 -11
- package/src/{commands.js → commands.ts} +179 -170
- package/src/{db.js → db.ts} +35 -26
- package/src/exports/db.ts +7 -0
- package/src/has-many.ts +91 -0
- package/src/{hooks.js → hooks.ts} +23 -27
- package/src/{index.js → index.ts} +4 -4
- package/src/{main.js → main.ts} +59 -34
- package/src/{manage-record.js → manage-record.ts} +41 -22
- package/src/{meta-request.js → meta-request.ts} +17 -14
- package/src/{migrate.js → migrate.ts} +9 -9
- package/src/{model-property.js → model-property.ts} +12 -6
- package/src/{model.js → model.ts} +5 -4
- package/src/mysql/{connection.js → connection.ts} +43 -28
- package/src/mysql/{migration-generator.js → migration-generator.ts} +332 -286
- package/src/mysql/{migration-runner.js → migration-runner.ts} +116 -110
- package/src/mysql/{mysql-db.js → mysql-db.ts} +533 -473
- package/src/mysql/{query-builder.js → query-builder.ts} +69 -64
- package/src/mysql/{schema-introspector.js → schema-introspector.ts} +355 -325
- package/src/mysql/{type-map.js → type-map.ts} +42 -37
- package/src/{orm-request.js → orm-request.ts} +165 -95
- package/src/plural-registry.ts +12 -0
- package/src/postgres/{connection.js → connection.ts} +14 -5
- package/src/postgres/{migration-generator.js → migration-generator.ts} +82 -38
- package/src/postgres/{migration-runner.js → migration-runner.ts} +11 -10
- package/src/postgres/{postgres-db.js → postgres-db.ts} +195 -114
- package/src/postgres/{query-builder.js → query-builder.ts} +27 -28
- package/src/postgres/{schema-introspector.js → schema-introspector.ts} +87 -58
- package/src/postgres/{type-map.js → type-map.ts} +10 -6
- package/src/{record.js → record.ts} +73 -34
- package/src/relationships.ts +48 -0
- package/src/{serializer.js → serializer.ts} +44 -36
- package/src/{setup-rest-server.js → setup-rest-server.ts} +18 -13
- package/src/{standalone-db.js → standalone-db.ts} +33 -24
- package/src/{store.js → store.ts} +90 -68
- package/src/timescale/{query-builder.js → query-builder.ts} +33 -38
- package/src/timescale/timescale-db.ts +107 -0
- package/src/transforms.ts +20 -0
- package/src/types/mysql2.d.ts +30 -0
- package/src/types/orm-types.ts +146 -0
- package/src/types/pg.d.ts +28 -0
- package/src/types/stonyx-cron.d.ts +5 -0
- package/src/types/stonyx-events.d.ts +4 -0
- package/src/types/stonyx-rest-server.d.ts +11 -0
- package/src/types/stonyx-utils.d.ts +33 -0
- package/src/types/stonyx.d.ts +21 -0
- package/src/utils.ts +16 -0
- package/src/{view-resolver.js → view-resolver.ts} +53 -28
- package/src/view.ts +22 -0
- package/src/has-many.js +0 -68
- package/src/relationships.js +0 -43
- package/src/timescale/timescale-db.js +0 -111
- package/src/transforms.js +0 -20
- package/src/utils.js +0 -12
- package/src/view.js +0 -21
|
@@ -1,286 +1,332 @@
|
|
|
1
|
-
import { introspectModels, introspectViews, buildTableDDL, buildViewDDL, schemasToSnapshot, viewSchemasToSnapshot, getTopologicalOrder } from './schema-introspector.js';
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
const
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
for (const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
for (const name of Object.
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
1
|
+
import { introspectModels, introspectViews, buildTableDDL, buildViewDDL, schemasToSnapshot, viewSchemasToSnapshot, getTopologicalOrder } from './schema-introspector.js';
|
|
2
|
+
import type { ModelSchema, SnapshotEntry, ViewSnapshotEntry, ForeignKeyDef } from './schema-introspector.js';
|
|
3
|
+
import { readFile, createFile, createDirectory, fileExists } from '@stonyx/utils/file';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
import config from 'stonyx/config';
|
|
6
|
+
import log from 'stonyx/log';
|
|
7
|
+
|
|
8
|
+
interface ColumnChange {
|
|
9
|
+
model: string;
|
|
10
|
+
column: string;
|
|
11
|
+
type: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ColumnTypeChange {
|
|
15
|
+
model: string;
|
|
16
|
+
column: string;
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface ForeignKeyChange {
|
|
22
|
+
model: string;
|
|
23
|
+
column: string;
|
|
24
|
+
references: ForeignKeyDef;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface SnapshotDiff {
|
|
28
|
+
hasChanges: boolean;
|
|
29
|
+
addedModels: string[];
|
|
30
|
+
removedModels: string[];
|
|
31
|
+
addedColumns: ColumnChange[];
|
|
32
|
+
removedColumns: ColumnChange[];
|
|
33
|
+
changedColumns: ColumnTypeChange[];
|
|
34
|
+
addedForeignKeys: ForeignKeyChange[];
|
|
35
|
+
removedForeignKeys: ForeignKeyChange[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
interface ViewDiff {
|
|
39
|
+
hasChanges: boolean;
|
|
40
|
+
addedViews: string[];
|
|
41
|
+
removedViews: string[];
|
|
42
|
+
changedViews: string[];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface GeneratedMigration {
|
|
46
|
+
filename: string;
|
|
47
|
+
content: string;
|
|
48
|
+
snapshot: Record<string, SnapshotEntry | ViewSnapshotEntry>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type Snapshot = Record<string, SnapshotEntry & { isView?: boolean; viewName?: string; viewQuery?: string; source?: string }>;
|
|
52
|
+
|
|
53
|
+
export async function generateMigration(description: string = 'migration'): Promise<GeneratedMigration | null> {
|
|
54
|
+
const { migrationsDir } = config.orm.mysql!;
|
|
55
|
+
const rootPath = config.rootPath;
|
|
56
|
+
const migrationsPath = path.resolve(rootPath, migrationsDir!);
|
|
57
|
+
|
|
58
|
+
await createDirectory(migrationsPath);
|
|
59
|
+
|
|
60
|
+
const schemas = introspectModels();
|
|
61
|
+
const currentSnapshot = schemasToSnapshot(schemas);
|
|
62
|
+
const previousSnapshot = await loadLatestSnapshot(migrationsPath) as Snapshot;
|
|
63
|
+
const diff = diffSnapshots(previousSnapshot, currentSnapshot);
|
|
64
|
+
|
|
65
|
+
// Don't return early — check view changes too before deciding
|
|
66
|
+
if (!diff.hasChanges) {
|
|
67
|
+
// Check if there are view changes before returning null
|
|
68
|
+
const viewSchemasPrelim = introspectViews();
|
|
69
|
+
const currentViewSnapshotPrelim = viewSchemasToSnapshot(viewSchemasPrelim);
|
|
70
|
+
const previousViewSnapshotPrelim = extractViewsFromSnapshot(previousSnapshot);
|
|
71
|
+
const viewDiffPrelim = diffViewSnapshots(previousViewSnapshotPrelim, currentViewSnapshotPrelim);
|
|
72
|
+
|
|
73
|
+
if (!viewDiffPrelim.hasChanges) {
|
|
74
|
+
log.db!('No schema changes detected.');
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const upStatements: string[] = [];
|
|
80
|
+
const downStatements: string[] = [];
|
|
81
|
+
|
|
82
|
+
// New tables — in topological order (parents before children)
|
|
83
|
+
const allOrder = getTopologicalOrder(schemas);
|
|
84
|
+
const addedOrdered = allOrder.filter(name => diff.addedModels.includes(name));
|
|
85
|
+
|
|
86
|
+
for (const name of addedOrdered) {
|
|
87
|
+
upStatements.push(buildTableDDL(name, schemas[name], schemas) + ';');
|
|
88
|
+
downStatements.unshift(`DROP TABLE IF EXISTS \`${schemas[name].table}\`;`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Removed tables (warn only, commented out)
|
|
92
|
+
for (const name of diff.removedModels) {
|
|
93
|
+
upStatements.push(`-- WARNING: Model '${name}' was removed. Uncomment to drop table:`);
|
|
94
|
+
upStatements.push(`-- DROP TABLE IF EXISTS \`${previousSnapshot[name].table}\`;`);
|
|
95
|
+
downStatements.push(`-- Recreate table for removed model '${name}' manually if needed`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Added columns
|
|
99
|
+
for (const { model, column, type } of diff.addedColumns) {
|
|
100
|
+
const table = currentSnapshot[model].table;
|
|
101
|
+
upStatements.push(`ALTER TABLE \`${table}\` ADD COLUMN \`${column}\` ${type};`);
|
|
102
|
+
downStatements.push(`ALTER TABLE \`${table}\` DROP COLUMN \`${column}\`;`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Removed columns
|
|
106
|
+
for (const { model, column, type } of diff.removedColumns) {
|
|
107
|
+
const table = previousSnapshot[model].table!;
|
|
108
|
+
upStatements.push(`ALTER TABLE \`${table}\` DROP COLUMN \`${column}\`;`);
|
|
109
|
+
downStatements.push(`ALTER TABLE \`${table}\` ADD COLUMN \`${column}\` ${type};`);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// Changed column types
|
|
113
|
+
for (const { model, column, from, to } of diff.changedColumns) {
|
|
114
|
+
const table = currentSnapshot[model].table;
|
|
115
|
+
upStatements.push(`ALTER TABLE \`${table}\` MODIFY COLUMN \`${column}\` ${to};`);
|
|
116
|
+
downStatements.push(`ALTER TABLE \`${table}\` MODIFY COLUMN \`${column}\` ${from};`);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Added foreign keys
|
|
120
|
+
for (const { model, column, references } of diff.addedForeignKeys) {
|
|
121
|
+
const table = currentSnapshot[model].table;
|
|
122
|
+
// Resolve FK column type from the referenced table's PK type
|
|
123
|
+
const refModel = Object.entries(currentSnapshot).find(([, s]) => s.table === references.references);
|
|
124
|
+
const fkType = refModel && refModel[1].idType === 'string' ? 'VARCHAR(255)' : 'INT';
|
|
125
|
+
upStatements.push(`ALTER TABLE \`${table}\` ADD COLUMN \`${column}\` ${fkType};`);
|
|
126
|
+
upStatements.push(`ALTER TABLE \`${table}\` ADD FOREIGN KEY (\`${column}\`) REFERENCES \`${references.references}\`(\`${references.column}\`) ON DELETE SET NULL;`);
|
|
127
|
+
downStatements.push(`ALTER TABLE \`${table}\` DROP FOREIGN KEY \`${column}\`;`);
|
|
128
|
+
downStatements.push(`ALTER TABLE \`${table}\` DROP COLUMN \`${column}\`;`);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Removed foreign keys
|
|
132
|
+
for (const { model, column, references } of diff.removedForeignKeys) {
|
|
133
|
+
const table = previousSnapshot[model].table!;
|
|
134
|
+
// Resolve FK column type from the referenced table's PK type in previous snapshot
|
|
135
|
+
const refModel = Object.entries(previousSnapshot).find(([, s]) => s.table === references.references);
|
|
136
|
+
const fkType = refModel && refModel[1].idType === 'string' ? 'VARCHAR(255)' : 'INT';
|
|
137
|
+
upStatements.push(`ALTER TABLE \`${table}\` DROP FOREIGN KEY \`${column}\`;`);
|
|
138
|
+
upStatements.push(`ALTER TABLE \`${table}\` DROP COLUMN \`${column}\`;`);
|
|
139
|
+
downStatements.push(`ALTER TABLE \`${table}\` ADD COLUMN \`${column}\` ${fkType};`);
|
|
140
|
+
downStatements.push(`ALTER TABLE \`${table}\` ADD FOREIGN KEY (\`${column}\`) REFERENCES \`${references.references}\`(\`${references.column}\`) ON DELETE SET NULL;`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// View migrations — views are created AFTER tables (dependency order)
|
|
144
|
+
const viewSchemas = introspectViews();
|
|
145
|
+
const currentViewSnapshot = viewSchemasToSnapshot(viewSchemas);
|
|
146
|
+
const previousViewSnapshot = extractViewsFromSnapshot(previousSnapshot);
|
|
147
|
+
const viewDiff = diffViewSnapshots(previousViewSnapshot, currentViewSnapshot);
|
|
148
|
+
|
|
149
|
+
if (viewDiff.hasChanges) {
|
|
150
|
+
upStatements.push('');
|
|
151
|
+
upStatements.push('-- Views');
|
|
152
|
+
downStatements.push('');
|
|
153
|
+
downStatements.push('-- Views');
|
|
154
|
+
|
|
155
|
+
// Added views
|
|
156
|
+
for (const name of viewDiff.addedViews) {
|
|
157
|
+
try {
|
|
158
|
+
const ddl = buildViewDDL(name, viewSchemas[name], schemas);
|
|
159
|
+
upStatements.push(ddl + ';');
|
|
160
|
+
downStatements.unshift(`DROP VIEW IF EXISTS \`${viewSchemas[name].viewName}\`;`);
|
|
161
|
+
} catch (error) {
|
|
162
|
+
upStatements.push(`-- WARNING: Could not generate DDL for view '${name}': ${error instanceof Error ? error.message : String(error)}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Removed views
|
|
167
|
+
for (const name of viewDiff.removedViews) {
|
|
168
|
+
upStatements.push(`-- WARNING: View '${name}' was removed. Uncomment to drop view:`);
|
|
169
|
+
upStatements.push(`-- DROP VIEW IF EXISTS \`${previousViewSnapshot[name].viewName}\`;`);
|
|
170
|
+
downStatements.push(`-- Recreate view for removed view '${name}' manually if needed`);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// Changed views (source or aggregates changed)
|
|
174
|
+
for (const name of viewDiff.changedViews) {
|
|
175
|
+
try {
|
|
176
|
+
const ddl = buildViewDDL(name, viewSchemas[name], schemas);
|
|
177
|
+
upStatements.push(ddl + ';');
|
|
178
|
+
} catch (error) {
|
|
179
|
+
upStatements.push(`-- WARNING: Could not generate DDL for changed view '${name}': ${error instanceof Error ? error.message : String(error)}`);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const combinedHasChanges = diff.hasChanges || viewDiff.hasChanges;
|
|
185
|
+
|
|
186
|
+
if (!combinedHasChanges) {
|
|
187
|
+
log.db!('No schema changes detected.');
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Merge view snapshot into the main snapshot
|
|
192
|
+
const combinedSnapshot: Record<string, SnapshotEntry | ViewSnapshotEntry> = { ...currentSnapshot };
|
|
193
|
+
for (const [name, viewSnap] of Object.entries(currentViewSnapshot)) {
|
|
194
|
+
combinedSnapshot[name] = viewSnap;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const sanitizedDescription = description.replace(/\s+/g, '_').replace(/[^a-zA-Z0-9_]/g, '');
|
|
198
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
199
|
+
const filename = `${timestamp}_${sanitizedDescription}.sql`;
|
|
200
|
+
const content = `-- UP\n${upStatements.join('\n')}\n\n-- DOWN\n${downStatements.join('\n')}\n`;
|
|
201
|
+
|
|
202
|
+
await createFile(path.join(migrationsPath, filename), content);
|
|
203
|
+
await createFile(path.join(migrationsPath, '.snapshot.json'), JSON.stringify(combinedSnapshot, null, 2));
|
|
204
|
+
|
|
205
|
+
log.db!(`Migration generated: ${filename}`);
|
|
206
|
+
|
|
207
|
+
return { filename, content, snapshot: combinedSnapshot };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export async function loadLatestSnapshot(migrationsPath: string): Promise<Record<string, unknown>> {
|
|
211
|
+
const snapshotPath = path.join(migrationsPath, '.snapshot.json');
|
|
212
|
+
const exists = await fileExists(snapshotPath);
|
|
213
|
+
|
|
214
|
+
if (!exists) return {};
|
|
215
|
+
|
|
216
|
+
return readFile(snapshotPath, { json: true }) as Promise<Record<string, unknown>>;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function diffSnapshots(previous: Record<string, SnapshotEntry>, current: Record<string, SnapshotEntry>): SnapshotDiff {
|
|
220
|
+
const addedModels: string[] = [];
|
|
221
|
+
const removedModels: string[] = [];
|
|
222
|
+
const addedColumns: ColumnChange[] = [];
|
|
223
|
+
const removedColumns: ColumnChange[] = [];
|
|
224
|
+
const changedColumns: ColumnTypeChange[] = [];
|
|
225
|
+
const addedForeignKeys: ForeignKeyChange[] = [];
|
|
226
|
+
const removedForeignKeys: ForeignKeyChange[] = [];
|
|
227
|
+
|
|
228
|
+
// Find added models
|
|
229
|
+
for (const name of Object.keys(current)) {
|
|
230
|
+
if (!previous[name]) addedModels.push(name);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Find removed models
|
|
234
|
+
for (const name of Object.keys(previous)) {
|
|
235
|
+
if (!current[name]) removedModels.push(name);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// Find column changes in existing models
|
|
239
|
+
for (const name of Object.keys(current)) {
|
|
240
|
+
if (!previous[name]) continue;
|
|
241
|
+
|
|
242
|
+
const { columns: prevCols = {} } = previous[name];
|
|
243
|
+
const { columns: currCols = {} } = current[name];
|
|
244
|
+
|
|
245
|
+
// Added columns
|
|
246
|
+
for (const [col, type] of Object.entries(currCols)) {
|
|
247
|
+
if (!prevCols[col]) {
|
|
248
|
+
addedColumns.push({ model: name, column: col, type });
|
|
249
|
+
} else if (prevCols[col] !== type) {
|
|
250
|
+
changedColumns.push({ model: name, column: col, from: prevCols[col], to: type });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Removed columns
|
|
255
|
+
for (const [col, type] of Object.entries(prevCols)) {
|
|
256
|
+
if (!currCols[col]) {
|
|
257
|
+
removedColumns.push({ model: name, column: col, type });
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Foreign key changes
|
|
262
|
+
const prevFKs = previous[name].foreignKeys || {};
|
|
263
|
+
const currFKs = current[name].foreignKeys || {};
|
|
264
|
+
|
|
265
|
+
for (const [col, refs] of Object.entries(currFKs)) {
|
|
266
|
+
if (!prevFKs[col]) {
|
|
267
|
+
addedForeignKeys.push({ model: name, column: col, references: refs });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
for (const [col, refs] of Object.entries(prevFKs)) {
|
|
272
|
+
if (!currFKs[col]) {
|
|
273
|
+
removedForeignKeys.push({ model: name, column: col, references: refs });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
const hasChanges = addedModels.length > 0 || removedModels.length > 0 ||
|
|
279
|
+
addedColumns.length > 0 || removedColumns.length > 0 ||
|
|
280
|
+
changedColumns.length > 0 || addedForeignKeys.length > 0 || removedForeignKeys.length > 0;
|
|
281
|
+
|
|
282
|
+
return {
|
|
283
|
+
hasChanges,
|
|
284
|
+
addedModels,
|
|
285
|
+
removedModels,
|
|
286
|
+
addedColumns,
|
|
287
|
+
removedColumns,
|
|
288
|
+
changedColumns,
|
|
289
|
+
addedForeignKeys,
|
|
290
|
+
removedForeignKeys,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
export function detectSchemaDrift(schemas: Record<string, ModelSchema>, snapshot: Record<string, SnapshotEntry>): SnapshotDiff {
|
|
295
|
+
const current = schemasToSnapshot(schemas);
|
|
296
|
+
return diffSnapshots(snapshot, current);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
export function extractViewsFromSnapshot(snapshot: Record<string, unknown>): Record<string, ViewSnapshotEntry> {
|
|
300
|
+
const views: Record<string, ViewSnapshotEntry> = {};
|
|
301
|
+
for (const [name, entry] of Object.entries(snapshot)) {
|
|
302
|
+
if ((entry as { isView?: boolean }).isView) views[name] = entry as ViewSnapshotEntry;
|
|
303
|
+
}
|
|
304
|
+
return views;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function diffViewSnapshots(previous: Record<string, ViewSnapshotEntry>, current: Record<string, ViewSnapshotEntry>): ViewDiff {
|
|
308
|
+
const addedViews: string[] = [];
|
|
309
|
+
const removedViews: string[] = [];
|
|
310
|
+
const changedViews: string[] = [];
|
|
311
|
+
|
|
312
|
+
for (const name of Object.keys(current)) {
|
|
313
|
+
if (!previous[name]) {
|
|
314
|
+
addedViews.push(name);
|
|
315
|
+
} else if (
|
|
316
|
+
current[name].viewQuery !== previous[name].viewQuery ||
|
|
317
|
+
current[name].source !== previous[name].source
|
|
318
|
+
) {
|
|
319
|
+
changedViews.push(name);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
for (const name of Object.keys(previous)) {
|
|
324
|
+
if (!current[name]) {
|
|
325
|
+
removedViews.push(name);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const hasChanges = addedViews.length > 0 || removedViews.length > 0 || changedViews.length > 0;
|
|
330
|
+
|
|
331
|
+
return { hasChanges, addedViews, removedViews, changedViews };
|
|
332
|
+
}
|