@mikro-orm/migrations 7.0.15-dev.9 → 7.0.15
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/JSMigrationGenerator.d.ts +10 -7
- package/JSMigrationGenerator.js +20 -20
- package/Migration.d.ts +29 -23
- package/Migration.js +46 -46
- package/MigrationGenerator.d.ts +35 -23
- package/MigrationGenerator.js +33 -33
- package/MigrationRunner.d.ts +9 -9
- package/MigrationRunner.js +45 -43
- package/MigrationStorage.d.ts +33 -27
- package/MigrationStorage.js +113 -108
- package/Migrator.d.ts +44 -34
- package/Migrator.js +282 -274
- package/README.md +1 -1
- package/TSMigrationGenerator.d.ts +10 -7
- package/TSMigrationGenerator.js +16 -16
- package/package.json +4 -4
package/Migrator.js
CHANGED
|
@@ -1,303 +1,311 @@
|
|
|
1
|
-
import { Utils, t, Type, UnknownType
|
|
1
|
+
import { Utils, t, Type, UnknownType } from '@mikro-orm/core';
|
|
2
2
|
import { AbstractMigrator } from '@mikro-orm/core/migrations';
|
|
3
|
-
import { DatabaseSchema, DatabaseTable
|
|
3
|
+
import { DatabaseSchema, DatabaseTable } from '@mikro-orm/sql';
|
|
4
4
|
import { MigrationRunner } from './MigrationRunner.js';
|
|
5
5
|
import { MigrationStorage } from './MigrationStorage.js';
|
|
6
6
|
import { TSMigrationGenerator } from './TSMigrationGenerator.js';
|
|
7
7
|
import { JSMigrationGenerator } from './JSMigrationGenerator.js';
|
|
8
8
|
/** Manages SQL database migrations: creation, execution, and rollback of schema changes. */
|
|
9
9
|
export class Migrator extends AbstractMigrator {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
#schemaGenerator;
|
|
11
|
+
#snapshotPath;
|
|
12
|
+
constructor(em) {
|
|
13
|
+
super(em);
|
|
14
|
+
this.#schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
|
|
15
|
+
}
|
|
16
|
+
static register(orm) {
|
|
17
|
+
orm.config.registerExtension('@mikro-orm/migrator', () => new Migrator(orm.em));
|
|
18
|
+
}
|
|
19
|
+
createRunner() {
|
|
20
|
+
return new MigrationRunner(this.driver, this.options, this.config);
|
|
21
|
+
}
|
|
22
|
+
createStorage() {
|
|
23
|
+
return new MigrationStorage(this.driver, this.options);
|
|
24
|
+
}
|
|
25
|
+
getDefaultGenerator() {
|
|
26
|
+
if (this.options.emit === 'js' || this.options.emit === 'cjs') {
|
|
27
|
+
return new JSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
15
28
|
}
|
|
16
|
-
|
|
17
|
-
|
|
29
|
+
return new TSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
30
|
+
}
|
|
31
|
+
async getSnapshotPath() {
|
|
32
|
+
if (!this.#snapshotPath) {
|
|
33
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
34
|
+
// for snapshots, we always want to use the path based on `emit` option, regardless of whether we run in TS context
|
|
35
|
+
/* v8 ignore next */
|
|
36
|
+
const snapshotPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
|
|
37
|
+
const absoluteSnapshotPath = fs.absolutePath(snapshotPath, this.config.get('baseDir'));
|
|
38
|
+
const dbName = this.config.get('dbName').replace(/\\/g, '/').split('/').pop().replace(/:/g, '');
|
|
39
|
+
const snapshotName = this.options.snapshotName ?? `.snapshot-${dbName}`;
|
|
40
|
+
this.#snapshotPath = fs.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
|
|
18
41
|
}
|
|
19
|
-
|
|
20
|
-
|
|
42
|
+
return this.#snapshotPath;
|
|
43
|
+
}
|
|
44
|
+
async init() {
|
|
45
|
+
if (this.initialized) {
|
|
46
|
+
return;
|
|
21
47
|
}
|
|
22
|
-
|
|
23
|
-
|
|
48
|
+
await super.init();
|
|
49
|
+
const created = await this.#schemaGenerator.ensureDatabase();
|
|
50
|
+
/* v8 ignore next */
|
|
51
|
+
if (created) {
|
|
52
|
+
this.initServices();
|
|
24
53
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
54
|
+
await this.storage.ensureTable();
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
async create(path, blank = false, initial = false, name) {
|
|
60
|
+
const offline = !initial && (await this.hasSnapshot());
|
|
61
|
+
await (offline ? this.initPaths() : this.init());
|
|
62
|
+
if (initial) {
|
|
63
|
+
return this.createInitial(path, name, blank);
|
|
30
64
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
// for snapshots, we always want to use the path based on `emit` option, regardless of whether we run in TS context
|
|
35
|
-
/* v8 ignore next */
|
|
36
|
-
const snapshotPath = this.options.emit === 'ts' && this.options.pathTs ? this.options.pathTs : this.options.path;
|
|
37
|
-
const absoluteSnapshotPath = fs.absolutePath(snapshotPath, this.config.get('baseDir'));
|
|
38
|
-
const dbName = this.config.get('dbName').replace(/\\/g, '/').split('/').pop().replace(/:/g, '');
|
|
39
|
-
const snapshotName = this.options.snapshotName ?? `.snapshot-${dbName}`;
|
|
40
|
-
this.#snapshotPath = fs.normalizePath(absoluteSnapshotPath, `${snapshotName}.json`);
|
|
41
|
-
}
|
|
42
|
-
return this.#snapshotPath;
|
|
65
|
+
const diff = await this.getSchemaDiff(blank, initial);
|
|
66
|
+
if (diff.up.length === 0) {
|
|
67
|
+
return { fileName: '', code: '', diff };
|
|
43
68
|
}
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
69
|
+
const migration = await this.generator.generate(diff, path, name);
|
|
70
|
+
await this.storeCurrentSchema();
|
|
71
|
+
return {
|
|
72
|
+
fileName: migration[1],
|
|
73
|
+
code: migration[0],
|
|
74
|
+
diff,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async getPending() {
|
|
78
|
+
if (!(await this.hasSnapshot())) {
|
|
79
|
+
return super.getPending();
|
|
55
80
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
const diff = await this.getSchemaDiff(blank, initial);
|
|
66
|
-
if (diff.up.length === 0) {
|
|
67
|
-
return { fileName: '', code: '', diff };
|
|
68
|
-
}
|
|
69
|
-
const migration = await this.generator.generate(diff, path, name);
|
|
70
|
-
await this.storeCurrentSchema();
|
|
71
|
-
return {
|
|
72
|
-
fileName: migration[1],
|
|
73
|
-
code: migration[0],
|
|
74
|
-
diff,
|
|
75
|
-
};
|
|
81
|
+
await this.initPaths();
|
|
82
|
+
const all = await this.discoverMigrations();
|
|
83
|
+
// probe the DB via `ensureTable` — if it fails, the DB is unreachable and
|
|
84
|
+
// we treat every discovered migration as pending; otherwise let errors
|
|
85
|
+
// from `storage.executed()` propagate so real bugs are not swallowed
|
|
86
|
+
try {
|
|
87
|
+
await this.storage.ensureTable();
|
|
88
|
+
} catch {
|
|
89
|
+
return all.map(m => ({ name: m.name, path: m.path }));
|
|
76
90
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// probe the DB via `ensureTable` — if it fails, the DB is unreachable and
|
|
84
|
-
// we treat every discovered migration as pending; otherwise let errors
|
|
85
|
-
// from `storage.executed()` propagate so real bugs are not swallowed
|
|
86
|
-
try {
|
|
87
|
-
await this.storage.ensureTable();
|
|
88
|
-
}
|
|
89
|
-
catch {
|
|
90
|
-
return all.map(m => ({ name: m.name, path: m.path }));
|
|
91
|
-
}
|
|
92
|
-
const executed = new Set(await this.storage.executed());
|
|
93
|
-
return all.filter(m => !executed.has(m.name)).map(m => ({ name: m.name, path: m.path }));
|
|
91
|
+
const executed = new Set(await this.storage.executed());
|
|
92
|
+
return all.filter(m => !executed.has(m.name)).map(m => ({ name: m.name, path: m.path }));
|
|
93
|
+
}
|
|
94
|
+
async hasSnapshot() {
|
|
95
|
+
if (!this.options.snapshot) {
|
|
96
|
+
return false;
|
|
94
97
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
98
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
99
|
+
return fs.pathExists(await this.getSnapshotPath());
|
|
100
|
+
}
|
|
101
|
+
async checkSchema() {
|
|
102
|
+
const snapshot = await this.getSchemaFromSnapshot();
|
|
103
|
+
if (!snapshot) {
|
|
104
|
+
await this.init();
|
|
101
105
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
106
|
+
const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
|
|
107
|
+
wrap: false,
|
|
108
|
+
safe: this.options.safe,
|
|
109
|
+
dropTables: this.options.dropTables,
|
|
110
|
+
fromSchema: snapshot,
|
|
111
|
+
});
|
|
112
|
+
return diff.up.trim().length > 0;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* @inheritDoc
|
|
116
|
+
*/
|
|
117
|
+
async createInitial(path, name, blank = false) {
|
|
118
|
+
await this.init();
|
|
119
|
+
const schemaExists = await this.validateInitialMigration(blank);
|
|
120
|
+
const diff = await this.getSchemaDiff(blank, true);
|
|
121
|
+
const migration = await this.generator.generate(diff, path, name);
|
|
122
|
+
await this.storeCurrentSchema();
|
|
123
|
+
if (schemaExists && !blank) {
|
|
124
|
+
await this.storage.logMigration({ name: migration[1] });
|
|
114
125
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
126
|
+
return {
|
|
127
|
+
fileName: migration[1],
|
|
128
|
+
code: migration[0],
|
|
129
|
+
diff,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async runMigrations(method, options) {
|
|
133
|
+
const result = await super.runMigrations(method, options);
|
|
134
|
+
if (result.length > 0 && this.options.snapshot) {
|
|
135
|
+
const ctx = Utils.isObject(options) ? options.transaction : undefined;
|
|
136
|
+
const schema = await DatabaseSchema.create(
|
|
137
|
+
this.em.getConnection(),
|
|
138
|
+
this.em.getPlatform(),
|
|
139
|
+
this.config,
|
|
140
|
+
undefined,
|
|
141
|
+
undefined,
|
|
142
|
+
undefined,
|
|
143
|
+
undefined,
|
|
144
|
+
undefined,
|
|
145
|
+
ctx,
|
|
146
|
+
);
|
|
147
|
+
try {
|
|
148
|
+
await this.storeCurrentSchema(schema);
|
|
149
|
+
} catch {
|
|
150
|
+
// Silently ignore for read-only filesystems (production).
|
|
151
|
+
}
|
|
132
152
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
153
|
+
return result;
|
|
154
|
+
}
|
|
155
|
+
getStorage() {
|
|
156
|
+
return this.storage;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Initial migration can be created only if:
|
|
160
|
+
* 1. no previous migrations were generated or executed
|
|
161
|
+
* 2. existing schema do not contain any of the tables defined by metadata
|
|
162
|
+
*
|
|
163
|
+
* If existing schema contains all of the tables already, we return true, based on that we mark the migration as already executed.
|
|
164
|
+
* If only some of the tables are present, exception is thrown.
|
|
165
|
+
*/
|
|
166
|
+
async validateInitialMigration(blank) {
|
|
167
|
+
const executed = await this.getExecuted();
|
|
168
|
+
const pending = await this.getPending();
|
|
169
|
+
if (executed.length > 0 || pending.length > 0) {
|
|
170
|
+
throw new Error('Initial migration cannot be created, as some migrations already exist');
|
|
146
171
|
}
|
|
147
|
-
|
|
148
|
-
|
|
172
|
+
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config);
|
|
173
|
+
const exists = new Set();
|
|
174
|
+
const expected = new Set();
|
|
175
|
+
[...this.em.getMetadata().getAll().values()]
|
|
176
|
+
.filter(meta => meta.tableName && !meta.embeddable && !meta.virtual)
|
|
177
|
+
.forEach(meta => {
|
|
178
|
+
const schema = meta.schema ?? this.config.get('schema', this.em.getPlatform().getDefaultSchemaName());
|
|
179
|
+
expected.add(schema ? `${schema}.${meta.collection}` : meta.collection);
|
|
180
|
+
});
|
|
181
|
+
schema.getTables().forEach(table => {
|
|
182
|
+
const schema = table.schema ?? this.em.getPlatform().getDefaultSchemaName();
|
|
183
|
+
const tableName = schema ? `${schema}.${table.name}` : table.name;
|
|
184
|
+
if (expected.has(tableName)) {
|
|
185
|
+
exists.add(table.schema ? `${table.schema}.${table.name}` : table.name);
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
if (expected.size === 0 && !blank) {
|
|
189
|
+
throw new Error('No entities found');
|
|
149
190
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
*
|
|
155
|
-
* If existing schema contains all of the tables already, we return true, based on that we mark the migration as already executed.
|
|
156
|
-
* If only some of the tables are present, exception is thrown.
|
|
157
|
-
*/
|
|
158
|
-
async validateInitialMigration(blank) {
|
|
159
|
-
const executed = await this.getExecuted();
|
|
160
|
-
const pending = await this.getPending();
|
|
161
|
-
if (executed.length > 0 || pending.length > 0) {
|
|
162
|
-
throw new Error('Initial migration cannot be created, as some migrations already exist');
|
|
163
|
-
}
|
|
164
|
-
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config);
|
|
165
|
-
const exists = new Set();
|
|
166
|
-
const expected = new Set();
|
|
167
|
-
[...this.em.getMetadata().getAll().values()]
|
|
168
|
-
.filter(meta => meta.tableName && !meta.embeddable && !meta.virtual)
|
|
169
|
-
.forEach(meta => {
|
|
170
|
-
const schema = meta.schema ?? this.config.get('schema', this.em.getPlatform().getDefaultSchemaName());
|
|
171
|
-
expected.add(schema ? `${schema}.${meta.collection}` : meta.collection);
|
|
172
|
-
});
|
|
173
|
-
schema.getTables().forEach(table => {
|
|
174
|
-
const schema = table.schema ?? this.em.getPlatform().getDefaultSchemaName();
|
|
175
|
-
const tableName = schema ? `${schema}.${table.name}` : table.name;
|
|
176
|
-
if (expected.has(tableName)) {
|
|
177
|
-
exists.add(table.schema ? `${table.schema}.${table.name}` : table.name);
|
|
178
|
-
}
|
|
179
|
-
});
|
|
180
|
-
if (expected.size === 0 && !blank) {
|
|
181
|
-
throw new Error('No entities found');
|
|
182
|
-
}
|
|
183
|
-
if (exists.size > 0 && expected.size !== exists.size) {
|
|
184
|
-
throw new Error(`Some tables already exist in your schema, remove them first to create the initial migration: ${[...exists].join(', ')}`);
|
|
185
|
-
}
|
|
186
|
-
return expected.size === exists.size;
|
|
191
|
+
if (exists.size > 0 && expected.size !== exists.size) {
|
|
192
|
+
throw new Error(
|
|
193
|
+
`Some tables already exist in your schema, remove them first to create the initial migration: ${[...exists].join(', ')}`,
|
|
194
|
+
);
|
|
187
195
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
194
|
-
if (!fs.pathExists(snapshotPath)) {
|
|
195
|
-
return undefined;
|
|
196
|
-
}
|
|
197
|
-
const data = fs.readJSONSync(snapshotPath);
|
|
198
|
-
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
|
|
199
|
-
const { tables, namespaces, ...rest } = data;
|
|
200
|
-
// share schema-level native enums by reference; fall back to per-table copy on older snapshots
|
|
201
|
-
const sharedNativeEnums = rest.nativeEnums ?? {};
|
|
202
|
-
const hasSharedNativeEnums = Object.keys(sharedNativeEnums).length > 0;
|
|
203
|
-
const tableInstances = tables.map((tbl) => {
|
|
204
|
-
const table = new DatabaseTable(this.driver.getPlatform(), tbl.name, tbl.schema);
|
|
205
|
-
table.nativeEnums = hasSharedNativeEnums ? sharedNativeEnums : (tbl.nativeEnums ?? {});
|
|
206
|
-
table.comment = tbl.comment;
|
|
207
|
-
if (tbl.indexes) {
|
|
208
|
-
table.setIndexes(tbl.indexes);
|
|
209
|
-
}
|
|
210
|
-
if (tbl.checks) {
|
|
211
|
-
table.setChecks(tbl.checks);
|
|
212
|
-
}
|
|
213
|
-
if (tbl.foreignKeys) {
|
|
214
|
-
table.setForeignKeys(tbl.foreignKeys);
|
|
215
|
-
}
|
|
216
|
-
const cols = tbl.columns;
|
|
217
|
-
Object.keys(cols).forEach(col => {
|
|
218
|
-
const column = { ...cols[col] };
|
|
219
|
-
/* v8 ignore next */
|
|
220
|
-
column.mappedType = Type.getType(t[cols[col].mappedType] ?? UnknownType);
|
|
221
|
-
table.addColumn(column);
|
|
222
|
-
});
|
|
223
|
-
return table;
|
|
224
|
-
});
|
|
225
|
-
schema.setTables(tableInstances);
|
|
226
|
-
schema.setNamespaces(new Set(namespaces));
|
|
227
|
-
if (rest.nativeEnums) {
|
|
228
|
-
schema.setNativeEnums(rest.nativeEnums);
|
|
229
|
-
}
|
|
230
|
-
if (rest.views) {
|
|
231
|
-
schema.setViews(rest.views);
|
|
232
|
-
}
|
|
233
|
-
return schema;
|
|
196
|
+
return expected.size === exists.size;
|
|
197
|
+
}
|
|
198
|
+
async getSchemaFromSnapshot() {
|
|
199
|
+
if (!this.options.snapshot) {
|
|
200
|
+
return undefined;
|
|
234
201
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
202
|
+
const snapshotPath = await this.getSnapshotPath();
|
|
203
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
204
|
+
if (!fs.pathExists(snapshotPath)) {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
const data = fs.readJSONSync(snapshotPath);
|
|
208
|
+
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
|
|
209
|
+
const { tables, namespaces, ...rest } = data;
|
|
210
|
+
// share schema-level native enums by reference; fall back to per-table copy on older snapshots
|
|
211
|
+
const sharedNativeEnums = rest.nativeEnums ?? {};
|
|
212
|
+
const hasSharedNativeEnums = Object.keys(sharedNativeEnums).length > 0;
|
|
213
|
+
const tableInstances = tables.map(tbl => {
|
|
214
|
+
const table = new DatabaseTable(this.driver.getPlatform(), tbl.name, tbl.schema);
|
|
215
|
+
table.nativeEnums = hasSharedNativeEnums ? sharedNativeEnums : (tbl.nativeEnums ?? {});
|
|
216
|
+
table.comment = tbl.comment;
|
|
217
|
+
if (tbl.indexes) {
|
|
218
|
+
table.setIndexes(tbl.indexes);
|
|
219
|
+
}
|
|
220
|
+
if (tbl.checks) {
|
|
221
|
+
table.setChecks(tbl.checks);
|
|
222
|
+
}
|
|
223
|
+
if (tbl.foreignKeys) {
|
|
224
|
+
table.setForeignKeys(tbl.foreignKeys);
|
|
225
|
+
}
|
|
226
|
+
const cols = tbl.columns;
|
|
227
|
+
Object.keys(cols).forEach(col => {
|
|
228
|
+
const column = { ...cols[col] };
|
|
229
|
+
/* v8 ignore next */
|
|
230
|
+
column.mappedType = Type.getType(t[cols[col].mappedType] ?? UnknownType);
|
|
231
|
+
table.addColumn(column);
|
|
232
|
+
});
|
|
233
|
+
return table;
|
|
234
|
+
});
|
|
235
|
+
schema.setTables(tableInstances);
|
|
236
|
+
schema.setNamespaces(new Set(namespaces));
|
|
237
|
+
if (rest.nativeEnums) {
|
|
238
|
+
schema.setNativeEnums(rest.nativeEnums);
|
|
239
|
+
}
|
|
240
|
+
if (rest.views) {
|
|
241
|
+
schema.setViews(rest.views);
|
|
243
242
|
}
|
|
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
|
-
return result;
|
|
271
|
-
};
|
|
272
|
-
if (blank) {
|
|
273
|
-
up.push('select 1');
|
|
274
|
-
down.push('select 1');
|
|
243
|
+
return schema;
|
|
244
|
+
}
|
|
245
|
+
async storeCurrentSchema(schema) {
|
|
246
|
+
if (!this.options.snapshot) {
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
const snapshotPath = await this.getSnapshotPath();
|
|
250
|
+
schema ??= this.#schemaGenerator.getTargetSchema();
|
|
251
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
252
|
+
await fs.writeFile(snapshotPath, JSON.stringify(schema, null, 2));
|
|
253
|
+
}
|
|
254
|
+
async getSchemaDiff(blank, initial) {
|
|
255
|
+
const up = [];
|
|
256
|
+
const down = [];
|
|
257
|
+
// Split SQL by statement boundaries (semicolons followed by newline) rather than
|
|
258
|
+
// just newlines, to preserve multiline statements like view definitions.
|
|
259
|
+
// Blank lines (from double newlines) are preserved as empty strings for grouping.
|
|
260
|
+
// Splits inside single-quoted string literals are re-merged (GH #7185).
|
|
261
|
+
const splitStatements = sql => {
|
|
262
|
+
const result = [];
|
|
263
|
+
let buf = '';
|
|
264
|
+
for (const chunk of sql.split(/;\n/)) {
|
|
265
|
+
buf += (buf ? ';\n' : '') + chunk;
|
|
266
|
+
// odd number of single quotes means we're inside a string literal
|
|
267
|
+
if (buf.split(`'`).length % 2 === 0) {
|
|
268
|
+
continue;
|
|
275
269
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
270
|
+
// A chunk starting with \n indicates there was a blank line (grouping separator)
|
|
271
|
+
if (buf.startsWith('\n')) {
|
|
272
|
+
result.push('');
|
|
279
273
|
}
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
safe: this.options.safe,
|
|
284
|
-
dropTables: this.options.dropTables,
|
|
285
|
-
fromSchema: await this.getSchemaFromSnapshot(),
|
|
286
|
-
});
|
|
287
|
-
up.push(...splitStatements(diff.up));
|
|
288
|
-
down.push(...splitStatements(diff.down));
|
|
274
|
+
const trimmed = buf.trim();
|
|
275
|
+
if (trimmed) {
|
|
276
|
+
result.push(trimmed.endsWith(';') ? trimmed : trimmed + ';');
|
|
289
277
|
}
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
278
|
+
buf = '';
|
|
279
|
+
}
|
|
280
|
+
return result;
|
|
281
|
+
};
|
|
282
|
+
if (blank) {
|
|
283
|
+
up.push('select 1');
|
|
284
|
+
down.push('select 1');
|
|
285
|
+
} else if (initial) {
|
|
286
|
+
const dump = await this.#schemaGenerator.getCreateSchemaSQL({ wrap: false });
|
|
287
|
+
up.push(...splitStatements(dump));
|
|
288
|
+
} else {
|
|
289
|
+
const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
|
|
290
|
+
wrap: false,
|
|
291
|
+
safe: this.options.safe,
|
|
292
|
+
dropTables: this.options.dropTables,
|
|
293
|
+
fromSchema: await this.getSchemaFromSnapshot(),
|
|
294
|
+
});
|
|
295
|
+
up.push(...splitStatements(diff.up));
|
|
296
|
+
down.push(...splitStatements(diff.down));
|
|
302
297
|
}
|
|
298
|
+
const cleanUp = diff => {
|
|
299
|
+
for (let i = diff.length - 1; i >= 0; i--) {
|
|
300
|
+
if (diff[i]) {
|
|
301
|
+
break;
|
|
302
|
+
}
|
|
303
|
+
/* v8 ignore next */
|
|
304
|
+
diff.splice(i, 1);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
cleanUp(up);
|
|
308
|
+
cleanUp(down);
|
|
309
|
+
return { up, down };
|
|
310
|
+
}
|
|
303
311
|
}
|
package/README.md
CHANGED
|
@@ -133,7 +133,7 @@ const author = await em.findOneOrFail(Author, 1, {
|
|
|
133
133
|
populate: ['books'],
|
|
134
134
|
});
|
|
135
135
|
author.name = 'Jon Snow II';
|
|
136
|
-
author.books.getItems().forEach(book => book.title += ' (2nd ed.)');
|
|
136
|
+
author.books.getItems().forEach(book => (book.title += ' (2nd ed.)'));
|
|
137
137
|
author.books.add(orm.em.create(Book, { title: 'New Book', author }));
|
|
138
138
|
|
|
139
139
|
// Flush computes change sets and executes them in a single transaction
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { MigrationGenerator } from './MigrationGenerator.js';
|
|
2
2
|
/** Generates migration files in TypeScript format. */
|
|
3
3
|
export declare class TSMigrationGenerator extends MigrationGenerator {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
4
|
+
/**
|
|
5
|
+
* @inheritDoc
|
|
6
|
+
*/
|
|
7
|
+
generateMigrationFile(
|
|
8
|
+
className: string,
|
|
9
|
+
diff: {
|
|
10
|
+
up: string[];
|
|
11
|
+
down: string[];
|
|
12
|
+
},
|
|
13
|
+
): string;
|
|
11
14
|
}
|
package/TSMigrationGenerator.js
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
1
|
import { MigrationGenerator } from './MigrationGenerator.js';
|
|
2
2
|
/** Generates migration files in TypeScript format. */
|
|
3
3
|
export class TSMigrationGenerator extends MigrationGenerator {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}
|
|
18
|
-
ret += `}\n`;
|
|
19
|
-
return ret;
|
|
4
|
+
/**
|
|
5
|
+
* @inheritDoc
|
|
6
|
+
*/
|
|
7
|
+
generateMigrationFile(className, diff) {
|
|
8
|
+
let ret = `import { Migration } from '@mikro-orm/migrations';\n\n`;
|
|
9
|
+
ret += `export class ${className} extends Migration {\n\n`;
|
|
10
|
+
ret += ` override up(): void | Promise<void> {\n`;
|
|
11
|
+
diff.up.forEach(sql => (ret += this.createStatement(sql, 4)));
|
|
12
|
+
ret += ` }\n\n`;
|
|
13
|
+
if (diff.down.length > 0) {
|
|
14
|
+
ret += ` override down(): void | Promise<void> {\n`;
|
|
15
|
+
diff.down.forEach(sql => (ret += this.createStatement(sql, 4)));
|
|
16
|
+
ret += ` }\n\n`;
|
|
20
17
|
}
|
|
18
|
+
ret += `}\n`;
|
|
19
|
+
return ret;
|
|
20
|
+
}
|
|
21
21
|
}
|