Beispiel #1
0
        /// <summary>
        /// <para>Gets list of SQL scripts that have not been ran against the target database</para>
        /// <para>Default script directory is ~/App_Data/scripts/ but it can bet set to any physical path</para>
        /// <para>-Script directory path must exist</para>
        /// <para>Returns a list of string with names of pending sql scripts</para>
        /// </summary>
        /// <param name="connectionString"></param>
        /// <param name="scriptDirectory"></param>
        /// <returns>Returns a list of string with names of pending sql scripts</returns>
        public List<string> PendingChanges(string connectionString, string scriptDirectory = "")
        {
            if (scriptDirectory == "")
            {
                scriptDirectory = Path.Combine(AppDomain.CurrentDomain.GetData("DataDirectory").ToString(), "scripts");
            }

            if (!Directory.Exists(scriptDirectory))
            {
                throw new ArgumentException("There are no scripts in the defined data directory.");
            }

            if (!DatabaseExists(connectionString))
            {
                var result = new List<string>();
                result.Add("Database does not exist");
                return result;
            }

            var filelocator = new SqlFileLocator();
            var allfiles = new List<string>();
            allfiles.AddRange(filelocator.GetSqlFilenames(scriptDirectory, "Create").ToList());
            allfiles.AddRange(filelocator.GetSqlFilenames(scriptDirectory, "Update").ToList());
            allfiles.AddRange(filelocator.GetSqlFilenames(scriptDirectory, "Everytime").ToList());
            var executedfiles = _queryExecutor.GetExecutedScripts(_connectionStringGenerator.GetConnectionSettings(connectionString));
            return allfiles.Select(f => f.Replace(Path.GetDirectoryName(f) + "\\", "")).Except(executedfiles).ToList();
        }
		public void Correctly_locates_sql_scripts_and_return_in_asc_order()
		{
			string scriptFolder = @"c:\scripts";

			string[] updateSqlFiles = new string[] { "02_Update.sql", "01_Update.sql" };

			MockRepository mocks = new MockRepository();
            IFileSystem fileSystem = mocks.StrictMock<IFileSystem>();

			using (mocks.Record())
			{
				Expect.Call(fileSystem.GetAllFilesWithExtensionWithinFolder(@"c:\scripts\Update", "sql")).Return(updateSqlFiles);
			}

			using (mocks.Playback())
			{
				ISqlFileLocator fileLocator = new SqlFileLocator(fileSystem);
				string[] sqlFilenames = fileLocator.GetSqlFilenames(scriptFolder, "Update");

				Assert.AreEqual(2, sqlFilenames.Length);
				Assert.AreEqual("01_Update.sql", sqlFilenames[0]);
				Assert.AreEqual("02_Update.sql", sqlFilenames[1]);
			}

			mocks.VerifyAll();
		}