Example #1
0
 void ThrowIfAnyDowngradeNotSupported()
 {
     if (Migrations.Any(m => !m.CanDowngrade))
     {
         throw new NotSupportedException("One or more migrations do not support downgrading.");
     }
 }
Example #2
0
        public void UseMigrations(IReadOnlyList <Migration> migrations)
        {
            Migrations = migrations.OrderBy(x => x.Version).Where((x, i) =>
            {
                var expectedVersion = i + 1;

                if (x.Version == expectedVersion)
                {
                    return(true);
                }

                throw new ArgumentException($"Missing migration for version {expectedVersion}.");
            }).ToList();

            ConfiguredVersion = Migrations.Any() ? Migrations.Last().Version : 0;
        }
Example #3
0
        private void LoadMigrationsIfNotLoaded()
        {
            if (Migrations != null)
            {
                return;
            }

            lock (_migrationsResolver)
            {
                if (Migrations != null)
                {
                    return;
                }

                Migrations = _migrationsResolver.Resolve()
                             .OrderBy(x => x.Version)
                             .ToList();

                LatestSchemaVersion = Migrations.Any() ? Migrations.Max(x => x.Version) : 0;
            }

            Interceptor?.DetectedMigrations(Migrations.ToArray(), LatestSchemaVersion);
        }
Example #4
0
        /// <summary>
        ///   Upgrade/downgrade to the specified version.
        /// </summary>
        /// <param name="version">
        ///   The required version of the repository.
        /// </param>
        /// <param name="cancel">
        /// </param>
        /// <returns></returns>
        public async Task MirgrateToVersionAsync(int version, CancellationToken cancel = default(CancellationToken))
        {
            if (version != 0 && !Migrations.Any(m => m.Version == version))
            {
                throw new ArgumentOutOfRangeException("version", $"Repository version '{version}' is unknown.");
            }

            var currentVersion = CurrentVersion;
            var increment      = CurrentVersion < version ? 1 : -1;

            while (currentVersion != version)
            {
                var nextVersion = currentVersion + increment;
                log.InfoFormat("Migrating to version {0}", nextVersion);

                if (increment > 0)
                {
                    var migration = Migrations.FirstOrDefault(m => m.Version == nextVersion);
                    if (migration.CanUpgrade)
                    {
                        await migration.UpgradeAsync(ipfs, cancel);
                    }
                }
                else if (increment < 0)
                {
                    var migration = Migrations.FirstOrDefault(m => m.Version == currentVersion);
                    if (migration.CanDowngrade)
                    {
                        await migration.DowngradeAsync(ipfs, cancel);
                    }
                }

                CurrentVersion = nextVersion;
                currentVersion = nextVersion;
            }
        }