示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sqlSyntax"></param>
        /// <param name="migrationEntryService"></param>
        /// <param name="logger"></param>
        public void RunMigrations(ISqlSyntaxProvider sqlSyntax, IMigrationEntryService migrationEntryService, ILogger logger)
        {
            var currentVersion  = new SemVersion(0);
            var migrations      = ApplicationContext.Current.Services.MigrationEntryService.GetAll(ProductName).OrderByDescending(x => x.CreateDate);
            var latestMigration = migrations.FirstOrDefault();

            if (latestMigration != null)
            {
                currentVersion = latestMigration.Version;
            }

            if (TargetVersion == currentVersion)
            {
                return;
            }

            IMigration[] scriptsForMigration =
            {
                (IMigration) new Versions.Migration100(sqlSyntax, logger)
            };

            MigrationRunner migrationsRunner = new MigrationRunner(migrationEntryService, logger, currentVersion, TargetVersion,
                                                                   ProductName, scriptsForMigration);

            try
            {
                migrationsRunner.Execute(ApplicationContext.Current.DatabaseContext.Database);
            }
            catch (Exception ex)
            {
                LogHelper.Error <Migration>($"Error running {ProductName} migration", ex);
            }
        }
        /// <summary>
        /// Checks in the db which version is installed based on the migrations that have been run
        /// </summary>
        /// <param name="migrationEntryService"></param>
        /// <returns></returns>
        public SemVersion DetermineInstalledVersionByMigrations(IMigrationEntryService migrationEntryService)
        {
            var allMigrations = migrationEntryService.GetAll(GlobalSettings.UmbracoMigrationName);
            var mostrecent    = allMigrations.OrderByDescending(x => x.Version).Select(x => x.Version).FirstOrDefault();

            return(mostrecent ?? new SemVersion(new Version(0, 0, 0)));
        }
示例#3
0
        public MigrationRunner(IMigrationEntryService migrationEntryService, ILogger logger, SemVersion currentVersion, SemVersion targetVersion, string productName, params IMigration[] migrations)
        {
            if (migrationEntryService == null)
            {
                throw new ArgumentNullException("migrationEntryService");
            }
            if (logger == null)
            {
                throw new ArgumentNullException("logger");
            }
            if (currentVersion == null)
            {
                throw new ArgumentNullException("currentVersion");
            }
            if (targetVersion == null)
            {
                throw new ArgumentNullException("targetVersion");
            }
            Mandate.ParameterNotNullOrEmpty(productName, "productName");

            _migrationEntryService = migrationEntryService;
            _logger         = logger;
            _currentVersion = currentVersion;
            _targetVersion  = targetVersion;
            _productName    = productName;
            //ensure this is null if there aren't any
            _migrations = migrations == null || migrations.Length == 0 ? null : migrations;
        }
示例#4
0
        /// <summary>
        /// Helper method called to update the migration entries with an entry that matches the
        /// latest target version
        /// </summary>
        /// <param name="migrationService"></param>
        private void AddTargetVersionMigrationEntry(IMigrationEntryService migrationService)
        {
            var match = migrationService.FindEntry(REDIRECTS_TABLE_NAME, this._targetVersion);

            if (match == null)
            {
                migrationService.CreateEntry(REDIRECTS_TABLE_NAME, this._targetVersion);
            }
        }
 public MigrationRunnerProxy(
     ILogger logger,
     IMigrationEntryService migrationEntryService,
     DatabaseContext umbracoContext)
 {
     _logger = logger;
     _migrationEntryService = migrationEntryService;
     _databaseContext       = umbracoContext;
 }
示例#6
0
        internal void CreateDatabaseSchemaDo(IMigrationEntryService migrationEntryService)
        {
            _logger.Info <Database>("Initializing database schema creation");

            var creation = new DatabaseSchemaCreation(_db, _logger, _syntaxProvider);

            creation.InitializeDatabaseSchema();

            _logger.Info <Database>("Finalized database schema creation");
        }
        /// <summary>
        /// Checks in the db which version is installed based on the migrations that have been run
        /// </summary>
        /// <param name="migrationEntryService"></param>
        /// <returns></returns>
        public SemVersion DetermineInstalledVersionByMigrations(IMigrationEntryService migrationEntryService)
        {
            SemVersion mostrecent = null;

            if (ValidTables.Any(x => x.InvariantEquals("umbracoMigration")))
            {
                var allMigrations = migrationEntryService.GetAll(Constants.System.UmbracoMigrationName);
                mostrecent = allMigrations.OrderByDescending(x => x.Version).Select(x => x.Version).FirstOrDefault();
            }

            return(mostrecent ?? new SemVersion(new Version(0, 0, 0)));
        }
