Phase 6 (Stufe 6): MongoDB vollstaendig entfernt
- Legacy-Form stillgelegt: frm_main(.Designer/.resx) + frm_analytics(.Designer/.resx) geloescht, 'Open Legacy'-Menue aus LauncherForm entfernt. - Program.cs: IMongoDatabase-Registrierung + closed_trades-Cleanup weg; Trade-Nummerierung liest jetzt hoechste TradeId aus ICopyTradeLogRepository. - StartupHydrationService: Mongo-/Bson-Fallback entfernt (Settings kommen aus dem Repo; fehlende Accounts erhalten Default-Settings). - Alle Mongo*Repository (Core 4 + Modul 4) geloescht; DI-Zweige auf EF/MySQL reduziert (AddCorePersistence, CopyTradingModule). - ConfigMigrator: Mongo-Pfad (Run/MigrateSettings) entfernt; nur noch RunFromJson + VerifyMySql (kein Mongo-Treiber mehr). - Modelle: [BsonId] entfernt (EF-Keys via HasKey); ObjectId-Defaults -> Guid. - LiteDB-Shim (MongoDbLiteDBShim) geloescht; MongoDB.Driver- + LiteDB-Pakete aus App/Core entfernt; stale usings + user-sichtbare Strings bereinigt. - DatabaseOptions/appsettings.json: Mongo-Provider/Connection-Felder entfernt. - Build gruen; --verify-mysql bestaetigt 3 Accounts/3 Settings/32 Trader/2344 Markets. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
d227d24ca8
commit
b951b912f4
@@ -8,15 +8,7 @@ namespace PolyTrader.Core.Configuration
|
||||
{
|
||||
public const string SectionName = "Database";
|
||||
|
||||
/// <summary>"MySql" (Zielzustand) oder "Mongo" (Übergang/Config-Migration).</summary>
|
||||
public string Provider { get; set; } = "MySql";
|
||||
|
||||
/// <summary>MySQL-Connection-String (aus gitignorierter appsettings.Local.json).</summary>
|
||||
public string MySqlConnectionString { get; set; } = string.Empty;
|
||||
|
||||
// --- MongoDB (nur noch für die einmalige Config-Migration nach MySQL) ---
|
||||
public string ConnectionString { get; set; } = "mongodb://localhost:27017";
|
||||
|
||||
public string DatabaseName { get; set; } = "PolyTraderDB";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,37 +4,25 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using PolyTrader.Core.Configuration;
|
||||
using PolyTrader.Core.Persistence;
|
||||
using PolyTrader.Core.Persistence.Ef;
|
||||
using PolyTrader.Core.Persistence.Mongo;
|
||||
|
||||
namespace PolyTrader.Core.DependencyInjection
|
||||
{
|
||||
public static class ServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registriert die Core-Persistenzschicht hinter den Repository-Interfaces.
|
||||
/// Provider "MySql" -> EF Core (Pomelo) über einen DbContextFactory (thread-safe,
|
||||
/// kurzlebiger Context je Operation). Provider "Mongo" -> Mongo-Implementierungen
|
||||
/// (Übergang / Config-Migration).
|
||||
/// Registriert die Core-Persistenzschicht (EF Core / Pomelo / MySQL) hinter den
|
||||
/// Repository-Interfaces über einen DbContextFactory (thread-safe, kurzlebiger
|
||||
/// Context je Operation).
|
||||
/// </summary>
|
||||
public static IServiceCollection AddCorePersistence(this IServiceCollection services, DatabaseOptions options)
|
||||
{
|
||||
if (string.Equals(options.Provider, "MySql", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var conn = options.MySqlConnectionString;
|
||||
services.AddDbContextFactory<CoreDbContext>(o => o.UseMySql(conn, ServerVersion.AutoDetect(conn)));
|
||||
var conn = options.MySqlConnectionString;
|
||||
services.AddDbContextFactory<CoreDbContext>(o => o.UseMySql(conn, ServerVersion.AutoDetect(conn)));
|
||||
|
||||
services.AddSingleton<IAccountRepository, EfAccountRepository>();
|
||||
services.AddSingleton<IMarketRepository, EfMarketRepository>();
|
||||
services.AddSingleton<IPositionRepository, EfPositionRepository>();
|
||||
services.AddSingleton<ITradeLogRepository, EfTradeLogRepository>();
|
||||
}
|
||||
else
|
||||
{
|
||||
services.AddSingleton<IAccountRepository, MongoAccountRepository>();
|
||||
services.AddSingleton<IMarketRepository, MongoMarketRepository>();
|
||||
services.AddSingleton<IPositionRepository, MongoPositionRepository>();
|
||||
services.AddSingleton<ITradeLogRepository, MongoTradeLogRepository>();
|
||||
}
|
||||
services.AddSingleton<IAccountRepository, EfAccountRepository>();
|
||||
services.AddSingleton<IMarketRepository, EfMarketRepository>();
|
||||
services.AddSingleton<IPositionRepository, EfPositionRepository>();
|
||||
services.AddSingleton<ITradeLogRepository, EfTradeLogRepository>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ namespace PolyTraderSharp.Models
|
||||
public class AccountState
|
||||
{
|
||||
[Browsable(false)]
|
||||
[MongoDB.Bson.Serialization.Attributes.BsonId] public int AccountId { get; set; }
|
||||
public int AccountId { get; set; }
|
||||
|
||||
[Category("01. General")]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
@@ -2,7 +2,7 @@ namespace PolyTraderSharp.Models
|
||||
{
|
||||
public class MarketData
|
||||
{
|
||||
[MongoDB.Bson.Serialization.Attributes.BsonId] public string Id { get; set; } = string.Empty;
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string ConditionId { get; set; } = string.Empty;
|
||||
public string Question { get; set; } = string.Empty;
|
||||
public string Slug { get; set; } = string.Empty;
|
||||
|
||||
@@ -7,7 +7,6 @@ namespace PolyTraderSharp.Models
|
||||
public int AccountId { get; set; }
|
||||
public bool IsDemo { get; set; }
|
||||
|
||||
[MongoDB.Bson.Serialization.Attributes.BsonId]
|
||||
public string TokenId { get; set; } = string.Empty;
|
||||
public string MarketSlug { get; set; } = string.Empty;
|
||||
public string ConditionId { get; set; } = string.Empty;
|
||||
|
||||
@@ -11,8 +11,7 @@ namespace PolyTraderSharp.Models
|
||||
/// </summary>
|
||||
public class TradeRecord
|
||||
{
|
||||
[MongoDB.Bson.Serialization.Attributes.BsonId]
|
||||
public string Id { get; set; } = MongoDB.Bson.ObjectId.GenerateNewId().ToString();
|
||||
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
||||
|
||||
/// <summary>Herkunftsmodul, z.B. "CopyTrading".</summary>
|
||||
public string ModuleName { get; set; } = string.Empty;
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoAccountRepository : IAccountRepository
|
||||
{
|
||||
private readonly IMongoCollection<AccountState> _col;
|
||||
|
||||
public MongoAccountRepository(IMongoDatabase db)
|
||||
{
|
||||
_col = db.GetCollection<AccountState>("accounts");
|
||||
}
|
||||
|
||||
public List<AccountState> GetAll() => _col.Find(_ => true).ToList();
|
||||
|
||||
public void Upsert(AccountState account) =>
|
||||
_col.ReplaceOne(x => x.AccountId == account.AccountId, account, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void Delete(int accountId) => _col.DeleteMany(x => x.AccountId == accountId);
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoMarketRepository : IMarketRepository
|
||||
{
|
||||
private readonly IMongoCollection<MarketData> _col;
|
||||
|
||||
public MongoMarketRepository(IMongoDatabase db)
|
||||
{
|
||||
_col = db.GetCollection<MarketData>("markets");
|
||||
}
|
||||
|
||||
public MarketData? GetById(string id) => _col.Find(x => x.Id == id).FirstOrDefault();
|
||||
|
||||
public MarketData? FindByTokenId(string tokenId) =>
|
||||
_col.Find(x => x.ClobTokenIds != null && x.ClobTokenIds.Contains(tokenId)).FirstOrDefault();
|
||||
|
||||
public List<MarketData> GetActive() => _col.Find(x => !x.Closed).ToList();
|
||||
|
||||
public void Upsert(MarketData market) =>
|
||||
_col.ReplaceOne(x => x.Id == market.Id, market, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void Insert(MarketData market) => _col.InsertOne(market);
|
||||
|
||||
public void Update(MarketData market) => _col.ReplaceOne(x => x.Id == market.Id, market);
|
||||
|
||||
public void EnsureIndexes()
|
||||
{
|
||||
try
|
||||
{
|
||||
var keys = Builders<MarketData>.IndexKeys.Ascending(x => x.Id);
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<MarketData>(keys));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoPositionRepository : IPositionRepository
|
||||
{
|
||||
private readonly IMongoDatabase _db;
|
||||
|
||||
public MongoPositionRepository(IMongoDatabase db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
private IMongoCollection<Position> Live(int accountId) =>
|
||||
_db.GetCollection<Position>($"open_positions_{accountId}");
|
||||
|
||||
private IMongoCollection<Position> Demo(int accountId) =>
|
||||
_db.GetCollection<Position>($"demo_positions_{accountId}");
|
||||
|
||||
public List<Position> GetLive(int accountId) => Live(accountId).Find(_ => true).ToList();
|
||||
public List<Position> GetDemo(int accountId) => Demo(accountId).Find(_ => true).ToList();
|
||||
|
||||
public Position? FindLive(int accountId, string tokenId) =>
|
||||
Live(accountId).Find(x => x.TokenId == tokenId).FirstOrDefault();
|
||||
|
||||
public Position? FindDemo(int accountId, string tokenId) =>
|
||||
Demo(accountId).Find(x => x.TokenId == tokenId).FirstOrDefault();
|
||||
|
||||
public void UpsertLive(int accountId, Position position) =>
|
||||
Live(accountId).ReplaceOne(x => x.TokenId == position.TokenId, position, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void UpsertDemo(int accountId, Position position) =>
|
||||
Demo(accountId).ReplaceOne(x => x.TokenId == position.TokenId, position, new ReplaceOptions { IsUpsert = true });
|
||||
|
||||
public void DeleteLive(int accountId, string tokenId) =>
|
||||
Live(accountId).DeleteOne(x => x.TokenId == tokenId);
|
||||
|
||||
public void DeleteDemo(int accountId, string tokenId) =>
|
||||
Demo(accountId).DeleteOne(x => x.TokenId == tokenId);
|
||||
|
||||
public void DropDemo(int accountId) => _db.DropCollection($"demo_positions_{accountId}");
|
||||
}
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using MongoDB.Driver;
|
||||
using PolyTraderSharp.Models;
|
||||
|
||||
namespace PolyTrader.Core.Persistence.Mongo
|
||||
{
|
||||
public class MongoTradeLogRepository : ITradeLogRepository
|
||||
{
|
||||
private readonly IMongoCollection<TradeRecord> _col;
|
||||
|
||||
public MongoTradeLogRepository(IMongoDatabase db)
|
||||
{
|
||||
_col = db.GetCollection<TradeRecord>("trade_log");
|
||||
}
|
||||
|
||||
public void EnsureIndexes()
|
||||
{
|
||||
try
|
||||
{
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<TradeRecord>(
|
||||
Builders<TradeRecord>.IndexKeys.Descending(x => x.ClosedAt)));
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<TradeRecord>(
|
||||
Builders<TradeRecord>.IndexKeys.Ascending(x => x.ModuleName)));
|
||||
_col.Indexes.CreateOne(new CreateIndexModel<TradeRecord>(
|
||||
Builders<TradeRecord>.IndexKeys.Ascending(x => x.AccountId)));
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
public void Insert(TradeRecord record) => _col.InsertOne(record);
|
||||
|
||||
public List<TradeRecord> GetRecent(int limit) =>
|
||||
_col.Find(_ => true).SortByDescending(x => x.ClosedAt).Limit(limit).ToList();
|
||||
|
||||
public List<TradeRecord> Find(Expression<Func<TradeRecord, bool>> predicate) =>
|
||||
_col.Find(predicate).ToList();
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,6 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.24.0" />
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.3" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.11">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
|
||||
@@ -2,7 +2,6 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using MongoDB.Driver;
|
||||
using PolyTrader.Core.Persistence;
|
||||
using PolyTraderSharp.Models;
|
||||
using PolyTraderSharp.Services;
|
||||
|
||||
Reference in New Issue
Block a user