Apply changes from remote database to sqlproj - sql-database-projects changes (#17738)

* update project from database

* update project from database

* Leftover merge update

* Slight refactor to add vscode entrypoints

* Re-adding leftover schemacompare bits that reference database project changes

* Removing unnecessary function

* Addiung GetDSP command to package.json

* tests and a race condition fix

* remove custom UUID generation code

* swapping awaits for voids on promises

* PR feedback

* PR feedback

* Hide update project command from vscode

* Swapping cross-extension commands for bound extension contract

* Re-adding schema compare radio buttons for sqlproj

* Adding refresh after project update

* Populating list of project scripts just before comparison to avoid missing script errors of project was separately edited

* Adding missing await for okay button enable check

* Correcting schema compare source when populated from a project

* Rename UpdateDataModel to be more clear

* Fix incorrectly changed type

* Added new runComparison schema compare command, hooked up to sqlproj extension

* Added progress indicator for "apply now" option

* moved string literal to constant

* Added missing await

* Setting missing "saveScmpButton" state to fix test

* Revert "Setting missing "saveScmpButton" state to fix test"

This reverts commit 55612c9def24ac9e3398f5bbd153d21d9d3ca37f.

* Removing preemptive resetWindow() call

* general cleanup

* PR feedback

* property renames

* Reverting rename; requires Tools Service change first

* Adding header to updateProject

* Adding missing header

* PR feedback

* adding missing await

* Handing race condition for UI enable

* Fixing broken okay enable case

* Fixing enum comparison wonk

Co-authored-by: Noureldine Yehia <t-nyehia@microsoft.com>
This commit is contained in:
Benjin Dubishar
2022-01-11 16:52:09 -08:00
committed by GitHub
parent 4fa2b50077
commit 292e60a767
20 changed files with 1103 additions and 53 deletions

View File

@@ -38,6 +38,10 @@
"light": "./images/light_icon.svg",
"dark": "./images/dark_icon.svg"
}
},
{
"command": "schemaCompare.runComparison",
"title": "%schemaCompare.runComparison%"
}
],
"languages": [
@@ -83,6 +87,10 @@
{
"command": "schemaCompare.start",
"when": "mssql:engineedition != 11"
},
{
"command": "schemaCompare.runComparison",
"when": "false"
}
]
}

View File

@@ -1,5 +1,6 @@
{
"displayName": "SQL Server Schema Compare",
"description": "SQL Server Schema Compare for Azure Data Studio supports comparing the schemas of databases and dacpacs.",
"schemaCompare.start": "Schema Compare"
}
"schemaCompare.start": "Schema Compare",
"schemaCompare.runComparison": "Run Schema Comparison"
}

View File

