Skip to content

Commit

Permalink
Refactor searchallinsteadoftraverse handling
Browse files Browse the repository at this point in the history
Add the FileLocator interface to abstract the selection of files from
the migration folders. This replaces the need for the Migration Runner
to explicitly decide what method to call on the FileSystemAccess
interface.

 - the Traverse class replaces the get_all_file_name_strings_in method.

 - the Recurse class replace the get_all_file_name_strings_recurevly_in
   method.

The dependency injection container picks the appropiate implementation
dependening on the prescene or lack of the /searchallinsteadoftraverse
switch.
  • Loading branch information
icedtoast committed Sep 6, 2012
1 parent 663b3c8 commit dd9afa2
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 91 deletions.
7 changes: 5 additions & 2 deletions product/roundhouse.console/Program.cs
Expand Up @@ -19,6 +19,7 @@
using migrators;
using resolvers;
using runners;
using roundhouse.infrastructure.filesystem.filelocators;

public class Program
{
Expand Down Expand Up @@ -417,15 +418,17 @@ private static RoundhouseMigrationRunner get_migration_runner(ConfigurationPrope
configuration.DoNotCreateDatabase,
configuration.WithTransaction,
configuration.RecoveryModeSimple,
configuration);
configuration,
Container.get_an_instance_of<FileLocator>());
}

private static RoundhouseRedGateCompareRunner get_diff_runner(ConfigurationPropertyHolder configuration, RoundhouseMigrationRunner migration_runner)
{
return new RoundhouseRedGateCompareRunner(
Container.get_an_instance_of<KnownFolders>(),
Container.get_an_instance_of<FileSystemAccess>(),
configuration, migration_runner);
configuration, migration_runner,
Container.get_an_instance_of<FileLocator>());
}
}
}
4 changes: 3 additions & 1 deletion product/roundhouse.tasks/Roundhouse.cs
Expand Up @@ -13,6 +13,7 @@
using resolvers;
using runners;
using Environment = environments.Environment;
using roundhouse.infrastructure.filesystem.filelocators;

public sealed class Roundhouse : ITask, ConfigurationPropertyHolder
{
Expand Down Expand Up @@ -160,7 +161,8 @@ public void run_the_task()
DoNotCreateDatabase,
WithTransaction,
RecoveryModeSimple,
this);
this,
Container.get_an_instance_of<FileLocator>());

roundhouse_runner.run();
}
Expand Down
4 changes: 3 additions & 1 deletion product/roundhouse/Migrate.cs
Expand Up @@ -13,6 +13,7 @@ namespace roundhouse
using resolvers;
using runners;
using Environment = roundhouse.environments.Environment;
using roundhouse.infrastructure.filesystem.filelocators;

public class Migrate
{
Expand Down Expand Up @@ -69,7 +70,8 @@ public void Run()
configuration.DoNotCreateDatabase,
configuration.WithTransaction,
configuration.RecoveryModeSimple,
configuration);
configuration,
Container.get_an_instance_of<FileLocator>());

migrator.run();
}
Expand Down
Expand Up @@ -23,6 +23,7 @@ namespace roundhouse.infrastructure.app
using StructureMap;
using Container = roundhouse.infrastructure.containers.Container;
using Environment = roundhouse.environments.Environment;
using roundhouse.infrastructure.filesystem.filelocators;

public static class ApplicationConfiguraton
{
Expand Down Expand Up @@ -176,6 +177,10 @@ private static InversionContainer build_items_for_container(ConfigurationPropert
cfg.For<VersionResolver>().Singleton().Use(
context => VersionResolverBuilder.build(context.GetInstance<FileSystemAccess>(), configuration_property_holder));
cfg.For<Environment>().Singleton().Use(new DefaultEnvironment(configuration_property_holder));
cfg.For<FileLocator>().Singleton().Use(
configuration_property_holder.SearchAllSubdirectoriesInsteadOfTraverse
? (FileLocator)new Recurse()
: new Traverse());
});

