@mikro-orm/migrations 7.0.10 → 7.0.11-dev.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/JSMigrationGenerator.d.ts +7 -10
- package/JSMigrationGenerator.js +20 -20
- package/Migration.d.ts +23 -29
- package/Migration.js +46 -46
- package/MigrationGenerator.d.ts +23 -35
- package/MigrationGenerator.js +33 -33
- package/MigrationRunner.d.ts +9 -9
- package/MigrationRunner.js +41 -43
- package/MigrationStorage.d.ts +27 -23
- package/MigrationStorage.js +108 -113
- package/Migrator.d.ts +32 -42
- package/Migrator.js +247 -256
- package/README.md +1 -1
- package/TSMigrationGenerator.d.ts +7 -10
- package/TSMigrationGenerator.js +16 -16
- package/package.json +3 -3
package/Migrator.js
CHANGED
|
@@ -1,283 +1,274 @@
|
|
|
1
|
-
import { Utils, t, Type, UnknownType } from '@mikro-orm/core';
|
|
1
|
+
import { Utils, t, Type, UnknownType, } from '@mikro-orm/core';
|
|
2
2
|
import { AbstractMigrator } from '@mikro-orm/core/migrations';
|
|
3
|
-
import { DatabaseSchema, DatabaseTable } from '@mikro-orm/sql';
|
|
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
|
-
|
|
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);
|
|
10
|
+
#schemaGenerator;
|
|
11
|
+
#snapshotPath;
|
|
12
|
+
constructor(em) {
|
|
13
|
+
super(em);
|
|
14
|
+
this.#schemaGenerator = this.config.getExtension('@mikro-orm/schema-generator');
|
|
28
15
|
}
|
|
29
|
-
|
|
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`);
|
|
16
|
+
static register(orm) {
|
|
17
|
+
orm.config.registerExtension('@mikro-orm/migrator', () => new Migrator(orm.em));
|
|
41
18
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
async init() {
|
|
45
|
-
if (this.initialized) {
|
|
46
|
-
return;
|
|
19
|
+
createRunner() {
|
|
20
|
+
return new MigrationRunner(this.driver, this.options, this.config);
|
|
47
21
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
/* v8 ignore next */
|
|
51
|
-
if (created) {
|
|
52
|
-
this.initServices();
|
|
22
|
+
createStorage() {
|
|
23
|
+
return new MigrationStorage(this.driver, this.options);
|
|
53
24
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
async create(path, blank = false, initial = false, name) {
|
|
60
|
-
await this.init();
|
|
61
|
-
if (initial) {
|
|
62
|
-
return this.createInitial(path, name, blank);
|
|
63
|
-
}
|
|
64
|
-
const diff = await this.getSchemaDiff(blank, initial);
|
|
65
|
-
if (diff.up.length === 0) {
|
|
66
|
-
return { fileName: '', code: '', diff };
|
|
67
|
-
}
|
|
68
|
-
const migration = await this.generator.generate(diff, path, name);
|
|
69
|
-
await this.storeCurrentSchema();
|
|
70
|
-
return {
|
|
71
|
-
fileName: migration[1],
|
|
72
|
-
code: migration[0],
|
|
73
|
-
diff,
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
async checkSchema() {
|
|
77
|
-
const snapshot = await this.getSchemaFromSnapshot();
|
|
78
|
-
if (!snapshot) {
|
|
79
|
-
await this.init();
|
|
25
|
+
getDefaultGenerator() {
|
|
26
|
+
if (this.options.emit === 'js' || this.options.emit === 'cjs') {
|
|
27
|
+
return new JSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
28
|
+
}
|
|
29
|
+
return new TSMigrationGenerator(this.driver, this.config.getNamingStrategy(), this.options);
|
|
80
30
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
await this.init();
|
|
94
|
-
const schemaExists = await this.validateInitialMigration(blank);
|
|
95
|
-
const diff = await this.getSchemaDiff(blank, true);
|
|
96
|
-
const migration = await this.generator.generate(diff, path, name);
|
|
97
|
-
await this.storeCurrentSchema();
|
|
98
|
-
if (schemaExists && !blank) {
|
|
99
|
-
await this.storage.logMigration({ name: migration[1] });
|
|
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`);
|
|
41
|
+
}
|
|
42
|
+
return this.#snapshotPath;
|
|
100
43
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
this.em.getConnection(),
|
|
113
|
-
this.em.getPlatform(),
|
|
114
|
-
this.config,
|
|
115
|
-
undefined,
|
|
116
|
-
undefined,
|
|
117
|
-
undefined,
|
|
118
|
-
undefined,
|
|
119
|
-
undefined,
|
|
120
|
-
ctx,
|
|
121
|
-
);
|
|
122
|
-
try {
|
|
123
|
-
await this.storeCurrentSchema(schema);
|
|
124
|
-
} catch {
|
|
125
|
-
// Silently ignore for read-only filesystems (production).
|
|
126
|
-
}
|
|
44
|
+
async init() {
|
|
45
|
+
if (this.initialized) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
await super.init();
|
|
49
|
+
const created = await this.#schemaGenerator.ensureDatabase();
|
|
50
|
+
/* v8 ignore next */
|
|
51
|
+
if (created) {
|
|
52
|
+
this.initServices();
|
|
53
|
+
}
|
|
54
|
+
await this.storage.ensureTable();
|
|
127
55
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
56
|
+
/**
|
|
57
|
+
* @inheritDoc
|
|
58
|
+
*/
|
|
59
|
+
async create(path, blank = false, initial = false, name) {
|
|
60
|
+
await this.init();
|
|
61
|
+
if (initial) {
|
|
62
|
+
return this.createInitial(path, name, blank);
|
|
63
|
+
}
|
|
64
|
+
const diff = await this.getSchemaDiff(blank, initial);
|
|
65
|
+
if (diff.up.length === 0) {
|
|
66
|
+
return { fileName: '', code: '', diff };
|
|
67
|
+
}
|
|
68
|
+
const migration = await this.generator.generate(diff, path, name);
|
|
69
|
+
await this.storeCurrentSchema();
|
|
70
|
+
return {
|
|
71
|
+
fileName: migration[1],
|
|
72
|
+
code: migration[0],
|
|
73
|
+
diff,
|
|
74
|
+
};
|
|
146
75
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (expected.has(tableName)) {
|
|
160
|
-
exists.add(table.schema ? `${table.schema}.${table.name}` : table.name);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
if (expected.size === 0 && !blank) {
|
|
164
|
-
throw new Error('No entities found');
|
|
76
|
+
async checkSchema() {
|
|
77
|
+
const snapshot = await this.getSchemaFromSnapshot();
|
|
78
|
+
if (!snapshot) {
|
|
79
|
+
await this.init();
|
|
80
|
+
}
|
|
81
|
+
const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
|
|
82
|
+
wrap: false,
|
|
83
|
+
safe: this.options.safe,
|
|
84
|
+
dropTables: this.options.dropTables,
|
|
85
|
+
fromSchema: snapshot,
|
|
86
|
+
});
|
|
87
|
+
return diff.up.trim().length > 0;
|
|
165
88
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
89
|
+
/**
|
|
90
|
+
* @inheritDoc
|
|
91
|
+
*/
|
|
92
|
+
async createInitial(path, name, blank = false) {
|
|
93
|
+
await this.init();
|
|
94
|
+
const schemaExists = await this.validateInitialMigration(blank);
|
|
95
|
+
const diff = await this.getSchemaDiff(blank, true);
|
|
96
|
+
const migration = await this.generator.generate(diff, path, name);
|
|
97
|
+
await this.storeCurrentSchema();
|
|
98
|
+
if (schemaExists && !blank) {
|
|
99
|
+
await this.storage.logMigration({ name: migration[1] });
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
fileName: migration[1],
|
|
103
|
+
code: migration[0],
|
|
104
|
+
diff,
|
|
105
|
+
};
|
|
170
106
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
107
|
+
async runMigrations(method, options) {
|
|
108
|
+
const result = await super.runMigrations(method, options);
|
|
109
|
+
if (result.length > 0 && this.options.snapshot) {
|
|
110
|
+
const ctx = Utils.isObject(options) ? options.transaction : undefined;
|
|
111
|
+
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config, undefined, undefined, undefined, undefined, undefined, ctx);
|
|
112
|
+
try {
|
|
113
|
+
await this.storeCurrentSchema(schema);
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// Silently ignore for read-only filesystems (production).
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return result;
|
|
176
120
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
if (!fs.pathExists(snapshotPath)) {
|
|
180
|
-
return undefined;
|
|
121
|
+
getStorage() {
|
|
122
|
+
return this.storage;
|
|
181
123
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
124
|
+
/**
|
|
125
|
+
* Initial migration can be created only if:
|
|
126
|
+
* 1. no previous migrations were generated or executed
|
|
127
|
+
* 2. existing schema do not contain any of the tables defined by metadata
|
|
128
|
+
*
|
|
129
|
+
* If existing schema contains all of the tables already, we return true, based on that we mark the migration as already executed.
|
|
130
|
+
* If only some of the tables are present, exception is thrown.
|
|
131
|
+
*/
|
|
132
|
+
async validateInitialMigration(blank) {
|
|
133
|
+
const executed = await this.getExecuted();
|
|
134
|
+
const pending = await this.getPending();
|
|
135
|
+
if (executed.length > 0 || pending.length > 0) {
|
|
136
|
+
throw new Error('Initial migration cannot be created, as some migrations already exist');
|
|
137
|
+
}
|
|
138
|
+
const schema = await DatabaseSchema.create(this.em.getConnection(), this.em.getPlatform(), this.config);
|
|
139
|
+
const exists = new Set();
|
|
140
|
+
const expected = new Set();
|
|
141
|
+
[...this.em.getMetadata().getAll().values()]
|
|
142
|
+
.filter(meta => meta.tableName && !meta.embeddable && !meta.virtual)
|
|
143
|
+
.forEach(meta => {
|
|
144
|
+
const schema = meta.schema ?? this.config.get('schema', this.em.getPlatform().getDefaultSchemaName());
|
|
145
|
+
expected.add(schema ? `${schema}.${meta.collection}` : meta.collection);
|
|
146
|
+
});
|
|
147
|
+
schema.getTables().forEach(table => {
|
|
148
|
+
const schema = table.schema ?? this.em.getPlatform().getDefaultSchemaName();
|
|
149
|
+
const tableName = schema ? `${schema}.${table.name}` : table.name;
|
|
150
|
+
if (expected.has(tableName)) {
|
|
151
|
+
exists.add(table.schema ? `${table.schema}.${table.name}` : table.name);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
if (expected.size === 0 && !blank) {
|
|
155
|
+
throw new Error('No entities found');
|
|
156
|
+
}
|
|
157
|
+
if (exists.size > 0 && expected.size !== exists.size) {
|
|
158
|
+
throw new Error(`Some tables already exist in your schema, remove them first to create the initial migration: ${[...exists].join(', ')}`);
|
|
159
|
+
}
|
|
160
|
+
return expected.size === exists.size;
|
|
211
161
|
}
|
|
212
|
-
|
|
213
|
-
|
|
162
|
+
async getSchemaFromSnapshot() {
|
|
163
|
+
if (!this.options.snapshot) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const snapshotPath = await this.getSnapshotPath();
|
|
167
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
168
|
+
if (!fs.pathExists(snapshotPath)) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
const data = fs.readJSONSync(snapshotPath);
|
|
172
|
+
const schema = new DatabaseSchema(this.driver.getPlatform(), this.config.get('schema'));
|
|
173
|
+
const { tables, namespaces, ...rest } = data;
|
|
174
|
+
const tableInstances = tables.map((tbl) => {
|
|
175
|
+
const table = new DatabaseTable(this.driver.getPlatform(), tbl.name, tbl.schema);
|
|
176
|
+
table.nativeEnums = tbl.nativeEnums ?? {};
|
|
177
|
+
table.comment = tbl.comment;
|
|
178
|
+
if (tbl.indexes) {
|
|
179
|
+
table.setIndexes(tbl.indexes);
|
|
180
|
+
}
|
|
181
|
+
if (tbl.checks) {
|
|
182
|
+
table.setChecks(tbl.checks);
|
|
183
|
+
}
|
|
184
|
+
if (tbl.foreignKeys) {
|
|
185
|
+
table.setForeignKeys(tbl.foreignKeys);
|
|
186
|
+
}
|
|
187
|
+
const cols = tbl.columns;
|
|
188
|
+
Object.keys(cols).forEach(col => {
|
|
189
|
+
const column = { ...cols[col] };
|
|
190
|
+
/* v8 ignore next */
|
|
191
|
+
column.mappedType = Type.getType(t[cols[col].mappedType] ?? UnknownType);
|
|
192
|
+
table.addColumn(column);
|
|
193
|
+
});
|
|
194
|
+
return table;
|
|
195
|
+
});
|
|
196
|
+
schema.setTables(tableInstances);
|
|
197
|
+
schema.setNamespaces(new Set(namespaces));
|
|
198
|
+
if (rest.nativeEnums) {
|
|
199
|
+
schema.setNativeEnums(rest.nativeEnums);
|
|
200
|
+
}
|
|
201
|
+
if (rest.views) {
|
|
202
|
+
schema.setViews(rest.views);
|
|
203
|
+
}
|
|
204
|
+
return schema;
|
|
214
205
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
206
|
+
async storeCurrentSchema(schema) {
|
|
207
|
+
if (!this.options.snapshot) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const snapshotPath = await this.getSnapshotPath();
|
|
211
|
+
schema ??= this.#schemaGenerator.getTargetSchema();
|
|
212
|
+
const { fs } = await import('@mikro-orm/core/fs-utils');
|
|
213
|
+
await fs.writeFile(snapshotPath, JSON.stringify(schema, null, 2));
|
|
220
214
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
215
|
+
async getSchemaDiff(blank, initial) {
|
|
216
|
+
const up = [];
|
|
217
|
+
const down = [];
|
|
218
|
+
// Split SQL by statement boundaries (semicolons followed by newline) rather than
|
|
219
|
+
// just newlines, to preserve multiline statements like view definitions.
|
|
220
|
+
// Blank lines (from double newlines) are preserved as empty strings for grouping.
|
|
221
|
+
// Splits inside single-quoted string literals are re-merged (GH #7185).
|
|
222
|
+
const splitStatements = (sql) => {
|
|
223
|
+
const result = [];
|
|
224
|
+
let buf = '';
|
|
225
|
+
for (const chunk of sql.split(/;\n/)) {
|
|
226
|
+
buf += (buf ? ';\n' : '') + chunk;
|
|
227
|
+
// odd number of single quotes means we're inside a string literal
|
|
228
|
+
if (buf.split(`'`).length % 2 === 0) {
|
|
229
|
+
continue;
|
|
230
|
+
}
|
|
231
|
+
// A chunk starting with \n indicates there was a blank line (grouping separator)
|
|
232
|
+
if (buf.startsWith('\n')) {
|
|
233
|
+
result.push('');
|
|
234
|
+
}
|
|
235
|
+
const trimmed = buf.trim();
|
|
236
|
+
if (trimmed) {
|
|
237
|
+
result.push(trimmed.endsWith(';') ? trimmed : trimmed + ';');
|
|
238
|
+
}
|
|
239
|
+
buf = '';
|
|
240
|
+
}
|
|
241
|
+
return result;
|
|
242
|
+
};
|
|
243
|
+
if (blank) {
|
|
244
|
+
up.push('select 1');
|
|
245
|
+
down.push('select 1');
|
|
241
246
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
247
|
+
else if (initial) {
|
|
248
|
+
const dump = await this.#schemaGenerator.getCreateSchemaSQL({ wrap: false });
|
|
249
|
+
up.push(...splitStatements(dump));
|
|
245
250
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
251
|
+
else {
|
|
252
|
+
const diff = await this.#schemaGenerator.getUpdateSchemaMigrationSQL({
|
|
253
|
+
wrap: false,
|
|
254
|
+
safe: this.options.safe,
|
|
255
|
+
dropTables: this.options.dropTables,
|
|
256
|
+
fromSchema: await this.getSchemaFromSnapshot(),
|
|
257
|
+
});
|
|
258
|
+
up.push(...splitStatements(diff.up));
|
|
259
|
+
down.push(...splitStatements(diff.down));
|
|
249
260
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
wrap: false,
|
|
263
|
-
safe: this.options.safe,
|
|
264
|
-
dropTables: this.options.dropTables,
|
|
265
|
-
fromSchema: await this.getSchemaFromSnapshot(),
|
|
266
|
-
});
|
|
267
|
-
up.push(...splitStatements(diff.up));
|
|
268
|
-
down.push(...splitStatements(diff.down));
|
|
261
|
+
const cleanUp = (diff) => {
|
|
262
|
+
for (let i = diff.length - 1; i >= 0; i--) {
|
|
263
|
+
if (diff[i]) {
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
/* v8 ignore next */
|
|
267
|
+
diff.splice(i, 1);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
cleanUp(up);
|
|
271
|
+
cleanUp(down);
|
|
272
|
+
return { up, down };
|
|
269
273
|
}
|
|
270
|
-
const cleanUp = diff => {
|
|
271
|
-
for (let i = diff.length - 1; i >= 0; i--) {
|
|
272
|
-
if (diff[i]) {
|
|
273
|
-
break;
|
|
274
|
-
}
|
|
275
|
-
/* v8 ignore next */
|
|
276
|
-
diff.splice(i, 1);
|
|
277
|
-
}
|
|
278
|
-
};
|
|
279
|
-
cleanUp(up);
|
|
280
|
-
cleanUp(down);
|
|
281
|
-
return { up, down };
|
|
282
|
-
}
|
|
283
274
|
}
|
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 =>
|
|
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,14 +1,11 @@
|
|
|
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
|
-
|
|
11
|
-
down: string[];
|
|
12
|
-
},
|
|
13
|
-
): string;
|
|
4
|
+
/**
|
|
5
|
+
* @inheritDoc
|
|
6
|
+
*/
|
|
7
|
+
generateMigrationFile(className: string, diff: {
|
|
8
|
+
up: string[];
|
|
9
|
+
down: string[];
|
|
10
|
+
}): string;
|
|
14
11
|
}
|
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
|
-
|
|
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`;
|
|
17
|
+
}
|
|
18
|
+
ret += `}\n`;
|
|
19
|
+
return ret;
|
|
17
20
|
}
|
|
18
|
-
ret += `}\n`;
|
|
19
|
-
return ret;
|
|
20
|
-
}
|
|
21
21
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mikro-orm/migrations",
|
|
3
|
-
"version": "7.0.
|
|
3
|
+
"version": "7.0.11-dev.0",
|
|
4
4
|
"description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"data-mapper",
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"copy": "node ../../scripts/copy.mjs"
|
|
48
48
|
},
|
|
49
49
|
"dependencies": {
|
|
50
|
-
"@mikro-orm/sql": "7.0.
|
|
50
|
+
"@mikro-orm/sql": "7.0.11-dev.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@mikro-orm/core": "^7.0.10"
|
|
54
54
|
},
|
|
55
55
|
"peerDependencies": {
|
|
56
|
-
"@mikro-orm/core": "7.0.
|
|
56
|
+
"@mikro-orm/core": "7.0.11-dev.0"
|
|
57
57
|
},
|
|
58
58
|
"engines": {
|
|
59
59
|
"node": ">= 22.17.0"
|