Skip to content

Commit

Permalink
分表数据库迁移支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldairarrow committed Sep 27, 2020
1 parent d16ff66 commit c0d9c1f
Show file tree
Hide file tree
Showing 12 changed files with 292 additions and 68 deletions.
2 changes: 1 addition & 1 deletion examples/Demo.DbMigrator/CustomContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using EFCore.Sharding;

namespace DbMigrator
namespace Demo.DbMigrator
{
public class CustomContext : GenericDbContext
{
Expand Down
12 changes: 10 additions & 2 deletions examples/Demo.DbMigrator/DesignTimeDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@
using Microsoft.Extensions.DependencyInjection;
using System;

namespace DbMigrator
namespace Demo.DbMigrator
{
public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<CustomContext>
{
private static readonly string _connectionString = "Data Source=localhost;Initial Catalog=DbMigrator;Integrated Security=True";

static DesignTimeDbContextFactory()
{
DateTime startTime = DateTime.Now.AddMinutes(-5);
ServiceCollection services = new ServiceCollection();
services.AddEFCoreSharding(x =>
{
x.MigrationsWithoutForeignKey();
//添加数据源
x.AddDataSource(_connectionString, ReadWriteType.Read | ReadWriteType.Write, DatabaseType.SqlServer);

//按分钟分表
x.SetDateSharding<Order>(nameof(Order.CreateTime), ExpandByDateMode.PerMinute, startTime);
});
ServiceProvider = services.BuildServiceProvider();
new EFCoreShardingBootstrapper(ServiceProvider).StartAsync(default).Wait();
Expand All @@ -31,7 +39,7 @@ public CustomContext CreateDbContext(string[] args)
.GetService<IDbFactory>()
.GetDbContext(new DbContextParamters
{
ConnectionString = "Data Source=localhost;Initial Catalog=DbMigrator;Integrated Security=True",
ConnectionString = _connectionString,
DbType = DatabaseType.SqlServer,
});

Expand Down
2 changes: 1 addition & 1 deletion examples/Demo.DbMigrator/Entities/Order.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class Order
{
[Key]
public Guid Id { get; set; }
public DateTime DateTime { get; set; }
public DateTime CreateTime { get; set; }
public List<OrderItem> OrderItems { get; set; } = new List<OrderItem>();
}
}

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

155 changes: 155 additions & 0 deletions examples/Demo.DbMigrator/Migrations/20200927141554_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Demo.DbMigrator.Migrations
{
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Order_202009272210",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272210", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272211",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272211", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272212",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272212", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272213",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272213", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272214",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272214", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272215",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272215", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272216",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272216", x => x.Id);
});

migrationBuilder.CreateTable(
name: "Order_202009272217",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
CreateTime = table.Column<DateTime>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Order_202009272217", x => x.Id);
});

migrationBuilder.CreateTable(
name: "OrderItem",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
OrderId = table.Column<Guid>(nullable: false),
Name = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_OrderItem", x => x.Id);
});

migrationBuilder.CreateIndex(
name: "IX_OrderItem_OrderId",
table: "OrderItem",
column: "OrderId");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "OrderItem");

migrationBuilder.DropTable(
name: "Order_202009272210");

migrationBuilder.DropTable(
name: "Order_202009272211");

migrationBuilder.DropTable(
name: "Order_202009272212");

migrationBuilder.DropTable(
name: "Order_202009272213");

migrationBuilder.DropTable(
name: "Order_202009272214");

migrationBuilder.DropTable(
name: "Order_202009272215");

migrationBuilder.DropTable(
name: "Order_202009272216");

migrationBuilder.DropTable(
name: "Order_202009272217");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// <auto-generated />
using System;
using DbMigrator;
using Demo.DbMigrator;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
Expand All @@ -25,7 +25,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");

b.Property<DateTime>("DateTime")
b.Property<DateTime>("CreateTime")
.HasColumnType("datetime2");

b.HasKey("Id");
Expand Down
1 change: 0 additions & 1 deletion examples/Demo.DbMigrator/a.json

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static IServiceCollection AddEFCoreSharding(this IServiceCollection servi

ShardingContainer container = new ShardingContainer(services);
shardingBuilder?.Invoke(container);

services.AddSingleton(container);
services.AddSingleton<IShardingBuilder>(container);
services.AddSingleton<IShardingConfig>(container);
services.AddSingleton<DbFactory>();
Expand Down
13 changes: 11 additions & 2 deletions src/EFCore.Sharding/DependencyInjection/ShardingContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public DatabaseType FindADbType()
{
return _dataSources.FirstOrDefault().DbType;
}
public readonly Dictionary<string, List<string>> ExistsShardingTables
= new Dictionary<string, List<string>>();

#endregion

Expand Down Expand Up @@ -301,14 +303,22 @@ public IShardingBuilder SetDateSharding<TEntity>(string shardingField, ExpandByD
{
var theSourceName = GetSourceName(theTime);
string suffix = shardingRule.GetTableSuffixByField(theTime);

string absTableName = AnnotationHelper.GetDbTableName(typeof(TEntity));
string fullTableName = $"{absTableName}_{suffix}";
if (!ExistsShardingTables.ContainsKey(absTableName))
{
ExistsShardingTables.Add(absTableName, new List<string>());
}
ExistsShardingTables[absTableName].Add(fullTableName);

CreateTable<TEntity>(serviceProvider, theSourceName, suffix);
AddPhysicTable<TEntity>(suffix, theSourceName);

theTime = (DateTime)method.Invoke(theTime, new object[] { 1 });
}

//定时自动建表

JobHelper.SetCronJob(() =>
{
DateTime trueDate = DateTime.Now + paramter.leadTime;
Expand All @@ -319,7 +329,6 @@ public IShardingBuilder SetDateSharding<TEntity>(string shardingField, ExpandByD
AddPhysicTable<TEntity>(suffix, theSourceName);
}, paramter.conExpression);


string GetSourceName(DateTime time)
{
return ranges.Where(x => time >= x.startTime && time < x.endTime).FirstOrDefault().sourceName;
Expand Down
Loading

0 comments on commit c0d9c1f

Please sign in to comment.