FluentMigrator:如何为 MigrationRunner 设置默认架构
FluentMigrator: How to set default schema for MigrationRunner
我有一个多租户数据库。特定于租户的数据存储在它自己的 (PostgreSQL) 模式中。
我希望能够根据需要使用 FluentMigrator 部署新租户。我已经设置了一个 example on GitHub 到端点的 HTTP post 将部署一个模式。但是默认将其部署到 public 模式。我希望能够指定要部署到的架构。
即
public class TenantService: ITenantService {
private readonly IServiceProvider _provider;
public TenantService(IServiceProvider provider) {
_provider = provider;
}
public void Create(string tenantName) {
using(var scope = _provider.CreateScope()) {
var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();
// TODO: Set the default schema = tenantName
migrationRunner.MigrateUp();
}
}
}
如何设置 MigrationRunner 的默认架构?
编辑: 我已经更新了 GitHub 存储库以反映已接受的答案。
我一直在为这个完全相同的问题和相同的环境而苦苦挣扎。 .Net-core、FluentMigrator 和 Postgre 具有不同的架构。我一直在使用 Obsolete 函数,并且已经注意到我必须创建自己的函数:ConventionSet。我能够在下面解决这个问题,首先我必须说:谢谢你的 git,这也帮助我解决了我的问题,这里是:
我做的第一件事是手动创建一个数据库和一个具有不同名称的模式:public。然后我调整了startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConventionSet>(new DefaultConventionSet("tenanta", null));
services.AddFluentMigratorCore();
services.ConfigureRunner(rb => rb
.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=nameofdatabase")
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations());
services.AddSingleton<ITenantService, TenantService>();
services.AddControllers();
}
通过上述更改,我们在其中添加了一个:具有模式名称的新 DefaultConventionSet 我终于可以 运行 在不同的模式上迁移然后 public。但我也希望它是 on-the-fly。所以为了实现这一点(不确定是否正确)我做了以下事情:
public class TenantService: ITenantService {
private readonly IServiceProvider _provider;
public TenantService(IServiceProvider provider) {
_provider = provider;
}
public void Create(string tenantName) {
var serviceProvider = new ServiceCollection()
.AddSingleton<IConventionSet>(new DefaultConventionSet(tenantName, null))
.AddFluentMigratorCore()
.ConfigureRunner(r => r.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=databasename")
.WithRunnerConventions(new MigrationRunnerConventions()
{
})
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations()
)
.Configure<RunnerOptions>(opt =>
{
opt.TransactionPerSession = true;
})
.BuildServiceProvider(false);
using (var scope = serviceProvider.CreateScope())
{
var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();
migrationRunner.MigrateUp();
}
}
}
当然,首先注册 ConventionSet 很重要 ;)
我有一个多租户数据库。特定于租户的数据存储在它自己的 (PostgreSQL) 模式中。
我希望能够根据需要使用 FluentMigrator 部署新租户。我已经设置了一个 example on GitHub 到端点的 HTTP post 将部署一个模式。但是默认将其部署到 public 模式。我希望能够指定要部署到的架构。
即
public class TenantService: ITenantService {
private readonly IServiceProvider _provider;
public TenantService(IServiceProvider provider) {
_provider = provider;
}
public void Create(string tenantName) {
using(var scope = _provider.CreateScope()) {
var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();
// TODO: Set the default schema = tenantName
migrationRunner.MigrateUp();
}
}
}
如何设置 MigrationRunner 的默认架构?
编辑: 我已经更新了 GitHub 存储库以反映已接受的答案。
我一直在为这个完全相同的问题和相同的环境而苦苦挣扎。 .Net-core、FluentMigrator 和 Postgre 具有不同的架构。我一直在使用 Obsolete 函数,并且已经注意到我必须创建自己的函数:ConventionSet。我能够在下面解决这个问题,首先我必须说:谢谢你的 git,这也帮助我解决了我的问题,这里是:
我做的第一件事是手动创建一个数据库和一个具有不同名称的模式:public。然后我调整了startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IConventionSet>(new DefaultConventionSet("tenanta", null));
services.AddFluentMigratorCore();
services.ConfigureRunner(rb => rb
.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=nameofdatabase")
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations());
services.AddSingleton<ITenantService, TenantService>();
services.AddControllers();
}
通过上述更改,我们在其中添加了一个:具有模式名称的新 DefaultConventionSet 我终于可以 运行 在不同的模式上迁移然后 public。但我也希望它是 on-the-fly。所以为了实现这一点(不确定是否正确)我做了以下事情:
public class TenantService: ITenantService {
private readonly IServiceProvider _provider;
public TenantService(IServiceProvider provider) {
_provider = provider;
}
public void Create(string tenantName) {
var serviceProvider = new ServiceCollection()
.AddSingleton<IConventionSet>(new DefaultConventionSet(tenantName, null))
.AddFluentMigratorCore()
.ConfigureRunner(r => r.AddPostgres()
.WithGlobalConnectionString("Server=localhost;Port=5432;User Id=postgres;Password=12345678;CommandTimeout=20;database=databasename")
.WithRunnerConventions(new MigrationRunnerConventions()
{
})
.ScanIn(typeof(AddSecuritySchema).Assembly).For.Migrations()
)
.Configure<RunnerOptions>(opt =>
{
opt.TransactionPerSession = true;
})
.BuildServiceProvider(false);
using (var scope = serviceProvider.CreateScope())
{
var migrationRunner = scope.ServiceProvider.GetService<IMigrationRunner>();
migrationRunner.MigrateUp();
}
}
}
当然,首先注册 ConventionSet 很重要 ;)