11using System ;
22using System . Collections . Generic ;
3+ using System . Data . Common ;
34using Abp . Data ;
45using Abp . Dependency ;
56using Abp . Domain . Repositories ;
@@ -15,8 +16,7 @@ namespace AbpCompanyName.AbpProjectName.Migrator
1516{
1617 public class MultiTenantMigrateExecuter : ITransientDependency
1718 {
18- public Log Log { get ; private set ; }
19-
19+ private readonly Log _log ;
2020 private readonly AbpZeroDbMigrator _migrator ;
2121 private readonly IRepository < Tenant > _tenantRepository ;
2222 private readonly IDbPerTenantConnectionStringResolver _connectionStringResolver ;
@@ -27,7 +27,7 @@ public MultiTenantMigrateExecuter(
2727 Log log ,
2828 IDbPerTenantConnectionStringResolver connectionStringResolver )
2929 {
30- Log = log ;
30+ _log = log ;
3131
3232 _migrator = migrator ;
3333 _tenantRepository = tenantRepository ;
@@ -36,52 +36,52 @@ public MultiTenantMigrateExecuter(
3636
3737 public bool Run ( bool skipConnVerification )
3838 {
39- var hostConnStr = _connectionStringResolver . GetNameOrConnectionString ( new ConnectionStringResolveArgs ( MultiTenancySides . Host ) ) ;
39+ var hostConnStr = CensorConnectionString ( _connectionStringResolver . GetNameOrConnectionString ( new ConnectionStringResolveArgs ( MultiTenancySides . Host ) ) ) ;
4040 if ( hostConnStr . IsNullOrWhiteSpace ( ) )
4141 {
42- Log . Write ( "Configuration file should contain a connection string named 'Default'" ) ;
42+ _log . Write ( "Configuration file should contain a connection string named 'Default'" ) ;
4343 return false ;
4444 }
4545
46- Log . Write ( "Host database: " + ConnectionStringHelper . GetConnectionString ( hostConnStr ) ) ;
46+ _log . Write ( "Host database: " + ConnectionStringHelper . GetConnectionString ( hostConnStr ) ) ;
4747 if ( ! skipConnVerification )
4848 {
49- Log . Write ( "Continue to migration for this host database and all tenants..? (Y/N): " ) ;
49+ _log . Write ( "Continue to migration for this host database and all tenants..? (Y/N): " ) ;
5050 var command = Console . ReadLine ( ) ;
5151 if ( ! command . IsIn ( "Y" , "y" ) )
5252 {
53- Log . Write ( "Migration canceled." ) ;
53+ _log . Write ( "Migration canceled." ) ;
5454 return false ;
5555 }
5656 }
5757
58- Log . Write ( "HOST database migration started..." ) ;
58+ _log . Write ( "HOST database migration started..." ) ;
5959
6060 try
6161 {
6262 _migrator . CreateOrMigrateForHost ( SeedHelper . SeedHostDb ) ;
6363 }
6464 catch ( Exception ex )
6565 {
66- Log . Write ( "An error occured during migration of host database:" ) ;
67- Log . Write ( ex . ToString ( ) ) ;
68- Log . Write ( "Canceled migrations." ) ;
66+ _log . Write ( "An error occured during migration of host database:" ) ;
67+ _log . Write ( ex . ToString ( ) ) ;
68+ _log . Write ( "Canceled migrations." ) ;
6969 return false ;
7070 }
7171
72- Log . Write ( "HOST database migration completed." ) ;
73- Log . Write ( "--------------------------------------------------------" ) ;
72+ _log . Write ( "HOST database migration completed." ) ;
73+ _log . Write ( "--------------------------------------------------------" ) ;
7474
7575 var migratedDatabases = new HashSet < string > ( ) ;
7676 var tenants = _tenantRepository . GetAllList ( t => t . ConnectionString != null && t . ConnectionString != "" ) ;
7777 for ( var i = 0 ; i < tenants . Count ; i ++ )
7878 {
7979 var tenant = tenants [ i ] ;
80- Log . Write ( string . Format ( "Tenant database migration started... ({0} / {1})" , ( i + 1 ) , tenants . Count ) ) ;
81- Log . Write ( "Name : " + tenant . Name ) ;
82- Log . Write ( "TenancyName : " + tenant . TenancyName ) ;
83- Log . Write ( "Tenant Id : " + tenant . Id ) ;
84- Log . Write ( "Connection string : " + SimpleStringCipher . Instance . Decrypt ( tenant . ConnectionString ) ) ;
80+ _log . Write ( string . Format ( "Tenant database migration started... ({0} / {1})" , ( i + 1 ) , tenants . Count ) ) ;
81+ _log . Write ( "Name : " + tenant . Name ) ;
82+ _log . Write ( "TenancyName : " + tenant . TenancyName ) ;
83+ _log . Write ( "Tenant Id : " + tenant . Id ) ;
84+ _log . Write ( "Connection string : " + SimpleStringCipher . Instance . Decrypt ( tenant . ConnectionString ) ) ;
8585
8686 if ( ! migratedDatabases . Contains ( tenant . ConnectionString ) )
8787 {
@@ -91,25 +91,41 @@ public bool Run(bool skipConnVerification)
9191 }
9292 catch ( Exception ex )
9393 {
94- Log . Write ( "An error occured during migration of tenant database:" ) ;
95- Log . Write ( ex . ToString ( ) ) ;
96- Log . Write ( "Skipped this tenant and will continue for others..." ) ;
94+ _log . Write ( "An error occured during migration of tenant database:" ) ;
95+ _log . Write ( ex . ToString ( ) ) ;
96+ _log . Write ( "Skipped this tenant and will continue for others..." ) ;
9797 }
9898
9999 migratedDatabases . Add ( tenant . ConnectionString ) ;
100100 }
101101 else
102102 {
103- Log . Write ( "This database has already migrated before (you have more than one tenant in same database). Skipping it...." ) ;
103+ _log . Write ( "This database has already migrated before (you have more than one tenant in same database). Skipping it...." ) ;
104104 }
105105
106- Log . Write ( string . Format ( "Tenant database migration completed. ({0} / {1})" , ( i + 1 ) , tenants . Count ) ) ;
107- Log . Write ( "--------------------------------------------------------" ) ;
106+ _log . Write ( string . Format ( "Tenant database migration completed. ({0} / {1})" , ( i + 1 ) , tenants . Count ) ) ;
107+ _log . Write ( "--------------------------------------------------------" ) ;
108108 }
109109
110- Log . Write ( "All databases have been migrated." ) ;
110+ _log . Write ( "All databases have been migrated." ) ;
111111
112112 return true ;
113113 }
114+
115+ private static string CensorConnectionString ( string connectionString )
116+ {
117+ var builder = new DbConnectionStringBuilder { ConnectionString = connectionString } ;
118+ var keysToMask = new [ ] { "password" , "pwd" , "user id" , "uid" } ;
119+
120+ foreach ( var key in keysToMask )
121+ {
122+ if ( builder . ContainsKey ( key ) )
123+ {
124+ builder [ key ] = "*****" ;
125+ }
126+ }
127+
128+ return builder . ToString ( ) ;
129+ }
114130 }
115131}
0 commit comments