public void SqlReaderShouldReturnAllFilesWhenNullVersionsArePassed() { // Arrange DirectoryInfo directoryInfo = DirectoryStructureHelper.CreateValidDatabaseDirStructure("SqlReaderShouldReturnAllFilesWhenNullVersionsArePassed"); DirectoryInfo[] subdirs = directoryInfo.GetDirectories(); DirectoryStructureHelper.CreateEmptyFile(subdirs[0], "1.3.0.0_DataSetup.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[1], "1.1.0.0_CreateDatabase.SQL"); DirectoryStructureHelper.CreateEmptyFile(subdirs[2], "1.2.0_CreateTables.sql"); SqlFilesListReader reader = new SqlFilesListReader(directoryInfo); // Act Dictionary <string, FileInfo> sqlFilesToExecuteInProperOrder = reader.GetSpecificVersionedFilesToExecute(null, null as Version); // Assert Assert.AreEqual(3, sqlFilesToExecuteInProperOrder.Count); }
public void SqlReaderShouldOrderSqlFilesInCorrectSequenceTest() { // Arrange DirectoryInfo directoryInfo = DirectoryStructureHelper.CreateValidDatabaseDirStructure("SqlReaderShouldOrderSqlFilesInCorrectSequenceTest"); DirectoryInfo[] subdirs = directoryInfo.GetDirectories(); DirectoryStructureHelper.CreateEmptyFile(subdirs[0], "1.1.0.0_DataSetup.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[0], "1.2.0_DataSetup.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[1], "1.7.0_StoredProc.sql"); SqlFilesListReader reader = new SqlFilesListReader(directoryInfo); string fromVersion = "1.0"; string toVersion = "1.7"; // Act Dictionary <string, FileInfo> sqlFilesToExecuteInProperOrder = reader.GetSpecificVersionedFilesToExecute(fromVersion, toVersion); // Assert Assert.AreEqual("1.1.0.0_DataSetup.sql", Path.GetFileName(sqlFilesToExecuteInProperOrder.ElementAt(0).Key)); Assert.AreEqual("1.2.0_DataSetup.sql", Path.GetFileName(sqlFilesToExecuteInProperOrder.ElementAt(1).Key)); Assert.AreEqual("1.7.0_StoredProc.sql", Path.GetFileName(sqlFilesToExecuteInProperOrder.ElementAt(2).Key)); }
public void SqlReaderShouldFilterFilesBasedOnPassenFromAndToVersion() { // Arrange DirectoryInfo directoryInfo = DirectoryStructureHelper.CreateValidDatabaseDirStructure("SqlReaderShouldFilterFilesBasedOnPassenFromAndToVersion"); DirectoryInfo[] subdirs = directoryInfo.GetDirectories(); DirectoryStructureHelper.CreateEmptyFile(subdirs[0], "1.1.0.0_DataSetup.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[0], "1.11.0.0_DataSetup.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[0], "1.2.0_DataSetup.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[1], "1.8.0.0_StoredProc.sql"); DirectoryStructureHelper.CreateEmptyFile(subdirs[1], "1.7.0_StoredProc.sql"); SqlFilesListReader reader = new SqlFilesListReader(directoryInfo); string fromVersion = "1.0"; string toVersion = "1.7"; // Act Dictionary <string, FileInfo> sqlFilesToExecuteInProperOrder = reader.GetSpecificVersionedFilesToExecute(fromVersion, toVersion); // Assert Assert.AreEqual(3, sqlFilesToExecuteInProperOrder.Count); }
public virtual ExcecutionResult ExecuteAllScriptsForDatabase( string fromVersion, string toVersion, DatabaseConnectionParameters connectionParameters, Encoding encodingForReadingSqlScripts) { if (string.IsNullOrEmpty(fromVersion)) { throw new ArgumentException(@"Cannot be null or empty.", "fromVersion"); } if (string.IsNullOrEmpty(toVersion)) { throw new ArgumentException(@"Cannot be null or empty.", "toVersion"); } if (encodingForReadingSqlScripts == null) { encodingForReadingSqlScripts = Encoding.Default; } Version fromVersionObject; Version toVersionObject; try { fromVersionObject = VersionNumberHelper.RemoveNegativeNumbersFromVersionObject(new Version(fromVersion)); } catch (Exception ex) { var result = new ExcecutionResult { Success = false }; result.Errors.Add( string.Format("Invalid fromVersion. Could not parse fromVersion to a usable version object. {0}", ex.Message)); return(result); } try { toVersionObject = VersionNumberHelper.RemoveNegativeNumbersFromVersionObject(new Version(toVersion)); } catch (Exception ex) { var result = new ExcecutionResult { Success = false }; result.Errors.Add(string.Format("Invalid toVersion. Could not parse toVersion to a usable version object. {0}", ex.Message)); return(result); } // Execute optional FileStructurePolicy if (_fileStructurePolicy != null) { PolicyResult policyResult = _fileStructurePolicy.Check(_databaseScriptsDirInfo); if (!policyResult.Success) { // Policy failed Return errors ExcecutionResult result = new ExcecutionResult { Success = false }; result.Errors.AddRange(policyResult.Messages); return(result); } } SqlFilesListReader filesReader = new SqlFilesListReader(_databaseScriptsDirInfo); Dictionary <string, FileInfo> filesToExecute = filesReader.GetSpecificVersionedFilesToExecute(fromVersionObject, toVersionObject); return(ExecuteSqlScripts(connectionParameters, filesToExecute, encodingForReadingSqlScripts)); }