using System.Globalization; using FluentMigrator; using Nop.Core; namespace Nop.Data.Migrations; /// /// Attribute for a migration /// public partial class NopMigrationAttribute : MigrationAttribute { #region Fields protected readonly MigrationConfig _config; #endregion #region Ctor /// /// Initializes a new instance of the NopMigrationAttribute class /// /// The migration date time string to convert on version /// The target migration process public NopMigrationAttribute(string dateTime, MigrationProcessType targetMigrationProcess = MigrationProcessType.NoMatter) : this(new MigrationConfig { DateTime = dateTime, TargetMigrationProcess = targetMigrationProcess }) { } /// /// Initializes a new instance of the NopMigrationAttribute class /// /// The migration date time string to convert on version /// The migration description /// The target migration process public NopMigrationAttribute(string dateTime, string description, MigrationProcessType targetMigrationProcess = MigrationProcessType.NoMatter) : this(new MigrationConfig { DateTime = dateTime, Description = description, TargetMigrationProcess = targetMigrationProcess }) { } /// /// Initializes a new instance of the NopMigrationAttribute class /// /// The migration date time string to convert on version /// nopCommerce full version /// The update migration type /// The target migration process public NopMigrationAttribute(string dateTime, string nopVersion, UpdateMigrationType updateMigrationType, MigrationProcessType targetMigrationProcess = MigrationProcessType.NoMatter) : this(new MigrationConfig { DateTime = dateTime, NopVersion = nopVersion, UpdateMigrationType = updateMigrationType, TargetMigrationProcess = targetMigrationProcess, }) { } /// /// Initializes a new instance of the NopMigrationAttribute class /// /// The migration configuration data protected NopMigrationAttribute(MigrationConfig config) : base(config.Version, config.Description) { _config = config; } #endregion #region Properties /// /// Target migration process /// public virtual MigrationProcessType TargetMigrationProcess => _config.TargetMigrationProcess; /// /// Gets the value which indicate is this schema migration /// /// /// If set to true than this migration will apply right after the migration runner will become available. /// Do not us dependency injection in migrations that are marked as schema migration, /// because IoC container not ready yet. /// public virtual bool IsSchemaMigration { get => _config.IsSchemaMigration; protected set => _config.IsSchemaMigration = value; } /// /// Gets the flag which indicate whether the migration should be applied into DB on the debug mode /// public virtual bool ApplyInDbOnDebugMode { get => _config.ApplyInDbOnDebugMode; protected set => _config.ApplyInDbOnDebugMode = value; } #endregion #region Nested class protected partial class MigrationConfig { #region Fields protected long? _version; protected string _description; #endregion /// /// Gets or sets the migration date time string to convert on version /// public string DateTime { get; set; } /// /// nopCommerce full version /// public string NopVersion { get; set; } /// /// Gets or sets the update migration type /// public virtual UpdateMigrationType? UpdateMigrationType { get; set; } /// /// Gets or sets the target migration process type /// public virtual MigrationProcessType TargetMigrationProcess { get; set; } = MigrationProcessType.NoMatter; /// /// Gets or sets the migration version /// public virtual long Version { get { if (_version.HasValue) return _version.Value; if (string.IsNullOrEmpty(DateTime)) throw new NopException("One of the following properties must be initialized: either Version or DateTime"); var version = System.DateTime .ParseExact(DateTime, NopMigrationDefaults.DateFormats, CultureInfo.InvariantCulture).Ticks; if (UpdateMigrationType.HasValue) version += (int)UpdateMigrationType; return version; } set => _version = value; } /// /// Gets or sets the migration description /// public virtual string Description { get { if (!string.IsNullOrEmpty(_description)) return _description; string description = null; if (string.IsNullOrEmpty(NopVersion)) throw new NopException("One of the following properties must be initialized: either Description or NopVersion"); if (UpdateMigrationType.HasValue) description = string.Format(NopMigrationDefaults.UpdateMigrationDescription, NopVersion, UpdateMigrationType.ToString()); return description; } set => _description = value; } /// /// Gets the flag which indicate whether the migration should be applied into DB on the debug mode /// public bool ApplyInDbOnDebugMode { get; set; } = true; /// /// Gets or sets the value which indicate is this schema migration /// /// /// If set to true than this migration will apply right after the migration runner will become available. /// Do not us dependency injection in migrations that are marked as schema migration, /// because IoC container not ready yet. /// public virtual bool IsSchemaMigration { get; set; } } #endregion }