Open schema compare generated script (#813)

This commit is contained in:
kisantia
2019-05-20 14:14:50 -07:00
committed by GitHub
parent 151a2de625
commit e68b6d62aa
5 changed files with 39 additions and 44 deletions

View File

@@ -15,10 +15,6 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare.Contracts
/// </summary>
public class SchemaCompareGenerateScriptParams : SchemaComparePublishChangesParams
{
/// <summary>
/// Gets or sets the filepath where to save the generated script
/// </summary>
public string ScriptFilePath { get; set; }
}
/// <summary>

View File

@@ -38,16 +38,23 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
public SchemaComparisonResult ComparisonResult { get; set; }
public SchemaCompareScriptGenerationResult ScriptGenerationResult { get; set; }
public SchemaCompareGenerateScriptOperation(SchemaCompareGenerateScriptParams parameters, SchemaComparisonResult comparisonResult)
{
Validate.IsNotNull("parameters", parameters);
Validate.IsNotNull("scriptFilePath", parameters.ScriptFilePath);
this.Parameters = parameters;
Validate.IsNotNull("comparisonResult", comparisonResult);
this.ComparisonResult = comparisonResult;
}
public void Execute(TaskExecutionMode mode)
{
Execute();
AddScriptToTask();
}
public void Execute()
{
if (this.CancellationToken.IsCancellationRequested)
{
@@ -56,15 +63,7 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
try
{
SchemaCompareScriptGenerationResult result = this.ComparisonResult.GenerateScript(this.Parameters.TargetDatabaseName);
File.WriteAllText(this.Parameters.ScriptFilePath, result.Script);
if (!string.IsNullOrEmpty(result.MasterScript))
{
// master script is only used if the target is Azure SQL db and the script contains all operations that must be done against the master database
string masterScriptPath = Path.Combine(Path.GetDirectoryName(this.Parameters.ScriptFilePath), string.Concat("master_", Path.GetFileName(this.Parameters.ScriptFilePath)));
File.WriteAllText(masterScriptPath, result.MasterScript);
}
this.ScriptGenerationResult = this.ComparisonResult.GenerateScript(this.Parameters.TargetDatabaseName);
}
catch (Exception e)
{
@@ -74,6 +73,17 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCompare
}
}
// Separated from Execute() since tests don't create SqlTasks
public void AddScriptToTask()
{
this.SqlTask.AddScript(SqlTaskStatus.Succeeded, this.ScriptGenerationResult.Script);
if (!string.IsNullOrEmpty(this.ScriptGenerationResult.MasterScript))
{
// master script is only used if the target is Azure SQL db and the script contains all operations that must be done against the master database
this.SqlTask.AddScript(SqlTaskStatus.Succeeded, ScriptGenerationResult.MasterScript);
}
}
// The schema compare public api doesn't currently take a cancellation token so the operation can't be cancelled
public void Cancel()
{

View File

@@ -117,9 +117,9 @@ namespace Microsoft.SqlTools.ServiceLayer.SchemaCopmare
SqlTask sqlTask = null;
TaskMetadata metadata = new TaskMetadata();
metadata.TaskOperation = operation;
// want to show filepath in task history instead of server and database
metadata.ServerName = parameters.ScriptFilePath;
metadata.DatabaseName = string.Empty;
metadata.TaskExecutionMode = parameters.TaskExecutionMode;
metadata.ServerName = parameters.TargetServerName;
metadata.DatabaseName = parameters.TargetDatabaseName;
metadata.Name = SR.GenerateScriptTaskName;
sqlTask = SqlTaskManagerInstance.CreateAndRun<SqlTask>(metadata);