// forcing a build of database to initialize connections so we can be sure server/database have values
Expand Down
30 changes: 0 additions & 30 deletions product/roundhouse/infrastructure/filesystem/FileSystemAccess.cs
Expand Up @@ -117,13 +117,6 @@ public interface FileSystemAccess
/// <returns>The extension of the file.</returns>
string get_file_extension_from(string file_path);

/// <summary>
/// Gets a list of files inside of an existing directory
/// </summary>
/// <param name="directory">Path to the directory</param>
/// <returns>A list of files inside of an existing directory</returns>
string[] get_all_file_name_strings_in(string directory);

#endregion

#region Directory
Expand Down Expand Up @@ -176,29 +169,6 @@ public interface FileSystemAccess
/// <param name="recursive">Would you like to delete the directories inside of this directory? Almost always true.</param>
void delete_directory(string directory, bool recursive);

/// <summary>
/// Gets a list of directories inside of an existing directory
/// </summary>
/// <param name="directory">Directory to look for subdirectories in</param>
/// <returns>A list of subdirectories inside of the existing directory</returns>
string[] get_all_directory_name_strings_in(string directory);

/// <summary>
/// Gets a list of files inside of an existing directory
/// </summary>
/// <param name="directory">Path to the directory</param>
/// <param name="pattern">Pattern or extension</param>
/// <returns>A list of files inside of an existing directory</returns>
string[] get_all_file_name_strings_in(string directory, string pattern);

/// <summary>
/// Gets a list of all files inside of an existing directory, includes files in subdirectories also
/// </summary>
/// <param name="directory">Path to the directory</param>
/// <param name="pattern">Pattern or extension</param>
/// <returns>A list of files inside of an existing directory</returns>
string[] get_all_file_name_strings_recurevly_in(string directory, string pattern);

#endregion

/// <summary>
Expand Down
Expand Up @@ -328,50 +328,6 @@ public void delete_directory(string directory, bool recursive)
Directory.Delete(directory, recursive);
}

/// <summary>
/// Gets a list of directories inside of an existing directory
/// </summary>
/// <param name="directory">Directory to look for subdirectories in</param>
/// <returns>A list of subdirectories inside of the existing directory</returns>
public string[] get_all_directory_name_strings_in(string directory)
{
return Directory.GetDirectories(directory);
}

/// <summary>
/// Gets a list of files inside of an existing directory
/// </summary>
/// <param name="directory">Path to the directory</param>
/// <returns>A list of files inside of an existing directory</returns>
public string[] get_all_file_name_strings_in(string directory)
{
return get_all_file_name_strings_in(directory, "*.*");
}

/// <summary>
/// Gets a list of files inside of an existing directory
/// </summary>
/// <param name="directory">Path to the directory</param>
/// <param name="pattern">Pattern or extension</param>
/// <returns>A list of files inside of an existing directory</returns>
public string[] get_all_file_name_strings_in(string directory, string pattern)
{
string[] returnList = Directory.GetFiles(directory, pattern);
return returnList.OrderBy(get_file_name_from).ToArray();
}

/// <summary>
/// Gets a list of all files inside of an existing directory, includes files in subdirectories also
/// </summary>
/// <param name="directory">Path to the directory</param>
/// <param name="pattern">Pattern or extension</param>
/// <returns>A list of files inside of an existing directory</returns>
public string[] get_all_file_name_strings_recurevly_in(string directory, string pattern)
{
string[] returnList = Directory.GetFiles(directory, pattern, SearchOption.AllDirectories);
return returnList.OrderBy(get_file_name_from).ToArray();
}

#endregion

