Skip to content

Commit

Permalink
- 增加 ICodeFirst.SyncStructure(TableInfo) 重载方法迁移表结构;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Nov 24, 2023
1 parent 2ed254d commit aad91e2
Show file tree
Hide file tree
Showing 29 changed files with 151 additions and 138 deletions.
26 changes: 14 additions & 12 deletions FreeSql/Interface/ICodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ public interface ICodeFirst
/// <param name="tableName">指定表名对比</param>
/// <returns></returns>
string GetComparisonDDLStatements(Type entityType, string tableName);
/// <summary>
/// 同步实体类型到数据库<para></para>
/// 注意:生产环境中谨慎使用
/// </summary>
/// <typeparam name="TEntity"></typeparam>
void SyncStructure<TEntity>();
string GetComparisonDDLStatements(TableInfo tableSchema, string tableName);
/// <summary>
/// 同步实体类型到数据库<para></para>
/// 注意:生产环境中谨慎使用
/// </summary>
/// <typeparam name="TEntity"></typeparam>
void SyncStructure<TEntity>();
/// <summary>
/// 同步实体类型集合到数据库<para></para>
/// 注意:生产环境中谨慎使用
Expand All @@ -82,13 +83,14 @@ public interface ICodeFirst
/// <param name="tableName">指定表名对比</param>
/// <param name="isForceSync">强制同步结构,无视缓存每次都同步</param>
void SyncStructure(Type entityType, string tableName, bool isForceSync = false);
void SyncStructure(TableInfo tableSchema, string tableName, bool isForceSync = false);

/// <summary>
/// 根据 System.Type 获取数据库信息
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
DbInfoResult GetDbInfo(Type type);
/// <summary>
/// 根据 System.Type 获取数据库信息
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
DbInfoResult GetDbInfo(Type type);
/// <summary>
/// FreeSql FluentApi 配置实体,方法名与特性相同
/// </summary>
Expand Down
53 changes: 32 additions & 21 deletions FreeSql/Internal/CommonProvider/CodeFirstProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,21 @@ protected string GetTableNameLowerOrUpper(string tableName)
return tableName;
}
public string GetComparisonDDLStatements<TEntity>() =>
this.GetComparisonDDLStatements(new TypeAndName(typeof(TEntity), ""));
this.GetComparisonDDLStatements(new TypeSchemaAndName(GetTableByEntity(typeof(TEntity)), ""));
public string GetComparisonDDLStatements(params Type[] entityTypes) => entityTypes == null ? null :
this.GetComparisonDDLStatements(entityTypes.Distinct().Select(a => new TypeAndName(a, "")).ToArray());
this.GetComparisonDDLStatements(entityTypes.Distinct().Select(a => new TypeSchemaAndName(GetTableByEntity(a), "")).ToArray());
public string GetComparisonDDLStatements(Type entityType, string tableName) =>
this.GetComparisonDDLStatements(new TypeAndName(entityType, GetTableNameLowerOrUpper(tableName)));
protected abstract string GetComparisonDDLStatements(params TypeAndName[] objects);
public class TypeAndName
this.GetComparisonDDLStatements(new TypeSchemaAndName(GetTableByEntity(entityType), GetTableNameLowerOrUpper(tableName)));
public string GetComparisonDDLStatements(TableInfo tableSchema, string tableName) =>
this.GetComparisonDDLStatements(new TypeSchemaAndName(tableSchema, GetTableNameLowerOrUpper(tableName)));
protected abstract string GetComparisonDDLStatements(params TypeSchemaAndName[] objects);
public class TypeSchemaAndName
{
public Type entityType { get; }
public TableInfo tableSchema { get; }
public string tableName { get; }
public TypeAndName(Type entityType, string tableName)
public TypeSchemaAndName(TableInfo tableSchema, string tableName)
{
this.entityType = entityType;
this.tableSchema = tableSchema;
this.tableName = tableName;
}
}
Expand All @@ -82,24 +84,33 @@ public void _dicSycedTryAdd(Type entityType, string tableName = null) =>
_dicSycedGetOrAdd(entityType).TryAdd(GetTableNameLowerOrUpper(tableName), true);

