Skip to content

Commit

Permalink
修复在 freesql 下的一些问题。配置的该删除基本能用。
Browse files Browse the repository at this point in the history
  • Loading branch information
agile.zhou committed Jan 2, 2024
1 parent 5428e92 commit e37858c
Show file tree
Hide file tree
Showing 14 changed files with 216 additions and 25 deletions.
15 changes: 11 additions & 4 deletions AgileConfig.sln
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.CommonTe
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.OIDC", "src\AgileConfig.Server.OIDC\AgileConfig.Server.OIDC.csproj", "{E49A2006-6D07-4434-AEC1-27E356A767AE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgileConfig.Server.Data.Mongodb", "src\AgileConfig.Server.Data.Mongodb\AgileConfig.Server.Data.Mongodb.csproj", "{4803646E-8327-4F69-8BA5-2244CB58BBA2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Mongodb", "src\AgileConfig.Server.Data.Mongodb\AgileConfig.Server.Data.Mongodb.csproj", "{4803646E-8327-4F69-8BA5-2244CB58BBA2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgileConfig.Server.Data.Abstraction", "src\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj", "{E8101403-72C9-40FB-BCEE-16ED5C23A495}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Abstraction", "src\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj", "{E8101403-72C9-40FB-BCEE-16ED5C23A495}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgileConfig.Server.Data.Repository.Freesql", "src\AgileConfig.Server.Data.Repository.Freesql\AgileConfig.Server.Data.Repository.Freesql.csproj", "{955F64CC-9EAC-4563-804D-6E2CF9547357}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Repository.Freesql", "src\AgileConfig.Server.Data.Repository.Freesql\AgileConfig.Server.Data.Repository.Freesql.csproj", "{955F64CC-9EAC-4563-804D-6E2CF9547357}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgileConfig.Server.Data.Repository.Mongodb", "src\AgileConfig.Server.Data.Repository.Mongodb\AgileConfig.Server.Data.Repository.Mongodb.csproj", "{C6B7A5A6-7287-4A20-9336-EBE82A5256F1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AgileConfig.Server.Data.Repository.Mongodb", "src\AgileConfig.Server.Data.Repository.Mongodb\AgileConfig.Server.Data.Repository.Mongodb.csproj", "{C6B7A5A6-7287-4A20-9336-EBE82A5256F1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AgileConfig.Server.Data.Repository.Selector", "src\AgileConfig.Server.Data.Repository.Selector\AgileConfig.Server.Data.Repository.Selector.csproj", "{15089E5A-12E4-4953-BA35-0CBB2B1189C0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -112,6 +114,10 @@ Global
{C6B7A5A6-7287-4A20-9336-EBE82A5256F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6B7A5A6-7287-4A20-9336-EBE82A5256F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6B7A5A6-7287-4A20-9336-EBE82A5256F1}.Release|Any CPU.Build.0 = Release|Any CPU
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{15089E5A-12E4-4953-BA35-0CBB2B1189C0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -132,6 +138,7 @@ Global
{E8101403-72C9-40FB-BCEE-16ED5C23A495} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
{955F64CC-9EAC-4563-804D-6E2CF9547357} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
{C6B7A5A6-7287-4A20-9336-EBE82A5256F1} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
{15089E5A-12E4-4953-BA35-0CBB2B1189C0} = {1D2FD643-CB85-40F9-BC8A-CE4A39E9F43E}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F10DB58-5B6F-4EAC-994F-14E8046B306F}
Expand Down
19 changes: 15 additions & 4 deletions src/AgileConfig.Server.Apisite/Controllers/ServerNodeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ServerNodeController(IServerNodeService serverNodeService,

[TypeFilter(typeof(PremissionCheckAttribute), Arguments = new object[] { "Node.Add", Functions.Node_Add })]
[HttpPost]
public async Task<IActionResult> Add([FromBody]ServerNodeVM model)
public async Task<IActionResult> Add([FromBody] ServerNodeVM model)
{
if (model == null)
{
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task<IActionResult> Add([FromBody]ServerNodeVM model)
TinyEventBus.Instance.Fire(EventKeys.ADD_NODE_SUCCESS, param);
await _remoteServerNodeProxy.TestEchoAsync(node.Id);
}

return Json(new
{
data = node,
Expand All @@ -75,7 +75,7 @@ public async Task<IActionResult> Add([FromBody]ServerNodeVM model)

[TypeFilter(typeof(PremissionCheckAttribute), Arguments = new object[] { "Node.Delete", Functions.Node_Delete })]
[HttpPost]
public async Task<IActionResult> Delete([FromBody]ServerNodeVM model)
public async Task<IActionResult> Delete([FromBody] ServerNodeVM model)
{
if (Appsettings.IsPreviewMode)
{
Expand Down Expand Up @@ -120,10 +120,21 @@ public async Task<IActionResult> All()
{
var nodes = await _serverNodeService.GetAllNodesAsync();

var vms = nodes.OrderBy(x => x.CreateTime).Select(x =>
{
return new ServerNodeVM
{
Address = x.Id,
Remark = x.Remark,
LastEchoTime = x.LastEchoTime,
Status = x.Status
};
});

return Json(new
{
success = true,
data = nodes.OrderBy(n => n.CreateTime)
data = vms
});
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/AgileConfig.Server.Apisite/appsettings.Development.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"pathBase": "", //使用反向代理的时候,或许需要修改这个值 /xxx 必须/开头
"adminConsole": true,
"cluster": false, // 集群模式:服务启动后自动加入节点列表,服务启动的时候会获取容器的ip,端口默认5000,适合 docker compose 环境使用
"preview_mode": true,
"preview_mode": false,
"db": {
"provider": "mongodb", //sqlite,mysql,sqlserver,npgsql,oracle,mongodb
"conn": "mongodb://localhost:27017,localhost:37017/AgileConfig",
"provider": "sqlite", //sqlite,mysql,sqlserver,npgsql,oracle,mongodb
"conn": "Data Source=agile_config.db",
//"provider": "sqlserver",
//"conn": "Encrypt=True;TrustServerCertificate=True;Persist Security Info = False; User ID =dev; Password =dev@123; Initial Catalog =agile_config; Server =192.168.18.82"
//"provider": "npgsql",
Expand All @@ -26,6 +26,8 @@
//"conn": "user id=CLINIC;password=CLINIC;data source=192.168.0.91/orcl"
//"provider": "mysql",
//"conn": "Database=agile_config;Data Source=192.168.0.125;User Id=root;Password=x;port=13306;Allow User Variables=true;",
//"provider": "mongodb",
//"conn": "mongodb://localhost:27017,localhost:37017/AgileConfig",
"env": {
"TEST": {
"provider": "sqlite", //sqlite,mysql,sqlserver,npgsql,oracle
Expand Down
3 changes: 0 additions & 3 deletions src/AgileConfig.Server.Apisite/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
"db": {
"provider": "", //sqlite,mysql,sqlserver,npgsql,oracle,mongodb
"conn": "",
//"conn": "Data Source=agile_config.db"
//"conn": "Persist Security Info = False; User ID =dev; Password =dev@123; Initial Catalog =agile_config; Server =."
//"conn": "mongodb://user:password@localhost:27017,localhost:37017/database-name?authSource=admin"
"env": {
"TEST": {
"provider": "", //sqlite,mysql,sqlserver,npgsql,oracle
Expand Down
2 changes: 1 addition & 1 deletion src/AgileConfig.Server.Data.Abstraction/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IRepository<T, T1> : IDisposable where T : IEntity<T1>
Task UpdateAsync(IList<T> entities);

Task DeleteAsync(T1 id);

Task DeleteAsync(T entity);

Task DeleteAsync(IList<T> entities);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
Expand Down
7 changes: 4 additions & 3 deletions src/AgileConfig.Server.Data.Freesql/EnvFreeSqlFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ namespace AgileConfig.Server.Data.Freesql
{
public class EnvFreeSqlFactory : IFreeSqlFactory
{
private readonly IEnvAccessor _envAccessor;
private readonly string _env;
public EnvFreeSqlFactory(IEnvAccessor envAccessor)
{
_envAccessor = envAccessor;
_env = envAccessor.Env;
}

public IFreeSql Create()
{
return FreeSQL.GetInstanceByEnv(_envAccessor.Env);
return FreeSQL.GetInstanceByEnv(_env);
}
}
}
5 changes: 5 additions & 0 deletions src/AgileConfig.Server.Data.Freesql/FreeSQL.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static IFreeSql GetInstanceByEnv(string env)
var provider = Global.Config[$"db:env:{env}:provider"];
var conn = Global.Config[$"db:env:{env}:conn"];

if (string.IsNullOrEmpty(provider))
{
return Instance;
}

var key = provider;

if (_envFreesqls.ContainsKey(key))
Expand Down
4 changes: 2 additions & 2 deletions src/AgileConfig.Server.Data.Freesql/FreesqlRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Task DeleteAsync(IList<T> entities)
return _repository.DeleteAsync(entities);
}

public Task<T> GetAsync(T1 id)
public Task<T?> GetAsync(T1 id)
{
return _repository.Where(x => x.Id.Equals(id)).ToOneAsync();
}
Expand Down Expand Up @@ -91,7 +91,7 @@ private Expression<Func<T, object>> Sort(string defaultSortField)
return defaultSort;
}

public async Task<long> CountAsync(Expression<Func<T, bool>> exp = null)
public async Task<long> CountAsync(Expression<Func<T, bool>>? exp = null)
{
return await (exp == null ? _repository.Select.CountAsync() : _repository.Select.Where(exp).CountAsync());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static void AddFreeSqlRepository(this IServiceCollection sc)
sc.AddScoped<ISysLogRepository, SysLogRepository>();
sc.AddScoped<IUserAppAuthRepository, UserAppAuthRepository>();
sc.AddScoped<IUserRepository, UserRepository>();
sc.AddScoped<IUserRoleRepository, IUserRoleRepository>();
sc.AddScoped<IUserRoleRepository, UserRoleRepository>();

sc.AddSingleton<ISysInitRepository, SysInitRepository>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public class SysInitRepository : ISysInitRepository

public void SaveInitSetting(Setting setting)
{
FreeSQL.Instance.Insert(setting);
FreeSQL.Instance.Insert(setting).ExecuteAffrows();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AgileConfig.Server.Data.Abstraction\AgileConfig.Server.Data.Abstraction.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Freesql\AgileConfig.Server.Data.Repository.Freesql.csproj" />
<ProjectReference Include="..\AgileConfig.Server.Data.Repository.Mongodb\AgileConfig.Server.Data.Repository.Mongodb.csproj" />
</ItemGroup>

</Project>
151 changes: 151 additions & 0 deletions src/AgileConfig.Server.Data.Repository.Selector/RepositoryExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using AgileConfig.Server.Common;
using AgileConfig.Server.Data.Abstraction;
using AgileConfig.Server.Data.Freesql;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Security.Cryptography;

namespace AgileConfig.Server.Data.Repository.Selector
{
public static class RepositoryExtension
{
public static IServiceCollection AddRepositories(this IServiceCollection sc)
{
sc.AddFreeRepository();

var config = Global.Config;
var defaultProvider = config["db:provider"];

if (string.IsNullOrEmpty(defaultProvider))
{
throw new ArgumentNullException(nameof(defaultProvider));
}

if (defaultProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase))
{
sc.AddScoped<IAppInheritancedRepository, Mongodb.AppInheritancedRepository>();
sc.AddScoped<IAppRepository, Mongodb.AppRepository>();
sc.AddScoped<IServerNodeRepository, Mongodb.ServerNodeRepository>();
sc.AddScoped<IServiceInfoRepository, Mongodb.ServiceInfoRepository>();
sc.AddScoped<ISettingRepository, Mongodb.SettingRepository>();
sc.AddScoped<ISysLogRepository, Mongodb.SysLogRepository>();
sc.AddScoped<IUserAppAuthRepository, Mongodb.UserAppAuthRepository>();
sc.AddScoped<IUserRepository, Mongodb.UserRepository>();
sc.AddScoped<IUserRoleRepository, Mongodb.UserRoleRepository>();
sc.AddSingleton<ISysInitRepository, Mongodb.SysInitRepository>();
}
else
{
sc.AddScoped<IAppInheritancedRepository, Freesql.AppInheritancedRepository>();
sc.AddScoped<IAppRepository, Freesql.AppRepository>();
sc.AddScoped<IServerNodeRepository, Freesql.ServerNodeRepository>();
sc.AddScoped<IServiceInfoRepository, Freesql.ServiceInfoRepository>();
sc.AddScoped<ISettingRepository, Freesql.SettingRepository>();
sc.AddScoped<ISysLogRepository, Freesql.SysLogRepository>();
sc.AddScoped<IUserAppAuthRepository, Freesql.UserAppAuthRepository>();
sc.AddScoped<IUserRepository, Freesql.UserRepository>();
sc.AddScoped<IUserRoleRepository, Freesql.UserRoleRepository>();
sc.AddSingleton<ISysInitRepository, Freesql.SysInitRepository>();
}

sc.AddScoped<Freesql.ConfigPublishedRepository>();
sc.AddScoped<Freesql.ConfigRepository>();
sc.AddScoped<Freesql.PublishDetailRepository>();
sc.AddScoped<Freesql.PublishTimelineRepository>();

sc.AddScoped<Mongodb.ConfigPublishedRepository>();
sc.AddScoped<Mongodb.ConfigRepository>();
sc.AddScoped<Mongodb.PublishDetailRepository>();
sc.AddScoped<Mongodb.PublishTimelineRepository>();

sc.AddScoped<Func<string, IConfigPublishedRepository>>(sp => env =>
{
string envProvider = GetEnvProvider(env, config, defaultProvider);

if (envProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase))
{
return sp.GetService<Mongodb.ConfigPublishedRepository>();
}
else
{
var envAccssor = new ManualEnvAccessor(env);
var factory = new EnvFreeSqlFactory(envAccssor);
return new Freesql.ConfigPublishedRepository(factory);
}
});

sc.AddScoped<Func<string, IConfigRepository>>(sp => env =>
{
string envProvider = GetEnvProvider(env, config, defaultProvider);

if (envProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase))
{
return sp.GetService<Mongodb.ConfigRepository>();
}
else
{
var envAccssor = new ManualEnvAccessor(env);
var factory = new EnvFreeSqlFactory(envAccssor);
return new Freesql.ConfigRepository(factory);
}
});

sc.AddScoped<Func<string, IPublishDetailRepository>>(sp => env =>
{
string envProvider = GetEnvProvider(env, config, defaultProvider);

if (envProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase))
{
return sp.GetService<Mongodb.PublishDetailRepository>();
}
else
{
var envAccssor = new ManualEnvAccessor(env);
var factory = new EnvFreeSqlFactory(envAccssor);
return new Freesql.PublishDetailRepository(factory);
}
});

sc.AddScoped((Func<IServiceProvider, Func<string, IPublishTimelineRepository>>)(sp => env =>
{
string envProvider = GetEnvProvider(env, config, defaultProvider);

if (envProvider.Equals("mongodb", StringComparison.OrdinalIgnoreCase))
{
return sp.GetService<Mongodb.PublishTimelineRepository>();
}
else
{
var envAccssor = new ManualEnvAccessor(env);
var factory = new EnvFreeSqlFactory(envAccssor);
return new Freesql.PublishTimelineRepository(factory);
}
}));

return sc;
}

private static string GetEnvProvider(string env, IConfiguration config, string defaultProvider)
{
var envProviderKey = $"db:env:{env}:provider";
var envProvider = config[envProviderKey];
if (string.IsNullOrEmpty(envProvider))
{
// use default provider
envProvider = defaultProvider;
}

return envProvider;
}

class ManualEnvAccessor : IEnvAccessor
{
string _env;
public ManualEnvAccessor(string env)
{
_env = env;
}
public string Env => _env;
}
}
}
5 changes: 3 additions & 2 deletions src/AgileConfig.Server.Service/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ public void Dispose()
waitPublishConfigs = waitPublishConfigs.Where(x => ids.Contains(x.Id)).ToList();
}
//这里默认admin console 实例只部署一个,如果部署多个同步操作,高并发的时候这个version会有问题
var versionMax = (await _publishTimelineRepository.QueryAsync(x => x.AppId == appId)).Max(x => x.Version);
var publishList = await _publishTimelineRepository.QueryAsync(x => x.AppId == appId);
var versionMax = publishList.Any() ? publishList.Max(x => x.Version) : 0;

var user = await _userService.GetUserAsync(operatorr);

Expand Down Expand Up @@ -732,7 +733,7 @@ public async Task<bool> RollbackAsync(string publishTimelineId, string env)
//删除发布时间轴version之后的版本
var deletePublishTimeLineItems = await _publishTimelineRepository.QueryAsync(x => x.AppId == appId && x.Env == env && x.Version > version);
await _publishTimelineRepository.DeleteAsync(deletePublishTimeLineItems);
var deletePublishDetailItems = await _publishDetailRepository.QueryAsync(x => x.PublishTimelineId == publishTimelineId && x.Env == env);
var deletePublishDetailItems = await _publishDetailRepository.QueryAsync(x => x.AppId == appId && x.Env == env && x.Version > version);
await _publishDetailRepository.DeleteAsync(deletePublishDetailItems);

ClearAppPublishedConfigsMd5Cache(appId, env);
Expand Down

0 comments on commit e37858c

Please sign in to comment.