1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Data . Common ;
3
4
using Abp . Data ;
4
5
using Abp . Dependency ;
5
6
using Abp . Domain . Repositories ;
@@ -15,8 +16,7 @@ namespace AbpCompanyName.AbpProjectName.Migrator
15
16
{
16
17
public class MultiTenantMigrateExecuter : ITransientDependency
17
18
{
18
- public Log Log { get ; private set ; }
19
-
19
+ private readonly Log _log ;
20
20
private readonly AbpZeroDbMigrator _migrator ;
21
21
private readonly IRepository < Tenant > _tenantRepository ;
22
22
private readonly IDbPerTenantConnectionStringResolver _connectionStringResolver ;
@@ -27,7 +27,7 @@ public MultiTenantMigrateExecuter(
27
27
Log log ,
28
28
IDbPerTenantConnectionStringResolver connectionStringResolver )
29
29
{
30
- Log = log ;
30
+ _log = log ;
31
31
32
32
_migrator = migrator ;
33
33
_tenantRepository = tenantRepository ;
@@ -36,52 +36,52 @@ public MultiTenantMigrateExecuter(
36
36
37
37
public bool Run ( bool skipConnVerification )
38
38
{
39
- var hostConnStr = _connectionStringResolver . GetNameOrConnectionString ( new ConnectionStringResolveArgs ( MultiTenancySides . Host ) ) ;
39
+ var hostConnStr = CensorConnectionString ( _connectionStringResolver . GetNameOrConnectionString ( new ConnectionStringResolveArgs ( MultiTenancySides . Host ) ) ) ;
40
40
if ( hostConnStr . IsNullOrWhiteSpace ( ) )
41
41
{
42
- Log . Write ( "Configuration file should contain a connection string named 'Default'" ) ;
42
+ _log . Write ( "Configuration file should contain a connection string named 'Default'" ) ;
43
43
return false ;
44
44
}
45
45
46
- Log . Write ( "Host database: " + ConnectionStringHelper . GetConnectionString ( hostConnStr ) ) ;
46
+ _log . Write ( "Host database: " + ConnectionStringHelper . GetConnectionString ( hostConnStr ) ) ;
47
47
if ( ! skipConnVerification )
48
48
{
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): " ) ;
50
50
var command = Console . ReadLine ( ) ;
51
51
if ( ! command . IsIn ( "Y" , "y" ) )
52
52
{
53
- Log . Write ( "Migration canceled." ) ;
53
+ _log . Write ( "Migration canceled." ) ;
54
54
return false ;
55
55
}
56
56
}
57
57
58
- Log . Write ( "HOST database migration started..." ) ;
58
+ _log . Write ( "HOST database migration started..." ) ;
59
59
60
60
try
61
61
{
62
62
_migrator . CreateOrMigrateForHost ( SeedHelper . SeedHostDb ) ;
63
63
}
64
64
catch ( Exception ex )
65
65
{
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." ) ;
69
69
return false ;
70
70
}
71
71
72
- Log . Write ( "HOST database migration completed." ) ;
73
- Log . Write ( "--------------------------------------------------------" ) ;
72
+ _log . Write ( "HOST database migration completed." ) ;
73
+ _log . Write ( "--------------------------------------------------------" ) ;
74
74
75
75
var migratedDatabases = new HashSet < string > ( ) ;
76
76
var tenants = _tenantRepository . GetAllList ( t => t . ConnectionString != null && t . ConnectionString != "" ) ;
77
77
for ( var i = 0 ; i < tenants . Count ; i ++ )
78
78
{
79
79
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 ) ) ;
85
85
86
86
if ( ! migratedDatabases . Contains ( tenant . ConnectionString ) )
87
87
{
@@ -91,25 +91,41 @@ public bool Run(bool skipConnVerification)
91
91
}
92
92
catch ( Exception ex )
93
93
{
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..." ) ;
97
97
}
98
98
99
99
migratedDatabases . Add ( tenant . ConnectionString ) ;
100
100
}
101
101
else
102
102
{
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...." ) ;
104
104
}
105
105
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 ( "--------------------------------------------------------" ) ;
108
108
}
109
109
110
- Log . Write ( "All databases have been migrated." ) ;
110
+ _log . Write ( "All databases have been migrated." ) ;
111
111
112
112
return true ;
113
113
}
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
+ }
114
130
}
115
131
}
0 commit comments