mirror of
https://github.com/ckaczor/sqltoolsservice.git
synced 2026-02-16 18:47:57 -05:00
Feature/delete peek scripts (#174)
* Delete temp script folder * Delete folder and refactor code * Add unit tests * create folder per workspace * Refactor and move creation to FileUtils * Separate multiple assignment
This commit is contained in:
@@ -21,6 +21,7 @@ using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
|||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Completion;
|
||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||||
@@ -216,6 +217,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
serviceHost.RegisterShutdownTask(async (shutdownParams, shutdownRequestContext) =>
|
serviceHost.RegisterShutdownTask(async (shutdownParams, shutdownRequestContext) =>
|
||||||
{
|
{
|
||||||
Logger.Write(LogLevel.Verbose, "Shutting down language service");
|
Logger.Write(LogLevel.Verbose, "Shutting down language service");
|
||||||
|
DeletePeekDefinitionScripts();
|
||||||
await Task.FromResult(0);
|
await Task.FromResult(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1232,5 +1234,14 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal void DeletePeekDefinitionScripts()
|
||||||
|
{
|
||||||
|
// Delete temp folder created to store peek definition scripts
|
||||||
|
if (FileUtils.SafeDirectoryExists(FileUtils.PeekDefinitionTempFolder))
|
||||||
|
{
|
||||||
|
FileUtils.SafeDirectoryDelete(FileUtils.PeekDefinitionTempFolder, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,9 @@ using System.Data.SqlClient;
|
|||||||
using Microsoft.SqlServer.Management.Smo;
|
using Microsoft.SqlServer.Management.Smo;
|
||||||
using Microsoft.SqlServer.Management.Common;
|
using Microsoft.SqlServer.Management.Common;
|
||||||
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
using Microsoft.SqlServer.Management.SqlParser.Intellisense;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Utility;
|
|
||||||
using Microsoft.SqlTools.ServiceLayer.Connection;
|
using Microsoft.SqlTools.ServiceLayer.Connection;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.Utility;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||||
|
|
||||||
@@ -47,9 +48,7 @@ namespace Microsoft.SqlTools.ServiceLayer.LanguageServices
|
|||||||
{
|
{
|
||||||
this.serverConnection = serverConnection;
|
this.serverConnection = serverConnection;
|
||||||
this.connectionInfo = connInfo;
|
this.connectionInfo = connInfo;
|
||||||
|
this.tempPath = FileUtils.GetPeekDefinitionTempFolder();
|
||||||
DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(Path.GetTempPath() + "mssql_definition");
|
|
||||||
this.tempPath = tempScriptDirectory.FullName;
|
|
||||||
Initialize();
|
Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,46 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
|||||||
{
|
{
|
||||||
internal static class FileUtils
|
internal static class FileUtils
|
||||||
{
|
{
|
||||||
|
internal static string PeekDefinitionTempFolder = Path.GetTempPath() + "mssql_definition";
|
||||||
|
internal static bool PeekDefinitionTempFolderCreated = false;
|
||||||
|
|
||||||
|
internal static string GetPeekDefinitionTempFolder()
|
||||||
|
{
|
||||||
|
string tempPath;
|
||||||
|
if (!PeekDefinitionTempFolderCreated)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// create new temp folder
|
||||||
|
string tempFolder = string.Format("{0}_{1}", FileUtils.PeekDefinitionTempFolder, DateTime.Now.ToString("yyyyMMddHHmmssffff"));
|
||||||
|
DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(tempFolder);
|
||||||
|
FileUtils.PeekDefinitionTempFolder = tempScriptDirectory.FullName;
|
||||||
|
tempPath = tempScriptDirectory.FullName;
|
||||||
|
PeekDefinitionTempFolderCreated = true;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// swallow exception and use temp folder to store scripts
|
||||||
|
tempPath = Path.GetTempPath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// use tempDirectory name created previously
|
||||||
|
DirectoryInfo tempScriptDirectory = Directory.CreateDirectory(FileUtils.PeekDefinitionTempFolder);
|
||||||
|
tempPath = tempScriptDirectory.FullName;
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// swallow exception and use temp folder to store scripts
|
||||||
|
tempPath = Path.GetTempPath();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tempPath;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Checks if file exists and swallows exceptions, if any
|
/// Checks if file exists and swallows exceptions, if any
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -42,5 +82,40 @@ namespace Microsoft.SqlTools.ServiceLayer.QueryExecution
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if file exists and swallows exceptions, if any
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"> path of the file</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
internal static bool SafeDirectoryExists(string path)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return Directory.Exists(path);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Swallow exception
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a directory and swallows exceptions, if any
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="path"></param>
|
||||||
|
internal static void SafeDirectoryDelete(string path, bool recursive)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.Delete(path, recursive);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// Swallow exception, do nothing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -19,6 +19,7 @@ using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts;
|
|||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices;
|
||||||
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.LanguageServices.Contracts;
|
||||||
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
using Microsoft.SqlTools.ServiceLayer.SqlContext;
|
||||||
|
using Microsoft.SqlTools.ServiceLayer.QueryExecution;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
|
using Microsoft.SqlTools.ServiceLayer.Test.QueryExecution;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
using Microsoft.SqlTools.ServiceLayer.Workspace;
|
||||||
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
using Microsoft.SqlTools.ServiceLayer.Workspace.Contracts;
|
||||||
@@ -194,6 +195,33 @@ namespace Microsoft.SqlTools.ServiceLayer.Test.LanguageServices
|
|||||||
Assert.Equal(actualSchemaName, expectedSchemaName);
|
Assert.Equal(actualSchemaName, expectedSchemaName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test Deletion of peek definition scripts for a valid temp folder that exists
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void DeletePeekDefinitionScriptsTest()
|
||||||
|
{
|
||||||
|
PeekDefinition peekDefinition = new PeekDefinition(null, null);
|
||||||
|
var languageService = LanguageService.Instance;
|
||||||
|
Assert.True(Directory.Exists(FileUtils.PeekDefinitionTempFolder));
|
||||||
|
languageService.DeletePeekDefinitionScripts();
|
||||||
|
Assert.False(Directory.Exists(FileUtils.PeekDefinitionTempFolder));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Test Deletion of peek definition scripts for a temp folder that does not exist
|
||||||
|
/// </summary>
|
||||||
|
[Fact]
|
||||||
|
public void DeletePeekDefinitionScriptsWhenFolderDoesNotExistTest()
|
||||||
|
{
|
||||||
|
var languageService = LanguageService.Instance;
|
||||||
|
PeekDefinition peekDefinition = new PeekDefinition(null, null);
|
||||||
|
FileUtils.SafeDirectoryDelete(FileUtils.PeekDefinitionTempFolder, true);
|
||||||
|
Assert.False(Directory.Exists(FileUtils.PeekDefinitionTempFolder));
|
||||||
|
// Expected not to throw any exception
|
||||||
|
languageService.DeletePeekDefinitionScripts();
|
||||||
|
}
|
||||||
|
|
||||||
#if LIVE_CONNECTION_TESTS
|
#if LIVE_CONNECTION_TESTS
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Test get definition for a table object with active connection
|
/// Test get definition for a table object with active connection
|
||||||
|
|||||||
Reference in New Issue
Block a user