public void SyncStructure<TEntity>() =>
this.SyncStructure(new TypeAndName(typeof(TEntity), ""));
this.SyncStructure(new TypeSchemaAndName(GetTableByEntity(typeof(TEntity)), ""));
public void SyncStructure(params Type[] entityTypes) =>
this.SyncStructure(entityTypes?.Distinct().Select(a => new TypeAndName(a, "")).ToArray());
public void SyncStructure(Type entityType, string tableName, bool isForceSync)
this.SyncStructure(entityTypes?.Distinct().Select(a => new TypeSchemaAndName(GetTableByEntity(a), "")).ToArray());
public void SyncStructure(Type entityType, string tableName, bool isForceSync) =>
this.SyncStructure(GetTableByEntity(entityType), tableName, isForceSync);
public void SyncStructure(TableInfo tableSchema, string tableName, bool isForceSync = false)
{
tableName = GetTableNameLowerOrUpper(tableName);
if (isForceSync && _dicSynced.TryGetValue(entityType, out var dic)) dic.TryRemove(tableName, out var old);
this.SyncStructure(new TypeAndName(entityType, tableName));
}
protected void SyncStructure(params TypeAndName[] objects)
if (isForceSync && tableSchema?.Type != null && _dicSynced.TryGetValue(tableSchema.Type, out var dic)) dic.TryRemove(tableName, out var old);
this.SyncStructure(new TypeSchemaAndName(tableSchema, tableName));
}

