mirror of
https://github.com/ckaczor/azuredatastudio.git
synced 2026-01-17 09:35:37 -05:00
Final ADS-side hookup for sqlcmd vars (#10815)
* Adding reading SqlCmdVars from project file * organized apiWrapper, added calls * Adding test to confirm deployment/script gen profiles with sqlcmd vars
This commit is contained in:
@@ -11,26 +11,98 @@ import * as azdata from 'azdata';
|
||||
* this API from our code
|
||||
*/
|
||||
export class ApiWrapper {
|
||||
public createOutputChannel(name: string): vscode.OutputChannel {
|
||||
return vscode.window.createOutputChannel(name);
|
||||
//#region azdata.accounts
|
||||
|
||||
public getAllAccounts(): Thenable<azdata.Account[]> {
|
||||
return azdata.accounts.getAllAccounts();
|
||||
}
|
||||
|
||||
public createTerminalWithOptions(options: vscode.TerminalOptions): vscode.Terminal {
|
||||
return vscode.window.createTerminal(options);
|
||||
public getSecurityToken(account: azdata.Account, resource: azdata.AzureResource): Thenable<{ [key: string]: any }> {
|
||||
return azdata.accounts.getSecurityToken(account, resource);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region azdata.connection
|
||||
|
||||
public getCurrentConnection(): Thenable<azdata.connection.ConnectionProfile> {
|
||||
return azdata.connection.getCurrentConnection();
|
||||
}
|
||||
|
||||
public openConnectionDialog(): Thenable<azdata.connection.Connection> {
|
||||
return azdata.connection.openConnectionDialog();
|
||||
public openConnectionDialog(providers?: string[],
|
||||
initialConnectionProfile?: azdata.IConnectionProfile,
|
||||
connectionCompletionOptions?: azdata.IConnectionCompletionOptions): Thenable<azdata.connection.Connection> {
|
||||
return azdata.connection.openConnectionDialog(providers, initialConnectionProfile, connectionCompletionOptions);
|
||||
}
|
||||
|
||||
public getCredentials(connectionId: string): Thenable<{ [name: string]: string }> {
|
||||
return azdata.connection.getCredentials(connectionId);
|
||||
}
|
||||
|
||||
public connectionConnect(connectionProfile: azdata.IConnectionProfile, saveConnection?: boolean, showDashboard?: boolean): Thenable<azdata.ConnectionResult> {
|
||||
return azdata.connection.connect(connectionProfile, saveConnection, showDashboard);
|
||||
}
|
||||
|
||||
public getUriForConnection(connectionId: string): Thenable<string> {
|
||||
return azdata.connection.getUriForConnection(connectionId);
|
||||
}
|
||||
|
||||
public listDatabases(connectionId: string): Thenable<string[]> {
|
||||
return azdata.connection.listDatabases(connectionId);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region azdata.dataprotocol
|
||||
|
||||
public getProvider<T extends azdata.DataProvider>(providerId: string, providerType: azdata.DataProviderType): T {
|
||||
return azdata.dataprotocol.getProvider<T>(providerId, providerType);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region azdata.queryeditor
|
||||
|
||||
public connect(fileUri: string, connectionId: string): Thenable<void> {
|
||||
return azdata.queryeditor.connect(fileUri, connectionId);
|
||||
}
|
||||
|
||||
public runQuery(fileUri: string, options?: Map<string, string>, runCurrentQuery?: boolean): void {
|
||||
azdata.queryeditor.runQuery(fileUri, options, runCurrentQuery);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region azdata.tasks
|
||||
|
||||
public registerTaskHandler(taskId: string, handler: (profile: azdata.IConnectionProfile) => void): void {
|
||||
azdata.tasks.registerTask(taskId, handler);
|
||||
}
|
||||
|
||||
public startBackgroundOperation(operationInfo: azdata.BackgroundOperationInfo): void {
|
||||
azdata.tasks.startBackgroundOperation(operationInfo);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region azdata.ui
|
||||
|
||||
public registerWidget(widgetId: string, handler: (view: azdata.ModelView) => void): void {
|
||||
azdata.ui.registerModelViewProvider(widgetId, handler);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region azdata.window
|
||||
|
||||
public closeDialog(dialog: azdata.window.Dialog) {
|
||||
azdata.window.closeDialog(dialog);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region vscode.commands
|
||||
|
||||
public registerCommand(command: string, callback: (...args: any[]) => any, thisArg?: any): vscode.Disposable {
|
||||
return vscode.commands.registerCommand(command, callback, thisArg);
|
||||
}
|
||||
@@ -39,26 +111,38 @@ export class ApiWrapper {
|
||||
return vscode.commands.executeCommand(command, ...rest);
|
||||
}
|
||||
|
||||
public registerTaskHandler(taskId: string, handler: (profile: azdata.IConnectionProfile) => void): void {
|
||||
azdata.tasks.registerTask(taskId, handler);
|
||||
//#endregion
|
||||
|
||||
//#region vscode.env
|
||||
|
||||
public openExternal(target: vscode.Uri): Thenable<boolean> {
|
||||
return vscode.env.openExternal(target);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region vscode.extensions
|
||||
|
||||
public getExtension(extensionId: string): vscode.Extension<any> | undefined {
|
||||
return vscode.extensions.getExtension(extensionId);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region vscode.window
|
||||
|
||||
public createOutputChannel(name: string): vscode.OutputChannel {
|
||||
return vscode.window.createOutputChannel(name);
|
||||
}
|
||||
|
||||
public createTerminalWithOptions(options: vscode.TerminalOptions): vscode.Terminal {
|
||||
return vscode.window.createTerminal(options);
|
||||
}
|
||||
|
||||
public registerTreeDataProvider<T>(viewId: string, treeDataProvider: vscode.TreeDataProvider<T>): vscode.Disposable {
|
||||
return vscode.window.registerTreeDataProvider(viewId, treeDataProvider);
|
||||
}
|
||||
|
||||
public createTreeView<T>(viewId: string, options: vscode.TreeViewOptions<T>): vscode.TreeView<T> {
|
||||
return vscode.window.createTreeView(viewId, options);
|
||||
}
|
||||
|
||||
public getUriForConnection(connectionId: string): Thenable<string> {
|
||||
return azdata.connection.getUriForConnection(connectionId);
|
||||
}
|
||||
|
||||
public getProvider<T extends azdata.DataProvider>(providerId: string, providerType: azdata.DataProviderType): T {
|
||||
return azdata.dataprotocol.getProvider<T>(providerId, providerType);
|
||||
}
|
||||
|
||||
public showErrorMessage(message: string, ...items: string[]): Thenable<string | undefined> {
|
||||
return vscode.window.showErrorMessage(message, ...items);
|
||||
}
|
||||
@@ -75,26 +159,6 @@ export class ApiWrapper {
|
||||
return vscode.window.showOpenDialog(options);
|
||||
}
|
||||
|
||||
public startBackgroundOperation(operationInfo: azdata.BackgroundOperationInfo): void {
|
||||
azdata.tasks.startBackgroundOperation(operationInfo);
|
||||
}
|
||||
|
||||
public openExternal(target: vscode.Uri): Thenable<boolean> {
|
||||
return vscode.env.openExternal(target);
|
||||
}
|
||||
|
||||
public getExtension(extensionId: string): vscode.Extension<any> | undefined {
|
||||
return vscode.extensions.getExtension(extensionId);
|
||||
}
|
||||
|
||||
public getConfiguration(section?: string, resource?: vscode.Uri | null): vscode.WorkspaceConfiguration {
|
||||
return vscode.workspace.getConfiguration(section, resource);
|
||||
}
|
||||
|
||||
public workspaceFolders(): readonly vscode.WorkspaceFolder[] | undefined {
|
||||
return vscode.workspace.workspaceFolders;
|
||||
}
|
||||
|
||||
public createTab(title: string): azdata.window.DialogTab {
|
||||
return azdata.window.createTab(title);
|
||||
}
|
||||
@@ -115,14 +179,6 @@ export class ApiWrapper {
|
||||
return azdata.window.openDialog(dialog);
|
||||
}
|
||||
|
||||
public getAllAccounts(): Thenable<azdata.Account[]> {
|
||||
return azdata.accounts.getAllAccounts();
|
||||
}
|
||||
|
||||
public getSecurityToken(account: azdata.Account, resource: azdata.AzureResource): Thenable<{ [key: string]: any }> {
|
||||
return azdata.accounts.getSecurityToken(account, resource);
|
||||
}
|
||||
|
||||
public showQuickPick<T extends vscode.QuickPickItem>(items: T[] | Thenable<T[]>, options?: vscode.QuickPickOptions, token?: vscode.CancellationToken): Thenable<T | undefined> {
|
||||
return vscode.window.showQuickPick(items, options, token);
|
||||
}
|
||||
@@ -135,22 +191,6 @@ export class ApiWrapper {
|
||||
return vscode.window.showSaveDialog(options);
|
||||
}
|
||||
|
||||
public listDatabases(connectionId: string): Thenable<string[]> {
|
||||
return azdata.connection.listDatabases(connectionId);
|
||||
}
|
||||
|
||||
public openTextDocument(options?: { language?: string; content?: string; }): Thenable<vscode.TextDocument> {
|
||||
return vscode.workspace.openTextDocument(options);
|
||||
}
|
||||
|
||||
public connect(fileUri: string, connectionId: string): Thenable<void> {
|
||||
return azdata.queryeditor.connect(fileUri, connectionId);
|
||||
}
|
||||
|
||||
public runQuery(fileUri: string, options?: Map<string, string>, runCurrentQuery?: boolean): void {
|
||||
azdata.queryeditor.runQuery(fileUri, options, runCurrentQuery);
|
||||
}
|
||||
|
||||
public showTextDocument(uri: vscode.Uri, options?: vscode.TextDocumentShowOptions): Thenable<vscode.TextEditor> {
|
||||
return vscode.window.showTextDocument(uri, options);
|
||||
}
|
||||
@@ -159,7 +199,25 @@ export class ApiWrapper {
|
||||
return azdata.window.createButton(label, position);
|
||||
}
|
||||
|
||||
public registerWidget(widgetId: string, handler: (view: azdata.ModelView) => void): void {
|
||||
azdata.ui.registerModelViewProvider(widgetId, handler);
|
||||
public createTreeView<T>(viewId: string, options: vscode.TreeViewOptions<T>): vscode.TreeView<T> {
|
||||
return vscode.window.createTreeView(viewId, options);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
|
||||
//#region vscode.workspace
|
||||
|
||||
public getConfiguration(section?: string, resource?: vscode.Uri | null): vscode.WorkspaceConfiguration {
|
||||
return vscode.workspace.getConfiguration(section, resource);
|
||||
}
|
||||
|
||||
public workspaceFolders(): readonly vscode.WorkspaceFolder[] | undefined {
|
||||
return vscode.workspace.workspaceFolders;
|
||||
}
|
||||
|
||||
public openTextDocument(options?: { language?: string; content?: string; }): Thenable<vscode.TextDocument> {
|
||||
return vscode.workspace.openTextDocument(options);
|
||||
}
|
||||
|
||||
//#endregion
|
||||
}
|
||||
|
||||
@@ -120,6 +120,8 @@ export const Files = 'Files';
|
||||
export const PackageReference = 'PackageReference';
|
||||
export const Version = 'Version';
|
||||
export const PrivateAssets = 'PrivateAssets';
|
||||
export const SqlCmdVariable = 'SqlCmdVariable';
|
||||
export const DefaultValue = 'DefaultValue';
|
||||
export const ArtifactReference = 'ArtifactReference';
|
||||
export const SuppressMissingDependenciesErrors = 'SuppressMissingDependenciesErrors';
|
||||
export const DatabaseVariableLiteralValue = 'DatabaseVariableLiteralValue';
|
||||
|
||||
@@ -136,10 +136,10 @@ export class DeployDatabaseDialog {
|
||||
};
|
||||
|
||||
if (dataSource.integratedSecurity) {
|
||||
connId = (await azdata.connection.connect(connProfile, false, false)).connectionId;
|
||||
connId = (await this.apiWrapper.connectionConnect(connProfile, false, false)).connectionId;
|
||||
}
|
||||
else {
|
||||
connId = (await azdata.connection.openConnectionDialog(undefined, connProfile)).connectionId;
|
||||
connId = (await this.apiWrapper.openConnectionDialog(undefined, connProfile)).connectionId;
|
||||
}
|
||||
}
|
||||
else {
|
||||
@@ -150,7 +150,7 @@ export class DeployDatabaseDialog {
|
||||
connId = this.connection?.connectionId;
|
||||
}
|
||||
|
||||
return await azdata.connection.getUriForConnection(connId);
|
||||
return await this.apiWrapper.getUriForConnection(connId);
|
||||
}
|
||||
|
||||
public async deployClick(): Promise<void> {
|
||||
@@ -161,7 +161,7 @@ export class DeployDatabaseDialog {
|
||||
sqlCmdVariables: this.project.sqlCmdVariables
|
||||
};
|
||||
|
||||
azdata.window.closeDialog(this.dialog);
|
||||
this.apiWrapper.closeDialog(this.dialog);
|
||||
await this.deploy!(this.project, profile);
|
||||
|
||||
this.dispose();
|
||||
@@ -170,10 +170,11 @@ export class DeployDatabaseDialog {
|
||||
public async generateScriptClick(): Promise<void> {
|
||||
const profile: IGenerateScriptProfile = {
|
||||
databaseName: this.getTargetDatabaseName(),
|
||||
connectionUri: await this.getConnectionUri()
|
||||
connectionUri: await this.getConnectionUri(),
|
||||
sqlCmdVariables: this.project.sqlCmdVariables
|
||||
};
|
||||
|
||||
azdata.window.closeDialog(this.dialog);
|
||||
this.apiWrapper.closeDialog(this.dialog);
|
||||
|
||||
if (this.generateScript) {
|
||||
await this.generateScript!(this.project, profile);
|
||||
@@ -182,7 +183,7 @@ export class DeployDatabaseDialog {
|
||||
this.dispose();
|
||||
}
|
||||
|
||||
private getTargetDatabaseName(): string {
|
||||
public getTargetDatabaseName(): string {
|
||||
return this.targetDatabaseTextBox?.value ?? '';
|
||||
}
|
||||
|
||||
|
||||
@@ -63,6 +63,15 @@ export class Project {
|
||||
this.importedTargets.push(importTarget.getAttribute(constants.Project));
|
||||
}
|
||||
|
||||
// find all SQLCMD variables to include
|
||||
for (let i = 0; i < this.projFileXmlDoc.documentElement.getElementsByTagName(constants.SqlCmdVariable).length; i++) {
|
||||
const sqlCmdVar = this.projFileXmlDoc.documentElement.getElementsByTagName(constants.SqlCmdVariable)[i];
|
||||
const varName = sqlCmdVar.getAttribute(constants.Include);
|
||||
|
||||
const varValue = sqlCmdVar.getElementsByTagName(constants.DefaultValue)[0].childNodes[0].nodeValue;
|
||||
this.sqlCmdVariables[varName] = varValue;
|
||||
}
|
||||
|
||||
// find all database references to include
|
||||
for (let r = 0; r < this.projFileXmlDoc.documentElement.getElementsByTagName(constants.ArtifactReference).length; r++) {
|
||||
const filepath = this.projFileXmlDoc.documentElement.getElementsByTagName(constants.ArtifactReference)[r].getAttribute(constants.Include);
|
||||
|
||||
@@ -73,6 +73,16 @@
|
||||
<Folder Include="Views\User" />
|
||||
<Build Include="Views\User\Profile.sql" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<SqlCmdVariable Include="ProdDatabaseName">
|
||||
<DefaultValue>MyProdDatabase</DefaultValue>
|
||||
<Value>$(SqlCmdVar__1)</Value>
|
||||
</SqlCmdVariable>
|
||||
<SqlCmdVariable Include="BackupDatabaseName">
|
||||
<DefaultValue>MyBackupDatabase</DefaultValue>
|
||||
<Value>$(SqlCmdVar__2)</Value>
|
||||
</SqlCmdVariable>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ArtifactReference Condition="'$(NetCoreBuild)' == 'true'" Include="$(NETCoreTargetsPath)\SystemDacpacs\130\master.dacpac">
|
||||
<SuppressMissingDependenciesErrors>False</SuppressMissingDependenciesErrors>
|
||||
|
||||
@@ -9,22 +9,29 @@ import * as os from 'os';
|
||||
import * as vscode from 'vscode';
|
||||
import * as baselines from './baselines/baselines';
|
||||
import * as templates from '../templates/templates';
|
||||
import * as testUtils from '../test/testUtils';
|
||||
import * as TypeMoq from 'typemoq';
|
||||
|
||||
import { DeployDatabaseDialog } from '../dialogs/deployDatabaseDialog';
|
||||
import { Project } from '../models/project';
|
||||
import { SqlDatabaseProjectTreeViewProvider } from '../controllers/databaseProjectTreeViewProvider';
|
||||
import { ProjectsController } from '../controllers/projectController';
|
||||
import { createContext, TestContext } from './testContext';
|
||||
import { IDeploymentProfile, IGenerateScriptProfile } from '../models/IDeploymentProfile';
|
||||
|
||||
|
||||
let testContext: TestContext;
|
||||
|
||||
describe('Deploy Database Dialog', () => {
|
||||
before(async function (): Promise<void> {
|
||||
testContext = createContext();
|
||||
await templates.loadTemplates(path.join(__dirname, '..', '..', 'resources', 'templates'));
|
||||
await baselines.loadBaselines();
|
||||
});
|
||||
|
||||
beforeEach(async function (): Promise<void> {
|
||||
testContext = createContext();
|
||||
});
|
||||
|
||||
it('Should open dialog successfully ', async function (): Promise<void> {
|
||||
const projController = new ProjectsController(testContext.apiWrapper.object, new SqlDatabaseProjectTreeViewProvider());
|
||||
const projFileDir = path.join(os.tmpdir(), `TestProject_${new Date().getTime()}`);
|
||||
@@ -47,4 +54,43 @@ describe('Deploy Database Dialog', () => {
|
||||
const deployDatabaseDialog = new DeployDatabaseDialog(testContext.apiWrapper.object, project);
|
||||
should.equal(deployDatabaseDialog.getDefaultDatabaseName(), project.projectFileName);
|
||||
});
|
||||
|
||||
it('Should include all info in deployment profile', async function (): Promise<void> {
|
||||
const proj = await testUtils.createTestProject(baselines.openProjectFileBaseline);
|
||||
const dialog = TypeMoq.Mock.ofType(DeployDatabaseDialog, undefined, undefined, testContext.apiWrapper.object, proj);
|
||||
dialog.setup(x => x.getConnectionUri()).returns(async () => { return 'Mock|Connection|Uri'; });
|
||||
dialog.setup(x => x.getTargetDatabaseName()).returns(() => 'MockDatabaseName');
|
||||
dialog.callBase = true;
|
||||
|
||||
let profile: IDeploymentProfile | IGenerateScriptProfile | undefined;
|
||||
|
||||
const expectedDeploy: IDeploymentProfile = {
|
||||
databaseName: 'MockDatabaseName',
|
||||
connectionUri: 'Mock|Connection|Uri',
|
||||
upgradeExisting: true,
|
||||
sqlCmdVariables: {
|
||||
'ProdDatabaseName': 'MyProdDatabase',
|
||||
'BackupDatabaseName': 'MyBackupDatabase'
|
||||
}
|
||||
};
|
||||
|
||||
dialog.object.deploy = async (_, prof) => { profile = prof; };
|
||||
await dialog.object.deployClick();
|
||||
|
||||
should(profile).deepEqual(expectedDeploy);
|
||||
|
||||
const expectedGenScript: IGenerateScriptProfile = {
|
||||
databaseName: 'MockDatabaseName',
|
||||
connectionUri: 'Mock|Connection|Uri',
|
||||
sqlCmdVariables: {
|
||||
'ProdDatabaseName': 'MyProdDatabase',
|
||||
'BackupDatabaseName': 'MyBackupDatabase'
|
||||
}
|
||||
};
|
||||
|
||||
dialog.object.generateScript = async (_, prof) => { profile = prof; };
|
||||
await dialog.object.generateScriptClick();
|
||||
|
||||
should(profile).deepEqual(expectedGenScript);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,12 +31,19 @@ describe('Project: sqlproj content operations', function (): void {
|
||||
const project: Project = new Project(projFilePath);
|
||||
await project.readProjFile();
|
||||
|
||||
// Files and folders
|
||||
should(project.files.filter(f => f.type === EntryType.File).length).equal(4);
|
||||
should(project.files.filter(f => f.type === EntryType.Folder).length).equal(5);
|
||||
|
||||
should(project.files.find(f => f.type === EntryType.Folder && f.relativePath === 'Views\\User')).not.equal(undefined); // mixed ItemGroup folder
|
||||
should(project.files.find(f => f.type === EntryType.File && f.relativePath === 'Views\\User\\Profile.sql')).not.equal(undefined); // mixed ItemGroup file
|
||||
|
||||
// SqlCmdVariables
|
||||
should(Object.keys(project.sqlCmdVariables).length).equal(2);
|
||||
should(project.sqlCmdVariables['ProdDatabaseName']).equal('MyProdDatabase');
|
||||
should(project.sqlCmdVariables['BackupDatabaseName']).equal('MyBackupDatabase');
|
||||
|
||||
// Database references
|
||||
should(project.databaseReferences.length).equal(1);
|
||||
should(project.databaseReferences[0]).containEql(constants.master);
|
||||
});
|
||||
|
||||
@@ -32,7 +32,10 @@ export async function createTestSqlProjFile(contents: string, folderPath?: strin
|
||||
}
|
||||
|
||||
export async function createTestProject(contents: string, folderPath?: string): Promise<Project> {
|
||||
return new Project(await createTestSqlProjFile(contents, folderPath));
|
||||
const proj = new Project(await createTestSqlProjFile(contents, folderPath));
|
||||
await proj.readProjFile();
|
||||
|
||||
return proj;
|
||||
}
|
||||
|
||||
export async function createTestDataSources(contents: string, folderPath?: string): Promise<string> {
|
||||
|
||||
@@ -17,7 +17,7 @@ describe('Tests for conversion within PascalCase and camelCase', function (): vo
|
||||
});
|
||||
|
||||
describe('Tests to verify exists function', function (): void {
|
||||
it('Should determine existance of files/folders', async () => {
|
||||
it('Should determine existence of files/folders', async () => {
|
||||
let testFolderPath = await createDummyFileStructure();
|
||||
|
||||
should(await exists(testFolderPath)).equal(true);
|
||||
|
||||
Reference in New Issue
Block a user