Skip to content
This repository
tree: c8e2a1174e
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 216 lines (187 sloc) 8.296 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;

using MigSharp.Core;
using MigSharp.Process;

namespace MigSharp
{
    /// <summary>
    /// Use this class to configure the behaviour of the <see cref="Migrator"/>.
    /// </summary>
    public class MigrationOptions
    {
        public const string DefaultVersioningTableName = "MigSharp";

        private string _versioningTableName = DefaultVersioningTableName;

        /// <summary>
        /// Gets or sets the table name of the versioning table.
        /// </summary>
        public string VersioningTableName
        {
            get { return _versioningTableName; }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentException("The versioning table name cannot be empty.");
                _versioningTableName = value;
            }
        }

        private SupportedProviders _supportedProviders = new SupportedProviders();
        /// <summary>
        /// Gets the providers that should be supported for all migrations. Compatibility validation of migrations is performed
        /// against the providers in this collection.
        /// </summary>
        public SupportedProviders SupportedProviders { get { return _supportedProviders; } internal set { _supportedProviders = value; } }

        private Predicate<string> _moduleSelector = n => true; // select all modules by default
        /// <summary>
        /// Gets or sets a function that selects the module based on its name. Only migrations for this module will be executed.
        /// </summary>
        public Predicate<string> ModuleSelector
        {
            get { return _moduleSelector; }
            set
            {
                if (value == null) throw new ArgumentNullException("value");
                _moduleSelector = value;
            }
        }

        private readonly List<Suppression> _warningSuppressions = new List<Suppression>();

        private ScriptingOptions _scriptingOptions = new ScriptingOptions(ScriptingMode.ExecuteOnly, null);
        internal ScriptingOptions ScriptingOptions { get { return _scriptingOptions; } }

        /// <summary>
        /// Initializes an instance of default options.
        /// </summary>
        public MigrationOptions()
        {
        }

        /// <summary>
        /// Initializes options that select migrations for specific module only.
        /// </summary>
        /// <param name="moduleName">The name of the selected module. Only migrations for this module will be executed.</param>
        public MigrationOptions(string moduleName)
        {
            if (string.IsNullOrEmpty(moduleName)) throw new ArgumentException("Empty moduleName.", "moduleName");

            ModuleSelector = n => n == moduleName;
        }

        /// <summary>
        /// Suppresses validation warnings for the provider called <paramref name="providerName"/> and the data type <paramref name="type"/> under the <paramref name="condition"/>.
        /// </summary>
        public void SuppressWarning(string providerName, DbType type, SuppressCondition condition)
        {
            _warningSuppressions.Add(new Suppression(providerName, type, condition));
        }

        /// <summary>
        /// Outputs the SQL used for the migrations to external files without affecting the database.
        /// </summary>
        public void OnlyScriptSqlTo(DirectoryInfo targetDirectory) // signature used in Wiki Manual
        {
            _scriptingOptions = new ScriptingOptions(ScriptingMode.ScriptOnly, targetDirectory);
        }

        /// <summary>
        /// Outputs the SQL used for the migrations to external files without affecting the database.
        /// </summary>
        public void OnlyScriptSqlTo(string targetDirectory) // signature used in Wiki Manual
        {
            OnlyScriptSqlTo(new DirectoryInfo(targetDirectory));
        }

        /// <summary>
        /// Outputs the SQL used for the migrations to external files while migrating the database.
        /// </summary>
        public void ExecuteAndScriptSqlTo(DirectoryInfo targetDirectory) // signature used in Wiki Manual
        {
            _scriptingOptions = new ScriptingOptions(ScriptingMode.ScriptAndExecute, targetDirectory);
        }

        /// <summary>
        /// Outputs the SQL used for the migrations to external files while migrating the database.
        /// </summary>
        public void ExecuteAndScriptSqlTo(string targetDirectory) // signature used in Wiki Manual
        {
            ExecuteAndScriptSqlTo(new DirectoryInfo(targetDirectory));
        }

        internal bool IsWarningSuppressed(string providerName, DbType type, int size, int scale)
        {
            foreach (Suppression suppression in _warningSuppressions
                .Where(s => s.ProviderName == providerName && s.Type == type))
            {
                switch (suppression.Condition)
                {
                    case SuppressCondition.WhenSpecifiedWithoutSize:
                        if (size == 0) return true;
                        break;
                    case SuppressCondition.WhenSpecifiedWithSize:
                        if (size > 0 && scale == 0) return true;
                        break;
                    case SuppressCondition.WhenSpecifiedWithSizeAndScale:
                        if (size > 0 && scale > 0) return true;
                        break;
                    case SuppressCondition.Always:
                        return true;
                    default:
                        continue;
                }
            }
            return false;
        }

        #region Static Options

        ///<summary>
        /// Sets the level of general information being traced.
        ///</summary>
        public static void SetGeneralTraceLevel(SourceLevels sourceLevels)
        {
            Log.SetTraceLevel(LogCategory.General, sourceLevels);
        }

        ///<summary>
        /// Sets the level of SQL information being traced.
        ///</summary>
        public static void SetSqlTraceLevel(SourceLevels sourceLevels) // signature used in a Wiki example
        {
            Log.SetTraceLevel(LogCategory.Sql, sourceLevels);
        }

        ///<summary>
        /// Sets the level of performance information being traced.
        ///</summary>
        public static void SetPerformanceTraceLevel(SourceLevels sourceLevels)
        {
            Log.SetTraceLevel(LogCategory.Performance, sourceLevels);
        }

        #endregion

        private class Suppression
        {
            private readonly string _providerName;
            private readonly DbType _type;
            private readonly SuppressCondition _condition;

            public string ProviderName { get { return _providerName; } }
            public DbType Type { get { return _type; } }
            public SuppressCondition Condition { get { return _condition; } }

            public Suppression(string providerName, DbType type, SuppressCondition condition)
            {
                _providerName = providerName;
                _type = type;
                _condition = condition;
            }
        }
    }

    /// <summary>
    /// Expresses under which circumstances a warning should be expressed for a given <see cref="DbType"/> and its OfSize parameters.
    /// </summary>
    public enum SuppressCondition
    {
        /// <summary>
        /// Suppresses all warnings for the specified <see cref="DbType"/>. Use diligently.
        /// </summary>
        Always = 0,

        /// <summary>
        /// Suppresses warnings for the specified <see cref="DbType"/> when it is used without a specified size.
        /// </summary>
        WhenSpecifiedWithoutSize = 1,

        /// <summary>
        /// Suppresses warnings for the specified <see cref="DbType"/> when it is used with a specified size.
        /// </summary>
        WhenSpecifiedWithSize = 2,

        /// <summary>
        /// Suppresses warnings for the specified <see cref="DbType"/> when it is used with a specified size and a specified scale.
        /// </summary>
        WhenSpecifiedWithSizeAndScale = 3,
    }
}
Something went wrong with that request. Please try again.