示例#8
0
 /// <summary>
 /// Hack to support legacy version checking. This supports the state that exists before
 /// we implemented migration version entry on database create, which will exist moving forward.
 /// This only supports legacy installs
 /// </summary>
 /// <param name="db">Database</param>
 /// <param name="migrationService">Migration service</param>
 /// <returns>True if success</returns>
 private bool ValidateTableRead(UmbracoDatabase db, IMigrationEntryService migrationService)
 {
     try
     {
         // run through creating and deleting a redirect.
         var redirect = RedirectRepository.AddRedirect(true, "old", "new", "notes");
         RedirectRepository.DeleteRedirect(redirect.Id);
         this.AddTargetVersionMigrationEntry(migrationService);
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
示例#9
0
 public UpgradeDeliverable(
     TextReader reader,
     TextWriter writer,
     IMigrationRunnerService migrationRunner,
     IMigrationEntryService migrationEntryService,
     IChauffeurSettings chauffeurSettings,
     IXmlDocumentWrapper xmlDocumentWrapper
     )
     : base(reader, writer)
 {
     this.migrationRunner       = migrationRunner;
     this.migrationEntryService = migrationEntryService;
     this.chauffeurSettings     = chauffeurSettings;
     this.xmlDocumentWrapper    = xmlDocumentWrapper;
 }
示例#10
0
        /// <summary>
        /// Handles checking and running migrations
        /// </summary>
        /// <param name="db">Database context</param>
        /// <param name="migrationService">Migration service</param>
        private void HandleMigrations(UmbracoDatabase db, IMigrationEntryService migrationService)
        {
            var latestMigrationVersion = new SemVersion(0, 0, 0);

            // get all migrations for "Redirects" already executed
            var migrations = migrationService.GetAll(REDIRECTS_TABLE_NAME);

            // get the latest migration for "Redirects" executed
            var latestMigration = migrations.OrderByDescending(x => x.Version).FirstOrDefault();

            if (latestMigration != null)
            {
                latestMigrationVersion = latestMigration.Version;
            }

            if (this._targetVersion == latestMigrationVersion)
            {
                return;
            }

            var migrationsRunner = new MigrationRunner(
                migrationService,
                ApplicationContext.Current.ProfilingLogger.Logger,
                latestMigrationVersion,
                this._targetVersion,
                REDIRECTS_TABLE_NAME);

            try
            {
                migrationsRunner.Execute(db);
            }
            catch (HttpException e) {}
            catch (Exception e)
            {
                LogHelper.Error <MigrationRunner>("Error running Redirects migration", e);
            }
        }
示例#11
0
        /// <summary>
        /// This assumes all of the previous checks are done!
        /// </summary>
        /// <returns></returns>
        internal Result UpgradeSchemaAndData(IMigrationEntryService migrationEntryService)
        {
            try
            {
                var readyForInstall = CheckReadyForInstall();
                if (readyForInstall.Success == false)
                {
                    return(readyForInstall.Result);
                }

                _logger.Info <DatabaseContext>("Database upgrade started");

                var database = new UmbracoDatabase(_connectionString, ProviderName, _logger);
                //var supportsCaseInsensitiveQueries = SqlSyntax.SupportsCaseInsensitiveQueries(database);

                var message = GetResultMessageForMySql();

                var schemaResult = ValidateDatabaseSchema();

                var installedSchemaVersion = new SemVersion(schemaResult.DetermineInstalledVersion());

                var installedMigrationVersion = schemaResult.DetermineInstalledVersionByMigrations(migrationEntryService);

                var targetVersion = UmbracoVersion.Current;

                //In some cases - like upgrading from 7.2.6 -> 7.3, there will be no migration information in the database and therefore it will
                // return a version of 0.0.0 and we don't necessarily want to run all migrations from 0 -> 7.3, so we'll just ensure that the
                // migrations are run for the target version
                if (installedMigrationVersion == new SemVersion(new Version(0, 0, 0)) && installedSchemaVersion > new SemVersion(new Version(0, 0, 0)))
                {
                    //set the installedMigrationVersion to be one less than the target so the latest migrations are guaranteed to execute
                    installedMigrationVersion = new SemVersion(targetVersion.SubtractRevision());
                }

                //Figure out what our current installed version is. If the web.config doesn't have a version listed, then we'll use the minimum
                // version detected between the schema installed and the migrations listed in the migration table.
                // If there is a version in the web.config, we'll take the minimum between the listed migration in the db and what
                // is declared in the web.config.

                var currentInstalledVersion = string.IsNullOrEmpty(GlobalSettings.ConfigurationStatus)
                                              //Take the minimum version between the detected schema version and the installed migration version
                    ? new[] { installedSchemaVersion, installedMigrationVersion }.Min()
                //Take the minimum version between the installed migration version and the version specified in the config
                    : new[] { SemVersion.Parse(GlobalSettings.ConfigurationStatus), installedMigrationVersion }.Min();

                //Ok, another edge case here. If the current version is a pre-release,
                // then we want to ensure all migrations for the current release are executed.
                if (currentInstalledVersion.Prerelease.IsNullOrWhiteSpace() == false)
                {
                    currentInstalledVersion = new SemVersion(currentInstalledVersion.GetVersion().SubtractRevision());
                }

                //DO the upgrade!

                var runner = new MigrationRunner(migrationEntryService, _logger, currentInstalledVersion, UmbracoVersion.GetSemanticVersion(), Constants.System.UmbracoMigrationName);

                var upgraded = runner.Execute(database, true);

                if (upgraded == false)
                {
                    throw new ApplicationException("Upgrading failed, either an error occurred during the upgrade process or an event canceled the upgrade process, see log for full details");
                }

                message = message + "<p>Upgrade completed!</p>";

                //now that everything is done, we need to determine the version of SQL server that is executing

                _logger.Info <DatabaseContext>("Database configuration status: " + message);

                return(new Result {
                    Message = message, Success = true, Percentage = "100"
                });
            }
            catch (Exception ex)
            {
                return(HandleInstallException(ex));
            }
        }
示例#12
0
 /// <summary>
 /// Check if we have an entry for the latest version
 /// </summary>
 /// <param name="migrationService">Migration service</param>
 /// <returns>True if we have an entry</returns>
 private bool MigrationVersionCheck(IMigrationEntryService migrationService)
 {
     return(migrationService.FindEntry(REDIRECTS_TABLE_NAME, this._targetVersion) != null);
 }
示例#13
0
 /// <summary>
 /// public ctor - will generally just be used for unit testing all items are optional and if not specified, the defaults will be used
 /// </summary>
 /// <param name="contentService"></param>
 /// <param name="mediaService"></param>
 /// <param name="contentTypeService"></param>
 /// <param name="dataTypeService"></param>
 /// <param name="fileService"></param>
 /// <param name="localizationService"></param>
 /// <param name="packagingService"></param>
 /// <param name="entityService"></param>
 /// <param name="relationService"></param>
 /// <param name="memberGroupService"></param>
 /// <param name="memberTypeService"></param>
 /// <param name="memberService"></param>
 /// <param name="userService"></param>
 /// <param name="sectionService"></param>
 /// <param name="treeService"></param>
 /// <param name="tagService"></param>
 /// <param name="notificationService"></param>
 /// <param name="localizedTextService"></param>
 /// <param name="auditService"></param>
 /// <param name="domainService"></param>
 /// <param name="taskService"></param>
 /// <param name="macroService"></param>
 /// <param name="publicAccessService"></param>
 /// <param name="externalLoginService"></param>
 /// <param name="migrationEntryService"></param>
 public ServiceContext(
     IContentService contentService         = null,
     IMediaService mediaService             = null,
     IContentTypeService contentTypeService = null,
     IDataTypeService dataTypeService       = null,
     IFileService fileService = null,
     ILocalizationService localizationService = null,
     IPackagingService packagingService       = null,
     IEntityService entityService             = null,
     IRelationService relationService         = null,
     IMemberGroupService memberGroupService   = null,
     IMemberTypeService memberTypeService     = null,
     IMemberService memberService             = null,
     IUserService userService            = null,
     ISectionService sectionService      = null,
     IApplicationTreeService treeService = null,
     ITagService tagService = null,
     INotificationService notificationService   = null,
     ILocalizedTextService localizedTextService = null,
     IAuditService auditService                   = null,
     IDomainService domainService                 = null,
     ITaskService taskService                     = null,
     IMacroService macroService                   = null,
     IPublicAccessService publicAccessService     = null,
     IExternalLoginService externalLoginService   = null,
     IMigrationEntryService migrationEntryService = null)
 {
     if (migrationEntryService != null)
     {
         _migrationEntryService = new Lazy <IMigrationEntryService>(() => migrationEntryService);
     }
     if (externalLoginService != null)
     {
         _externalLoginService = new Lazy <IExternalLoginService>(() => externalLoginService);
     }
     if (auditService != null)
     {
         _auditService = new Lazy <IAuditService>(() => auditService);
     }
     if (localizedTextService != null)
     {
         _localizedTextService = new Lazy <ILocalizedTextService>(() => localizedTextService);
     }
     if (tagService != null)
     {
         _tagService = new Lazy <ITagService>(() => tagService);
     }
     if (contentService != null)
     {
         _contentService = new Lazy <IContentService>(() => contentService);
     }
     if (mediaService != null)
     {
         _mediaService = new Lazy <IMediaService>(() => mediaService);
     }
     if (contentTypeService != null)
     {
         _contentTypeService = new Lazy <IContentTypeService>(() => contentTypeService);
     }
     if (dataTypeService != null)
     {
         _dataTypeService = new Lazy <IDataTypeService>(() => dataTypeService);
     }
     if (fileService != null)
     {
         _fileService = new Lazy <IFileService>(() => fileService);
     }
     if (localizationService != null)
     {
         _localizationService = new Lazy <ILocalizationService>(() => localizationService);
     }
     if (packagingService != null)
     {
         _packagingService = new Lazy <IPackagingService>(() => packagingService);
     }
     if (entityService != null)
     {
         _entityService = new Lazy <IEntityService>(() => entityService);
     }
     if (relationService != null)
     {
         _relationService = new Lazy <IRelationService>(() => relationService);
     }
     if (sectionService != null)
     {
         _sectionService = new Lazy <ISectionService>(() => sectionService);
     }
     if (memberGroupService != null)
     {
         _memberGroupService = new Lazy <IMemberGroupService>(() => memberGroupService);
     }
     if (memberTypeService != null)
     {
         _memberTypeService = new Lazy <IMemberTypeService>(() => memberTypeService);
     }
     if (treeService != null)
     {
         _treeService = new Lazy <IApplicationTreeService>(() => treeService);
     }
     if (memberService != null)
     {
         _memberService = new Lazy <IMemberService>(() => memberService);
     }
     if (userService != null)
     {
         _userService = new Lazy <IUserService>(() => userService);
     }
     if (notificationService != null)
     {
         _notificationService = new Lazy <INotificationService>(() => notificationService);
     }
     if (domainService != null)
     {
         _domainService = new Lazy <IDomainService>(() => domainService);
     }
     if (taskService != null)
     {
         _taskService = new Lazy <ITaskService>(() => taskService);
     }
     if (macroService != null)
     {
         _macroService = new Lazy <IMacroService>(() => macroService);
     }
     if (publicAccessService != null)
     {
         _publicAccessService = new Lazy <IPublicAccessService>(() => publicAccessService);
     }
 }
 public MigrationManager(ApplicationContext applicationContext)
 {
     _migrationService = applicationContext.Services.MigrationEntryService;
     _logger           = applicationContext.ProfilingLogger.Logger;
     _databaseContext  = applicationContext.DatabaseContext;
 }
示例#15
0
 public MigrationRunnerService(IMigrationEntryService migrationEntryService, ILogger logger, UmbracoDatabase database)
 {
     this.logger = logger;
     this.migrationEntryService = migrationEntryService;
     this.database = database;
 }