Skip to content

Commit

Permalink
Extracted data access helpers and migration support to Weasel.
Browse files Browse the repository at this point in the history
Closes GH-1467. Closes GH-1646. Closes GH-1803
  • Loading branch information
jeremydmiller committed May 7, 2021
1 parent af97bf3 commit d324906
Show file tree
Hide file tree
Showing 441 changed files with 2,822 additions and 9,942 deletions.
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ services:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: marten_testing
NAMEDATALEN: 100

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weasel.Postgresql;

namespace AspNetCoreWithMarten.Samples.ByNestedClosure
{
Expand Down
1 change: 1 addition & 0 deletions src/AspNetCoreWithMarten/Samples/ByStoreOptions/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weasel.Postgresql;

namespace AspNetCoreWithMarten.Samples.ByStoreOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System;
using System.Data;
using Marten;
using Marten.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Npgsql;
using Weasel.Postgresql;

namespace AspNetCoreWithMarten.Samples.ConfiguringSessionCreation
{

#region sample_CustomSessionFactory

public class CustomSessionFactory: ISessionFactory
{
private readonly IDocumentStore _store;
Expand All @@ -37,35 +34,37 @@ public IDocumentSession OpenSession()
return _store.LightweightSession(IsolationLevel.Serializable);
}
}

#endregion sample_CustomSessionFactory

#region sample_AddMartenWithCustomSessionCreation

public class Startup
{
public IConfiguration Configuration { get; }
public IHostEnvironment Hosting { get; }

public Startup(IConfiguration configuration, IHostEnvironment hosting)
{
Configuration = configuration;
Hosting = hosting;
}

public IConfiguration Configuration { get; }
public IHostEnvironment Hosting { get; }

public void ConfigureServices(IServiceCollection services)
{
var connectionString = Configuration.GetConnectionString("postgres");

services.AddMarten(opts =>
{
opts.Connection(connectionString);

// Use the more permissive schema auto create behavior
// while in development
if (Hosting.IsDevelopment())
{
opts.AutoCreateSchemaObjects = AutoCreate.All;
}
})
opts.Connection(connectionString);

// Use the more permissive schema auto create behavior
// while in development
if (Hosting.IsDevelopment())
{
opts.AutoCreateSchemaObjects = AutoCreate.All;
}
})

// Chained helper to replace the built in
// session factory behavior
Expand All @@ -74,5 +73,6 @@ public void ConfigureServices(IServiceCollection services)

// And other methods we don't care about here...
}

#endregion sample_AddMartenWithCustomSessionCreation
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Npgsql;
using Weasel.Postgresql;

namespace AspNetCoreWithMarten.Samples.PerScopeSessionCreation
{
Expand Down
8 changes: 3 additions & 5 deletions src/AspNetCoreWithMarten/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.Events.Daemon.Resiliency;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weasel.Postgresql;

namespace AspNetCoreWithMarten
{
#region sample_StartupConfigureServices

public class Startup
{
public IConfiguration Configuration { get; }
Expand Down Expand Up @@ -46,6 +43,7 @@ public void ConfigureServices(IServiceCollection services)
}

// and other methods we don't care about right now...

#endregion sample_StartupConfigureServices

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
10 changes: 5 additions & 5 deletions src/CommandLineRunner/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.AsyncDaemon.Testing;
Expand All @@ -9,6 +6,7 @@
using Marten.Testing.Harness;
using Microsoft.Extensions.Hosting;
using Oakton;
using Weasel.Postgresql;

namespace CommandLineRunner
{
Expand All @@ -19,8 +17,9 @@ public static Task<int> Main(string[] args)
return CreateHostBuilder(args).RunOaktonCommands(args);
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddMarten(opts =>
Expand All @@ -34,5 +33,6 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
opts.Events.Projections.Add(new DistanceProjection(), ProjectionLifecycle.Async);
});
});
}
}
}
2 changes: 1 addition & 1 deletion src/EventPublisher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Timers;
using Baseline.Dates;
using Marten;
using Marten.AsyncDaemon.Testing.TestingSupport;
using Marten.Testing.Harness;
using Weasel.Postgresql;