protected void SyncStructure(params TypeSchemaAndName[] objects)
{
if (objects == null) return;
var syncObjects = objects.Where(a => a.entityType != null && a.entityType != typeof(object) && _dicSycedGetOrAdd(a.entityType).ContainsKey(GetTableNameLowerOrUpper(a.tableName)) == false && GetTableByEntity(a.entityType)?.DisableSyncStructure == false)
.Select(a => new TypeAndName(a.entityType, GetTableNameLowerOrUpper(a.tableName)))
.Where(a => !(string.IsNullOrEmpty(a.tableName) == true && GetTableByEntity(a.entityType)?.AsTableImpl != null))
var syncObjects = objects.Where(a => a.tableSchema?.Type != null &&
(
a.tableSchema.Type.Name == "DynamicRepository" && a.tableSchema.Columns.Any()
||
a.tableSchema.Type != typeof(object) && _dicSycedGetOrAdd(a.tableSchema.Type).ContainsKey(GetTableNameLowerOrUpper(a.tableName)) == false
) &&
a.tableSchema?.DisableSyncStructure == false)
.Select(a => new TypeSchemaAndName(a.tableSchema, GetTableNameLowerOrUpper(a.tableName)))
.Where(a => !(string.IsNullOrEmpty(a.tableName) == true && a.tableSchema?.AsTableImpl != null))
.ToArray();
if (syncObjects.Any() == false) return;
var before = new Aop.SyncStructureBeforeEventArgs(syncObjects.Select(a => a.entityType).ToArray());
var before = new Aop.SyncStructureBeforeEventArgs(syncObjects.Select(a => a.tableSchema.Type).ToArray());
_orm.Aop.SyncStructureBeforeHandler?.Invoke(this, before);
Exception exception = null;
string ddl = null;
Expand All @@ -110,11 +121,11 @@ protected void SyncStructure(params TypeAndName[] objects)
ddl = this.GetComparisonDDLStatements(syncObjects);
if (string.IsNullOrEmpty(ddl))
{
foreach (var syncObject in syncObjects) _dicSycedTryAdd(syncObject.entityType, syncObject.tableName);
foreach (var syncObject in syncObjects) _dicSycedTryAdd(syncObject.tableSchema.Type, syncObject.tableName);
return;
}
var affrows = ExecuteDDLStatements(ddl);
foreach (var syncObject in syncObjects) _dicSycedTryAdd(syncObject.entityType, syncObject.tableName);
foreach (var syncObject in syncObjects) _dicSycedTryAdd(syncObject.tableSchema.Type, syncObject.tableName);
return;
}
}
Expand Down
8 changes: 4 additions & 4 deletions Providers/FreeSql.Provider.ClickHouse/ClickHouseCodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override DbInfoResult GetDbInfo(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
Object<DbConnection> conn = null;
string database = null;
Expand All @@ -100,11 +100,11 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
{
if (sb.Length > 0)
sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
var tb = obj.tableSchema;
if (tb == null)
throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false)
throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = _commonUtils.SplitTableName(tb.DbName);
if (tbname?.Length == 1)
tbname = new[] { database, tbname[0] };
Expand Down
2 changes: 1 addition & 1 deletion Providers/FreeSql.Provider.Custom/CustomCodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ public override DbInfoResult GetDbInfo(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects) => throw new NotImplementedException($"FreeSql.Provider.Custom {CoreStrings.S_Not_Implemented_Feature}");
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects) => throw new NotImplementedException($"FreeSql.Provider.Custom {CoreStrings.S_Not_Implemented_Feature}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override DbInfoResult GetDbInfo(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
Object<DbConnection> conn = null;
string database = null;
Expand All @@ -88,9 +88,9 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
var tb = obj.tableSchema;
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = _commonUtils.SplitTableName(tb.DbName);
if (tbname?.Length == 1) tbname = new[] { database, tbname[0] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public override DbInfoResult GetDbInfo(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
var userId = (_orm.Ado as CustomOracleAdo)?.UserId;
if (string.IsNullOrEmpty(userId))
Expand All @@ -90,15 +90,15 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
var tb = obj.tableSchema;
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = _commonUtils.SplitTableName(tb.DbName);
if (tbname?.Length == 1) tbname = new[] { userId, tbname[0] };

var tboldname = _commonUtils.SplitTableName(tb.DbOldName); //旧表名
if (tboldname?.Length == 1) tboldname = new[] { userId, tboldname[0] };
var primaryKeyName = (obj.entityType.GetCustomAttributes(typeof(OraclePrimaryKeyNameAttribute), false)?.FirstOrDefault() as OraclePrimaryKeyNameAttribute)?.Name;
var primaryKeyName = (obj.tableSchema.Type.GetCustomAttributes(typeof(OraclePrimaryKeyNameAttribute), false)?.FirstOrDefault() as OraclePrimaryKeyNameAttribute)?.Name;
if (string.IsNullOrEmpty(obj.tableName) == false)
{
var tbtmpname = _commonUtils.SplitTableName(obj.tableName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ CsToDb<DbType> GetDbInfoNoneArray(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
var sb = new StringBuilder();
var seqcols = new List<NativeTuple<ColumnInfo, string[], bool>>(); //序列
Expand All @@ -96,9 +96,9 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
var tb = obj.tableSchema;
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = _commonUtils.SplitTableName(tb.DbName);
if (tbname?.Length == 1) tbname = new[] { "public", tbname[0] };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ void AddOrUpdateMS_Description(StringBuilder sb, string schema, string table, st
, @level2type = 'COLUMN', @level2name = N'{2}'
", schema.Replace("'", "''"), table.Replace("'", "''"), column.Replace("'", "''"), comment?.Replace("'", "''") ?? "");
}
protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
Object<DbConnection> conn = null;
string database = null;
Expand All @@ -148,9 +148,9 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
var tb = obj.tableSchema;
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = _commonUtils.SplitTableName(tb.DbName);
if (tbname?.Length == 1) tbname = new[] { database, "dbo", tbname[0] };
if (tbname?.Length == 2) tbname = new[] { database, tbname[0], tbname[1] };
Expand Down
10 changes: 5 additions & 5 deletions Providers/FreeSql.Provider.Dameng/DamengCodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public override DbInfoResult GetDbInfo(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
var userId = (_orm.Ado.MasterPool as DamengConnectionPool)?.UserId;
if (string.IsNullOrEmpty(userId))
Expand All @@ -91,15 +91,15 @@ protected override string GetComparisonDDLStatements(params TypeAndName[] object
foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
var tb = obj.tableSchema;
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = _commonUtils.SplitTableName(tb.DbName);
if (tbname?.Length == 1) tbname = new[] { userId, tbname[0] };

var tboldname = _commonUtils.SplitTableName(tb.DbOldName); //旧表名
if (tboldname?.Length == 1) tboldname = new[] { userId, tboldname[0] };
var primaryKeyName = (obj.entityType.GetCustomAttributes(typeof(OraclePrimaryKeyNameAttribute), false)?.FirstOrDefault() as OraclePrimaryKeyNameAttribute)?.Name;
var primaryKeyName = (obj.tableSchema.Type.GetCustomAttributes(typeof(OraclePrimaryKeyNameAttribute), false)?.FirstOrDefault() as OraclePrimaryKeyNameAttribute)?.Name;
if (string.IsNullOrEmpty(obj.tableName) == false)
{
var tbtmpname = _commonUtils.SplitTableName(obj.tableName);
Expand Down
8 changes: 4 additions & 4 deletions Providers/FreeSql.Provider.Firebird/FirebirdCodeFirst.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,16 @@ CsToDb<FbDbType> GetDbInfoNoneArray(Type type)
return null;
}

protected override string GetComparisonDDLStatements(params TypeAndName[] objects)
protected override string GetComparisonDDLStatements(params TypeSchemaAndName[] objects)
{
var sb = new StringBuilder();

foreach (var obj in objects)
{
if (sb.Length > 0) sb.Append("\r\n");
var tb = _commonUtils.GetTableByEntity(obj.entityType);
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.entityType.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.entityType.FullName));
var tb = obj.tableSchema;
if (tb == null) throw new Exception(CoreStrings.S_Type_IsNot_Migrable(obj.tableSchema.Type.FullName));
if (tb.Columns.Any() == false) throw new Exception(CoreStrings.S_Type_IsNot_Migrable_0Attributes(obj.tableSchema.Type.FullName));
var tbname = tb.DbName;
var tboldname = tb.DbOldName; //旧表名
if (string.IsNullOrEmpty(obj.tableName) == false)
Expand Down
Loading

0 comments on commit aad91e2

Please sign in to comment.