Skip to content

Commit

Permalink
Adding repository version and upgrade/downgrade. All unit tested.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfred Hall committed Mar 10, 2011
1 parent cf741ac commit 90d806c
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 59 deletions.
69 changes: 10 additions & 59 deletions Sector.Tests/MigrateApiTest.cs
Expand Up @@ -14,71 +14,22 @@ namespace Sector.Tests
[TestFixture()]
public class MigrateApiTest
{
private const string DbServer = "localhost";
private const string DbUsername = "ahall";
private const string DbPassword = "temp123";
private const string Database = "sector_test";

private const string CONNSTRING_TEMPLATE =
@"Server={0};Database={1};User Id={2};Password={3}";

private static readonly string ConnectionString = string.Format(CONNSTRING_TEMPLATE, DbServer,
Database, DbUsername, DbPassword);
private static readonly string TestFiles = GetTestFilesDir();
private ISessionFactory dbFactory;

private static IPersistenceConfigurer BuildDatabase()
{
return PostgreSQLConfiguration.PostgreSQL82.ConnectionString(ConnectionString);
}

private static void BuildSchema(NHibernate.Cfg.Configuration cfg)
{
new SchemaExport(cfg).Create(script: false, export: true);
}

private static ISessionFactory MakeFactory(bool export = false)
{
FluentConfiguration dbCfg = Fluently.Configure().Database(BuildDatabase());
dbCfg.Mappings(x => x.FluentMappings.Add<MigrateVersionMap>());

if (export)
dbCfg.ExposeConfiguration(BuildSchema);
return dbCfg.BuildSessionFactory();
}

[SetUp]
public void Setup()
{
// Clean out schema to start fresh.
MakeFactory(export: true).Dispose();

dbFactory = MakeFactory(export: false);
}

private static string GetTestFilesDir()
{
string solnDir = new DirectoryInfo(Environment.CurrentDirectory)
.Parent.Parent.FullName;
return Path.Combine(solnDir, "testfiles");
}
TestUtils.MakeFactory(export: true).Dispose();

private static Repository MakeRepository()
{
return new Repository(Path.Combine(TestFiles, "repo"));
}

private ISectorDb MakeSectorDb()
{
return new SectorDb(server: DbServer, username: DbUsername,
password: DbPassword, database: Database);
dbFactory = TestUtils.MakeFactory(export: false);
}

[Test()]
public void IsVersionControlled()
{
var sectorDb = MakeSectorDb();
var repository = MakeRepository();
var sectorDb = TestUtils.MakeSectorDb();
var repository = TestUtils.MakeRepository();

MigrateApi migrateApi = new MigrateApi(sectorDb);
bool success = migrateApi.IsVersionControlled(repository);
Expand All @@ -100,8 +51,8 @@ public void IsVersionControlled()
[Test()]
public void VersionControl()
{
var sectorDb = MakeSectorDb();
var repository = MakeRepository();
var sectorDb = TestUtils.MakeSectorDb();
var repository = TestUtils.MakeRepository();

MigrateApi migrateApi = new MigrateApi(sectorDb);
Assert.IsFalse(migrateApi.IsVersionControlled(repository));
Expand Down Expand Up @@ -133,8 +84,8 @@ public void VersionControl()
[ExpectedException(typeof(Exception))]
public void GetDbVersion_EmptyDb()
{
var sectorDb = MakeSectorDb();
var repository = MakeRepository();
var sectorDb = TestUtils.MakeSectorDb();
var repository = TestUtils.MakeRepository();

MigrateApi migrateApi = new MigrateApi(sectorDb);
migrateApi.GetDbVersion(repository);
Expand All @@ -143,8 +94,8 @@ public void GetDbVersion_EmptyDb()
[Test()]
public void GetDbVersion()
{
var sectorDb = MakeSectorDb();
var repository = MakeRepository();
var sectorDb = TestUtils.MakeSectorDb();
var repository = TestUtils.MakeRepository();

MigrateApi migrateApi = new MigrateApi(sectorDb);

Expand Down
66 changes: 66 additions & 0 deletions Sector.Tests/RepositoryTest.cs
@@ -0,0 +1,66 @@
using System;
using NUnit.Framework;

namespace Sector.Tests
{
[TestFixture()]
public class RepositoryTest
{
[Test()]
public void GetVersion()
{
var repository = TestUtils.MakeRepository();
Assert.AreEqual(2, repository.GetVersion());
}

[Test()]
[ExpectedException(typeof(Exception))]
public void GetUpgradeSql_Invalid()
{
var repository = TestUtils.MakeRepository();
repository.GetUpgradeSql(232323);
}

[Test()]
public void GetUpgradeSql()
{
var repository = TestUtils.MakeRepository();

string version1 = @"CREATE TABLE testie(
id serial PRIMARY KEY NOT NULL,
age integer NOT NULL UNIQUE,
description varchar(255)
);
";
Assert.AreEqual(version1, repository.GetUpgradeSql(1));

string version2 = @"CREATE TABLE moon(
id serial PRIMARY KEY NOT NULL,
age2 integer NOT NULL UNIQUE,
description2 varchar(255)
);
";
Assert.AreEqual(version2, repository.GetUpgradeSql(2));
}

[Test()]
[ExpectedException(typeof(Exception))]
public void GetDowngradeSql_Invalid()
{
var repository = TestUtils.MakeRepository();
repository.GetDowngradeSql(232323);
}

[Test()]
public void GetDowngradeSql()
{
var repository = TestUtils.MakeRepository();

string version1 = @"DROP TABLE testie;" + Environment.NewLine;
Assert.AreEqual(version1, repository.GetDowngradeSql(1));

string version2 = @"DROP TABLE moon;" + Environment.NewLine;
Assert.AreEqual(version2, repository.GetDowngradeSql(2));
}
}
}
2 changes: 2 additions & 0 deletions Sector.Tests/Sector.Tests.csproj
Expand Up @@ -44,6 +44,8 @@
</ItemGroup>
<ItemGroup>
<Compile Include="MigrateApiTest.cs" />
<Compile Include="RepositoryTest.cs" />
<Compile Include="TestUtils.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
65 changes: 65 additions & 0 deletions Sector.Tests/TestUtils.cs
@@ -0,0 +1,65 @@
using System;
using NHibernate.Tool.hbm2ddl;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Cfg;
using Sector.Mappings;
using NHibernate;
using System.IO;

namespace Sector.Tests
{
public static class TestUtils
{
private const string DbServer = "localhost";
private const string DbUsername = "ahall";
private const string DbPassword = "temp123";
private const string Database = "sector_test";

private static readonly string ConnectionString = string.Format(CONNSTRING_TEMPLATE, DbServer,
Database, DbUsername, DbPassword);
private static readonly string TestFiles = GetTestFilesDir();

private const string CONNSTRING_TEMPLATE =
@"Server={0};Database={1};User Id={2};Password={3}";


private static string GetTestFilesDir()
{
string solnDir = new DirectoryInfo(Environment.CurrentDirectory)
.Parent.Parent.FullName;
return Path.Combine(solnDir, "testfiles");
}

private static IPersistenceConfigurer BuildDatabase()
{
return PostgreSQLConfiguration.PostgreSQL82.ConnectionString(ConnectionString);
}

private static void BuildSchema(NHibernate.Cfg.Configuration cfg)
{
new SchemaExport(cfg).Create(script: false, export: true);
}

public static ISessionFactory MakeFactory(bool export = false)
{
FluentConfiguration dbCfg = Fluently.Configure().Database(BuildDatabase());
dbCfg.Mappings(x => x.FluentMappings.Add<MigrateVersionMap>());

if (export)
dbCfg.ExposeConfiguration(BuildSchema);
return dbCfg.BuildSessionFactory();
}

public static Repository MakeRepository()
{
return new Repository(Path.Combine(TestFiles, "repo"));
}

public static ISectorDb MakeSectorDb()
{
return new SectorDb(server: DbServer, username: DbUsername,
password: DbPassword, database: Database);
}
}
}

31 changes: 31 additions & 0 deletions Sector/IRepository.cs
Expand Up @@ -6,6 +6,37 @@ public interface IRepository
{
string RepositoryId { get; }
string RepositoryPath { get; }

/// <summary>
/// Gets the version.
/// </summary>
/// <returns>
/// Returns the version of the repository, the latest version
/// available.
/// </returns>
int GetVersion();

/// <summary>
/// Gets the upgrade sql.
/// </summary>
/// <returns>
/// The sql for the version given.
/// </returns>
/// <param name='version'>
/// Version.
/// </param>
string GetUpgradeSql(int version);

/// <summary>
/// Gets the downgrade sql.
/// </summary>
/// <returns>
/// The sql for the version given.
/// </returns>
/// <param name='version'>
/// Version.
/// </param>
string GetDowngradeSql(int version);
}
}

47 changes: 47 additions & 0 deletions Sector/Repository.cs
@@ -1,6 +1,8 @@
using System;
using Nini.Config;
using System.Linq;
using System.IO;
using System.Collections.Generic;

namespace Sector
{
Expand All @@ -11,6 +13,8 @@ public class Repository : IRepository
{
public string RepositoryId { get; private set; }
public string RepositoryPath { get; private set; }
private string versionDir;
private ISet<int> versions;

IConfigSource configSource;
IConfig mainConfig;
Expand All @@ -22,8 +26,51 @@ public Repository(string repoPath)
configSource = new IniConfigSource(configPath);
mainConfig = configSource.Configs["main"];
RepositoryId = mainConfig.GetString("repository_id");
versionDir = Path.Combine(RepositoryPath, "versions");
versions = new HashSet<int>();
ScanFiles();
}

private void ScanFiles()
{
foreach (FileInfo file in new DirectoryInfo(versionDir).GetFiles("*.sql"))
{
var elements = file.Name.Split(new char[] { '_' }, 2);
int version;
if ((elements.Length < 2) || (!int.TryParse(elements[0], out version)))
{
// Skip files that are not e.g. 1_blah.sql
continue;
}

versions.Add(version);
}
}

public int GetVersion()
{
return versions.Max();
}

public string GetUpgradeSql(int version)
{
if (!versions.Contains(version))
throw new Exception("Repository does not contain this version");

string filename = string.Format("{0}_upgrade.sql", version);
string fullPath = Path.Combine(versionDir, filename);
return File.ReadAllText(fullPath);
}

public string GetDowngradeSql(int version)
{
if (!versions.Contains(version))
throw new Exception("Repository does not contain this version");

string filename = string.Format("{0}_downgrade.sql", version);
string fullPath = Path.Combine(versionDir, filename);
return File.ReadAllText(fullPath);
}
}
}

1 change: 1 addition & 0 deletions Sector/Sector.csproj
Expand Up @@ -69,6 +69,7 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Libs\Nini.dll</HintPath>
</Reference>
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
Expand Down

0 comments on commit 90d806c

Please sign in to comment.