示例#1
0
 /// <summary>
 /// Создать Состояние секции бд.
 /// </summary>
 /// <param name="dbState">Состояние бд. </param>
 /// <param name="dbSectionStatus">Статус секции бд.</param>
 /// <param name="message">Сообщение.</param>
 /// <param name="canResolve">Можно ли разрешить конфликт бд.</param>
 /// <param name="sectionVersion">Версия секции.</param>
 public DbSectionState(DbState dbState, DbSectionStatus dbSectionStatus, ScoutVersion? sectionVersion = null, bool canResolve = false, string message = null)
     : this()
 {
     DbState = dbState;
     DbSectionStatus = dbSectionStatus;
     Message = message;
     CanResolve = canResolve;
     SectionVersion = sectionVersion;
 }
        public DbState CheckDbState(ScoutVersion requiredVersion, bool newerVersionIsValid = false)
        {
            try
            {
                if (!IsDbExist(_connectionString))
                    return new DbState(DbStatus.NotExists, message: "БД не существует.");


                using (var connection = GetConnection(_connectionString))
                {
                    connection.Open();

                    var checkDbTablesCommand = connection.CreateCommand();
                    checkDbTablesCommand.CommandText = string.Format(CheckDbTablesCommandFormat, "DbVersion");
                    checkDbTablesCommand.CommandType = CommandType.Text;

                    using (var reader = checkDbTablesCommand.ExecuteReader())
                    {
                        if (reader == null || !reader.Read())
                            return new DbState(DbStatus.NoSchema, message: "Схема бд невалидна.");
                    }

                    var versionInfoCommand = connection.CreateCommand();
                    versionInfoCommand.CommandText = GetDbVersionCommand;
                    versionInfoCommand.CommandType = CommandType.Text;

                    using (var reader = versionInfoCommand.ExecuteReader())
                    {
                        if (reader != null && reader.Read())
                        {
                            var currentDbVersion = new ScoutVersion(new Version(reader.GetString(0)));
                            var dbState = GetDbState(requiredVersion, currentDbVersion, newerVersionIsValid);
                            return dbState;
                        }
                        return new DbState(DbStatus.Unknown, message: "Весрию бд не удалось определить.");
                    }
                }
            }
            catch (SqlException ex)
            {
                return GetDbstateForError(ex);
            }
            catch (TimeoutException)
            {
                return new DbState(DbStatus.NotAvailable);
            }
            catch (InvalidOperationException)
            {
                return new DbState(DbStatus.NotAvailable);
            }
            catch (Exception ex)
            {
                LogManager.ExceptionLog.ErrorException(ex);
                return new DbState(DbStatus.Unknown);
            }
        }
 private static DbScript ExtractCreateScript(string createScriptName, 
                                             Assembly packageAssembly, 
                                             IReadOnlyDictionary<string, string> replaceAliases )
 {
     if (createScriptName == null) return null;
     var stringVersion = createScriptName.Split(new[] { '_' })[1].Replace(".sql", string.Empty);
     var version = Version.Parse(stringVersion);
     var scoutVersion = new ScoutVersion(version);
     var stream = packageAssembly.GetManifestResourceStream(createScriptName);
     string createScriptText = GetScript(stream, replaceAliases);
     return new DbScript(createScriptText, scoutVersion);
 }
 private DbSectionState GetNotExistDbSectionState(DbState dbState, ScoutVersion? requiredVersion)
 {
     return new DbSectionState(dbState, DbSectionStatus.NotExists, requiredVersion, true);
 }
        private DbSectionState GetSectionState(DbState dbState, ScoutVersion? currentSectionVersion, ScoutVersion? requiredVersion)
        {
            if (dbState.Status == DbStatus.Unknown) return GetDefaultDbSectionState();

            if (currentSectionVersion == null && requiredVersion != null)
            {
                return GetNotExistDbSectionState(dbState, requiredVersion);
            }

            if (currentSectionVersion == null || requiredVersion == null)
            {
                return new DbSectionState(dbState, DbSectionStatus.Error, currentSectionVersion, message: "Не удалось определить версию секции бд.");
            }

            try
            {
                if (currentSectionVersion.Value < requiredVersion.Value)
                {
                    return new DbSectionState(dbState, DbSectionStatus.Outdated, currentSectionVersion, true, "Версия секции базы данных ниже требуемой.");
                }
                if (currentSectionVersion.Value > requiredVersion.Value)
                {
                    return new DbSectionState(dbState, DbSectionStatus.Newer, currentSectionVersion, message: "Версия секции базы данных не совместима для работы расширения.");
                }
                return new DbSectionState(dbState, DbSectionStatus.Ok, currentSectionVersion, message: "Версия секции базы данных корректна");


            }
            catch (Exception)
            {
                return new DbSectionState(dbState, DbSectionStatus.Error, currentSectionVersion, message: "Не удалось определить версию секции бд.");
            }
        }
        private Dictionary<Guid, ScoutVersion> GetSectionsVersions()
        {
            try
            {
                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    var sessionInfoCommand = new SqlCommand
                    {
                        CommandText = string.Format(GetSectionsInfoFormat, _dbName),
                        CommandType = CommandType.Text,
                        Connection = connection
                    };

                    using (var reader = sessionInfoCommand.ExecuteReader())
                    {
                        var sectionsInfo = new Dictionary<Guid, ScoutVersion>();
                        while (reader.Read())
                        {
                            var id = reader.GetGuid(0);
                            var version = new ScoutVersion(new Version(reader.GetString(1)));
                            sectionsInfo.Add(id, version);
                        }
                        return sectionsInfo;
                    }
                }
            }

            catch (Exception ex)
            {
                LogManager.ExceptionLog.Error(ex);
            }
            return new Dictionary<Guid, ScoutVersion>();
        }
 private string GetSetVersionCommand(Guid dbSectionId, ScoutVersion requiredVersion)
 {
     return String.Format(InsertSectionVersionFormat, _dbName, dbSectionId, requiredVersion);
 }
 private string GetUpdateVersionCommand(Guid dbSectionId, ScoutVersion requiredVersion)
 {
     return String.Format(UpdateSectionVersionFormat, _dbName, requiredVersion, dbSectionId);
 }
 private string GetUpdateVersionCommand(ScoutVersion requiredVersion, string dbName)
 {
     return String.Format(UpdateDbVersionCommandFormat, dbName, requiredVersion);
 }
 private static DbState GetDbState(ScoutVersion requieredVersion, ScoutVersion currentDbSectionVersion, bool newerVeresionIsValid = false)
 {
     try
     {
         if (currentDbSectionVersion.Version < requieredVersion.Version)
         {
             return new DbState(DbStatus.Outdated, currentDbSectionVersion, "Версия базы ниже требуемой.");
         }
         if ((currentDbSectionVersion.Version > requieredVersion.Version) && newerVeresionIsValid)
         {
             return new DbState(DbStatus.Invalid, currentDbSectionVersion, "Версия базы несовместима с требуемой версией.");
         }
         return new DbState(DbStatus.Ok, currentDbSectionVersion, "Версия базы корректна");
     }
     catch (Exception)
     {
         return new DbState(DbStatus.Unknown, message: "Весрию бд не удалось определить.");
     }
 }
        private static DbState GetDbState(ScoutVersion requieredVersion, ScoutVersion currentDbSectionVersion,
                                          bool newerVeresionIsValid = false)
        {

            if (currentDbSectionVersion.Version < requieredVersion.Version)
            {
                return new DbState(DbStatus.Outdated, currentDbSectionVersion, "Версия базы ниже требуемой.");
            }
            if ((currentDbSectionVersion.Version > requieredVersion.Version) && newerVeresionIsValid)
            {
                return new DbState(DbStatus.Invalid, currentDbSectionVersion,
                                   "Версия базы несовместима с требуемой версией.");
            }
            return new DbState(DbStatus.Ok, currentDbSectionVersion, "Версия базы корректна");
        }
示例#12
0
 private static DbScript[] GetUpdateScripts(ScoutVersion requiredVersion, ScoutVersion? currentVersion, IEnumerable<DbScript> scripts)
 {
     var result = scripts.Where(dbScript => (currentVersion == null || dbScript.Version.Version > currentVersion.Value.Version) && dbScript.Version.Version <= requiredVersion.Version).ToList();
     result.Sort((x, y) => x.Version.CompareTo(y.Version));
     return result.ToArray();
 }
示例#13
0
 public DbSectionInfo(Guid dbSectionId, ScoutVersion requiredVersion, DbScriptSet dbScripts)
 {
     DbSectionId = dbSectionId;
     RequiredVersion = requiredVersion;
     DbScripts = dbScripts;
 }
示例#14
0
 /// <summary>
 /// Создать Sql скрипт.
 /// </summary>
 public DbScript(string sql, ScoutVersion version)
 {
     Sql = sql;
     Version = version;
 }