Skip to content

Commit da8ef00

Browse files
committed
Seeder project used for seeding
1 parent 3c21250 commit da8ef00

15 files changed

Lines changed: 82 additions & 71 deletions

src/RavenDB.Samples.Library.App/Api.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
using Microsoft.AspNetCore.Http;
22
using Microsoft.AspNetCore.Mvc;
33
using Microsoft.Azure.Functions.Worker;
4-
using Microsoft.Extensions.Configuration;
54
using Microsoft.Extensions.Logging;
65
using Raven.Client.Documents;
76
using Raven.Client.Documents.Linq;
87
using Raven.Client.Documents.Session;
9-
using Raven.Migrations;
108
using RavenDB.Samples.Library.Model;
119
using RavenDB.Samples.Library.Model.Indexes;
1210

1311
namespace RavenDB.Samples.Library.App;
1412

15-
public class Api(ILogger<Api> logger, IAsyncDocumentSession session, IConfiguration config, MigrationRunner migrations)
13+
public class Api(ILogger<Api> logger, IAsyncDocumentSession session)
1614
{
17-
public const string EnvVarAdminCommandKeyName = "CommandKey";
18-
public const string HeaderAdminCommandKeyName = "X-Command-Key";
1915

2016
[Function(nameof(BooksGetById))]
2117
public async Task<IActionResult> BooksGetById([HttpTrigger("get", Route = "books/{id}")] HttpRequest req, string id)
@@ -146,19 +142,4 @@ public async Task<IActionResult> HomeBooks([HttpTrigger("get", Route = "home/boo
146142
return new JsonResult(results);
147143
}
148144

149-
[Function(nameof(Migrate))]
150-
public async Task<IActionResult> Migrate([HttpTrigger("post", Route = "migrate")] HttpRequest req)
151-
{
152-
var actual = req.Headers[HeaderAdminCommandKeyName];
153-
var expected = config.GetValue<string>(EnvVarAdminCommandKeyName);
154-
155-
if (actual != expected)
156-
{
157-
return new StatusCodeResult(StatusCodes.Status403Forbidden);
158-
}
159-
160-
migrations.Run();
161-
162-
return new StatusCodeResult(StatusCodes.Status202Accepted);
163-
}
164145
}

src/RavenDB.Samples.Library.App/Program.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
using Microsoft.Azure.Functions.Worker.Builder;
33
using Microsoft.Extensions.DependencyInjection;
44
using Microsoft.Extensions.Hosting;
5-
using Raven.Migrations;
6-
using RavenDB.Samples.Library.Setup.Migrations;
75

86
var builder = FunctionsApplication.CreateBuilder(args);
97

@@ -23,23 +21,9 @@
2321
builder.ConfigureFunctionsWebApplication();
2422

2523
// https://learn.microsoft.com/en-us/dotnet/aspire/community-toolkit/ravendb?tabs=dotnet-cli#client-integration
26-
builder.AddRavenDBClient("library",
27-
settings =>
28-
{
29-
settings.CreateDatabase = true;
30-
});
31-
32-
33-
// TODO: extract from the value under container. As RavenDB is used in a container, the wiring is different from the app.
34-
var connection = "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint=http://storage:10001/devstoreaccount1;";
35-
builder.Services.AddSingleton(new MigrationContext(connection));
24+
builder.AddRavenDBClient("library");
3625

3726
builder.Services
3827
.AddOpenTelemetry().WithLogging();
39-
builder.Services
40-
.AddRavenDbMigrations(migrations =>
41-
{
42-
migrations.Assemblies = [typeof(ImportGoodBooks).Assembly];
43-
});
4428

4529
builder.Build().Run();

src/RavenDB.Samples.Library.App/RavenDB.Samples.Library.App.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<ItemGroup>
1313
<FrameworkReference Include="Microsoft.AspNetCore.App"/>
1414
<PackageReference Include="CommunityToolkit.Aspire.RavenDB.Client" />
15-
<PackageReference Include="RavenMigrations" />
1615
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService"/>
1716
<PackageReference Include="Microsoft.Azure.Functions.Worker" />
1817
<PackageReference Include="Microsoft.Azure.Functions.Worker.ApplicationInsights" />
@@ -23,7 +22,6 @@
2322

2423
<ItemGroup>
2524
<ProjectReference Include="..\RavenDB.Samples.Library.Model\RavenDB.Samples.Library.Model.csproj" />
26-
<ProjectReference Include="..\RavenDB.Samples.Library.Setup\RavenDB.Samples.Library.Setup.csproj" />
2725
</ItemGroup>
2826

2927
</Project>

src/RavenDB.Samples.Library.AppHost/AppHost.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,39 +33,27 @@
3333
var db = ravenDbServer
3434
.AddDatabase(dbName);
3535

36-
const string commandKey = "CommandKey";
37-
38-
var secretKey = builder.AddParameterWithRandomValue(commandKey, secret: true);
36+
// Seeder — runs migrations once, then exits 0; backend waits for completion
37+
var seeder = builder.AddProject<Projects.RavenDB_Samples_Library_Seeder>("seeder")
38+
.WithReference(queues)
39+
.WaitFor(queues)
40+
.WithReference(db)
41+
.WaitFor(db);
3942

4043
// Library App
4144
var functions = builder.AddAzureFunctionsProject<Projects.RavenDB_Samples_Library_App>("app")
4245
.WithHostStorage(storage)
43-
46+
4447
.WithReference(queues)
4548
.WaitFor(queues)
46-
47-
// Required to go separately with as the bindings are not wired with the host storage
49+
50+
// Required to go separately as the bindings are not wired with the host storage
4851
.WithEnvironment("BindingConnection", queues.Resource.ConnectionStringExpression)
49-
52+
5053
.WithReference(db)
5154
.WaitFor(db)
52-
53-
.WithEnvironment(commandKey, secretKey)
54-
.WithHttpCommand(
55-
path: "/api/migrate",
56-
displayName: "Migrate DB",
57-
58-
commandOptions: new HttpCommandOptions
59-
{
60-
Description = "Runs database migrations",
61-
PrepareRequest = async context =>
62-
{
63-
var key = await secretKey.Resource.GetValueAsync(context.CancellationToken);
64-
context.Request.Headers.Add("X-Command-Key", key);
65-
},
66-
IconName = "databaseArrowUp",
67-
IsHighlighted = true
68-
});
55+
56+
.WaitForCompletion(seeder);
6957

7058
// Frontend
7159
var frontend = builder.AddNpmApp("Frontend", "../RavenDB.Samples.Library.Frontend", "dev")

src/RavenDB.Samples.Library.AppHost/RavenDB.Samples.Library.AppHost.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<ItemGroup>
1919
<ProjectReference Include="..\RavenDB.Samples.Library.App\RavenDB.Samples.Library.App.csproj" />
2020
<ProjectReference Include="..\RavenDB.Samples.Library.ServiceDefaults\RavenDB.Samples.Library.ServiceDefaults.csproj" />
21-
<ProjectReference Include="..\RavenDB.Samples.Library.Setup\RavenDB.Samples.Library.Setup.csproj" />
21+
<ProjectReference Include="..\RavenDB.Samples.Library.Seeder\RavenDB.Samples.Library.Seeder.csproj" />
2222
</ItemGroup>
2323

2424

src/RavenDB.Samples.Library.Setup/Migrations/001_ImportGoodbooks.cs renamed to src/RavenDB.Samples.Library.Seeder/Migrations/001_ImportGoodbooks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Raven.Migrations;
1212
using RavenDB.Samples.Library.Model;
1313

14-
namespace RavenDB.Samples.Library.Setup.Migrations;
14+
namespace RavenDB.Samples.Library.Seeder.Migrations;
1515

1616
[Migration(1)]
1717
public sealed class ImportGoodBooks : Migration

src/RavenDB.Samples.Library.Setup/Migrations/002_CreateIndexes.cs renamed to src/RavenDB.Samples.Library.Seeder/Migrations/002_CreateIndexes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Raven.Migrations;
33
using RavenDB.Samples.Library.Model.Indexes;
44

5-
namespace RavenDB.Samples.Library.Setup.Migrations;
5+
namespace RavenDB.Samples.Library.Seeder.Migrations;
66

77
[Migration(2)]
88
public sealed class CreateIndexes : Migration

src/RavenDB.Samples.Library.Setup/Migrations/003_TimeoutManager.cs renamed to src/RavenDB.Samples.Library.Seeder/Migrations/003_TimeoutManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Raven.Migrations;
44
using RavenDB.Samples.Library.Model.Indexes;
55

6-
namespace RavenDB.Samples.Library.Setup.Migrations;
6+
namespace RavenDB.Samples.Library.Seeder.Migrations;
77

88
[Migration(3)]
99
public sealed class TimeoutManagerMigration : Migration

src/RavenDB.Samples.Library.Setup/Migrations/004_ConfigureAzureQueueETL.cs renamed to src/RavenDB.Samples.Library.Seeder/Migrations/004_ConfigureAzureQueueETL.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Raven.Migrations;
55
using RavenDB.Samples.Library.Model;
66

7-
namespace RavenDB.Samples.Library.Setup.Migrations;
7+
namespace RavenDB.Samples.Library.Seeder.Migrations;
88

99
[Migration(4)]
1010
public sealed class ConfigureAzureQueueETL(MigrationContext context) : Migration

src/RavenDB.Samples.Library.Setup/Migrations/MigrationContext.cs renamed to src/RavenDB.Samples.Library.Seeder/Migrations/MigrationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace RavenDB.Samples.Library.Setup.Migrations;
1+
namespace RavenDB.Samples.Library.Seeder.Migrations;
22

33
/// <summary>
44
/// The context passed for the migrations.

0 commit comments

Comments
 (0)