Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

コマンドタイムアウト秒を構成ファイルから取得するようにします。 #5

Merged
merged 1 commit into from
Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SQL Server データベースの完全バックアップ、および新しい場
"ConnectionString": "Data Source=localhost;Integrated Security=True",
"BackupDirectory": "C:\\DB\\BACKUP",
"RestoreDirectory": "C:\\DB\\RESTORE",
"CommandTimeoutSeconds": 60,
"Databases": [
{ "Name": "DB1" },
{ "Name": "DB2" },
Expand All @@ -52,6 +53,7 @@ SQL Server データベースの完全バックアップ、および新しい場
"ConnectionString": "Data Source=localhost;Integrated Security=True",
"BackupDirectory": "C:\\DB\\BACKUP",
"RestoreDirectory": "C:\\DB\\RESTORE",
"CommandTimeoutSeconds": 60,
"Databases": [
{ "Name": "DB1" },
{ "Name": "DB2" },
Expand All @@ -76,6 +78,7 @@ SQL Server データベースの完全バックアップ、および新しい場
|ConnectionString|接続文字列|`Data Source=localhost;Integrated Security=True`||
|BackupDirectory|`*.bak` の格納先フォルダ|`C:\\DB\\BACKUP`|ローカルフォルダのみ|
|RestoreDirectory|`*.bak` の復元先フォルダ|`C:\\DB\\RESTORE`|ローカルフォルダのみ、要アクセス権|
|CommandTimeoutSeconds|コマンドタイムアウト秒|60|既定値は60秒|
|Databases|データベース設定の配列||
|Databases:Name|データベース名|`DB1`|

Expand Down
9 changes: 9 additions & 0 deletions src/SqlDatabaseToolkit/SqlDatabaseOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ public class SqlDatabaseOptions
/// </value>
public string RestoreDirectory { get; set; } = null!;

/// <summary>
/// コマンドタイムアウト秒を取得または設定します。
/// </summary>
/// <value>
/// 値を表す <see cref="int" /> 型。
/// <para>コマンドタイムアウト秒。既定値は <see langword="60" /> です。</para>
/// </value>
public int CommandTimeoutSeconds { get; set; } = 60;

/// <summary>
/// データベース設定のコレクションを取得または設定します。
/// </summary>
Expand Down
23 changes: 17 additions & 6 deletions src/SqlDatabaseToolkit/SqlDatabaseToolkit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ private async Task BackupCoreAsync(
.ToString();

var connection = new SqlConnection(this._connectionString);
var commandTimeoutSeconds = this._options.CommandTimeoutSeconds;
try
{
this._logger?.LogDebug("完全バックアップを開始します。{Database} {Backup}", databaseName, backupFilePath);
Expand All @@ -114,7 +115,7 @@ await connection.ExecuteAsync(
sql,
new { databaseName, description, backupFilePath },
commandType: CommandType.Text,
commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds)
commandTimeout: commandTimeoutSeconds)
.ConfigureAwait(false);
this._logger?.LogInformation("完全バックアップが完了しました。{Database} {Backup}", databaseName, backupFilePath);
}
Expand Down Expand Up @@ -151,13 +152,21 @@ private async Task RestoreCoreAsync(
backupFilePath,
restoreDirectoryPath);

var commandTimeoutSeconds = this._options.CommandTimeoutSeconds;
var pairs = await GetFilePairsAsync(
this._connectionString,
backupFilePath,
restoreDirectoryPath,
commandTimeoutSeconds,
cancellationToken)
.ConfigureAwait(false);
await RestoreCurrentlyAsync(this._connectionString, databaseName, backupFilePath, pairs, cancellationToken)
await RestoreCurrentlyAsync(
this._connectionString,
databaseName,
backupFilePath,
pairs,
commandTimeoutSeconds,
cancellationToken)
.ConfigureAwait(false);

this._logger?.LogInformation(
Expand All @@ -184,6 +193,7 @@ static string ResolveNewPhysicalPath(string originalFilePath, string moveToDirec
string connectionString,
string backupFilePath,
string restoreDirectoryPath,
int commandTimeoutSeconds,
CancellationToken cancellationToken = default)
{
var connection = new SqlConnection(connectionString);
Expand All @@ -198,7 +208,7 @@ static string ResolveNewPhysicalPath(string originalFilePath, string moveToDirec
sql,
new { backupFilePath },
commandType: CommandType.Text,
commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds)
commandTimeout: commandTimeoutSeconds)
.ConfigureAwait(false);

var results = records
Expand All @@ -224,6 +234,7 @@ static async Task RestoreCurrentlyAsync(
string databaseName,
string backupFilePath,
IEnumerable<(string LogicalName, string PhysicalName, string MoveToFilePath)> filePairs,
int commandTimeoutSeconds,
CancellationToken cancellationToken = default)
{
var builder = new StringBuilder()
Expand All @@ -245,18 +256,18 @@ static async Task RestoreCurrentlyAsync(
await connection.ExecuteAsync(
$"ALTER DATABASE [{databaseName}] SET OFFLINE WITH ROLLBACK IMMEDIATE",
commandType: CommandType.Text,
commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds)
commandTimeout: commandTimeoutSeconds)
.ConfigureAwait(false);
await connection.ExecuteAsync(
sql,
new { databaseName, backupFilePath },
commandType: CommandType.Text,
commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds)
commandTimeout: commandTimeoutSeconds)
.ConfigureAwait(false);
await connection.ExecuteAsync(
$"ALTER DATABASE [{databaseName}] SET ONLINE",
commandType: CommandType.Text,
commandTimeout: (int?)TimeSpan.FromSeconds(10).TotalSeconds)
commandTimeout: commandTimeoutSeconds)
.ConfigureAwait(false);
}
finally
Expand Down
1 change: 1 addition & 0 deletions src/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"ConnectionString": "Data Source=localhost;Integrated Security=True",
"BackupDirectory": "C:\\DB\\BACKUP",
"RestoreDirectory": "C:\\DB\\RESTORE",
"CommandTimeoutSeconds": 30,
"Databases": [
{ "Name": "DB1" },
{ "Name": "DB2" },
Expand Down