Skip to content

Commit

Permalink
fix(mongo): drop migrations table when requested
Browse files Browse the repository at this point in the history
Closes #4513
  • Loading branch information
B4nan committed Jul 15, 2023
1 parent 72df9ad commit 278ba3a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion packages/mongodb/src/MongoSchemaGenerator.ts
Expand Up @@ -12,6 +12,7 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
options.ensureIndexes ??= true;
const existing = await this.connection.listCollections();
const metadata = this.getOrderedMetadata();
metadata.push({ collection: this.config.get('migrations').tableName } as any);

/* istanbul ignore next */
const promises = metadata
Expand All @@ -32,11 +33,16 @@ export class MongoSchemaGenerator extends AbstractSchemaGenerator<MongoDriver> {
await Promise.all(promises);
}

async dropSchema(): Promise<void> {
async dropSchema(options: { dropMigrationsTable?: boolean } = {}): Promise<void> {
const db = this.connection.getDb();
const collections = await db.listCollections().toArray();
const existing = collections.map(c => c.name);
const metadata = this.getOrderedMetadata();

if (options.dropMigrationsTable) {
metadata.push({ collection: this.config.get('migrations').tableName } as any);
}

const promises = metadata
.filter(meta => existing.includes(meta.collection))
.map(meta => this.connection.dropCollection(meta.collection));
Expand Down
2 changes: 1 addition & 1 deletion tests/features/embeddables/embedded-entities.mongo.test.ts
Expand Up @@ -252,7 +252,7 @@ describe('embedded entities in mongo', () => {
const createCollection = jest.spyOn(MongoConnection.prototype, 'createCollection');
createCollection.mockResolvedValue({} as any);
await orm.schema.createSchema();
expect(createCollection.mock.calls.map(c => c[0])).toEqual(['custom-user', 'parent', 'user']);
expect(createCollection.mock.calls.map(c => c[0])).toEqual(['custom-user', 'parent', 'user', 'mikro_orm_migrations']);
createCollection.mockRestore();
});

Expand Down
Expand Up @@ -87,7 +87,7 @@ describe('embedded entities in mongo', () => {
const createCollection = jest.spyOn(MongoConnection.prototype, 'createCollection');
createCollection.mockResolvedValue({} as any);
await orm.schema.createSchema();
expect(createCollection.mock.calls.map(c => c[0])).toEqual(['user']);
expect(createCollection.mock.calls.map(c => c[0])).toEqual(['user', 'mikro_orm_migrations']);
createCollection.mockRestore();
});

Expand Down
4 changes: 4 additions & 0 deletions tests/features/schema-generator/SchemaGenerator.mongo.test.ts
Expand Up @@ -21,6 +21,10 @@ describe('SchemaGenerator', () => {
await orm.schema.createSchema();
collections = await driver.getConnection().listCollections();
expect(collections).toContain('foo-bar');
expect(collections).toContain('mikro_orm_migrations');
await orm.schema.dropSchema({ dropMigrationsTable: true });
collections = await driver.getConnection().listCollections();
expect(collections).toHaveLength(0);
});

test('refresh collections', async () => {
Expand Down

0 comments on commit 278ba3a

Please sign in to comment.