-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Perform migration in the app host in local dev scenarios
- Add an executable resource if the database directory does not exist.
- Loading branch information
Showing
3 changed files
with
40 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
internal static class TodoApiEfMigrationsExtensions | ||
{ | ||
public static IResourceBuilder<ExecutableResource>? AddTodoDbMigration(this IDistributedApplicationBuilder builder) | ||
{ | ||
IResourceBuilder<ExecutableResource>? migrateOperation = default; | ||
|
||
if (builder.ExecutionContext.IsRunMode) | ||
{ | ||
var projectDirectory = Path.GetDirectoryName(new Projects.TodoApi().ProjectPath)!; | ||
var dbDirectory = Path.Combine(projectDirectory, ".db"); | ||
|
||
if (!Directory.Exists(dbDirectory)) | ||
{ | ||
Directory.CreateDirectory(dbDirectory); | ||
|
||
migrateOperation = builder.AddEfMigration<Projects.TodoApi>("todo-db-migration"); | ||
} | ||
} | ||
|
||
return migrateOperation; | ||
} | ||
|
||
public static IResourceBuilder<ExecutableResource> AddEfMigration<TProject>(this IDistributedApplicationBuilder builder, string name) | ||
where TProject : IProjectMetadata, new() | ||
{ | ||
var projectDirectory = Path.GetDirectoryName(new TProject().ProjectPath)!; | ||
|
||
// TODO: Support passing a connection string | ||
return builder.AddExecutable(name, "dotnet", projectDirectory, "ef", "database", "update", "--no-build"); | ||
} | ||
} |