/// <summary>
Expand Down
@@ -0,0 +1,18 @@
namespace roundhouse.infrastructure.filesystem.filelocators
{
/// <summary>
/// Interface which abstracts the method for retrieving files
/// from a directory.
/// </summary>
public interface FileLocator
{
/// <summary>
/// Return all the files in the given directory.
/// The files are returned in an implementation dependent order.
/// </summary>
/// <param name="directory">The full path to the directory</param>
/// <param name="pattern">The file name pattern</param>
/// <returns>All the files in the directory and all subdirectories, in an implementation defined order.</returns>
string[] locate_all_files_in(string directory, string pattern);
}
}
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace roundhouse.infrastructure.filesystem.filelocators
{
/// <summary>
/// The recurse files option.
/// </summary>
public class Recurse : FileLocator
{
/// <summary>
/// Returns all files in this directory and it's subdirectories, globally sorted using the
/// filename.
/// </summary>
/// <example>
/// It will return like this:
/// subdir\a.txt
/// b.txt
/// subdir\c.txt
/// d.txt
///
/// </example>
/// <param name="directory">The directory to search in</param>
/// <param name="pattern">The filename pattern</param>
/// <returns>A globally sorted list of files.</returns>
public string[] locate_all_files_in(string directory, string pattern)
{
string[] returnList = Directory.GetFiles(directory, pattern, SearchOption.AllDirectories);
return returnList.OrderBy(Path.GetFileName).ToArray();
}
}
}
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace roundhouse.infrastructure.filesystem.filelocators
{
/// <summary>
/// The traverse files option.
/// </summary>
public class Traverse : FileLocator
{
/// <summary>
/// Returns the files in the provided directory first, in sorted order based on their FileName.
/// Then each subdirectory, with the each subdirectory files, in sorted order based on their FileName.
/// </summary>
/// <example>
/// It will return like this (files are only sorted local to their directory):
/// b.txt
/// d.txt
/// subdir\a.txt
/// subdir\c.txt
/// </example>
/// <param name="directory">The directory to find files in</param>
/// <param name="pattern">The wildcard pattern for those files</param>
/// <returns>An ordered list of files in this directory and it's subdirectories</returns>
public string[] locate_all_files_in(string directory, string pattern)
{
var files = new List<string>();

// find all the files, sort them add to our list.
files.AddRange(Directory.GetFiles(directory, pattern).OrderBy(Path.GetFileName));

// find all the subdirectories.
foreach (var subDir in Directory.GetDirectories(directory))
{
// find all the files in the subdirectories.
files.AddRange(locate_all_files_in(subDir, pattern));
}

return files.ToArray();
}
}
}
3 changes: 3 additions & 0 deletions product/roundhouse/roundhouse.csproj
Expand Up @@ -117,6 +117,8 @@
<Compile Include="infrastructure.app\DatabaseTypeSynonyms.cs" />
<Compile Include="infrastructure.app\persistence\NHibernateMigrationSessionFactory.cs" />
<Compile Include="infrastructure\extensions\ObjectExtensions.cs" />
<Compile Include="infrastructure\filesystem\filelocators\Recurse.cs" />
<Compile Include="infrastructure\filesystem\filelocators\Traverse.cs" />
<Compile Include="infrastructure\logging\custom\ConsoleLogger.cs" />
<Compile Include="infrastructure\persistence\AuditEventListener.cs" />
<Compile Include="infrastructure\persistence\DifferencingNHibernateSessionFactory.cs" />
Expand All @@ -137,6 +139,7 @@
<Compile Include="RoundhouseMode.cs" />
<Compile Include="runners\RoundhouseNHibernateCompareRunner.cs" />
<Compile Include="runners\RoundhouseRedGateCompareRunner.cs" />
<Compile Include="infrastructure\filesystem\filelocators\FileLocator.cs" />
<Compile Include="sqlsplitters\StatementSplitter.cs" />
<Compile Include="consoles\DefaultConfiguration.cs" />
<Compile Include="cryptography\CryptographicService.cs" />
Expand Down
17 changes: 6 additions & 11 deletions product/roundhouse/runners/RoundhouseMigrationRunner.cs
Expand Up @@ -12,6 +12,7 @@ namespace roundhouse.runners
using migrators;
using resolvers;
using Environment = environments.Environment;
using roundhouse.infrastructure.filesystem.filelocators;

