Skip to content

Commit

Permalink
require patch id when adding patch
Browse files Browse the repository at this point in the history
build task - add InstallPatch(); start build logic
add testing constructor
  • Loading branch information
ormico committed Mar 6, 2016
1 parent 03dc189 commit e914e39
Showing 1 changed file with 88 additions and 21 deletions.
109 changes: 88 additions & 21 deletions src/Ormico.DbPatchManager/PatchManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,32 @@ public class PatchManager
public PatchManager(string ConfigFile)
{
_configFileName = ConfigFile;
_rand = new Random();
_io = new FileSystem();
}

/// <summary>
/// Testing constructor. This constructor allows setting
/// the the Random and FileSystem objects in addition to the normal
/// constructor parameter(s).
/// </summary>
/// <param name="ConfigFile"></param>
/// <param name="Rand"></param>
/// <param name="Io"></param>
public PatchManager(string ConfigFile, Random Rand, FileSystem Io)
{
_configFileName = ConfigFile;
_rand = Rand;
_io = Io;
}

readonly string _configFileName;
readonly Random _rand;

/// <summary>
/// Use System.IO.Abstraction to make testing easier.
/// </summary>
FileSystem _io = new FileSystem();
readonly FileSystem _io;

public void InitConfig(InitOptions Options)
{
Expand All @@ -34,38 +52,87 @@ public void InitConfig(InitOptions Options)
}

public void AddPatch(string PatchID, PatchOptions Options = null)
{
if(string.IsNullOrWhiteSpace(PatchID))
{
throw new ApplicationException("Patch ID required");
}
else
{
var cfgWriter = new BuildConfigurationWriter(_configFileName);
var cfg = cfgWriter.Read();

//create unique id prefix to avoid collisions
string prefix = string.Format("{0:yyyyMMddHHmm}-{1:0000}",
DateTime.Now,
_rand.Next(0, 9999));
string finalId = string.Format("{0}-{1}", prefix, PatchID.Trim());

if(!_io.Directory.Exists(finalId))
{
_io.Directory.CreateDirectory(finalId);

// when adding a patch, make it dependent on all patches
// that don't already have a dependency.
// need to guard against circular dependencies
var openPatches = cfg.GetOpenPatches();
cfg.patches.Add(new Patch(finalId, openPatches));
cfgWriter.Write(cfg);
}
else
{
// create custom exception
throw new ApplicationException(string.Format("A folder named '{0}' already exists", finalId));
}
}
}

public void Build()
{
var cfgWriter = new BuildConfigurationWriter(_configFileName);
var cfg = cfgWriter.Read();
var first = cfg.GetFirstPatch();
if(first != null)
{
using (var db = new TestDatabase())
{
var installedPatches = db.GetInstalledPatches();
db.Connect(cfg.ConnectionString);

//todo: log or console first patch
InstallPatch(first, db, installedPatches);

//create unique id prefix
string prefix = string.Format("{0:yyyyMMddHHmm}-{1:0000}",
DateTime.Now,
(new Random()).Next(0, 9999));

string finalId = string.Format("{0}-{1}", prefix, PatchID);
}
}
}

if(!_io.Directory.Exists(finalId))
private void InstallPatch(Patch patch, TestDatabase db, List<InstalledPatchInfo> installedPatches)
{
bool isInstalled = installedPatches.Any(i => string.Equals(i.Id, patch.Id));
if (!isInstalled)
{
_io.Directory.CreateDirectory(finalId);
var files = _io.Directory.GetFiles(patch.Id);
foreach (var file in files)
{
var ext = _io.Path.GetExtension(file);
if (string.Equals(ext, "sql", StringComparison.OrdinalIgnoreCase))
{
//todo: check file size before reading all text
string sql = _io.File.ReadAllText(file);
db.ExecuteDDL(sql);
}
else if (string.Equals(ext, "js", StringComparison.OrdinalIgnoreCase))
{

// when adding a patch, make it dependent on all patches
// that don't already have a dependency.
// need to guard against circular dependencies
var openPatches = cfg.GetOpenPatches();
cfg.patches.Add(new Patch(finalId, openPatches));
cfgWriter.Write(cfg);
}
}
db.LogInstalledPatch(patch.Id);
}
else
{
// create custom exception
throw new ApplicationException(string.Format("a folder named {0} already exists", finalId));
//todo: log or console that patch already installed
}
}

public void Build()
{

}
}
}

0 comments on commit e914e39

Please sign in to comment.