-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RDMP-15 Use .bak files as Data Loads (#1656)
* basic ui triggers * working dl --------- Co-authored-by: James A Sutherland <j@sutherland.pw>
- Loading branch information
Showing
9 changed files
with
178 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
Rdmp.Core/DataLoad/Engine/LoadExecution/Components/Runtime/ExecuteSqlBakFileRuntimeTask.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// Copyright (c) The University of Dundee 2018-2023 | ||
// This file is part of the Research Data Management Platform (RDMP). | ||
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. | ||
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
using System; | ||
using System.Data; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Data.SqlClient; | ||
using MongoDB.Driver.Core.Servers; | ||
using Rdmp.Core.Curation.Data.DataLoad; | ||
using Rdmp.Core.DataFlowPipeline; | ||
using Rdmp.Core.DataLoad.Engine.Job; | ||
using Rdmp.Core.DataLoad.Engine.LoadExecution.Components.Arguments; | ||
using Rdmp.Core.DataLoad.Modules.Mutilators; | ||
using Rdmp.Core.ReusableLibraryCode.Checks; | ||
using Rdmp.Core.ReusableLibraryCode.Progress; | ||
|
||
namespace Rdmp.Core.DataLoad.Engine.LoadExecution.Components.Runtime; | ||
|
||
/// <summary> | ||
/// RuntimeTask that executes a single .bak file specified by the user in a ProcessTask with ProcessTaskType SQLBakFile. | ||
/// </summary> | ||
public class ExecuteSqlBakFileRuntimeTask : RuntimeTask | ||
{ | ||
public string Filepath; | ||
private readonly IProcessTask _task; | ||
|
||
private LoadStage _loadStage; | ||
|
||
public ExecuteSqlBakFileRuntimeTask(IProcessTask task, RuntimeArgumentCollection args) : base(task, args) | ||
{ | ||
_task = task; | ||
Filepath = task.Path; | ||
} | ||
|
||
public override ExitCodeType Run(IDataLoadJob job, GracefulCancellationToken cancellationToken) | ||
{ | ||
var db = RuntimeArguments.StageSpecificArguments.DbInfo; | ||
_loadStage = RuntimeArguments.StageSpecificArguments.LoadStage; | ||
|
||
if (!Exists()) | ||
throw new Exception($"The sql bak file {Filepath} does not exist"); | ||
|
||
string fileOnlyCommand = $"RESTORE FILELISTONLY FROM DISK = '{Filepath}'"; | ||
using var fileInfo = new DataTable(); | ||
using var con = (SqlConnection)db.Server.GetConnection(); | ||
using (var cmd = new SqlCommand(fileOnlyCommand, con)) | ||
using (var da = new SqlDataAdapter(cmd)) | ||
da.Fill(fileInfo); | ||
DataRow[] primaryFiles = fileInfo.Select("Type = 'D'"); | ||
DataRow[] logFiles = fileInfo.Select("Type = 'L'"); | ||
if (primaryFiles.Length != 1 || logFiles.Length != 1) | ||
{ | ||
//Something has gone wrong | ||
return ExitCodeType.Error; | ||
} | ||
|
||
|
||
DataRow primaryFile = primaryFiles[0]; | ||
DataRow logFile = logFiles[0]; | ||
string primaryFilePhysicalName = primaryFile["PhysicalName"].ToString(); | ||
string logFilePhysicalName = logFile["PhysicalName"].ToString(); | ||
|
||
if (File.Exists(primaryFilePhysicalName) || File.Exists(logFilePhysicalName)) | ||
{ | ||
string timestamp = DateTime.Now.Millisecond.ToString(); | ||
string primaryFileName = primaryFilePhysicalName.Substring(0, primaryFilePhysicalName.Length - 4); | ||
string primaryFileExtention = primaryFilePhysicalName.Substring(primaryFilePhysicalName.Length - 4); | ||
primaryFilePhysicalName = $"{primaryFileName}_{timestamp}{primaryFileExtention}"; | ||
string logFileName = logFilePhysicalName.Substring(0, logFilePhysicalName.Length - 4); | ||
string logFileExtention = logFilePhysicalName.Substring(logFilePhysicalName.Length - 4); | ||
logFilePhysicalName = $"{logFileName}_{timestamp}{logFileExtention}"; | ||
} | ||
|
||
string name = db.ToString(); | ||
|
||
string restoreCommand = @$" | ||
use master; | ||
ALTER DATABASE {name} SET SINGLE_USER WITH ROLLBACK IMMEDIATE; | ||
RESTORE DATABASE {name} | ||
FROM DISK = '{Filepath}' | ||
WITH MOVE '{primaryFile["LogicalName"]}' TO '{primaryFilePhysicalName}', | ||
MOVE '{logFile["LogicalName"]}' TO '{logFilePhysicalName}' , NOUNLOAD, REPLACE, STATS = 5; | ||
ALTER DATABASE {name} SET MULTI_USER; | ||
"; | ||
|
||
job.OnNotify(this, new NotifyEventArgs(ProgressEventType.Information, | ||
$"Executing script {Filepath} ( against {db})")); | ||
|
||
var executer = new ExecuteSqlInDleStage(job, _loadStage); | ||
return executer.Execute(restoreCommand, db); | ||
} | ||
|
||
|
||
public override bool Exists() => File.Exists(Filepath); | ||
|
||
public override void Abort(IDataLoadEventListener postLoadEventListener) | ||
{ | ||
} | ||
|
||
public override void LoadCompletedSoDispose(ExitCodeType exitCode, IDataLoadEventListener postLoadEventListener) | ||
{ | ||
} | ||
|
||
public override void Check(ICheckNotifier notifier) | ||
{ | ||
if (string.IsNullOrWhiteSpace(Filepath)) | ||
{ | ||
notifier.OnCheckPerformed( | ||
new CheckEventArgs($"ExecuteSqlFileTask {_task} does not have a path specified", | ||
CheckResult.Fail)); | ||
return; | ||
} | ||
|
||
if (!File.Exists(Filepath)) | ||
notifier.OnCheckPerformed( | ||
new CheckEventArgs( | ||
$"File '{Filepath}' does not exist! (the only time this would be legal is if you have an exe or a freaky plugin that creates this file)", | ||
CheckResult.Warning)); | ||
else | ||
notifier.OnCheckPerformed(new CheckEventArgs($"Found File '{Filepath}'", | ||
CheckResult.Success)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters