Skip to content

Commit a894188

Browse files
authored
feat(restore): logical file rename (#102)
* feat(restore): logical file rename * fix(logical rename): after chain restored * refactor(logical rename): files arrays * fix(rename): rename on primary only * Revert "fix(rename): rename on primary only" This reverts commit 2c151d7. * feat(renamer): public func * refactor(method): name * feat(move): logical rename on finalize
1 parent 47aaae4 commit a894188

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/AgDatabase.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface IAgDatabase
3434
void Restore(IEnumerable<BackupMetadata> backupOrder, Func<int, TimeSpan> retryDurationProvider,
3535
Func<string, string> fileRelocation = null);
3636

37+
void RenameLogicalFileName(Func<string, string> fileRenamer);
3738
void AddLogin(LoginProperties login);
3839
IEnumerable<LoginProperties> AssociatedLogins();
3940
void DropLogin(LoginProperties login);
@@ -118,6 +119,11 @@ public void Restore(IEnumerable<BackupMetadata> backupOrder, Func<int, TimeSpan>
118119
_listener.ForEachAgInstance(s => s.Restore(backupOrder, Name, retryDurationProvider, fileRelocation));
119120
}
120121

122+
public void RenameLogicalFileName(Func<string, string> fileRenamer)
123+
{
124+
_listener.Primary.RenameLogicalFileName(Name, fileRenamer);
125+
}
126+
121127
/// <summary>
122128
/// Builds a list of recent backups from msdb on each AG instance.
123129
/// </summary>

src/AgDatabaseMove.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public decimal Move(decimal? lastLsn = null)
8080
foreach(var loginProperty in _options.Source.AssociatedLogins().Select(UpdateDefaultDb))
8181
_options.Destination.AddLogin(loginProperty);
8282

83-
if(_options.Finalize)
83+
if(_options.Finalize) {
8484
_options.Destination.JoinAg();
85+
_options.Destination.RenameLogicalFileName(_options.FileRelocator);
86+
}
8587

8688
return backupList.Max(bl => bl.LastLsn);
8789
}

src/SmoFacade/Database.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace AgDatabaseMove.SmoFacade
1212
/// </summary>
1313
public class Database
1414
{
15-
private readonly Microsoft.SqlServer.Management.Smo.Database _database;
15+
internal readonly Microsoft.SqlServer.Management.Smo.Database _database;
1616
private readonly Server _server;
1717

1818
internal Database(Microsoft.SqlServer.Management.Smo.Database database, Server server)

src/SmoFacade/Server.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,23 @@ public void Restore(IEnumerable<BackupMetadata> backupOrder, string databaseName
227227
}
228228
}
229229

230+
public void RenameLogicalFileName(string databaseName, Func<string, string> fileRenamer)
231+
{
232+
var db = Database(databaseName);
233+
234+
foreach (FileGroup fileGroup in db._database.FileGroups)
235+
{
236+
var dataFiles = new DataFile[fileGroup.Files.Count];
237+
fileGroup.Files.CopyTo(dataFiles, 0);
238+
foreach (var dataFile in dataFiles)
239+
dataFile.Rename(fileRenamer(dataFile.Name));
240+
}
241+
var logFiles = new LogFile[db._database.LogFiles.Count];
242+
db._database.LogFiles.CopyTo(logFiles, 0);
243+
foreach (var logFile in logFiles)
244+
logFile.Rename(fileRenamer(logFile.Name));
245+
}
246+
230247
/// <summary>
231248
/// Generate a log backup, truncating the transaction log, and storing it in the default backup destination.
232249
/// TODO: we should support a backup path somehow with configuration

0 commit comments

Comments
 (0)