feat: Aggiunta gestione nome database sorgente nei profili DataCoupler

 Nuove funzionalità:
- Aggiunto campo SourceDatabaseName nella tabella DataCouplerProfiles
- Implementato recupero automatico del nome database dalle credenziali
- Migliorata applicazione profili con supporto database specifico
- Aggiornata logica di connessione database con selezione database

🔧 Modifiche tecniche:
- Aggiunta migration per colonna SourceDatabaseName
- Estesi modelli DataCouplerProfile e DataCouplerProfileDto
- Aggiornato DataCouplerProfileService per gestire nuovo campo
- Modificato ProfileSaver per recupero automatico database name
- Implementato metodo ConnectToDatabaseWithSpecificDatabase

🐛 Correzioni:
- Migliorata gestione connessioni database multi-database
- Corretta formattazione codice e spaziature
- Rimosse linee vuote eccessive nel codice sorgente

🧪
This commit is contained in:
2025-07-05 18:10:09 +02:00
parent 65ed2bb93a
commit 7d2961702c
21 changed files with 1002 additions and 367 deletions
+20
View File
@@ -44,6 +44,22 @@
</div>
</div>
<!-- Fonte -->
<div class="mb-3">
<label class="form-label">Fonte</label>
<div class="bg-light p-3 rounded">
@* Contenuto esistente per la fonte *@
</div>
</div>
<!-- Destinazione -->
<div class="mb-3">
<label class="form-label">Destinazione</label>
<div class="bg-light p-3 rounded">
@* Contenuto esistente per la destinazione *@
</div>
</div>
@if (!string.IsNullOrEmpty(SaveMessage))
{
<div class="alert alert-@(SaveMessageType) mb-3">
@@ -63,6 +79,10 @@
{
<span class="text-muted">Credenziali: @SourceCredentialName</span><br />
}
@if (!string.IsNullOrEmpty(SourceDatabaseName))
{
<span class="text-muted">Database: @SourceDatabaseName <em>(dalla connessione attiva)</em></span><br />
}
@if (!string.IsNullOrEmpty(SourceSchema))
{
<span class="text-muted">Schema: @SourceSchema</span><br />
+36
View File
@@ -1,15 +1,19 @@
using Microsoft.AspNetCore.Components;
using CredentialManager.Models;
using CredentialManager.Services;
using System.ComponentModel.DataAnnotations;
namespace Components;
public partial class ProfileSaver
{
[Inject] private ICredentialService CredentialService { get; set; } = default!;
[Parameter] public bool CanSave { get; set; }
[Parameter] public string SourceType { get; set; } = "";
[Parameter] public int? SourceCredentialId { get; set; }
[Parameter] public string? SourceCredentialName { get; set; }
[Parameter] public string? SourceDatabaseName { get; set; }
[Parameter] public string? SourceSchema { get; set; }
[Parameter] public string? SourceTable { get; set; }
[Parameter] public string? SourceFilePath { get; set; }
@@ -51,6 +55,9 @@ public partial class ProfileSaver
try
{
// Recupera automaticamente il nome del database dalla connessione attiva
var sourceDatabaseName = await GetSourceDatabaseNameAsync();
var profileDto = new DataCouplerProfileDto
{
Name = ProfileData.Name,
@@ -58,6 +65,7 @@ public partial class ProfileSaver
SourceType = SourceType,
SourceCredentialId = SourceCredentialId,
SourceCredentialName = SourceCredentialName,
SourceDatabaseName = sourceDatabaseName,
SourceSchema = SourceSchema,
SourceTable = SourceTable,
SourceFilePath = SourceFilePath,
@@ -119,6 +127,34 @@ public partial class ProfileSaver
SaveMessageType = type;
}
private async Task<string?> GetSourceDatabaseNameAsync()
{
// Prima priorità: se SourceDatabaseName è già impostato come parametro, usa quello
if (!string.IsNullOrEmpty(SourceDatabaseName))
{
return SourceDatabaseName;
}
// Seconda priorità: se abbiamo un SourceCredentialId, recupera il database dalle credenziali
if (SourceCredentialId.HasValue)
{
try
{
var credential = await CredentialService.GetDatabaseCredentialAsync(SourceCredentialId.Value);
if (credential != null && !string.IsNullOrEmpty(credential.DatabaseName))
{
return credential.DatabaseName;
}
}
catch (Exception)
{
// Se non riesce a recuperare le credenziali, continua con null
}
}
return null;
}
public class ProfileFormModel
{
[Required(ErrorMessage = "Il nome del profilo è obbligatorio")]
@@ -0,0 +1,337 @@
// <auto-generated />
using System;
using CredentialManager.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace CredentialManager.Migrations
{
[DbContext(typeof(CredentialDbContext))]
[Migration("20250704135720_AddSourceDatabaseNameColumn")]
partial class AddSourceDatabaseNameColumn
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "9.0.0");
modelBuilder.Entity("CredentialManager.Models.CredentialEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("AdditionalParameters")
.HasMaxLength(2000)
.HasColumnType("TEXT");
b.Property<int>("CommandTimeout")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(30);
b.Property<string>("ConnectionString")
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("CreatedBy")
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<string>("DatabaseName")
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<string>("DatabaseType")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<string>("EncryptedApiKey")
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<string>("EncryptedAuthToken")
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<string>("EncryptedPassword")
.HasColumnType("TEXT");
b.Property<string>("Headers")
.HasMaxLength(2000)
.HasColumnType("TEXT");
b.Property<string>("Host")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<bool>("IgnoreSslErrors")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(false);
b.Property<bool>("IsActive")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(true);
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<int?>("Port")
.HasColumnType("INTEGER");
b.Property<string>("RestServiceType")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<int>("TimeoutSeconds")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(100);
b.Property<string>("Type")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("TEXT");
b.Property<string>("Username")
.HasMaxLength(100)
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("DatabaseType");
b.HasIndex("IsActive");
b.HasIndex("Name")
.IsUnique();
b.HasIndex("Type");
b.ToTable("Credentials", (string)null);
});
modelBuilder.Entity("CredentialManager.Models.DataCouplerProfile", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<DateTime>("CreatedAt")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT")
.HasDefaultValueSql("CURRENT_TIMESTAMP");
b.Property<string>("CreatedBy")
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<string>("Description")
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<int?>("DestinationCredentialId")
.HasColumnType("INTEGER");
b.Property<string>("DestinationEndpoint")
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<string>("DestinationSchema")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("DestinationTable")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("DestinationType")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("TEXT");
b.Property<string>("FieldMappingJson")
.HasMaxLength(4000)
.HasColumnType("TEXT");
b.Property<bool>("IsActive")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(true);
b.Property<DateTime?>("LastUsedAt")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<int?>("SourceCredentialId")
.HasColumnType("INTEGER");
b.Property<string>("SourceDatabaseName")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("SourceFilePath")
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<string>("SourceKeyField")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("SourceSchema")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("SourceTable")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("SourceType")
.IsRequired()
.HasMaxLength(20)
.HasColumnType("TEXT");
b.Property<bool>("UseRecordAssociations")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.HasIndex("CreatedAt");
b.HasIndex("DestinationCredentialId");
b.HasIndex("DestinationType");
b.HasIndex("IsActive");
b.HasIndex("LastUsedAt");
b.HasIndex("Name")
.IsUnique();
b.HasIndex("SourceCredentialId");
b.HasIndex("SourceType");
b.ToTable("DataCouplerProfiles", (string)null);
});
modelBuilder.Entity("CredentialManager.Models.KeyAssociation", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("AdditionalInfo")
.HasMaxLength(2000)
.HasColumnType("TEXT");
b.Property<DateTime>("CreatedAt")
.HasColumnType("TEXT");
b.Property<string>("DestinationEntity")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("DestinationId")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("DestinationKeyField")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<bool>("IsActive")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(true);
b.Property<string>("KeyValue")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("TEXT");
b.Property<DateTime?>("LastVerifiedAt")
.HasColumnType("TEXT");
b.Property<string>("RestCredentialName")
.IsRequired()
.HasMaxLength(100)
.HasColumnType("TEXT");
b.Property<string>("SourceKeyField")
.IsRequired()
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("SourcesInfo")
.HasMaxLength(2000)
.HasColumnType("TEXT");
b.Property<DateTime?>("UpdatedAt")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("CreatedAt");
b.HasIndex("DestinationEntity");
b.HasIndex("IsActive");
b.HasIndex("KeyValue")
.HasDatabaseName("IX_KeyAssociations_KeyValue");
b.HasIndex("LastVerifiedAt");
b.HasIndex("RestCredentialName");
b.HasIndex("KeyValue", "DestinationEntity", "RestCredentialName")
.IsUnique()
.HasDatabaseName("IX_KeyAssociations_Unique");
b.ToTable("KeyAssociations", (string)null);
});
modelBuilder.Entity("CredentialManager.Models.DataCouplerProfile", b =>
{
b.HasOne("CredentialManager.Models.CredentialEntity", "DestinationCredential")
.WithMany()
.HasForeignKey("DestinationCredentialId")
.OnDelete(DeleteBehavior.SetNull);
b.HasOne("CredentialManager.Models.CredentialEntity", "SourceCredential")
.WithMany()
.HasForeignKey("SourceCredentialId")
.OnDelete(DeleteBehavior.SetNull);
b.Navigation("DestinationCredential");
b.Navigation("SourceCredential");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace CredentialManager.Migrations
{
/// <inheritdoc />
public partial class AddSourceDatabaseNameColumn : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "SourceDatabaseName",
table: "DataCouplerProfiles",
type: "TEXT",
maxLength: 200,
nullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "SourceDatabaseName",
table: "DataCouplerProfiles");
}
}
}
@@ -182,6 +182,10 @@ namespace CredentialManager.Migrations
b.Property<int?>("SourceCredentialId")
.HasColumnType("INTEGER");
b.Property<string>("SourceDatabaseName")
.HasMaxLength(200)
.HasColumnType("TEXT");
b.Property<string>("SourceFilePath")
.HasMaxLength(500)
.HasColumnType("TEXT");
@@ -25,6 +25,9 @@ public class DataCouplerProfile
public int? SourceCredentialId { get; set; }
[MaxLength(200)]
public string? SourceDatabaseName { get; set; }
[MaxLength(200)]
public string? SourceSchema { get; set; }
@@ -13,6 +13,7 @@ public class DataCouplerProfileDto
public string SourceType { get; set; } = string.Empty;
public int? SourceCredentialId { get; set; }
public string? SourceCredentialName { get; set; }
public string? SourceDatabaseName { get; set; }
public string? SourceSchema { get; set; }
public string? SourceTable { get; set; }
public string? SourceFilePath { get; set; }
@@ -214,6 +214,7 @@ public class DataCouplerProfileService : IDataCouplerProfileService
SourceType = profile.SourceType,
SourceCredentialId = profile.SourceCredentialId,
SourceCredentialName = profile.SourceCredential?.Name,
SourceDatabaseName = profile.SourceDatabaseName,
SourceSchema = profile.SourceSchema,
SourceTable = profile.SourceTable,
SourceFilePath = profile.SourceFilePath,
@@ -241,6 +242,7 @@ public class DataCouplerProfileService : IDataCouplerProfileService
Description = dto.Description,
SourceType = dto.SourceType,
SourceCredentialId = dto.SourceCredentialId,
SourceDatabaseName = dto.SourceDatabaseName,
SourceSchema = dto.SourceSchema,
SourceTable = dto.SourceTable,
SourceFilePath = dto.SourceFilePath,
Binary file not shown.
+1
View File
@@ -1005,6 +1005,7 @@
SourceType="@selectedSourceType"
SourceCredentialId="@(GetCurrentSourceCredentialIdAsync().Result)"
SourceCredentialName="@selectedDatabaseCredential"
SourceDatabaseName="@selectedDatabase"
SourceSchema="@GetCurrentDatabaseSchema()"
SourceTable="@(useCustomQuery ? "custom_query" : selectedTable)"
SourceFilePath="@selectedFileName"
File diff suppressed because it is too large Load Diff
Binary file not shown.
Binary file not shown.
Binary file not shown.
+53
View File
@@ -0,0 +1,53 @@
using CredentialManager.Data;
using CredentialManager.Models;
using CredentialManager.Services;
using Microsoft.EntityFrameworkCore;
Console.WriteLine("🧪 Testing SourceDatabaseName retrieval from credentials...");
// Configurazione del database temporaneo
var options = new DbContextOptionsBuilder<CredentialDbContext>()
.UseSqlite("Data Source=test_credential_db.db")
.Options;
using var context = new CredentialDbContext(options);
await context.Database.EnsureCreatedAsync();
var credentialService = new CredentialService(context);
// Test 1: Crea una credenziale database con nome database
var testCredential = new DatabaseCredential
{
Name = "TestDatabaseCredential",
DatabaseType = "SqlServer",
Host = "localhost",
Port = 1433,
DatabaseName = "MyProductionDB",
Username = "testuser",
Password = "testpassword"
};
Console.WriteLine($"📝 Creando credenziale con DatabaseName: {testCredential.DatabaseName}");
var credentialId = await credentialService.SaveDatabaseCredentialAsync(testCredential);
Console.WriteLine($"✅ Credenziale salvata con ID: {credentialId}");
// Test 2: Recupera la credenziale
var retrievedCredential = await credentialService.GetDatabaseCredentialAsync(credentialId);
Console.WriteLine($"✅ Credenziale recuperata: {retrievedCredential?.Name}");
Console.WriteLine($" DatabaseName: {retrievedCredential?.DatabaseName}");
// Test 3: Simula il recupero del database name come farebbe ProfileSaver
if (retrievedCredential != null && !string.IsNullOrEmpty(retrievedCredential.DatabaseName))
{
Console.WriteLine($"✅ SUCCESSO: DatabaseName recuperato dalle credenziali: {retrievedCredential.DatabaseName}");
}
else
{
Console.WriteLine("❌ ERRORE: DatabaseName non recuperato dalle credenziali");
}
// Pulizia
await context.Database.EnsureDeletedAsync();
Console.WriteLine("🧹 Database temporaneo eliminato");
Console.WriteLine("\n🎯 Test completato con successo!");
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CredentialManager\CredentialManager.csproj" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
</ItemGroup>
</Project>
+62
View File
@@ -0,0 +1,62 @@
using CredentialManager.Data;
using CredentialManager.Models;
using CredentialManager.Services;
using Microsoft.EntityFrameworkCore;
Console.WriteLine("🧪 Testing SourceDatabaseName database persistence...");
// Configurazione del database temporaneo
var options = new DbContextOptionsBuilder<CredentialDbContext>()
.UseSqlite("Data Source=test_sourcedatabase.db")
.Options;
using var context = new CredentialDbContext(options);
await context.Database.EnsureCreatedAsync();
var profileService = new DataCouplerProfileService(context);
// Test: Creazione e salvataggio di un profilo con SourceDatabaseName
var testProfile = new DataCouplerProfile
{
Name = "Test Profile DB",
Description = "Test per verificare il salvataggio del SourceDatabaseName",
SourceType = "database",
SourceDatabaseName = "MyProductionDatabase",
SourceSchema = "dbo",
SourceTable = "customers",
DestinationType = "rest",
DestinationEndpoint = "/api/customers",
CreatedBy = "TestUser"
};
Console.WriteLine($"📝 Creando profilo con SourceDatabaseName: {testProfile.SourceDatabaseName}");
// Salvataggio nel database
var savedProfile = await profileService.SaveProfileAsync(testProfile);
Console.WriteLine($"✅ Profilo salvato con ID: {savedProfile.Id}");
// Recupero dal database
var retrievedProfile = await profileService.GetProfileByIdAsync(savedProfile.Id);
Console.WriteLine($"✅ Profilo recuperato dal database");
// Verifica che il SourceDatabaseName sia stato salvato e recuperato correttamente
if (retrievedProfile != null && retrievedProfile.SourceDatabaseName == testProfile.SourceDatabaseName)
{
Console.WriteLine($"✅ SUCCESSO: SourceDatabaseName salvato e recuperato correttamente: {retrievedProfile.SourceDatabaseName}");
}
else
{
Console.WriteLine($"❌ ERRORE: SourceDatabaseName non salvato correttamente");
Console.WriteLine($" Originale: {testProfile.SourceDatabaseName}");
Console.WriteLine($" Recuperato: {retrievedProfile?.SourceDatabaseName ?? "NULL"}");
}
// Test conversione DTO
var dto = profileService.ToDto(retrievedProfile!);
Console.WriteLine($"✅ DTO convertito con SourceDatabaseName: {dto.SourceDatabaseName}");
// Pulizia
await context.Database.EnsureDeletedAsync();
Console.WriteLine("🧹 Database temporaneo eliminato");
Console.WriteLine("\n🎯 Test completato con successo!");
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CredentialManager\CredentialManager.csproj" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.0" />
</ItemGroup>
</Project>