MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / clear

Method clear

packages/sql/src/schema/SqlSchemaGenerator.ts:184–220  ·  view source on GitHub ↗
(options?: ClearDatabaseOptions)

Source from the content-addressed store, hash-verified

182 }
183
184 override async clear(options?: ClearDatabaseOptions): Promise<void> {
185 // truncate by default, so no value is considered as true
186 /* v8 ignore next */
187 if (options?.truncate === false) {
188 return super.clear(options);
189 }
190
191 if (this.options.disableForeignKeysForClear) {
192 await this.execute(this.helper.disableForeignKeysSQL());
193 }
194
195 const schema = options?.schema ?? this.config.get('schema', this.platform.getDefaultSchemaName());
196
197 for (const meta of this.getOrderedMetadataForClear(schema).reverse()) {
198 try {
199 await this.driver
200 .createQueryBuilder(meta.class, this.em?.getTransactionContext(), 'write', false)
201 .withSchema(schema)
202 .truncate()
203 .execute();
204 } catch (e) {
205 if (this.platform.getExceptionConverter().convertException(e as Error) instanceof TableNotFoundException) {
206 continue;
207 }
208
209 throw e;
210 }
211 }
212
213 if (this.options.disableForeignKeysForClear) {
214 await this.execute(this.helper.enableForeignKeysSQL());
215 }
216
217 if (options?.clearIdentityMap ?? true) {
218 this.clearIdentityMap();
219 }
220 }
221
222 override async getDropSchemaSQL(options: Omit<DropSchemaOptions, 'dropDb'> = {}): Promise<string> {
223 await this.ensureDatabase();

Callers 1

ensureDatabaseMethod · 0.95

Calls 12

executeMethod · 0.95
getTransactionContextMethod · 0.80
clearMethod · 0.65
getMethod · 0.65
executeMethod · 0.65
truncateMethod · 0.65
withSchemaMethod · 0.65
convertExceptionMethod · 0.65
disableForeignKeysSQLMethod · 0.45
getDefaultSchemaNameMethod · 0.45
createQueryBuilderMethod · 0.45
enableForeignKeysSQLMethod · 0.45

Tested by

no test coverage detected