public sealed class RoundhouseMigrationRunner : IRunner
{
Expand All @@ -27,6 +28,7 @@ public sealed class RoundhouseMigrationRunner : IRunner
private bool run_in_a_transaction;
private readonly bool use_simple_recovery;
private readonly ConfigurationPropertyHolder configuration;
private readonly FileLocator file_locator;
private const string SQL_EXTENSION = "*.sql";

public RoundhouseMigrationRunner(
Expand All @@ -41,7 +43,8 @@ public sealed class RoundhouseMigrationRunner : IRunner
bool dont_create_the_database,
bool run_in_a_transaction,
bool use_simple_recovery,
ConfigurationPropertyHolder configuration)
ConfigurationPropertyHolder configuration,
FileLocator file_locator)
{
this.known_folders = known_folders;
this.repository_path = repository_path;
Expand All @@ -55,6 +58,7 @@ public sealed class RoundhouseMigrationRunner : IRunner
this.run_in_a_transaction = run_in_a_transaction;
this.use_simple_recovery = use_simple_recovery;
this.configuration = configuration;
this.file_locator = file_locator;
}

public void run()
Expand Down Expand Up @@ -256,10 +260,7 @@ private void remove_share_from_change_drop_folder()
{
if (!file_system.directory_exists(directory)) return;

var fileNames = configuration.SearchAllSubdirectoriesInsteadOfTraverse
? file_system.get_all_file_name_strings_recurevly_in(directory, SQL_EXTENSION)
: file_system.get_all_file_name_strings_in(directory, SQL_EXTENSION);
foreach (string sql_file in fileNames)
foreach (string sql_file in file_locator.locate_all_files_in(directory, SQL_EXTENSION))
{
string sql_file_text = replace_tokens(get_file_text(sql_file));
Log.bound_to(this).log_a_debug_event_containing(" Found and running {0}.", sql_file);
Expand All @@ -280,12 +281,6 @@ private void remove_share_from_change_drop_folder()
}
}
}

if (configuration.SearchAllSubdirectoriesInsteadOfTraverse) return;
foreach (string child_directory in file_system.get_all_directory_name_strings_in(directory))
{
traverse_files_and_run_sql(child_directory, version_id, migration_folder, migrating_environment, repository_version, connection_type);
}
}

public string get_file_text(string file_location)
Expand Down
8 changes: 6 additions & 2 deletions product/roundhouse/runners/RoundhouseRedGateCompareRunner.cs
@@ -1,3 +1,5 @@
using roundhouse.infrastructure.filesystem.filelocators;

namespace roundhouse.runners
{
using System.Collections.Generic;
Expand All @@ -15,15 +17,17 @@ public class RoundhouseRedGateCompareRunner : IRunner
private readonly FileSystemAccess file_system;
private readonly ConfigurationPropertyHolder configuration;
private readonly RoundhouseMigrationRunner migration_runner;
private readonly FileLocator file_locator;
private readonly string redgate_install_location = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\Red Gate\SQL Compare 8";
private const string redgate_compare_tool = @"sqlcompare.exe";

public RoundhouseRedGateCompareRunner(KnownFolders known_folders, FileSystemAccess file_system, ConfigurationPropertyHolder configuration, RoundhouseMigrationRunner migration_runner)
public RoundhouseRedGateCompareRunner(KnownFolders known_folders, FileSystemAccess file_system, ConfigurationPropertyHolder configuration, RoundhouseMigrationRunner migration_runner, FileLocator file_locator)
{
this.known_folders = known_folders;
this.file_system = file_system;
this.configuration = configuration;
this.migration_runner = migration_runner;
this.file_locator = file_locator;
}


Expand Down Expand Up @@ -55,7 +59,7 @@ public void run()

public string get_new_change_number()
{
string[] files = file_system.get_all_file_name_strings_in(known_folders.up.folder_full_path);
string[] files = file_locator.locate_all_files_in(known_folders.up.folder_full_path, "*.*");
IList<string> file_numbers = files.Select(file => file_system.get_file_name_from(file).Substring(0, file_system.get_file_name_from(file).IndexOf('_'))).ToList();

int length_of_number_format = 0;
Expand Down

0 comments on commit dd9afa2

Please sign in to comment.