Esempio n. 1
0
        public void ComputeAdlerCRCRegressionTest()
        {
            (uint crc, ulong length)expected = (0, 0);
            (uint crc, ulong length)crc_check;
            crc_check = CommonUtilities.ComputeAdlerCRC(null);
            Assert.AreEqual(expected, crc_check);
            crc_check = CommonUtilities.ComputeAdlerCRC("filenotfound.txt");
            Assert.AreEqual(expected, crc_check);

            // Stable?
            expected  = (790105748, 38);
            crc_check = CommonUtilities.ComputeAdlerCRC("TestContent/CRCTest1.txt");
            Assert.AreEqual(expected, crc_check);
            crc_check = CommonUtilities.ComputeAdlerCRC("TestContent/CRCTest1.txt");
            Assert.AreEqual(expected, crc_check);

            // Not the same as another file
            (uint crc, ulong length)crc_check2 = CommonUtilities.ComputeAdlerCRC("TestContent/CRCTest2.txt");
            Assert.AreNotEqual(crc_check, crc_check2);
        }
Esempio n. 2
0
        public bool DeployCurrent()
        {
            bool database_updated = false;

            if (!Directory.Exists(_currentScriptLocation))
            {
                Log.Logger.Warning("CURRENT not found at {0}; skipping CURRENT.", _currentScriptLocation);
                return(database_updated);
            }

            Log.Logger.Information("Reading current scripts from {0}", _currentScriptLocation);

            // Get all current scripts to run
            List <string> currentScripts = new List <string>();

            string[] extensionOrder = _database.GetScriptTypes();
            string[] prefix_order   = new string[extensionOrder.Length];
            int      i = 0;

            foreach (string script_type in extensionOrder)
            {
                prefix_order[i] = _database.GetFileNamePrefix(script_type);

                string ext = String.Concat(prefix_order[i], "*", _database.GetFileNameExtension());
                currentScripts.AddRange(Directory.EnumerateFiles(_currentScriptLocation, ext, SearchOption.AllDirectories).ToList());

                i++;
            }

            if (currentScripts.Count() > 0)
            {
                // Establish order of the scripts
                Dictionary <string, List <string> > dependencies   = ResolveAllDependencies(currentScripts);
                Dictionary <string, int>            orderedScripts = CommonUtilities.TopSort(dependencies);
                List <string> scriptsToRun = currentScripts
                                             .OrderByDescending(fn => orderedScripts[Path.GetFileNameWithoutExtension(fn).ToLower()]) // by dependency order
                                             .ThenBy(fn2 => {
                    for (int j = 0; j < prefix_order.Length; j++)
                    {
                        if (Path.GetFileNameWithoutExtension(fn2).StartsWith(prefix_order[j], StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(j);
                        }
                    }
                    throw new NotSupportedException($"Invalid prefix detected in script list {fn2}");
                })     // then by database provider order defined
                                             .Select(n => n).ToList();

                // Start the deployment process
                DateTime deploymentTime = DateTime.UtcNow;
                using (DataContext dataContext = GetDataContext())
                {
                    // Retrieve the existing script information we have in the database
                    Dictionary <string, Tuple <int, long> > existingScriptInfo = GetExistingScriptInfo(dataContext);

                    // Now, process each file we have on the disk
                    foreach (string currentFileName in scriptsToRun)
                    {
                        // Get the CRC and length of the file
                        (uint crc, ulong length) = CommonUtilities.ComputeAdlerCRC(currentFileName);

                        // Determine if we need to hit the database
                        bool   isUpdate = false;
                        string key      = Path.GetFileNameWithoutExtension(currentFileName).ToLower();
                        if (existingScriptInfo.ContainsKey(key))
                        {
                            // The script is found in the database, but lets see if the adler and length match
                            if (existingScriptInfo[key].Item1 == (int)crc && existingScriptInfo[key].Item2 == (long)length)
                            {
                                // Skip this script
                                continue;
                            }

                            isUpdate = true;
                        }

                        // Run the script against the database
                        using (TransactionScope ts = dataContext.StartTransaction(TransactionScopeOption.Required))
                        {
                            Stopwatch elapsedTime = Stopwatch.StartNew();

                            Log.Logger.Information("Running current script: {0}", currentFileName);
                            if (!NoDbUpdates)
                            {
                                // Run the referenced script
                                try
                                {
                                    dataContext.ExecuteScript(currentFileName, _database.GetSplitPhrase(), _scriptReplacements);
                                }
                                catch (Exception ex)
                                {
                                    throw new SystemException($"Error running script {currentFileName}: {ex.Message}", ex);
                                }

                                // Update the script tracking table with CRC and length
                                IDbDataParameter systemid   = dataContext.CreateParameter(_database.GetParameterName("sysId"), SYSTEM_ID);
                                IDbDataParameter scriptname = dataContext.CreateParameter(_database.GetParameterName("scriptname"), Path.GetFileNameWithoutExtension(currentFileName));
                                IDbDataParameter checksum   = dataContext.CreateParameter(_database.GetParameterName("checksum"), (int)crc);
                                IDbDataParameter len        = dataContext.CreateParameter(_database.GetParameterName("length"), (long)length);
                                IDbDataParameter modified   = dataContext.CreateParameter(_database.GetParameterName("modified"), deploymentTime);

                                // Some providers require parameter order to match; separate insert versus update
                                if (isUpdate)
                                {
                                    dataContext.ExecuteNonQuery(_database.GetUpdateCurrentRecordSQL(), new List <IDbDataParameter>()
                                    {
                                        checksum, len, modified, systemid, scriptname
                                    });
                                }
                                else
                                {
                                    dataContext.ExecuteNonQuery(_database.GetInsertCurrentRecordSQL(), new List <IDbDataParameter>()
                                    {
                                        systemid, scriptname, checksum, len, modified
                                    });
                                }

                                ts.Complete();

                                // Record the script in the history
                                ScriptHistory.Add(currentFileName);
                                database_updated = true;
                            }

                            Log.Logger.Information("Finished running current script {0} in {1}", currentFileName, elapsedTime.ShowElapsedTime());
                        }
                    }
                }
            }

            return(database_updated);
        }