Skip to content

Commit

Permalink
Add DacPac adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenPelin committed Aug 9, 2017
1 parent b60c4a7 commit 168a563
Show file tree
Hide file tree
Showing 19 changed files with 587 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -46,3 +46,7 @@ src/packages/
src/.vs/
src/SIM.FileSystem.AutoTests/bin/
src/SIM.Client.UnitTests/bin/
src/SIM.Foundation.SqlAdapter/bin/
src/SIM.Foundation.SqlAdapter/obj/
src/SIM.Foundation.SqlAdapter.IntegrationTests/bin/
src/SIM.Foundation.SqlAdapter.IntegrationTests/obj/
30 changes: 30 additions & 0 deletions src/SIM.Base/FileSystem/FilePath.cs
@@ -0,0 +1,30 @@
namespace SIM.Base.FileSystem
{
using JetBrains.Annotations;
using System.IO;

public sealed class FilePath
{
[NotNull]
public string FullName { get; }

public FilePath([NotNull] string fullname)
{
FullName = Path.GetFullPath(fullname);
}

[NotNull]
public string Extension => Path.GetExtension(FullName);

[NotNull]
public static implicit operator string([NotNull] FilePath filePath)
{
return filePath.FullName;
}

public override string ToString()
{
return FullName;
}
}
}
4 changes: 4 additions & 0 deletions src/SIM.Base/SIM.Base.csproj
Expand Up @@ -51,6 +51,7 @@
<Reference Include="System" />
<Reference Include="System.configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Data" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Xml" />
</ItemGroup>
Expand All @@ -66,6 +67,7 @@
<Compile Include="EventHelper.cs" />
<Compile Include="Extensions\Extensions.cs" />
<Compile Include="Extensions\StringExtensions.cs" />
<Compile Include="FileSystem\FilePath.cs" />
<Compile Include="FileSystem\FileProvider.cs" />
<Compile Include="FileSystem\DirectoryProvider.cs" />
<Compile Include="FileSystem\FileSystem.cs" />
Expand Down Expand Up @@ -97,6 +99,8 @@
<DesignTime>True</DesignTime>
</Compile>
<Compile Include="ReflectionUtil.cs" />
<Compile Include="Services\ConnectionString.cs" />
<Compile Include="Services\SqlConnectionString.cs" />
<Compile Include="WebRequestHelper.cs" />
<Compile Include="XmlBasedAdvancedSettingsStorage.cs" />
<Compile Include="AppConfigSectionHandler.cs" />
Expand Down
21 changes: 21 additions & 0 deletions src/SIM.Base/Services/ConnectionString.cs
@@ -0,0 +1,21 @@
namespace SIM.Base.Services
{
using JetBrains.Annotations;

public abstract class ConnectionString
{
[NotNull]
public string Value { get; }

protected ConnectionString([NotNull] string value)
{
Value = value;
}

[NotNull]
public static implicit operator string([NotNull] ConnectionString connectionString)
{
return connectionString.Value;
}
}
}
17 changes: 17 additions & 0 deletions src/SIM.Base/Services/SqlConnectionString.cs
@@ -0,0 +1,17 @@
namespace SIM.Base.Services
{
using System.Data.SqlClient;
using JetBrains.Annotations;

public sealed class SqlConnectionString : ConnectionString
{
[NotNull]
public SqlConnectionStringBuilder Builder { get; }

public SqlConnectionString([NotNull] string value)
: base(value)
{
Builder = new SqlConnectionStringBuilder(value);
}
}
}
3 changes: 3 additions & 0 deletions src/SIM.FileSystem/IO/IFile.cs
Expand Up @@ -6,6 +6,9 @@

