Ejemplo n.º 1
0
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        protected virtual void PopulateMigrations(
            [NotNull] IEnumerable <string> appliedMigrationEntries,
            [NotNull] string targetMigration,
            [NotNull] out IReadOnlyList <Migration> migrationsToApply,
            [NotNull] out IReadOnlyList <Migration> migrationsToRevert,
            [NotNull] out Migration actualTargetMigration)
        {
            var appliedMigrations        = new Dictionary <string, TypeInfo>();
            var unappliedMigrations      = new Dictionary <string, TypeInfo>();
            var appliedMigrationEntrySet = new HashSet <string>(appliedMigrationEntries, StringComparer.OrdinalIgnoreCase);

            if (_migrationsAssembly.Migrations.Count == 0)
            {
                _logger.MigrationsNotFound(this, _migrationsAssembly);
            }

            foreach (var migration in _migrationsAssembly.Migrations)
            {
                if (appliedMigrationEntrySet.Contains(migration.Key))
                {
                    appliedMigrations.Add(migration.Key, migration.Value);
                }
                else
                {
                    unappliedMigrations.Add(migration.Key, migration.Value);
                }
            }

            if (string.IsNullOrEmpty(targetMigration))
            {
                migrationsToApply = unappliedMigrations
                                    .OrderBy(m => m.Key)
                                    .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                    .ToList();
                migrationsToRevert    = Array.Empty <Migration>();
                actualTargetMigration = null;
            }
            else if (targetMigration == Migration.InitialDatabase)
            {
                migrationsToApply  = Array.Empty <Migration>();
                migrationsToRevert = appliedMigrations
                                     .OrderByDescending(m => m.Key)
                                     .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                     .ToList();
                actualTargetMigration = null;
            }
            else
            {
                targetMigration   = _migrationsAssembly.GetMigrationId(targetMigration);
                migrationsToApply = unappliedMigrations
                                    .Where(m => string.Compare(m.Key, targetMigration, StringComparison.OrdinalIgnoreCase) <= 0)
                                    .OrderBy(m => m.Key)
                                    .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                    .ToList();
                migrationsToRevert = appliedMigrations
                                     .Where(m => string.Compare(m.Key, targetMigration, StringComparison.OrdinalIgnoreCase) > 0)
                                     .OrderByDescending(m => m.Key)
                                     .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                     .ToList();
                actualTargetMigration = appliedMigrations
                                        .Where(m => string.Compare(m.Key, targetMigration, StringComparison.OrdinalIgnoreCase) == 0)
                                        .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                        .SingleOrDefault();
            }
        }
Ejemplo n.º 2
0
        private IEnumerable <Func <IReadOnlyList <IRelationalCommand> > > GetMigrationCommands(
            IReadOnlyList <HistoryRow> appliedMigrationEntries,
            string targetMigration = null)
        {
            var appliedMigrations   = new Dictionary <string, TypeInfo>();
            var unappliedMigrations = new Dictionary <string, TypeInfo>();

            foreach (var migration in _migrationsAssembly.Migrations)
            {
                if (appliedMigrationEntries.Any(
                        e => string.Equals(e.MigrationId, migration.Key, StringComparison.OrdinalIgnoreCase)))
                {
                    appliedMigrations.Add(migration.Key, migration.Value);
                }
                else
                {
                    unappliedMigrations.Add(migration.Key, migration.Value);
                }
            }

            IReadOnlyList <Migration> migrationsToApply;
            IReadOnlyList <Migration> migrationsToRevert;

            if (string.IsNullOrEmpty(targetMigration))
            {
                migrationsToApply = unappliedMigrations
                                    .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                    .ToList();
                migrationsToRevert = new Migration[0];
            }
            else if (targetMigration == Migration.InitialDatabase)
            {
                migrationsToApply  = new Migration[0];
                migrationsToRevert = appliedMigrations
                                     .OrderByDescending(m => m.Key)
                                     .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                     .ToList();
            }
            else
            {
                targetMigration   = _migrationsAssembly.GetMigrationId(targetMigration);
                migrationsToApply = unappliedMigrations
                                    .Where(m => string.Compare(m.Key, targetMigration, StringComparison.OrdinalIgnoreCase) <= 0)
                                    .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                    .ToList();
                migrationsToRevert = appliedMigrations
                                     .Where(m => string.Compare(m.Key, targetMigration, StringComparison.OrdinalIgnoreCase) > 0)
                                     .OrderByDescending(m => m.Key)
                                     .Select(p => _migrationsAssembly.CreateMigration(p.Value, _activeProvider))
                                     .ToList();
            }

            for (var i = 0; i < migrationsToRevert.Count; i++)
            {
                var migration = migrationsToRevert[i];

                yield return(() =>
                {
                    _logger.LogInformation(RelationalStrings.RevertingMigration(migration.GetId()));

                    return GenerateDownSql(
                        migration,
                        i != migrationsToRevert.Count - 1
                                ? migrationsToRevert[i + 1]
                                : null);
                });
            }

            foreach (var migration in migrationsToApply)
            {
                yield return(() =>
                {
                    _logger.LogInformation(RelationalStrings.ApplyingMigration(migration.GetId()));

                    return GenerateUpSql(migration);
                });
            }
        }