Browse Source
Merge pull request #15360 from abpframework/IAbpConnectionStringChecker
Add `IAbpConnectionStringChecker`.
pull/15362/head
Galip Tolga Erdem
4 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with
304 additions and
0 deletions
-
framework/src/Volo.Abp.Data/Volo/Abp/Data/AbpConnectionStringCheckResult.cs
-
framework/src/Volo.Abp.Data/Volo/Abp/Data/DefaultConnectionStringChecker.cs
-
framework/src/Volo.Abp.Data/Volo/Abp/Data/IConnectionStringChecker.cs
-
framework/src/Volo.Abp.EntityFrameworkCore.MySQL/Volo/Abp/EntityFrameworkCore/ConnectionStrings/MySqlConnectionStringChecker.cs
-
framework/src/Volo.Abp.EntityFrameworkCore.Oracle.Devart/Volo/Abp/EntityFrameworkCore/ConnectionStrings/OracleDevartConnectionStringChecker.cs
-
framework/src/Volo.Abp.EntityFrameworkCore.Oracle/Volo/Abp/EntityFrameworkCore/ConnectionStrings/OracleConnectionStringChecker.cs
-
framework/src/Volo.Abp.EntityFrameworkCore.PostgreSql/Volo/Abp/EntityFrameworkCore/ConnectionStrings/NpgsqlConnectionStringChecker.cs
-
framework/src/Volo.Abp.EntityFrameworkCore.SqlServer/Volo/Abp/EntityFrameworkCore/ConnectionStrings/SqlServerConnectionStringChecker.cs
-
framework/src/Volo.Abp.EntityFrameworkCore.Sqlite/Volo/Abp/EntityFrameworkCore/ConnectionStrings/SqliteConnectionStringChecker.cs
-
framework/src/Volo.Abp.MongoDB/Volo/Abp/MongoDB/ConnectionStrings/MongoDBConnectionStringChecker.cs
-
framework/test/Volo.Abp.EntityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/ConnectionStrings/AbpConnectionStringChecker_Tests.cs
|
|
|
@ -0,0 +1,8 @@ |
|
|
|
namespace Volo.Abp.Data; |
|
|
|
|
|
|
|
public class AbpConnectionStringCheckResult |
|
|
|
{ |
|
|
|
public bool Connected { get; set; } |
|
|
|
|
|
|
|
public bool DatabaseExists { get; set; } |
|
|
|
} |
|
|
|
@ -0,0 +1,16 @@ |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.Data; |
|
|
|
|
|
|
|
public class DefaultConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
return Task.FromResult(new AbpConnectionStringCheckResult |
|
|
|
{ |
|
|
|
Connected = false, |
|
|
|
DatabaseExists = false |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,8 @@ |
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
namespace Volo.Abp.Data; |
|
|
|
|
|
|
|
public interface IConnectionStringChecker |
|
|
|
{ |
|
|
|
Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString); |
|
|
|
} |
|
|
|
@ -0,0 +1,40 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using MySqlConnector; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class MySqlConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
var result = new AbpConnectionStringCheckResult(); |
|
|
|
var connString = new MySqlConnectionStringBuilder(connectionString) |
|
|
|
{ |
|
|
|
ConnectionLifeTime = 1 |
|
|
|
}; |
|
|
|
|
|
|
|
var oldDatabaseName = connString.Database; |
|
|
|
connString.Database = "mysql"; |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await using var conn = new MySqlConnection(connString.ConnectionString); |
|
|
|
await conn.OpenAsync(); |
|
|
|
result.Connected = true; |
|
|
|
await conn.ChangeDatabaseAsync(oldDatabaseName); |
|
|
|
result.DatabaseExists = true; |
|
|
|
|
|
|
|
await conn.CloseAsync(); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,36 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Devart.Data.Oracle; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class OracleDevartConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
var result = new AbpConnectionStringCheckResult(); |
|
|
|
var connString = new OracleConnectionStringBuilder(connectionString) |
|
|
|
{ |
|
|
|
ConnectionTimeout = 1 |
|
|
|
}; |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await using var conn = new OracleConnection(connString.ConnectionString); |
|
|
|
await conn.OpenAsync(); |
|
|
|
result.Connected = true; |
|
|
|
result.DatabaseExists = true; |
|
|
|
|
|
|
|
await conn.CloseAsync(); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,36 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Oracle.ManagedDataAccess.Client; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class OracleConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
var result = new AbpConnectionStringCheckResult(); |
|
|
|
var connString = new OracleConnectionStringBuilder(connectionString) |
|
|
|
{ |
|
|
|
ConnectionTimeout = 1 |
|
|
|
}; |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await using var conn = new OracleConnection(connString.ConnectionString); |
|
|
|
await conn.OpenAsync(); |
|
|
|
result.Connected = true; |
|
|
|
result.DatabaseExists = true; |
|
|
|
|
|
|
|
await conn.CloseAsync(); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,40 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Npgsql; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class NpgsqlConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
var result = new AbpConnectionStringCheckResult(); |
|
|
|
var connString = new NpgsqlConnectionStringBuilder(connectionString) |
|
|
|
{ |
|
|
|
Timeout = 1 |
|
|
|
}; |
|
|
|
|
|
|
|
var oldDatabaseName = connString.Database; |
|
|
|
connString.Database = "postgres"; |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await using var conn = new NpgsqlConnection(connString.ConnectionString); |
|
|
|
await conn.OpenAsync(); |
|
|
|
result.Connected = true; |
|
|
|
await conn.ChangeDatabaseAsync(oldDatabaseName); |
|
|
|
result.DatabaseExists = true; |
|
|
|
|
|
|
|
await conn.CloseAsync(); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,40 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.Data.SqlClient; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class SqlServerConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
var result = new AbpConnectionStringCheckResult(); |
|
|
|
var connString = new SqlConnectionStringBuilder(connectionString) |
|
|
|
{ |
|
|
|
ConnectTimeout = 1 |
|
|
|
}; |
|
|
|
|
|
|
|
var oldDatabaseName = connString.InitialCatalog; |
|
|
|
connString.InitialCatalog = "master"; |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await using var conn = new SqlConnection(connString.ConnectionString); |
|
|
|
await conn.OpenAsync(); |
|
|
|
result.Connected = true; |
|
|
|
await conn.ChangeDatabaseAsync(oldDatabaseName); |
|
|
|
result.DatabaseExists = true; |
|
|
|
|
|
|
|
await conn.CloseAsync(); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,32 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Microsoft.Data.Sqlite; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class SqliteConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual async Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
var result = new AbpConnectionStringCheckResult(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
|
await using var conn = new SqliteConnection(connectionString); |
|
|
|
await conn.OpenAsync(); |
|
|
|
result.Connected = true; |
|
|
|
result.DatabaseExists = true; |
|
|
|
|
|
|
|
await conn.CloseAsync(); |
|
|
|
|
|
|
|
return result; |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,30 @@ |
|
|
|
using System; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using MongoDB.Driver; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Volo.Abp.DependencyInjection; |
|
|
|
|
|
|
|
namespace Volo.Abp.MongoDB.ConnectionStrings; |
|
|
|
|
|
|
|
[Dependency(ReplaceServices = true)] |
|
|
|
public class MongoDBConnectionStringChecker : IConnectionStringChecker, ITransientDependency |
|
|
|
{ |
|
|
|
public virtual Task<AbpConnectionStringCheckResult> CheckAsync(string connectionString) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
var mongoUrl = MongoUrl.Create(connectionString); |
|
|
|
var client = new MongoClient(mongoUrl); |
|
|
|
client.GetDatabase(mongoUrl.DatabaseName); |
|
|
|
return Task.FromResult(new AbpConnectionStringCheckResult() |
|
|
|
{ |
|
|
|
Connected = true, |
|
|
|
DatabaseExists = true |
|
|
|
}); |
|
|
|
} |
|
|
|
catch (Exception e) |
|
|
|
{ |
|
|
|
return Task.FromResult(new AbpConnectionStringCheckResult()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -0,0 +1,18 @@ |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Shouldly; |
|
|
|
using Volo.Abp.Data; |
|
|
|
using Xunit; |
|
|
|
|
|
|
|
namespace Volo.Abp.EntityFrameworkCore.ConnectionStrings; |
|
|
|
|
|
|
|
public class AbpConnectionStringChecker_Tests : EntityFrameworkCoreTestBase |
|
|
|
{ |
|
|
|
[Fact] |
|
|
|
public async Task IsValidAsync() |
|
|
|
{ |
|
|
|
var connectionStringChecker = GetRequiredService<IConnectionStringChecker>(); |
|
|
|
var result = await connectionStringChecker.CheckAsync(@"Data Source=:memory:"); |
|
|
|
result.Connected.ShouldBeTrue(); |
|
|
|
result.DatabaseExists.ShouldBeTrue(); |
|
|
|
} |
|
|
|
} |