namespace EventPublisher
{
Expand Down
5 changes: 1 addition & 4 deletions src/EventSourceWorker/Program.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Marten;
using Marten.AsyncDaemon.Testing.TestingSupport;
using Marten.Events.Daemon.Resiliency;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Weasel.Postgresql;

namespace EventSourceWorker
{
Expand Down
1 change: 1 addition & 0 deletions src/Marten.AsyncDaemon.Testing/HighWaterDetectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Marten.AsyncDaemon.Testing.TestingSupport;
using Marten.Events;
using Marten.Events.Daemon.HighWater;
using Weasel.Postgresql;
using Marten.Services;
using Marten.Util;
using NpgsqlTypes;
Expand Down
33 changes: 33 additions & 0 deletions src/Marten.CommandLine.Testing/ApplyThenAssertSmokeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Threading.Tasks;
using Marten.CommandLine.Commands;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Marten.CommandLine.Testing
{
public class ApplyThenAssertSmokeTests
{
[Fact]
public async Task do_it_all()
{
var options = new StoreOptions();
options.Connection(ConnectionSource.ConnectionString);
options.Schema.For<User>();
options.Schema.For<Target>();

await new DocumentStore(options).Advanced.Clean.CompletelyRemoveAllAsync();

await new ApplyCommand().Execute(new MartenInput
{
HostBuilder = new HostBuilder().ConfigureServices(x => x.AddMarten(options))
});

await new AssertCommand().Execute(new MartenInput
{
HostBuilder = new HostBuilder().ConfigureServices(x => x.AddMarten(options))
});
}
}
}
2 changes: 2 additions & 0 deletions src/Marten.CommandLine.Testing/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
using Xunit;
[assembly: CollectionBehavior(DisableTestParallelization = true)]
13 changes: 7 additions & 6 deletions src/Marten.CommandLine.Testing/DumpCommandSmokeTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Baseline;
using Marten.CommandLine.Commands.Dump;
using Marten.Testing.Documents;
Expand All @@ -12,7 +13,7 @@ namespace Marten.CommandLine.Testing
public class DumpCommandSmokeTests
{
[Fact]
public void can_clean_repeatedly_for_directory()
public async Task can_clean_repeatedly_for_directory()
{
var options = new StoreOptions();
options.Connection(ConnectionSource.ConnectionString);
Expand All @@ -27,15 +28,15 @@ public void can_clean_repeatedly_for_directory()

};

new DumpCommand().Execute(input);
await new DumpCommand().Execute(input);
Thread.Sleep(100); // Let the file system calm down

input.HostBuilder = new HostBuilder().ConfigureServices(x => x.AddMarten(options));
new DumpCommand().Execute(input);
await new DumpCommand().Execute(input);
}

[Fact]
public void can_clean_repeatedly_for_file()
public async Task can_clean_repeatedly_for_file()
{
var options = new StoreOptions();
options.Connection(ConnectionSource.ConnectionString);
Expand All @@ -52,11 +53,11 @@ public void can_clean_repeatedly_for_file()

};

new DumpCommand().Execute(input);
await new DumpCommand().Execute(input);
Thread.Sleep(100); // Let the file system calm down

input.HostBuilder = new HostBuilder().ConfigureServices(x => x.AddMarten(options));
new DumpCommand().Execute(input);
await new DumpCommand().Execute(input);
}
}
}
35 changes: 35 additions & 0 deletions src/Marten.CommandLine.Testing/PatchCommandSmokeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.IO;
using System.Reflection.Metadata;
using System.Threading.Tasks;
using Baseline;
using Marten.CommandLine.Commands.Patch;
using Marten.Testing.Documents;
using Marten.Testing.Harness;
using Microsoft.Extensions.Hosting;
using Xunit;

namespace Marten.CommandLine.Testing
{
public class PatchCommandSmokeTests
{

[Fact]
public async Task can_write_both_files()
{
var options = new StoreOptions();
options.Connection(ConnectionSource.ConnectionString);
options.Schema.For<User>();
options.Schema.For<Target>();

var input = new PatchInput()
{
HostBuilder = new HostBuilder().ConfigureServices(x => x.AddMarten(options)),
FileName = Path.GetTempPath().AppendPath("dump1.sql"),


};

await new PatchCommand().Execute(input);
}
}
}
5 changes: 3 additions & 2 deletions src/Marten.CommandLine/Commands/ApplyCommand.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Threading.Tasks;
using Oakton;

namespace Marten.CommandLine.Commands
{
[Description("Applies all outstanding changes to the database based on the current configuration", Name = "marten-apply")]
public class ApplyCommand: MartenCommand<MartenInput>
{
protected override bool execute(IDocumentStore store, MartenInput input)
protected override async Task<bool> execute(IDocumentStore store, MartenInput input)
{
try
{
store.Schema.ApplyAllConfiguredChangesToDatabase();
await store.Schema.ApplyAllConfiguredChangesToDatabase();

input.WriteLine(ConsoleColor.Green, "Successfully applied outstanding database changes");
return true;
Expand Down
5 changes: 3 additions & 2 deletions src/Marten.CommandLine/Commands/AssertCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading.Tasks;
using Marten.Exceptions;
using Marten.Schema;
using Oakton;
Expand All @@ -8,11 +9,11 @@ namespace Marten.CommandLine.Commands
[Description("Assert that the existing database matches the current Marten configuration", Name = "marten-assert")]
public class AssertCommand: MartenCommand<MartenInput>
{
protected override bool execute(IDocumentStore store, MartenInput input)
protected override async Task<bool> execute(IDocumentStore store, MartenInput input)
{
try
{
store.Schema.AssertDatabaseMatchesConfiguration();
await store.Schema.AssertDatabaseMatchesConfiguration();

input.WriteLine(ConsoleColor.Green, "No database differences detected.");

Expand Down
Loading

0 comments on commit d324906

Please sign in to comment.