Skip to content
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using Google.Cloud.EntityFrameworkCore.Spanner.Extensions;
using Google.Cloud.EntityFrameworkCore.Spanner.Infrastructure;
using Google.Cloud.EntityFrameworkCore.Spanner.Metadata;
Expand Down Expand Up @@ -65,9 +66,14 @@ public SpannerSampleDbContext(string connectionString, DbContextOptions<SpannerS
public virtual DbSet<Invoice> Invoices { get; set; }
public virtual DbSet<InvoiceLine> InvoiceLines { get; set; }

public void Log(string message)
{
Console.WriteLine(message);
}

protected override void OnConfiguring(DbContextOptionsBuilder options)
// Configure Entity Framework to use a Cloud Spanner database.
=> options.UseSpanner(_connectionString);
=> options.LogTo(Log).UseSpanner(_connectionString).UseMutations(MutationUsage.Never);

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
Expand Down
67 changes: 56 additions & 11 deletions Google.Cloud.EntityFrameworkCore.Spanner.Samples/SampleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,30 @@ private static void RunSample(string sampleName, bool failOnException)

private static async Task RunSampleAsync(Func<string, Task> sampleMethod)
{
var emulatorRunner = new EmulatorRunner();
bool.TryParse(Environment.GetEnvironmentVariable("USE_EXISTING_EMULATOR") ?? "false", out var useExistingEmulator);
var emulatorRunner = useExistingEmulator ? null : new EmulatorRunner();
try
{
Console.WriteLine("");
Console.WriteLine("Starting emulator...");
var portBinding = await emulatorRunner.StartEmulator();
Console.WriteLine($"Emulator started on port {portBinding.HostPort}");
Console.WriteLine("");
Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", $"localhost:{portBinding.HostPort}");
if (useExistingEmulator)
{
var host = Environment.GetEnvironmentVariable("SPANNER_EMULATOR_HOST");
if (host == null)
{
host = "localhost:9010";
Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", host);
}
Console.WriteLine("");
Console.WriteLine($"Using existing emulator on {host}...");
}
else
{
Console.WriteLine("");
Console.WriteLine("Starting emulator...");
var portBinding = await emulatorRunner.StartEmulator();
Console.WriteLine($"Emulator started on port {portBinding.HostPort}");
Console.WriteLine("");
Environment.SetEnvironmentVariable("SPANNER_EMULATOR_HOST", $"localhost:{portBinding.HostPort}");
}

var projectId = "sample-project";
var instanceId = "sample-instance";
Expand All @@ -117,6 +132,10 @@ private static async Task RunSampleAsync(Func<string, Task> sampleMethod)
{
EmulatorDetection = EmulatorDetection.EmulatorOnly,
};
if (useExistingEmulator)
{
await MaybeDeleteInstanceOnEmulatorAsync(databaseName.ProjectId, databaseName.InstanceId);
}
await MaybeCreateInstanceOnEmulatorAsync(databaseName.ProjectId, databaseName.InstanceId);
await MaybeCreateDatabaseOnEmulatorAsync(databaseName);

Expand All @@ -129,10 +148,13 @@ private static async Task RunSampleAsync(Func<string, Task> sampleMethod)
}
finally
{
Console.WriteLine("");
Console.WriteLine("Stopping emulator...");
emulatorRunner.StopEmulator().WaitWithUnwrappedExceptions();
Console.WriteLine("");
if (!useExistingEmulator)
{
Console.WriteLine("");
Console.WriteLine("Stopping emulator...");
emulatorRunner.StopEmulator().WaitWithUnwrappedExceptions();
Console.WriteLine("");
}
}
}

Expand Down Expand Up @@ -164,6 +186,29 @@ private static MethodInfo GetSampleMethod(string sampleName)
}
}

private static async Task MaybeDeleteInstanceOnEmulatorAsync(string projectId, string instanceId)
{
// Try to delete the instance on the emulator and ignore any NotFound error.
var adminClientBuilder = new InstanceAdminClientBuilder
{
EmulatorDetection = EmulatorDetection.EmulatorOnly
};
var instanceAdminClient = await adminClientBuilder.BuildAsync();

var instanceName = InstanceName.FromProjectInstance(projectId, instanceId);
try
{
await instanceAdminClient.DeleteInstanceAsync(new DeleteInstanceRequest
{
InstanceName = InstanceName.FromProjectInstance(projectId, instanceId),
});
}
catch (RpcException e) when (e.StatusCode == StatusCode.NotFound)
{
// Ignore
}
}

private static async Task MaybeCreateInstanceOnEmulatorAsync(string projectId, string instanceId)
{
// Try to create an instance on the emulator and ignore any AlreadyExists error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Xunit;
using SpannerDate = Google.Cloud.EntityFrameworkCore.Spanner.Storage.SpannerDate;
using V1 = Google.Cloud.Spanner.V1;
Expand Down
Loading