diff --git a/src/sql/azdata.d.ts b/src/sql/azdata.d.ts index 0a77a0fae0..51088a8fd7 100644 --- a/src/sql/azdata.d.ts +++ b/src/sql/azdata.d.ts @@ -4688,17 +4688,19 @@ declare module 'azdata' { export interface ICellContents { cell_type: CellType; source: string | string[]; - metadata?: { - language?: string; - tags?: string[]; - azdata_cell_guid?: string; - }; + metadata?: ICellMetadata; execution_count?: number; outputs?: ICellOutput[]; } export type CellType = 'code' | 'markdown' | 'raw'; + export interface ICellMetadata { + language?: string; + tags?: string[]; + azdata_cell_guid?: string; + } + export interface ICellOutput { output_type: OutputTypeName; metadata?: ICellOutputMetadata; diff --git a/src/sql/azdata.proposed.d.ts b/src/sql/azdata.proposed.d.ts index 5d76bb0999..d7179d0604 100644 --- a/src/sql/azdata.proposed.d.ts +++ b/src/sql/azdata.proposed.d.ts @@ -81,6 +81,10 @@ declare module 'azdata' { export interface INotebookMetadata { connection_name?: string; } + + export interface ICellMetadata { + connection_name?: string; + } } export type SqlDbType = 'BigInt' | 'Binary' | 'Bit' | 'Char' | 'DateTime' | 'Decimal' diff --git a/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts b/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts index ad0377f28b..dec51f6cb3 100644 --- a/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts +++ b/src/sql/workbench/contrib/notebook/test/electron-browser/cell.test.ts @@ -1024,4 +1024,20 @@ suite('Cell Model', function (): void { assert(!isEditMode); }); + test('Should read connection name from notebook metadata', async function () { + const connectionName = 'connectionName'; + let notebookModel = new NotebookModelStub({ + name: '', + version: '', + mimetype: '' + }); + let contents: nb.ICellContents = { + cell_type: CellTypes.Code, + source: '', + metadata: { connection_name: connectionName } + }; + let model = factory.createCell(contents, { notebook: notebookModel, isTrusted: false }); + assert.equal(model.savedConnectionName, connectionName); + }); + }); diff --git a/src/sql/workbench/services/notebook/browser/models/cell.ts b/src/sql/workbench/services/notebook/browser/models/cell.ts index 5a8684c9b6..080bfdf523 100644 --- a/src/sql/workbench/services/notebook/browser/models/cell.ts +++ b/src/sql/workbench/services/notebook/browser/models/cell.ts @@ -39,6 +39,7 @@ export class CellModel extends Disposable implements ICellModel { private _cellType: nb.CellType; private _source: string | string[]; private _language: string; + private _savedConnectionName: string | undefined; private _cellGuid: string; private _future: FutureInternal; private _outputs: nb.ICellOutput[] = []; @@ -55,10 +56,10 @@ export class CellModel extends Disposable implements ICellModel { private _cellUri: URI; private _connectionManagementService: IConnectionManagementService; private _stdInHandler: nb.MessageHandler; - private _metadata: { language?: string; tags?: string[]; cellGuid?: string; }; private _onCellLoaded = new Emitter(); private _loaded: boolean; private _stdInVisible: boolean; + private _metadata: nb.ICellMetadata; private _isCollapsed: boolean; private _onCollapseStateChanged = new Emitter(); private _modelContentChangedEvent: IModelContentChangedEvent; @@ -272,6 +273,10 @@ export class CellModel extends Disposable implements ICellModel { return this._options.notebook.language; } + public get savedConnectionName(): string | undefined { + return this._savedConnectionName; + } + public get cellGuid(): string { return this._cellGuid; } @@ -804,6 +809,9 @@ export class CellModel extends Disposable implements ICellModel { cellJson.metadata.tags = metadata.tags; cellJson.outputs = this._outputs; cellJson.execution_count = this.executionCount ? this.executionCount : null; + if (this._configurationService?.getValue('notebook.saveConnectionName')) { + metadata.connection_name = this._savedConnectionName; + } } return cellJson as nb.ICellContents; } @@ -828,6 +836,7 @@ export class CellModel extends Disposable implements ICellModel { this._cellGuid = cell.metadata && cell.metadata.azdata_cell_guid ? cell.metadata.azdata_cell_guid : generateUuid(); this.setLanguageFromContents(cell); + this._savedConnectionName = this._metadata.connection_name; if (cell.outputs) { for (let output of cell.outputs) { // For now, we're assuming it's OK to save these as-is with no modification diff --git a/src/sql/workbench/services/notebook/browser/models/modelInterfaces.ts b/src/sql/workbench/services/notebook/browser/models/modelInterfaces.ts index d1b26c8177..2867e178d9 100644 --- a/src/sql/workbench/services/notebook/browser/models/modelInterfaces.ts +++ b/src/sql/workbench/services/notebook/browser/models/modelInterfaces.ts @@ -499,6 +499,7 @@ export interface ICellModel { readonly onCellMarkdownModeChanged: Event; sendChangeToNotebook(change: NotebookChangeType): void; cellSourceChanged: boolean; + readonly savedConnectionName: string | undefined; } export interface IModelFactory {