@@ -5,12 +5,13 @@
import * as azdata from 'azdata';
import * as vscode from 'vscode';
import * as loc from '../localizedConstants';
import * as path from 'path';
import * as sqldbproj from 'sqldbproj';
import * as mssql from '../../../mssql';
import * as loc from '../localizedConstants';
import { SchemaCompareMainWindow } from '../schemaCompareMainWindow';
import { TelemetryReporter, TelemetryViews } from '../telemetry';
import { getEndpointName, getRootPath, exists, getAzdataApi, getSchemaCompareEndpointString } from '../utils';
import * as mssql from '../../../mssql';
const titleFontSize: number = 13;
@@ -141,8 +142,8 @@ export class SchemaCompareDialog {
this.schemaCompareMainWindow.sourceEndpointInfo = {
endpointType: mssql.SchemaCompareEndpointType.Project,
projectFilePath: this.sourceTextBox.value,
targetScripts: await this.getTargetScripts(true),
dataSchemaProvider: await this.getDsp(this.sourceTextBox.value),
targetScripts: await this.getProjectScriptFiles(this.sourceTextBox.value),
dataSchemaProvider: await this.getDatabaseSchemaProvider(this.sourceTextBox.value),
folderStructure: '',
serverDisplayName: '',
serverName: '',
@@ -190,8 +191,8 @@ export class SchemaCompareDialog {
endpointType: mssql.SchemaCompareEndpointType.Project,
projectFilePath: this.targetTextBox.value,
folderStructure: this.targetStructureDropdown!.value as string,
targetScripts: await this.getTargetScripts(false),
dataSchemaProvider: await this.getDsp(this.targetTextBox.value),
targetScripts: await this.getProjectScriptFiles(this.targetTextBox.value),
dataSchemaProvider: await this.getDatabaseSchemaProvider(this.targetTextBox.value),
serverDisplayName: '',
serverName: '',
databaseName: '',
@@ -528,10 +529,9 @@ export class SchemaCompareDialog {
let radioButtons = [this.sourceDatabaseRadioButton, this.sourceDacpacRadioButton];
// TODO: re-add once database projects changes are checked in; chicken-and-egg problem (https://github.com/microsoft/azuredatastudio/pull/17738)
// if (vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId)) {
// radioButtons.push(this.sourceProjectRadioButton);
// }
if (vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId)) {
radioButtons.push(this.sourceProjectRadioButton);
}
let flexRadioButtonsModel = this.view.modelBuilder.flexContainer()
.withLayout({ flexFlow: 'column' })
@@ -617,10 +617,9 @@ export class SchemaCompareDialog {
let radioButtons = [targetDatabaseRadioButton, targetDacpacRadioButton];
// TODO: re-add once database projects changes are checked in; chicken-and-egg problem (https://github.com/microsoft/azuredatastudio/pull/17738)
// if (vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId)) {
// radioButtons.push(targetProjectRadioButton);
// }
if (vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId)) {
radioButtons.push(targetProjectRadioButton);
}
let flexRadioButtonsModel = this.view.modelBuilder.flexContainer()
.withLayout({ flexFlow: 'column' })
@@ -636,10 +635,10 @@ export class SchemaCompareDialog {
private async shouldEnableOkayButton(): Promise<boolean> {
let sourcefilled = (this.sourceEndpointType === mssql.SchemaCompareEndpointType.Dacpac && await this.existsDacpac(this.sourceTextBox.value))
|| (this.sourceEndpointType === mssql.SchemaCompareEndpointType.Project && this.existsProjectFile(this.sourceTextBox.value))
|| (this.sourceEndpointType === mssql.SchemaCompareEndpointType.Project && await this.existsProjectFile(this.sourceTextBox.value))
|| (this.sourceEndpointType === mssql.SchemaCompareEndpointType.Database && !isNullOrUndefined(this.sourceDatabaseDropdown.value) && this.sourceDatabaseDropdown.values.findIndex(x => this.matchesValue(x, this.sourceDbEditable)) !== -1);
let targetfilled = (this.targetEndpointType === mssql.SchemaCompareEndpointType.Dacpac && await this.existsDacpac(this.targetTextBox.value))
|| (this.targetEndpointType === mssql.SchemaCompareEndpointType.Project && this.existsProjectFile(this.targetTextBox.value))
|| (this.targetEndpointType === mssql.SchemaCompareEndpointType.Project && await this.existsProjectFile(this.targetTextBox.value))
|| (this.targetEndpointType === mssql.SchemaCompareEndpointType.Database && !isNullOrUndefined(this.targetDatabaseDropdown.value) && this.targetDatabaseDropdown.values.findIndex(x => this.matchesValue(x, this.targetDbEditable)) !== -1);
return sourcefilled && targetfilled;
@@ -670,7 +669,7 @@ export class SchemaCompareDialog {
// check Database Schema Providers are set and valid
if (this.sourceEndpointType === mssql.SchemaCompareEndpointType.Project) {
try {
await this.getDsp(this.sourceTextBox.value);
await this.getDatabaseSchemaProvider(this.sourceTextBox.value);
} catch (err) {
this.showErrorMessage(loc.dspErrorSource);
}
@@ -678,7 +677,7 @@ export class SchemaCompareDialog {
if (this.targetEndpointType === mssql.SchemaCompareEndpointType.Project) {
try {
await this.getDsp(this.targetTextBox.value);
await this.getDatabaseSchemaProvider(this.targetTextBox.value);
} catch (err) {
this.showErrorMessage(loc.dspErrorTarget);
}
@@ -703,13 +702,20 @@ export class SchemaCompareDialog {
return !isNullOrUndefined(filename) && await exists(filename) && (filename.toLocaleLowerCase().endsWith('.sqlproj'));
}
private async getTargetScripts(source: boolean): Promise<string[]> {
const projectFilePath = source ? this.sourceTextBox.value : this.targetTextBox.value;
return await vscode.commands.executeCommand(loc.sqlDatabaseProjectsGetTargetScripts, projectFilePath);
private async getProjectScriptFiles(projectFilePath: string): Promise<string[]> {
const databaseProjectsExtension = vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId);
if (databaseProjectsExtension) {
return await (await databaseProjectsExtension.activate() as sqldbproj.IExtension).getProjectScriptFiles(projectFilePath);
}
}
private async getDsp(projectFilePath: string): Promise<string> {
return await vscode.commands.executeCommand(loc.sqlDatabaseProjectsGetDsp, projectFilePath);
private async getDatabaseSchemaProvider(projectFilePath: string): Promise<string> {
const databaseProjectsExtension = vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId);
if (databaseProjectsExtension) {
return await (await databaseProjectsExtension.activate() as sqldbproj.IExtension).getProjectDatabaseSchemaProvider(projectFilePath);
}
}
protected createSourceServerDropdown(): azdata.FormComponent {

View File

@@ -4,10 +4,12 @@
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import * as mssql from '../../mssql/src/mssql';
import { SchemaCompareMainWindow } from './schemaCompareMainWindow';
export async function activate(extensionContext: vscode.ExtensionContext): Promise<void> {
vscode.commands.registerCommand('schemaCompare.start', async (sourceContext: any, targetContext: any = undefined, comparisonResult: any = undefined) => { await new SchemaCompareMainWindow(undefined, extensionContext, undefined).start(sourceContext, targetContext, comparisonResult); });
vscode.commands.registerCommand('schemaCompare.runComparison', async (source: mssql.SchemaCompareEndpointInfo | undefined, target: mssql.SchemaCompareEndpointInfo | undefined, runComparison: boolean = false, comparisonResult: mssql.SchemaCompareResult | undefined) => { await new SchemaCompareMainWindow(undefined, extensionContext, undefined).launch(source, target, runComparison, comparisonResult); });
}
export function deactivate(): void {

View File

@@ -339,7 +339,4 @@ export const applySuccess: string = localize('schemaCompare.applySuccess', "Proj
export const sqlDatabaseProjectExtensionId: string = 'microsoft.sql-database-projects';
// Commands
export const sqlDatabaseProjectsGetTargetScripts: string = 'sqlDatabaseProjects.schemaCompareGetTargetScripts';
export const sqlDatabaseProjectsGetDsp: string = 'sqlDatabaseProjects.schemaCompareGetDsp';
export const sqlDatabaseProjectsPublishChanges: string = 'sqlDatabaseProjects.schemaComparePublishProjectChanges';
export const sqlDatabaseProjectsShowProjectsView: string = 'sqlDatabaseProjects.schemaCompareShowProjectsView';

View File

@@ -7,11 +7,12 @@ import * as azdata from 'azdata';
import * as vscode from 'vscode';
import * as os from 'os';
import * as path from 'path';
import * as sqldbproj from 'sqldbproj';
import * as mssql from '../../mssql';
import * as loc from './localizedConstants';
import { SchemaCompareOptionsDialog } from './dialogs/schemaCompareOptionsDialog';
import { TelemetryReporter, TelemetryViews } from './telemetry';
import { getTelemetryErrorType, getEndpointName, verifyConnectionAndGetOwnerUri, getRootPath, getSchemaCompareEndpointString } from './utils';
import { getTelemetryErrorType, getEndpointName, verifyConnectionAndGetOwnerUri, getRootPath, getSchemaCompareEndpointString, getDataWorkspaceExtensionApi } from './utils';
import { SchemaCompareDialog } from './dialogs/schemaCompareDialog';
import { isNullOrUndefined } from 'util';
@@ -87,6 +88,9 @@ export class SchemaCompareMainWindow {
// 3. dacpac
// 4. project
public async start(sourceContext: any, targetContext: mssql.SchemaCompareEndpointInfo = undefined, comparisonResult: mssql.SchemaCompareResult = undefined): Promise<void> {
let source: mssql.SchemaCompareEndpointInfo;
let target: mssql.SchemaCompareEndpointInfo;
const targetIsSetAsProject: boolean = targetContext && targetContext.endpointType === mssql.SchemaCompareEndpointType.Project;
// if schema compare was launched from a db or a connection profile, set that as the source
@@ -94,7 +98,7 @@ export class SchemaCompareMainWindow {
if (targetIsSetAsProject) {
profile = sourceContext;
this.targetEndpointInfo = targetContext;
target = targetContext;
} else {
profile = sourceContext ? <azdata.IConnectionProfile>sourceContext.connectionProfile : undefined;
}
@@ -115,7 +119,7 @@ export class SchemaCompareMainWindow {
usr = loc.defaultText;
}
this.sourceEndpointInfo = {
source = {
endpointType: mssql.SchemaCompareEndpointType.Database,
serverDisplayName: `${profile.serverName} (${usr})`,
serverName: profile.serverName,
@@ -130,7 +134,7 @@ export class SchemaCompareMainWindow {
folderStructure: ''
};
} else if (sourceDacpac) {
this.sourceEndpointInfo = {
source = {
endpointType: mssql.SchemaCompareEndpointType.Dacpac,
serverDisplayName: '',
serverName: '',
@@ -144,7 +148,7 @@ export class SchemaCompareMainWindow {
folderStructure: ''
};
} else if (sourceProject) {
this.sourceEndpointInfo = {
source = {
endpointType: mssql.SchemaCompareEndpointType.Project,
packageFilePath: '',
serverDisplayName: '',
@@ -159,14 +163,38 @@ export class SchemaCompareMainWindow {
};
}
await this.launch(source, target, false, comparisonResult);
}
/**
* Primary functional entrypoint for opening the schema comparison window, and optionally running it.
* @param source
* @param target
* @param runComparison whether to immediately run the schema comparison. Requires both source and target to be specified. Cannot be true when comparisonResult is set.
* @param comparisonResult a pre-computed schema comparison result to display. Cannot be set when runComparison is true.
*/
public async launch(source: mssql.SchemaCompareEndpointInfo | undefined, target: mssql.SchemaCompareEndpointInfo | undefined, runComparison: boolean = false, comparisonResult: mssql.SchemaCompareResult | undefined): Promise<void> {
if (runComparison && comparisonResult) {
throw new Error('Cannot both pass a comparison result and request a new comparison be run.');
}
this.sourceEndpointInfo = source;
this.targetEndpointInfo = target;
await this.GetDefaultDeploymentOptions();
await Promise.all([
this.registerContent(),
this.editor.openEditor()
]);
if (targetIsSetAsProject) {
if (comparisonResult) {
await this.execute(comparisonResult);
} else if (runComparison) {
if (!source || !target) {
throw new Error('source and target must both be set when runComparison is true.');
}
await this.startCompare();
}
}
@@ -321,6 +349,18 @@ export class SchemaCompareMainWindow {
this.deploymentOptions = deploymentOptions;
}
private async populateProjectScripts(endpointInfo: mssql.SchemaCompareEndpointInfo): Promise<void> {
if (endpointInfo.endpointType !== mssql.SchemaCompareEndpointType.Project) {
return;
}
const databaseProjectsExtension = vscode.extensions.getExtension(loc.sqlDatabaseProjectExtensionId);
if (databaseProjectsExtension) {
endpointInfo.targetScripts = await (await databaseProjectsExtension.activate() as sqldbproj.IExtension).getProjectScriptFiles(endpointInfo.projectFilePath);
}
}
public async execute(comparisonResult: mssql.SchemaCompareCompletionResult = undefined) {
const service = await this.getService();
@@ -336,6 +376,8 @@ export class SchemaCompareMainWindow {
this.operationId = generateGuid();
}
await Promise.all([this.populateProjectScripts(this.sourceEndpointInfo), this.populateProjectScripts(this.targetEndpointInfo)]);
this.comparisonResult = await service.schemaCompare(this.operationId, this.sourceEndpointInfo, this.targetEndpointInfo, azdata.TaskExecutionMode.execute, this.deploymentOptions);
if (!this.comparisonResult || !this.comparisonResult.success) {
@@ -831,7 +873,8 @@ export class SchemaCompareMainWindow {
TelemetryReporter.createActionEvent(TelemetryViews.SchemaCompareMainWindow, 'SchemaCompareApplyStarted')
.withAdditionalProperties({
'startTime': Date.now().toString(),
'operationId': this.comparisonResult.operationId
'operationId': this.comparisonResult.operationId,
'targetType': getSchemaCompareEndpointString(this.targetEndpointInfo.endpointType)
}).send();
// disable apply and generate script buttons because the results are no longer valid after applying the changes
@@ -844,7 +887,12 @@ export class SchemaCompareMainWindow {
case mssql.SchemaCompareEndpointType.Database:
result = await service.schemaComparePublishDatabaseChanges(this.comparisonResult.operationId, this.targetEndpointInfo.serverName, this.targetEndpointInfo.databaseName, azdata.TaskExecutionMode.execute);
break;
case mssql.SchemaCompareEndpointType.Project: // Project apply needs sql-database-projects updates in (circular dependency; coming next) // TODO: re-add this and show project logic below
case mssql.SchemaCompareEndpointType.Project:
result = await vscode.commands.executeCommand(loc.sqlDatabaseProjectsPublishChanges, this.comparisonResult.operationId, this.targetEndpointInfo.projectFilePath, this.targetEndpointInfo.folderStructure);
if (!result.success) {
void vscode.window.showErrorMessage(loc.applyError);
}
break;
case mssql.SchemaCompareEndpointType.Dacpac: // Dacpac is an invalid publish target
default:
throw new Error(`Unsupported SchemaCompareEndpointType: ${getSchemaCompareEndpointString(this.targetEndpointInfo.endpointType)}`);
@@ -854,7 +902,8 @@ export class SchemaCompareMainWindow {
TelemetryReporter.createErrorEvent(TelemetryViews.SchemaCompareMainWindow, 'SchemaCompareApplyFailed', undefined, getTelemetryErrorType(result?.errorMessage))
.withAdditionalProperties({
'operationId': this.comparisonResult.operationId
'operationId': this.comparisonResult.operationId,
'targetType': getSchemaCompareEndpointString(this.targetEndpointInfo.endpointType)
}).send();
vscode.window.showErrorMessage(loc.applyErrorMessage(result?.errorMessage));
@@ -868,8 +917,16 @@ export class SchemaCompareMainWindow {
TelemetryReporter.createActionEvent(TelemetryViews.SchemaCompareMainWindow, 'SchemaCompareApplyEnded')
.withAdditionalProperties({
'endTime': Date.now().toString(),
'operationId': this.comparisonResult.operationId
'operationId': this.comparisonResult.operationId,
'targetType': getSchemaCompareEndpointString(this.targetEndpointInfo.endpointType)
}).send();
if (this.targetEndpointInfo.endpointType === mssql.SchemaCompareEndpointType.Project) {
const workspaceApi = getDataWorkspaceExtensionApi();
workspaceApi.showProjectsView();
void vscode.window.showInformationMessage(loc.applySuccess);
}
}
});
}

View File

@@ -6,4 +6,6 @@
/// <reference path='../../../../src/vs/vscode.d.ts'/>
/// <reference path='../../../../src/sql/azdata.d.ts'/>
/// <reference path='../../../../src/sql/azdata.proposed.d.ts'/>
/// <reference types='@types/node'/>
/// <reference path='../../../data-workspace/src/dataworkspace.d.ts'/>
/// <reference path='../../../sql-database-projects/src/sqldbproj.d.ts'/>
/// <reference types='@types/node'/>

View File

@@ -9,6 +9,7 @@ import * as vscode from 'vscode';
import * as mssql from '../../mssql';
import * as os from 'os';
import * as loc from './localizedConstants';
import * as dataworkspace from 'dataworkspace';
import { promises as fs } from 'fs';
export interface IPackageInfo {
@@ -182,3 +183,8 @@ try {
export function getAzdataApi(): typeof azdataType | undefined {
return azdataApi;
}
export function getDataWorkspaceExtensionApi(): dataworkspace.IExtension {
const extension = vscode.extensions.getExtension(dataworkspace.extension.name)!;
return extension.exports;
}