2016-07-21 3 views
0

, SQL2012FluentMigratorto control our database migrations을 사용하고 있습니다. 우리는 솔루션에서 여러 데이터베이스를 실행 중이므로 일부 데이터베이스에는 일부 데이터 삽입을 실행해야하지만 다른 데이터베이스에는 일부 데이터 삽입을 실행해야합니다.FluentMigrator를 사용할 때 특정 데이터베이스 이름에서만 마이그레이션을 실행하는 방법

특정 데이터베이스 이름을 기반으로 일부 데이터베이스 마이그레이션을 실행하려면 어떻게해야합니까?

+0

이 Q/A는 나에게 많은 시간을 저장하고 난 그게 더 upvotes을 얻을 바랍니다. 어쩌면 "FM은 주어진 dll에서 찾을 수있는 모든 마이그레이션을 실행합니다", "태그 속성은 특정 마이그레이션에 유용하지만 그 목적이 광범위하지는 않을 것"과 "다른 DBMS를 대상으로하는 방법을 설명하는 문맥을 추가 할 수 있습니다. 하지만 다른 DB에 대해서는 아무 것도 없습니다. "그래서 질문하기 전에 적절한 검색을하지 않은 것과는 대조적으로 문서가 빈약하다는 것이 분명합니다. –

+0

@ RenaudGauthier 안녕하세요 Renaud, 이유는이 Q는 upvotes가 문안 (제목은 대답에 해당)하지만 질문은 매우 틈새가되고, 그냥 전망을 봐 ... 나는 또한 TBH .. 상관 없어 . 그래서 사람들을 돕는 것은별로 보람이 없어서 나는 은퇴했다. ' –

답변

1

나는 실행할 데이터베이스를 제어하는이 클래스를 도입했습니다. Migration에서 상속 그래서 오히려 지금 OnlyRunOnSpecificDatabaseMigration에서 상속 것 :

한 주! :이 기본 동작 (마이그레이션을 실행하기 위해)에 폴백 않습니다 DatabaseNamesToRunMigrationOnList에 나열된 데이터베이스가없는 경우 - 일부는 반 직관적을 찾을 수 있습니다

namespace Infrastructure.Migrations 
{ 
    using System.Collections.Generic; 
    using FluentMigrator; 
    using FluentMigrator.Infrastructure; 

    public abstract class OnlyRunOnSpecificDatabaseMigration : Migration 
    { 
     public abstract List<string> DatabaseNamesToRunMigrationOnList { get; } 

     private bool DoRunMigraton(IMigrationContext context) 
     { 
      return this.DatabaseNamesToRunMigrationOnList == null || 
        this.DatabaseNamesToRunMigrationOnList.Contains(new System.Data.SqlClient.SqlConnectionStringBuilder(context.Connection).InitialCatalog); 
     } 

     public override void GetUpExpressions(IMigrationContext context) 
     { 
      if (this.DoRunMigraton(context)) 
      { 
       base.GetUpExpressions(context); 
      } 
     } 

     public override void GetDownExpressions(IMigrationContext context) 
     { 
      if (this.DoRunMigraton(context)) 
      { 
       base.GetDownExpressions(context); 
      } 
     } 
    } 
} 

사용 예 :

public class RiskItems : OnlyRunOnSpecificDatabaseMigration 
{ 
    public override void Up() 
    { 

     Execute.Sql(@"update [Items] set 
        CanBeX = 
        case when exists(select 1 from [SomeTable] where Key = [Items].Key and position like 'Factor%') then 1 else 0 end"); 
    } 

    public override void Down() 
    { 

    } 

    public override List<string> DatabaseNamesToRunMigrationOnList 
    { 
     get 
     { 
      return new List<string> {"my_database_name"}; 
     } 
    } 
}