Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

minor changes: #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions SampleTests/TestDbLocationModifiers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public void TestDbLocationModifierForImport()
options.ImportContributorArguments =
Utils.BuildContributorArguments(new Dictionary<string, string>()
{
{DbLocationModifier.DbSaveLocationArg, dataFolder},
{DbLocationModifier.DbSaveDataLocationArg, dataFolder},
{DbLocationModifier.DbSaveLogDataLocationArg, dataFolder},
{DbLocationModifier.DbFilePrefixArg, filePrefix},
});

Expand Down Expand Up @@ -184,7 +185,8 @@ private static DacDeployOptions SetLocationChangingContributorOptions(string dat
options.AdditionalDeploymentContributorArguments =
Utils.BuildContributorArguments(new Dictionary<string, string>()
{
{DbLocationModifier.DbSaveLocationArg, dataFolder},
{DbLocationModifier.DbSaveDataLocationArg, dataFolder},
{DbLocationModifier.DbSaveLogDataLocationArg, dataFolder},
{DbLocationModifier.DbFilePrefixArg, filePrefix},
});
return options;
Expand Down
41 changes: 30 additions & 11 deletions Samples/Contributors/DbLocationModifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,14 @@ public class DbLocationModifier : DeploymentPlanModifier
public const string ContributorId = "Public.Dac.Samples.Contributors.DbLocationModifier";

/// <summary>
/// Contributor argument defining the directory to save the MDF and LDF files for the database
/// Contributor argument defining the directory to save the MDF file for the database
/// </summary>
public const string DbSaveLocationArg = "DbLocationModifier.SaveLocation";
public const string DbSaveDataLocationArg = "DbLocationModifier.SaveDataLocation";

/// <summary>
/// Contributor argument defining the directory to save the LDF file for the database
/// </summary>
public const string DbSaveLogDataLocationArg = "DbLocationModifier.SaveLogDataLocation";

/// <summary>
/// Contributor argument defining the prefix to use for the database files
Expand All @@ -72,16 +77,30 @@ public class DbLocationModifier : DeploymentPlanModifier
/// </summary>
/// <param name="context"></param>
protected override void OnExecute(DeploymentPlanContributorContext context)
{
{
// Run only if a location is defined and we're targeting a serverless (LocalDB) instance
string location, filePrefix;
if (context.Arguments.TryGetValue(DbSaveLocationArg, out location)
string datalocation, logdatalocation, filePrefix;
if (context.Arguments.TryGetValue(DbSaveDataLocationArg, out datalocation)
&& context.Arguments.TryGetValue(DbFilePrefixArg, out filePrefix))
{
if (TargetConnectionMatchesPattern(context))
logdatalocation = context.Arguments.TryGetValue(DbSaveLogDataLocationArg, out logdatalocation) ? logdatalocation : datalocation;
//Assuming the path for SQL Server on Linux starts with "/"
if (datalocation.StartsWith("/") && logdatalocation.StartsWith("/"))
{

if (TargetConnectionMatchesPattern(context))
{
ChangeNewDatabaseLocation(context, datalocation, logdatalocation, filePrefix);
}
}
else
{
location = new DirectoryInfo(location).FullName + "\\";
ChangeNewDatabaseLocation(context, location, filePrefix);
if (TargetConnectionMatchesPattern(context))
{
datalocation = new DirectoryInfo(datalocation).FullName + "\\";
logdatalocation = new DirectoryInfo(logdatalocation).FullName + "\\";
ChangeNewDatabaseLocation(context, datalocation, logdatalocation, filePrefix);
}
}
}
}
Expand All @@ -98,7 +117,7 @@ private bool TargetConnectionMatchesPattern(DeploymentPlanContributorContext con
return true;
}

private void ChangeNewDatabaseLocation(DeploymentPlanContributorContext context, string location, string filePrefix)
private void ChangeNewDatabaseLocation(DeploymentPlanContributorContext context, string datalocation, string logdatalocation, string filePrefix)
{
DeploymentStep nextStep = context.PlanHandle.Head;

Expand Down Expand Up @@ -129,9 +148,9 @@ private void ChangeNewDatabaseLocation(DeploymentPlanContributorContext context,

// Override setvars before the deployment begins
StringBuilder sb = new StringBuilder();
sb.AppendFormat(":setvar DefaultDataPath \"{0}\"", location)
sb.AppendFormat(":setvar DefaultDataPath \"{0}\"", datalocation)
.AppendLine()
.AppendFormat(":setvar DefaultLogPath \"{0}\"", location)
.AppendFormat(":setvar DefaultLogPath \"{0}\"", logdatalocation)
.AppendLine()
.AppendFormat(":setvar DefaultFilePrefix \"{0}\"", filePrefix)
.AppendLine();
Expand Down