Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add PostgreSqlKernel #3688

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions dotnet-interactive.sln
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactiv
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.DotNet.Interactive.Parsing.Tests", "src\Microsoft.DotNet.Interactive.Parsing.Tests\Microsoft.DotNet.Interactive.Parsing.Tests.csproj", "{55138BD7-111A-43A5-BFBB-326606C4C1B5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.DotNet.Interactive.PostgreSql", "src\Microsoft.DotNet.Interactive.PostgreSql\Microsoft.DotNet.Interactive.PostgreSql.csproj", "{C70A388E-7BDC-4455-AC6D-71D4B8832061}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.DotNet.Interactive.PostgreSql.Tests", "src\Microsoft.DotNet.Interactive.PostgreSql.Tests\Microsoft.DotNet.Interactive.PostgreSql.Tests.csproj", "{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -709,6 +713,30 @@ Global
{55138BD7-111A-43A5-BFBB-326606C4C1B5}.Release|x64.Build.0 = Release|Any CPU
{55138BD7-111A-43A5-BFBB-326606C4C1B5}.Release|x86.ActiveCfg = Release|Any CPU
{55138BD7-111A-43A5-BFBB-326606C4C1B5}.Release|x86.Build.0 = Release|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Debug|x64.ActiveCfg = Debug|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Debug|x64.Build.0 = Debug|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Debug|x86.ActiveCfg = Debug|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Debug|x86.Build.0 = Debug|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Release|Any CPU.Build.0 = Release|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Release|x64.ActiveCfg = Release|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Release|x64.Build.0 = Release|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Release|x86.ActiveCfg = Release|Any CPU
{C70A388E-7BDC-4455-AC6D-71D4B8832061}.Release|x86.Build.0 = Release|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Debug|x64.ActiveCfg = Debug|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Debug|x64.Build.0 = Debug|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Debug|x86.ActiveCfg = Debug|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Debug|x86.Build.0 = Debug|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Release|Any CPU.Build.0 = Release|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Release|x64.ActiveCfg = Release|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Release|x64.Build.0 = Release|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Release|x86.ActiveCfg = Release|Any CPU
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -762,6 +790,8 @@ Global
{BF68D266-500C-49AB-80EB-1B673E37E13A} = {B95A8485-8C53-4F56-B0CE-19C0726B5805}
{B12834B8-373E-4932-852B-90E332A4BCED} = {11BA3480-4584-435C-BA9A-8C554DB60E9F}
{55138BD7-111A-43A5-BFBB-326606C4C1B5} = {11BA3480-4584-435C-BA9A-8C554DB60E9F}
{C70A388E-7BDC-4455-AC6D-71D4B8832061} = {B95A8485-8C53-4F56-B0CE-19C0726B5805}
{D76BA16C-BA16-46D5-9973-B5BCACDDADCC} = {11BA3480-4584-435C-BA9A-8C554DB60E9F}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {6D05A9AF-CFFB-4187-8599-574387B76727}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<PropertyGroup>
<UseBetaVersion>true</UseBetaVersion>
</PropertyGroup>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>preview</LangVersion>
<NoWarn>$(NoWarn);8002;VSTHRD002;VSTHRD200</NoWarn> <!-- Assent isn't strongly signed -->
</PropertyGroup>

<ItemGroup>
<Compile Remove="TestResults\**" />
<EmbeddedResource Remove="TestResults\**" />
<None Remove="TestResults\**" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Microsoft.DotNet.Interactive.Tests\Utility\MarkupTestFile.cs" Link="MarkupTestFile.cs" />
<Compile Include="..\Microsoft.DotNet.Interactive.Tests\Utility\TestUtility.cs" Link="TestUtility.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Assent" Version="1.7.0" />
<PackageReference Include="xunit" Version="$(xunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(xunitrunnervisualstudioVersion)" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\dotnet-interactive\dotnet-interactive.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.ExtensionLab\Microsoft.DotNet.Interactive.ExtensionLab.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.CSharp\Microsoft.DotNet.Interactive.CSharp.csproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.FSharp\Microsoft.DotNet.Interactive.FSharp.fsproj" />
<ProjectReference Include="..\Microsoft.DotNet.Interactive.PostgreSql\Microsoft.DotNet.Interactive.PostgreSql.csproj" />
<PackageReference Include="System.Security.Cryptography.X509Certificates" Version="4.3.2" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ProjectConfiguration>
<Settings>
<DefaultTestTimeout>15000</DefaultTestTimeout>
</Settings>
</ProjectConfiguration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using FluentAssertions;
using FluentAssertions.Execution;
using Microsoft.DotNet.Interactive.App;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;
using Microsoft.DotNet.Interactive.Formatting.Csv;
using Microsoft.DotNet.Interactive.Formatting.TabularData;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;

namespace Microsoft.DotNet.Interactive.PostgreSql.Tests;

public class PSqlConnectionTests : IDisposable
{
private CompositeKernel CreateKernel()
{
Formatter.SetPreferredMimeTypesFor(typeof(TabularDataResource), HtmlFormatter.MimeType, CsvFormatter.MimeType);
var csharpKernel = new CSharpKernel().UseNugetDirective().UseValueSharing();

// TODO: remove SQLKernel it is used to test current patch
var kernel = new CompositeKernel
{
new SqlDiscoverabilityKernel(),
csharpKernel,
new KeyValueStoreKernel()
};

kernel.DefaultKernelName = csharpKernel.Name;

PSqlKernelExtension.Load(kernel);

return kernel;
}


[PSqlFact]
public async Task It_can_connect_and_query_data()
{
var connectionString = PSqlFactAttribute.GetConnectionStringForTests();
using var kernel = CreateKernel();
var connect = $"#!connect psql --kernel-name adventureworks \"{connectionString}\"";
var result = await kernel.SubmitCodeAsync(connect);

result.Events
.Should()
.NotContainErrors();

result = await kernel.SubmitCodeAsync("""
#!sql-adventureworks
SELECT * FROM Person.Person LIMIT 100;
""");

result.Events.Should()
.NotContainErrors()
.And
.ContainSingle<DisplayedValueProduced>(e =>
e.FormattedValues.Any(f => f.MimeType == PlainTextFormatter.MimeType));

result.Events.Should()
.ContainSingle<DisplayedValueProduced>(e =>
e.FormattedValues.Any(f => f.MimeType == HtmlFormatter.MimeType));
}

public void Dispose()
{
DataExplorer.ResetToDefault();
Formatter.ResetToDefault();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Data.SqlClient;
using Xunit;

namespace Microsoft.DotNet.Interactive.PostgreSql.Tests;

public sealed class PSqlFactAttribute : FactAttribute
{
private const string TEST_PSQL_CONNECTION_STRING = nameof(TEST_PSQL_CONNECTION_STRING);
private static readonly string _skipReason;

static PSqlFactAttribute()
{
_skipReason = TestConnectionAndReturnSkipReason();
}

public PSqlFactAttribute()
{
if (_skipReason is not null)
{
Skip = _skipReason;
}
}

internal static string TestConnectionAndReturnSkipReason()
{
string connectionString = GetConnectionStringForTests();
if (string.IsNullOrWhiteSpace(connectionString))
{
return $"Environment variable {TEST_PSQL_CONNECTION_STRING} is not set. To run tests that require "
+ "SQL Server, this environment variable must be set to a valid connection string value.";
}

try
{
using var connection = new SqlConnection(connectionString);
connection.Open();
}
catch (Exception e)
{
return $"A connection could not be established to SQL Server. Verify the connection string value used " +
$"for environment variable {TEST_PSQL_CONNECTION_STRING} targets a running SQL Server instance. " +
$"Connection failed failed with error: {e}";
}

return null;
}

public static string GetConnectionStringForTests()
{
// example:
// Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=postgres
return Environment.GetEnvironmentVariable(TEST_PSQL_CONNECTION_STRING);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.AspNetCore.Html;

namespace Microsoft.DotNet.Interactive.PostgreSql.Tests;

public class PSqlKernelExtension
{
public static void Load(Kernel kernel)
{
if (kernel is CompositeKernel compositeKernel)
{
compositeKernel
.AddKernelConnector(new ConnectPostgreSqlDirective());

KernelInvocationContext.Current?.Display(
new HtmlString(@"<details><summary>Query Posgres databases.</summary>
<p>This extension adds support for connecting to PostgreSql Server databases using the <code>#!connect psql</code> magic command. For more information, run a cell using the <code>#!sql</code> magic command.</p>
</details>"),
"text/html");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Xunit;

namespace Microsoft.DotNet.Interactive.PostgreSql.Tests;

public sealed class PSqlTheoryAttribute : TheoryAttribute
{
private static readonly string _skipReason;

static PSqlTheoryAttribute()
{
_skipReason = PSqlFactAttribute.TestConnectionAndReturnSkipReason();
}

public PSqlTheoryAttribute()
{
if (_skipReason is not null)
{
Skip = _skipReason;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.DotNet.Interactive.Connection;
using Microsoft.DotNet.Interactive.Directives;

namespace Microsoft.DotNet.Interactive.PostgreSql;

public class ConnectPostgreSqlDirective : ConnectKernelDirective<ConnectPostgreSqlKernel>
{
public ConnectPostgreSqlDirective()
: base("psql", "Connects to a PostgreSQL database")
{
Parameters.Add(ConnectionStringParameter);
}

public KernelDirectiveParameter ConnectionStringParameter { get; } =
new("--connection-string", "The connection string used to connect to the database")
{
AllowImplicitName = true,
Required = true,
TypeHint = "connectionstring-psql"
};

public override Task<IEnumerable<Kernel>> ConnectKernelsAsync(
ConnectPostgreSqlKernel connectCommand,
KernelInvocationContext context)
{
var connectionString = connectCommand.ConnectionString;
var localName = connectCommand.ConnectedKernelName;
var kernel = new PostgreSqlKernel($"sql-{localName}", connectionString);
return Task.FromResult<IEnumerable<Kernel>>([kernel]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.DotNet.Interactive.Commands;

namespace Microsoft.DotNet.Interactive.PostgreSql;

public class ConnectPostgreSqlKernel(string connectedKernelName)
: ConnectKernelCommand(connectedKernelName)
{

public string ConnectionString { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project>

<PropertyGroup>
<UseBetaVersion>true</UseBetaVersion>
</PropertyGroup>

<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>true</IsPackable>
<PackageDescription>Microsoft PostgreSQL support for .NET Interactive</PackageDescription>
<PackageTags>polyglot notebook dotnet interactive SQL PostgreSQL Data</PackageTags>
<IncludeBuildOutput>true</IncludeBuildOutput>
<NoWarn>$(NoWarn);NU5100;VSTHRD002</NoWarn>
<!-- dll outside of lib/ folder -->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Npgsql" Version="8.0.4" />
<PackageReference Include="Pocket.Disposable" Version="1.2.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="PocketLogger" Version="0.9.1">
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Microsoft.DotNet.Interactive\Microsoft.DotNet.Interactive.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="extension.dib" Pack="true" PackagePath="interactive-extensions/dotnet" />
</ItemGroup>

</Project>
Loading