Skip to content

Commit

Permalink
Open dacfx deploy script instead of saving to file path (#822)
Browse files Browse the repository at this point in the history
* open dacfx deploy script

* moving test db cleanups into finally blocks
  • Loading branch information
kisantia committed Jun 10, 2019
1 parent 43abb8d commit f7c86a8
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ namespace Microsoft.SqlTools.ServiceLayer.DacFx.Contracts
/// </summary>
public class GenerateDeployScriptParams : DacFxParams
{
/// <summary>
/// Gets or sets the filepath where to save the generated script
/// </summary>
public string ScriptFilePath { get; set; }

/// <summary>
/// Gets or sets whether a Deployment Report should be generated during deploy.
/// </summary>
Expand Down
15 changes: 8 additions & 7 deletions src/Microsoft.SqlTools.ServiceLayer/DacFx/DacFxService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ public async Task HandleGenerateDeployScriptRequest(GenerateDeployScriptParams p
{
GenerateDeployScriptOperation operation = new GenerateDeployScriptOperation(parameters, connInfo);
SqlTask sqlTask = null;
TaskMetadata metadata = TaskMetadata.Create(parameters, SR.GenerateScriptTaskName, operation, ConnectionServiceInstance);

// want to show filepath in task history instead of server and database
metadata.ServerName = parameters.ScriptFilePath;
metadata.DatabaseName = string.Empty;
TaskMetadata metadata = new TaskMetadata();
metadata.TaskOperation = operation;
metadata.TaskExecutionMode = parameters.TaskExecutionMode;
metadata.ServerName = connInfo.ConnectionDetails.ServerName;
metadata.DatabaseName = parameters.DatabaseName;
metadata.Name = SR.GenerateScriptTaskName;

sqlTask = SqlTaskManagerInstance.CreateAndRun<SqlTask>(metadata);

Expand Down Expand Up @@ -275,9 +276,9 @@ internal static ConnectionService ConnectionServiceInstance
/// For testing purpose only
/// </summary>
/// <param name="operation"></param>
internal void PerformOperation(DacFxOperation operation)
internal void PerformOperation(DacFxOperation operation, TaskExecutionMode taskExecutionMode)
{
operation.Execute(TaskExecutionMode.Execute);
operation.Execute(taskExecutionMode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class GenerateDeployScriptOperation : DacFxOperation
{
public GenerateDeployScriptParams Parameters { get; }

public PublishResult Result { get; set; }

public GenerateDeployScriptOperation(GenerateDeployScriptParams parameters, ConnectionInfo connInfo) : base(connInfo)
{
Validate.IsNotNull("parameters", parameters);
Expand All @@ -34,15 +36,23 @@ public override void Execute()
publishOptions.GenerateDeploymentReport = this.Parameters.GenerateDeploymentReport;
publishOptions.CancelToken = this.CancellationToken;
publishOptions.DeployOptions = this.GetDefaultDeployOptions();
publishOptions.DatabaseScriptPath = this.Parameters.ScriptFilePath;
// 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
publishOptions.MasterDbScriptPath = Path.Combine(Path.GetDirectoryName(this.Parameters.ScriptFilePath), string.Concat("master_", Path.GetFileName(this.Parameters.ScriptFilePath)));

PublishResult result = this.DacServices.Script(dacpac, this.Parameters.DatabaseName, publishOptions);
this.Result = this.DacServices.Script(dacpac, this.Parameters.DatabaseName, publishOptions);

// tests don't create a SqlTask, so only add the script when the SqlTask isn't null
if (this.SqlTask != null)
{
this.SqlTask.AddScript(SqlTaskStatus.Succeeded, Result.DatabaseScript);
if (!string.IsNullOrEmpty(this.Result.MasterDbScript))
{
// 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, this.Result.MasterDbScript);
}
}

if(this.Parameters.GenerateDeploymentReport && !string.IsNullOrEmpty(this.Parameters.DeploymentReportFilePath))
if (this.Parameters.GenerateDeploymentReport && !string.IsNullOrEmpty(this.Parameters.DeploymentReportFilePath))
{
File.WriteAllText(this.Parameters.DeploymentReportFilePath, result.DeploymentReport);
File.WriteAllText(this.Parameters.DeploymentReportFilePath, this.Result.DeploymentReport);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ public SchemaCompareGenerateScriptOperation(SchemaCompareGenerateScriptParams pa
}

public void Execute(TaskExecutionMode mode)
{
Execute();
AddScriptToTask();
}

public void Execute()
{
if (this.CancellationToken.IsCancellationRequested)
{
Expand All @@ -64,6 +58,17 @@ public void Execute()
try
{
this.ScriptGenerationResult = this.ComparisonResult.GenerateScript(this.Parameters.TargetDatabaseName);

// tests don't create a SqlTask, so only add the script when the SqlTask isn't null
if (this.SqlTask != null)
{
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);
}
}
}
catch (Exception e)
{
Expand All @@ -73,17 +78,6 @@ public void Execute()
}
}

// 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()
{
Expand Down
Loading

0 comments on commit f7c86a8

Please sign in to comment.