From ed24e886cb2731820cdc51e7bc54b23a0b176061 Mon Sep 17 00:00:00 2001 From: Rhys Date: Wed, 27 Apr 2022 10:58:36 -0400 Subject: [PATCH] feat(playground): Add `clusteredIndex` option to createCollection playground template VSCODE-330 (#409) --- src/editors/playgroundController.ts | 21 --- .../playgroundCreateCollectionTemplate.ts | 12 +- ...laygroundCreateCollectionWithTSTemplate.ts | 45 ------ src/test/suite/mdbExtensionController.test.ts | 146 +----------------- 4 files changed, 14 insertions(+), 210 deletions(-) delete mode 100644 src/templates/playgroundCreateCollectionWithTSTemplate.ts diff --git a/src/editors/playgroundController.ts b/src/editors/playgroundController.ts index 8d8bc2d7a..43485caaf 100644 --- a/src/editors/playgroundController.ts +++ b/src/editors/playgroundController.ts @@ -1,6 +1,5 @@ import * as vscode from 'vscode'; import vm from 'vm'; -import semver from 'semver'; import ActiveConnectionCodeLensProvider from './activeConnectionCodeLensProvider'; import CodeActionProvider from './codeActionProvider'; @@ -15,7 +14,6 @@ import { LanguageServerController } from '../language'; import { OutputChannel, ProgressLocation, TextEditor } from 'vscode'; import playgroundCreateIndexTemplate from '../templates/playgroundCreateIndexTemplate'; import playgroundCreateCollectionTemplate from '../templates/playgroundCreateCollectionTemplate'; -import playgroundCreateCollectionWithTSTemplate from '../templates/playgroundCreateCollectionWithTSTemplate'; import { PlaygroundResult, ShellExecuteAllResult, @@ -35,16 +33,6 @@ import TelemetryService from '../telemetry/telemetryService'; const log = createLogger('playground controller'); const transpiler = require('bson-transpilers'); -const MIN_TIME_SERIES_SERVER_VERSION = '5.0.0-alpha0'; - -const hasTimeSeriesSupport = (serverVersion) => { - try { - return semver.gte(serverVersion, MIN_TIME_SERIES_SERVER_VERSION); - } catch (e) { - return true; - } -}; - interface ToCompile { filter?: string; aggregation?: string; @@ -241,17 +229,8 @@ export default class PlaygroundController { async createPlaygroundForCreateCollection( element: ConnectionTreeItem | DatabaseTreeItem ): Promise { - const dataService = this._connectionController.getActiveDataService(); let content = playgroundCreateCollectionTemplate; - if (dataService) { - const instance = await dataService.instance(); - - if (hasTimeSeriesSupport(instance.build.version)) { - content = playgroundCreateCollectionWithTSTemplate; - } - } - element.cacheIsUpToDate = false; if (element instanceof DatabaseTreeItem) { diff --git a/src/templates/playgroundCreateCollectionTemplate.ts b/src/templates/playgroundCreateCollectionTemplate.ts index 3313b88de..dc0b5d842 100644 --- a/src/templates/playgroundCreateCollectionTemplate.ts +++ b/src/templates/playgroundCreateCollectionTemplate.ts @@ -25,9 +25,19 @@ db.createCollection(collection); viewOn: , pipeline: , collation: , - writeConcern: + writeConcern: , + timeseries: { // Added in MongoDB 5.0 + timeField: , // required for time series collections + metaField: , + granularity: + }, + expireAfterSeconds: , + clusteredIndex: , // Added in MongoDB 5.3 } )*/ + +// More information on the \`createCollection\` command can be found at: +// https://www.mongodb.com/docs/manual/reference/method/db.createCollection/ `; export default template; diff --git a/src/templates/playgroundCreateCollectionWithTSTemplate.ts b/src/templates/playgroundCreateCollectionWithTSTemplate.ts deleted file mode 100644 index 9ce35e6cc..000000000 --- a/src/templates/playgroundCreateCollectionWithTSTemplate.ts +++ /dev/null @@ -1,45 +0,0 @@ -const template = `// MongoDB Playground -// Use Ctrl+Space inside a snippet or a string literal to trigger completions. - -const database = 'NEW_DATABASE_NAME'; -const collection = 'NEW_COLLECTION_NAME'; - -// Create a new database. -use(database); - -// Create a new collection. -db.createCollection(collection); - -// The prototype form to create a regular collection: -/* db.createCollection( , - { - capped: , - autoIndexId: , - size: , - max: , - storageEngine: , - validator: , - validationLevel: , - validationAction: , - indexOptionDefaults: , - viewOn: , - pipeline: , - collation: , - writeConcern: - } -) */ - -// The prototype form to create a time-series collection: -/* db.createCollection( , - { - timeseries: { - timeField: , - metaField: , - granularity: - }, - expireAfterSeconds: - } -) */ -`; - -export default template; diff --git a/src/test/suite/mdbExtensionController.test.ts b/src/test/suite/mdbExtensionController.test.ts index 9c4426cb7..3ac0f01c7 100644 --- a/src/test/suite/mdbExtensionController.test.ts +++ b/src/test/suite/mdbExtensionController.test.ts @@ -582,7 +582,7 @@ suite('MDBExtensionController Test Suite', function () { .then(done, done); }); - test('mdb.addDatabase should create a MongoDB playground with create collection template without time-series', async () => { + test('mdb.addDatabase should create a MongoDB playground with create collection template', async () => { const mockTreeItem = new ConnectionTreeItem( 'tasty_sandwhich', vscode.TreeItemCollapsibleState.None, @@ -592,74 +592,6 @@ suite('MDBExtensionController Test Suite', function () { {} ); - const mockGetActiveDataService: any = sinon.fake.returns({ - instance: () => Promise.resolve({ - dataLake: {}, - build: { version: '4.9.7' }, - genuineMongoDB: {}, - host: {} - }), - createCollection: (namespace, options, callback) => { - callback(null); - } - }); - sinon.replace( - mdbTestExtension.testExtensionController._connectionController, - 'getActiveDataService', - mockGetActiveDataService - ); - const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich'); - sinon.replace( - mdbTestExtension.testExtensionController._connectionController, - 'getActiveConnectionId', - mockActiveConnectionId - ); - - const mockOpenTextDocument: any = sinon.fake.resolves('untitled'); - sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument); - - const mockShowTextDocument: any = sinon.fake(); - sinon.replace(vscode.window, 'showTextDocument', mockShowTextDocument); - - await vscode.commands.executeCommand('mdb.addDatabase', mockTreeItem); - - assert(mockOpenTextDocument.firstArg.language === 'mongodb'); - assert( - mockOpenTextDocument.firstArg.content.includes( - '// Create a new database.' - ) - ); - assert(mockOpenTextDocument.firstArg.content.includes('NEW_DATABASE_NAME')); - assert(mockOpenTextDocument.firstArg.content.includes('NEW_COLLECTION_NAME')); - assert(!mockOpenTextDocument.firstArg.content.includes('time-series')); - }); - - test('mdb.addDatabase should create a MongoDB playground with create collection template with time-series', async () => { - const mockTreeItem = new ConnectionTreeItem( - 'tasty_sandwhich', - vscode.TreeItemCollapsibleState.None, - false, - mdbTestExtension.testExtensionController._connectionController, - false, - {} - ); - - const mockGetActiveDataService: any = sinon.fake.returns({ - instance: () => Promise.resolve({ - dataLake: {}, - build: { version: '5.0.1' }, - genuineMongoDB: {}, - host: {} - }), - createCollection: (namespace, options, callback) => { - callback(null); - } - }); - sinon.replace( - mdbTestExtension.testExtensionController._connectionController, - 'getActiveDataService', - mockGetActiveDataService - ); const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich'); sinon.replace( mdbTestExtension.testExtensionController._connectionController, @@ -683,7 +615,6 @@ suite('MDBExtensionController Test Suite', function () { ); assert(mockOpenTextDocument.firstArg.content.includes('NEW_DATABASE_NAME')); assert(mockOpenTextDocument.firstArg.content.includes('NEW_COLLECTION_NAME')); - assert(mockOpenTextDocument.firstArg.content.includes('time-series')); }); test('mdb.addDatabase command fails when disconnecting', (done) => { @@ -782,31 +713,15 @@ suite('MDBExtensionController Test Suite', function () { .then(done, done); }); - test('mdb.addCollection should create a MongoDB playground with create collection template without time-series', async () => { - const mockGetActiveDataService: any = sinon.fake.returns({ - instance: () => Promise.resolve({ - dataLake: {}, - build: { version: '4.9.7' }, - genuineMongoDB: {}, - host: {} - }), - createCollection: (namespace, options, callback) => { - callback(null); - } - }); + test('mdb.addCollection should create a MongoDB playground with create collection template', async () => { const mockTreeItem = new DatabaseTreeItem( 'iceCreamDB', - mockGetActiveDataService, + {}, false, false, {} ); - sinon.replace( - mdbTestExtension.testExtensionController._connectionController, - 'getActiveDataService', - mockGetActiveDataService - ); const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich'); sinon.replace( mdbTestExtension.testExtensionController._connectionController, @@ -833,61 +748,6 @@ suite('MDBExtensionController Test Suite', function () { assert(!mockOpenTextDocument.firstArg.content.includes('time-series')); }); - test('mdb.addCollection should create a MongoDB playground with create collection template with time-series', async () => { - const mockTreeItem = new DatabaseTreeItem( - 'iceCreamDB', - { - createCollection: (namespace, options, callback): void => { - callback(null); - } - }, - false, - false, - {} - ); - - const mockGetActiveDataService: any = sinon.fake.returns({ - instance: () => Promise.resolve({ - dataLake: {}, - build: { version: '5.0.1' }, - genuineMongoDB: {}, - host: {} - }), - createCollection: (namespace, options, callback) => { - callback(null); - } - }); - sinon.replace( - mdbTestExtension.testExtensionController._connectionController, - 'getActiveDataService', - mockGetActiveDataService - ); - const mockActiveConnectionId: any = sinon.fake.returns('tasty_sandwhich'); - sinon.replace( - mdbTestExtension.testExtensionController._connectionController, - 'getActiveConnectionId', - mockActiveConnectionId - ); - - const mockOpenTextDocument: any = sinon.fake.resolves('untitled'); - sinon.replace(vscode.workspace, 'openTextDocument', mockOpenTextDocument); - - const mockShowTextDocument: any = sinon.fake(); - sinon.replace(vscode.window, 'showTextDocument', mockShowTextDocument); - - await vscode.commands.executeCommand('mdb.addCollection', mockTreeItem); - - assert(mockOpenTextDocument.firstArg.language === 'mongodb'); - assert( - mockOpenTextDocument.firstArg.content.includes( - '// The current database to use.' - ) - ); - assert(mockOpenTextDocument.firstArg.content.includes('iceCreamDB')); - assert(mockOpenTextDocument.firstArg.content.includes('NEW_COLLECTION_NAME')); - assert(mockOpenTextDocument.firstArg.content.includes('time-series')); - }); - test('mdb.addCollection command fails when disconnecting', (done) => { const mockTreeItem = new DatabaseTreeItem( 'iceCreamDB',