-
Notifications
You must be signed in to change notification settings - Fork 0
Sprint 3 Parte 2: AllowedCities Admin CRUD + Automações + Integração Providers ↔ ServiceCatalogs #71
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
Merged
Merged
Sprint 3 Parte 2: AllowedCities Admin CRUD + Automações + Integração Providers ↔ ServiceCatalogs #71
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit
Hold shift + click to select a range
ef5414a
docs(roadmap): atualizar para Sprint 3 Parte 2
a57c34c
feat(locations): adicionar endpoints CRUD admin para AllowedCities
313a622
wip: migrar Commands/Queries/Handlers para implementação própria do M…
b0cb72f
test(locations): adicionar testes unitários e de integração para Allo…
ce1f3fa
test(locations): adicionar testes E2E para endpoints AllowedCities
2f81213
fix(locations): registrar handlers no DI container e corrigir parâmet…
4cbde3a
refactor(locations): IbgeService agora usa banco de dados ao invés de…
ea1a963
feat(locations): implementar exception handling com status codes HTTP…
0c48b7a
fix(locations): remover 15 warnings do módulo Locations
0f7ff0a
docs(roadmap): atualizar progresso do Sprint 3 Parte 2
d1ce745
fix: corrigir erros de compilação e exception handling em E2E tests
c21e2c3
docs: adicionar tarefas originais do roadmap à Sprint 3 Parte 4
e8683c0
refactor: substituir NSubstitute por Moq para padronização
0a44810
refactor: substituir Guid.CreateVersion7() por UuidGenerator.NewId()
1de5dc1
build: migrar solução para formato .slnx (.NET 9+)
ae6ef2d
feat(docs): automação completa de OpenAPI e GitHub Pages
ae65929
feat(scripts): adicionar scripts de seeding de dados
fe5a964
feat(shared): implementar seeding automático de dados de desenvolvimento
3d2b260
feat(aspire): migrar migrations para Aspire AppHost e remover Migrati…
53943da
feat(providers): implementar integração Providers ↔ ServiceCatalogs
32f854a
Merge branch 'master' into sprint3-parte2-admin-endpoints
e334c4d
fix: resolve build errors and code quality issues
7b37dc6
docs(roadmap): atualizar Sprint 3 como 100% completo
f4fac92
fix: ajustes finais em testes e cache service
fe94741
fix: corrigir assinaturas dos métodos de seeding
fe216b9
fix: corrigir erros de compilação e formatar código
5c16c0c
fix: implementar tuple (value, isCached) no ICacheService.GetAsync e …
7c2c0ea
fix: implementar melhorias do PR review
27e9113
feat: adicionar Bruno Collections completas para ServiceCatalogs
b09fcc8
fix: resolver feedback de code review e testes de integração
9bfa947
fix: usar idMap nos services + documentar scripts de coverage
b0b9470
docs: auditoria completa e documentação de todos os scripts
ef3aa47
refactor: simplificar scripts - remover redundâncias e over-engineering
afb3b17
refactor: simplificar infrastructure - remover redundâncias
18550b0
fix: aplicar correções de code review
618d193
chore: aplicar dotnet format e remover documento de trabalho
5b0d74a
fix: corrigir issues de code review
c5f79b4
fix(ci): corrigir falso positivo no check de formatação
fa50798
fix(ci): corrigir check de formatação em ambos workflows
d8bb00d
refactor: resolve SonarQube warnings (S1135 TODOs, S2139 exception ha…
dd768d0
fix(ci): resolve pr-validation workflow issues - fix trailing whitesp…
2a349f5
fix: apply code review feedback - improve cancellation handling, cach…
302c5e0
docs(roadmap): atualizar Sprint 3 como 100% concluída - todas as 4 pa…
9f7b09a
refactor: remover ExampleSchemaFilter problemático e seus testes
2559cb8
fix: corrigir erros de compilação críticos
5543174
fix: aplicar correções do code review
311a6fe
refactor: remover overengineering - ModuleTagsDocumentFilter e CacheW…
2e9e085
fix(ci): focar dotnet format apenas em style (não code quality analyz…
72115b6
fix(tests): UserProfileUpdatedDomainEventHandler deve re-lançar exceç…
6ca7dcc
fix(tests): Providers event handlers devem re-lançar exceção original
c76bc62
fix(code-review): apply GitHub bot suggestions - security, robustness…
4e26c1a
fix(tests): UserProfileUpdatedDomainEventHandler test deve esperar ex…
da56552
fix(code-review): aplicar sugestões críticas do CodeRabbit
0a666cf
fix(build): DapperConnection.HandleDapperError deve retornar exceção
bc2d0e9
fix(warnings): resolve critical SonarQube warnings without pragma sup…
0fd9c0c
fix(warnings): resolve all actionable warnings without pragma suppres…
0506176
fix(tests): update AzureBlobStorageService test name to match excepti…
8f5ff20
fix(editorconfig): add trailing newline to comply with insert_final_n…
95436e2
fix(code-quality): address security, exception handling, and document…
15d1263
fix(exceptions): preserve exception types to maintain HTTP status cod…
3bfede4
fix(security,performance,tests): prevent PII leakage, optimize token …
ff1fe58
fix(tests): update RequestLoggingMiddleware test for unwrapped except…
f88b246
fix(warnings,migrations,api): resolve build warnings and environment …
c9f268c
fix(security,tests): remove PII from logs and update ArgumentExceptio…
213bf76
fix(auth,tests): use dynamic token expiry and update ArgumentExceptio…
29543fd
fix(auth,security): correct HybridCache factory signature and prevent…
8b5d0f4
fix(auth,security): correct HybridCache factory signature and prevent…
3b67333
refactor(auth): simplify HybridCache factory with async ValueTask lambda
184ce63
fix(auth): use dynamic token expiration and improve cache factory sig…
3cbb5f8
fix(auth): remove redundant token fetch before cache check
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat(aspire): migrar migrations para Aspire AppHost e remover Migrati…
…onTool - MigrationExtensions.cs no AppHost/Extensions: - WithMigrations() extension method para PostgreSQL resources - MigrationHostedService roda migrations na inicialização - Descobre todos os DbContexts automaticamente via reflection - Lê connection string de variáveis de ambiente Aspire - Logging detalhado de progresso - Retry automático em caso de falha - Integrado em Program.cs: - Development: postgresql.MainDatabase.WithMigrations() - Testing: postgresql.MainDatabase.WithMigrations() - Removida pasta tools/MigrationTool (obsoleta) Vantagens sobre MigrationTool: - Integração nativa com Aspire (WaitFor, recursos) - Connection strings do Aspire (sem hardcoding) - Logs no Aspire Dashboard - Execução automática na ordem correta - Sem necessidade de script separado Migration agora é parte da orquestração Aspire
- Loading branch information
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
196 changes: 196 additions & 0 deletions
196
src/Aspire/MeAjudaAi.AppHost/Extensions/MigrationExtensions.cs
This file contains hidden or 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,196 @@ | ||
| using System.Reflection; | ||
| using Aspire.Hosting; | ||
| using Aspire.Hosting.ApplicationModel; | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace MeAjudaAi.AppHost.Extensions; | ||
|
|
||
| /// <summary> | ||
| /// Extensões para aplicar migrations automaticamente no Aspire | ||
| /// </summary> | ||
| public static class MigrationExtensions | ||
| { | ||
| /// <summary> | ||
| /// Adiciona e executa migrations de todos os módulos antes de iniciar a aplicação | ||
| /// </summary> | ||
| public static IResourceBuilder<T> WithMigrations<T>( | ||
| this IResourceBuilder<T> builder) where T : IResourceWithConnectionString | ||
| { | ||
| builder.ApplicationBuilder.Services.AddHostedService<MigrationHostedService>(); | ||
| return builder; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Hosted service que roda migrations na inicialização do AppHost | ||
| /// </summary> | ||
| internal class MigrationHostedService : IHostedService | ||
| { | ||
| private readonly ILogger<MigrationHostedService> _logger; | ||
| private readonly IServiceProvider _serviceProvider; | ||
|
|
||
| public MigrationHostedService( | ||
| ILogger<MigrationHostedService> logger, | ||
| IServiceProvider serviceProvider) | ||
| { | ||
| _logger = logger; | ||
| _serviceProvider = serviceProvider; | ||
| } | ||
|
|
||
| public async Task StartAsync(CancellationToken cancellationToken) | ||
| { | ||
| _logger.LogInformation("🔄 Iniciando migrations de todos os módulos..."); | ||
|
|
||
| try | ||
| { | ||
| var connectionString = GetConnectionString(); | ||
| if (string.IsNullOrEmpty(connectionString)) | ||
| { | ||
| _logger.LogWarning("⚠️ Connection string não encontrada, pulando migrations"); | ||
| return; | ||
| } | ||
|
frigini marked this conversation as resolved.
|
||
|
|
||
| var dbContextTypes = DiscoverDbContextTypes(); | ||
| _logger.LogInformation("📋 Encontrados {Count} DbContexts para migração", dbContextTypes.Count); | ||
|
|
||
| foreach (var contextType in dbContextTypes) | ||
| { | ||
| await MigrateDbContextAsync(contextType, connectionString, cancellationToken); | ||
| } | ||
|
|
||
| _logger.LogInformation("✅ Todas as migrations foram aplicadas com sucesso!"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "❌ Erro ao aplicar migrations"); | ||
| throw; | ||
| } | ||
|
frigini marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
|
||
| private string? GetConnectionString() | ||
| { | ||
| // Tenta obter de variáveis de ambiente (padrão Aspire) | ||
| var host = Environment.GetEnvironmentVariable("POSTGRES_HOST") | ||
| ?? Environment.GetEnvironmentVariable("DB_HOST") | ||
| ?? "localhost"; | ||
| var port = Environment.GetEnvironmentVariable("POSTGRES_PORT") | ||
| ?? Environment.GetEnvironmentVariable("DB_PORT") | ||
| ?? "5432"; | ||
| var database = Environment.GetEnvironmentVariable("POSTGRES_DB") | ||
| ?? Environment.GetEnvironmentVariable("MAIN_DATABASE") | ||
| ?? "meajudaai"; | ||
| var username = Environment.GetEnvironmentVariable("POSTGRES_USER") | ||
| ?? Environment.GetEnvironmentVariable("DB_USERNAME") | ||
| ?? "postgres"; | ||
| var password = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") | ||
| ?? Environment.GetEnvironmentVariable("DB_PASSWORD") | ||
| ?? "test123"; | ||
|
|
||
| return $"Host={host};Port={port};Database={database};Username={username};Password={password}"; | ||
| } | ||
|
frigini marked this conversation as resolved.
|
||
|
|
||
| private List<Type> DiscoverDbContextTypes() | ||
| { | ||
| var dbContextTypes = new List<Type>(); | ||
| var assemblies = AppDomain.CurrentDomain.GetAssemblies() | ||
| .Where(a => a.FullName?.Contains("MeAjudaAi.Modules") == true) | ||
| .ToList(); | ||
|
|
||
| foreach (var assembly in assemblies) | ||
| { | ||
| try | ||
| { | ||
| var types = assembly.GetTypes() | ||
| .Where(t => t.IsClass && !t.IsAbstract && typeof(DbContext).IsAssignableFrom(t)) | ||
| .Where(t => t.Name.EndsWith("DbContext")) | ||
| .ToList(); | ||
|
|
||
| dbContextTypes.AddRange(types); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogWarning(ex, "⚠️ Erro ao descobrir tipos no assembly {AssemblyName}", assembly.FullName); | ||
| } | ||
| } | ||
|
|
||
| return dbContextTypes; | ||
| } | ||
|
frigini marked this conversation as resolved.
|
||
|
|
||
| private async Task MigrateDbContextAsync(Type contextType, string connectionString, CancellationToken cancellationToken) | ||
| { | ||
| var moduleName = ExtractModuleName(contextType); | ||
| _logger.LogInformation("🔧 Aplicando migrations para {Module}...", moduleName); | ||
|
|
||
| try | ||
| { | ||
| // Criar DbContextOptionsBuilder dinamicamente | ||
| var optionsBuilderType = typeof(DbContextOptionsBuilder<>).MakeGenericType(contextType); | ||
| var optionsBuilder = Activator.CreateInstance(optionsBuilderType) as DbContextOptionsBuilder; | ||
|
|
||
| if (optionsBuilder == null) | ||
| { | ||
| throw new InvalidOperationException($"Não foi possível criar DbContextOptionsBuilder para {contextType.Name}"); | ||
| } | ||
|
|
||
| // Configurar PostgreSQL | ||
| optionsBuilder.UseNpgsql(connectionString, npgsqlOptions => | ||
| { | ||
| npgsqlOptions.MigrationsAssembly(contextType.Assembly.FullName); | ||
| npgsqlOptions.EnableRetryOnFailure(maxRetryCount: 3); | ||
| }); | ||
|
|
||
| // Criar instância do DbContext | ||
| var context = Activator.CreateInstance(contextType, optionsBuilder.Options) as DbContext; | ||
|
|
||
| if (context == null) | ||
| { | ||
| throw new InvalidOperationException($"Não foi possível criar instância de {contextType.Name}"); | ||
| } | ||
|
frigini marked this conversation as resolved.
frigini marked this conversation as resolved.
|
||
|
|
||
| using (context) | ||
| { | ||
| // Aplicar migrations | ||
| var pendingMigrations = (await context.Database.GetPendingMigrationsAsync(cancellationToken)).ToList(); | ||
|
|
||
| if (pendingMigrations.Any()) | ||
| { | ||
| _logger.LogInformation("📦 {Module}: {Count} migrations pendentes", moduleName, pendingMigrations.Count); | ||
| foreach (var migration in pendingMigrations) | ||
| { | ||
| _logger.LogDebug(" - {Migration}", migration); | ||
| } | ||
|
|
||
| await context.Database.MigrateAsync(cancellationToken); | ||
| _logger.LogInformation("✅ {Module}: Migrations aplicadas com sucesso", moduleName); | ||
| } | ||
| else | ||
| { | ||
| _logger.LogInformation("✓ {Module}: Nenhuma migration pendente", moduleName); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| _logger.LogError(ex, "❌ Erro ao aplicar migrations para {Module}", moduleName); | ||
| throw; | ||
| } | ||
| } | ||
|
|
||
| private string ExtractModuleName(Type contextType) | ||
| { | ||
| // Extrai nome do módulo do namespace (ex: MeAjudaAi.Modules.Users.Infrastructure.Persistence.UsersDbContext -> Users) | ||
| var namespaceParts = contextType.Namespace?.Split('.') ?? Array.Empty<string>(); | ||
| var moduleIndex = Array.IndexOf(namespaceParts, "Modules"); | ||
|
|
||
| if (moduleIndex >= 0 && moduleIndex + 1 < namespaceParts.Length) | ||
| { | ||
| return namespaceParts[moduleIndex + 1]; | ||
| } | ||
|
|
||
| return contextType.Name.Replace("DbContext", ""); | ||
| } | ||
| } | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.