Пример #1
0
        private static bool IsVersion(string currentVersion, string expectedVersion, IInstallLogger logger)
        {
            if (string.IsNullOrWhiteSpace(currentVersion) || string.IsNullOrWhiteSpace(expectedVersion))
            {
                return(false);
            }

            if (logger != null)
            {
                logger.LogInstallProgress($"Comparing if current version value \"{currentVersion}\" is at least (greater than or equal to) provided version value \"{expectedVersion}\"");
            }

            string[] currentVersionSplit  = currentVersion.Split('.');
            string[] expectedVersionSplit = expectedVersion.Split('.');

            // [ MajorVersion, MinorVersion, Build, Revision ]
            int[] currentVersionValue = new int[4] {
                int.Parse(currentVersionSplit[0]), int.Parse(currentVersionSplit[1]), int.Parse(currentVersionSplit[2]), int.Parse(currentVersionSplit[3])
            };
            int[] expectedVersionValue = new int[4] {
                int.Parse(expectedVersionSplit[0]), int.Parse(expectedVersionSplit[1]), int.Parse(expectedVersionSplit[2]), int.Parse(expectedVersionSplit[3])
            };

            return(currentVersionValue.Sum() == expectedVersionValue.Sum());
        }
Пример #2
0
        private static void IterateVersionsAndFileNamesToExecute(IInstallLogger logger, IReadOnlyList <string> versions, string[] fileNames, bool doVersionCheck)
        {
            if (versions == null || !versions.Any())
            {
                return;
            }

            foreach (string version in versions)
            {
                // Run regardless if this is the first version + database wasn't created yet; otherwise check version
                if (doVersionCheck && (!version.Equals(versions[0]) || DatabaseExists()) && IsAtleastVersion(version, logger))
                {
                    continue;
                }

                if (logger != null)
                {
                    logger.LogInstallProgress($"Installing / updating database version {version} against the following scripts: {string.Join(", ", fileNames)}");
                }

                foreach (string fileName in fileNames)
                {
                    if (string.IsNullOrWhiteSpace(fileName))
                    {
                        continue;
                    }

                    string filePath = Path.Combine(Config.CustomActionTarget, $"SqlScripts\\{version}\\{fileName.Replace(".sql", "")}.sql");
                    if (!File.Exists(filePath))
                    {
                        continue;
                    }

                    string fileContents = File.ReadAllText(filePath);
                    ExecuteBatch(fileContents, Config.SqlMasterDBConnectionString);
                }

                if (logger != null)
                {
                    logger.LogInstallProgress($"Successfully installed / updated database version {version}");
                }
            }
        }
Пример #3
0
        public static void CreateAndUpdateDatabase(IInstallLogger logger)
        {
            if (logger != null)
            {
                logger.LogInstallProgress("Create and Update Database");
            }

            string[] fileNames = { Config.DataScriptNameSetup, Config.DataScriptNameViews, Config.DataScriptNameStoredProcedures };

            IterateVersionsAndFileNamesToExecute(logger, Config.DatabaseVersions, fileNames, true);
        }
Пример #4
0
        private static bool IsAtleastVersion(string version, IInstallLogger logger)
        {
            if (string.IsNullOrWhiteSpace(version))
            {
                throw new ArgumentNullException(nameof(version), "Unable to determine if the database is a specified version without a provided version to compare to.");
            }

            string databaseVersion = DatabaseVersion(logger);

            if (string.IsNullOrWhiteSpace(databaseVersion))
            {
                return(false);
            }

            if (logger != null)
            {
                logger.LogInstallProgress($"Comparing if database version value \"{databaseVersion}\" is at least (greater than or equal to) provided version value \"{version}\"");
            }

            string[] databaseVersionSplit = databaseVersion.Split('.');
            string[] expectedVersionSplit = version.Split('.');

            // [ MajorVersion, MinorVersion, Build, Revision ]
            int[] currentVersion = new int[4] {
                int.Parse(databaseVersionSplit[0]), int.Parse(databaseVersionSplit[1]), int.Parse(databaseVersionSplit[2]), int.Parse(databaseVersionSplit[3])
            };
            int[] expectedVersion = new int[4] {
                int.Parse(expectedVersionSplit[0]), int.Parse(expectedVersionSplit[1]), int.Parse(expectedVersionSplit[2]), int.Parse(expectedVersionSplit[3])
            };

            // (Major newer)
            // OR (Major same AND Minor newer)
            // OR (Major same AND Minor same AND Build newer)
            // OR (Major same AND Minor same AND Build same AND Revision newer)
            // OR (Major same AND Minor same AND Build same AND Revision same)
            return((currentVersion[0] > expectedVersion[0]) ||
                   (currentVersion[0] == expectedVersion[0] && currentVersion[1] > expectedVersion[1]) ||
                   (currentVersion[0] == expectedVersion[0] && currentVersion[1] == expectedVersion[1] && currentVersion[2] > expectedVersion[2]) ||
                   (currentVersion[0] == expectedVersion[0] && currentVersion[1] == expectedVersion[1] && currentVersion[2] == expectedVersion[2] && currentVersion[3] > expectedVersion[3]) ||
                   (currentVersion[0] == expectedVersion[0] && currentVersion[1] == expectedVersion[1] && currentVersion[2] == expectedVersion[2] && currentVersion[3] == expectedVersion[3]));
        }
Пример #5
0
        private static string DatabaseVersion(IInstallLogger logger)
        {
            string query = string.Format("{0}{1}{0}{2}{0}{3}",
                                         Environment.NewLine,
                                         "SELECT TOP 1 MajorVersion, MinorVersion, RevisionVersion, BuildVersion",
                                         "FROM dbo.DatabaseVersion",
                                         "ORDER BY MajorVersion DESC, MinorVersion DESC, RevisionVersion DESC, BuildVersion DESC"
                                         );

            DataRow row = ExecuteDataRow(query, Config.SqlConnectionString);

            // MajorVersion.MinorVersion.RevisionVersion.BuildVersion // Trimmed just in case spaces manage to slip in
            string version = $"{row["MajorVersion"].ToString().Trim()}.{row["MinorVersion"].ToString().Trim()}.{row["RevisionVersion"].ToString().Trim()}.{row["BuildVersion"].ToString().Trim()}";

            if (logger != null)
            {
                logger.LogInstallProgress($"Database version string: | {version}");
            }

            return(version);
        }