public interface IFile : IFileSystemEntry, IEquatable<IFile>
{
[NotNull]
string Extension { get; }

[NotNull]
Stream Open(OpenFileMode mode, OpenFileAccess access, OpenFileShare share);

Expand Down
2 changes: 2 additions & 0 deletions src/SIM.FileSystem/IO/Real/RealFile.cs
Expand Up @@ -19,6 +19,8 @@ public RealFile([NotNull] IFileSystem fileSystem, [NotNull] string path) : base(

public override IFolder Parent => _Parent ?? (_Parent = FileSystem.ParseFolder(Path.GetDirectoryName(FileInfo.FullName)));

public string Extension => FileInfo.Extension;

public override void Create()
{
FileInfo.OpenWrite().Close();
Expand Down
Binary file not shown.
@@ -0,0 +1,84 @@
namespace SIM.Adapters
{
using System;
using System.IO;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SIM.Base.Services;
using SIM.IO.Real;

[TestClass]
public class SqlAdapter_Tests
{
private const string DefaultEnvSqlPath = @"C:\Sitecore\etc\sim2\env\default\SqlServer.txt";

[NotNull]
private SqlAdapter Adapter { get; } = new SqlAdapter(new SqlConnectionString(File.ReadAllText(DefaultEnvSqlPath)));

[TestMethod]
public void DeleteDatabase_MissingDatabase()
{
Adapter.DeleteDatabase(GetRandomDatabaseName());
}

[TestMethod]
public void GetDatabaseFilePath_MissingDatabase()
{
var databaseName = GetRandomDatabaseName();

try
{
Adapter.GetDatabaseFilePath(databaseName);
}
catch (DatabaseDoesNotExistException ex)
{
Assert.AreEqual(databaseName, ex.DatabaseName);
Assert.AreEqual($"Failed to perform an operation with SqlServer. The requested '{databaseName}' database does not exist", ex.Message);

return;
}

Assert.Fail();
}

[TestMethod]
[DeploymentItem("Adapters\\SqlAdapter_Database.dacpac")]
public void Deploy_Check_Delete_Check()
{
var fileSystem = new RealFileSystem();
var databaseName = GetRandomDatabaseName();
var dacpac = fileSystem.ParseFile("SqlAdapter_Database.dacpac");
Assert.AreEqual(true, dacpac.Exists);

int count;
try
{
Adapter.DeployDatabase(databaseName, dacpac);
Assert.AreEqual(true, Adapter.DatabaseExists(databaseName));
Assert.AreEqual(true, !string.IsNullOrEmpty(Adapter.GetDatabaseFilePath(databaseName)));

var databases = Adapter.GetDatabases();
Assert.AreEqual(true, databases.Contains(databaseName));

count = databases.Count;
Assert.AreEqual(true, count >= 1);
}
finally
{
Adapter.DeleteDatabase(databaseName);
}

Assert.AreEqual(false, Adapter.DatabaseExists(databaseName));

var newCount = Adapter.GetDatabases().Count;
Assert.AreEqual(count - 1, newCount);
}

[NotNull]
private static string GetRandomDatabaseName()
{
return Guid.NewGuid().ToString("N");
}
}
}
@@ -0,0 +1,15 @@
#region Global

using System.Reflection;
using System.Runtime.InteropServices;

[assembly: AssemblyProduct("SIM")]
[assembly: ComVisible(false)]

[assembly: AssemblyVersion("0.0.0.0")]
[assembly: AssemblyFileVersion("0.0.0.0")]
[assembly: AssemblyInformationalVersion("0.0.0.0")]

#endregion

[assembly: AssemblyTitle("SIM.Foundation.SqlAdapter.IntegrationTests")]
@@ -0,0 +1,88 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.props" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{293D8E00-5C24-4E62-9BF7-4E0FB224D1BA}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SIM</RootNamespace>
<AssemblyName>SIM.Foundation.SqlAdapter.IntegrationTests</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">15.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
<IsCodedUITest>False</IsCodedUITest>
<TestProjectType>UnitTest</TestProjectType>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="JetBrains.Annotations, Version=10.4.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.10.4.0\lib\net\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.1.17\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MSTest.TestFramework.1.1.17\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="Adapters\SqlAdapter_Tests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Adapters\SqlAdapter_Database.dacpac">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SIM.Base\SIM.Base.csproj">
<Project>{CA9339A0-9A7D-4900-839E-F21B7269BDAA}</Project>
<Name>SIM.Base</Name>
</ProjectReference>
<ProjectReference Include="..\SIM.FileSystem\SIM.FileSystem.csproj">
<Project>{02B6C2D7-3083-4DF3-B86D-B6D4728C4EF2}</Project>
<Name>SIM.FileSystem</Name>
</ProjectReference>
<ProjectReference Include="..\SIM.Foundation.SqlAdapter\SIM.Foundation.SqlAdapter.csproj">
<Project>{3748B30C-ABF5-4DEE-9F22-74E4F69FFF78}</Project>
<Name>SIM.Foundation.SqlAdapter</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.props'))" />
<Error Condition="!Exists('..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.targets'))" />
</Target>
<Import Project="..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.targets" Condition="Exists('..\packages\MSTest.TestAdapter.1.1.17\build\net45\MSTest.TestAdapter.targets')" />
</Project>
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="JetBrains.Annotations" version="10.4.0" targetFramework="net45" />
<package id="MSTest.TestAdapter" version="1.1.17" targetFramework="net45" />
<package id="MSTest.TestFramework" version="1.1.17" targetFramework="net45" />
</packages>
@@ -0,0 +1,16 @@
namespace SIM.Adapters
{
using JetBrains.Annotations;

public class DatabaseDoesNotExistException : SqlAdapterException
{
[NotNull]
public string DatabaseName { get; }

public DatabaseDoesNotExistException([NotNull] string databaseName)
: base($"The requested '{databaseName}' database does not exist")
{
DatabaseName = databaseName;
}
}
}

0 comments on commit 168a563

Please sign in to comment.