/// <summary>
        /// Update database values as required for the current version. Typically this is used to apply bug fixes
        /// that require updates to database settings (such as media and UI templates).
        /// </summary>
        /// <param name="ctx">Context to be used for updating data.</param>
        /// <remarks>This function detects the current app schema version as defined in the AppSetting table and applies
        /// all relevant updates to bring it up to the current version. By the time this method exits, the app schema version
        /// in the AppSetting table will match the current schema version as defined in <see cref="GalleryDb.DataSchemaVersion" />.
        /// </remarks>
        /// <exception cref="System.Exception"></exception>
        public static void ApplyDbUpdates(GalleryDb ctx)
        {
            if (!ctx.AppSettings.Any())
            {
                SeedController.InsertSeedData(ctx);
            }

            var curSchema = GetCurrentSchema(ctx);

            while (curSchema < GalleryDb.DataSchemaVersion)
            {
                var oldSchema = curSchema;

                switch (curSchema)
                {
                case GalleryDataSchemaVersion.V3_0_0: UpgradeTo301(ctx); break;

                case GalleryDataSchemaVersion.V3_0_1: UpgradeTo302(ctx); break;

                case GalleryDataSchemaVersion.V3_0_2: UpgradeTo303(ctx); break;

                case GalleryDataSchemaVersion.V3_0_3: UpgradeTo310(ctx); break;
                }

                curSchema = GetCurrentSchema(ctx);

                if (curSchema == oldSchema)
                {
                    throw new Exception(String.Format("The migration function for schema {0} should have incremented the data schema version in the AppSetting table, but it did not.", curSchema));
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Verify there are gallery settings for the current gallery that match every template gallery setting, creating any
        /// if necessary.
        /// </summary>
        /// <returns><c>true</c> if data was changed that necessitates reloading data from the data store, <c>false</c> otherwise.</returns>
        private bool ConfigureGallerySettingsTable()
        {
            var foundTmplGallerySettings = false;
            var needToClearCache         = false;

            using (var repo = new GallerySettingRepository())
            {
                //repo.Load();
                var gallerySettingNamesInGallery = repo.Where(gs => gs.FKGalleryId == GalleryId).Select(gs => gs.SettingName).ToList();

                // Loop through each template gallery setting.
                foreach (var gsTmpl in repo.Where(g => g.Gallery.IsTemplate))
                {
                    foundTmplGallerySettings = true;
                    //if (!repo.Local.Any(gs => gs.SettingName == gsTmpl.SettingName && gs.FKGalleryId == GalleryId))
                    //if (!repo.Where(gs => gs.SettingName == gsTmpl.SettingName && gs.FKGalleryId == GalleryId).Any())
                    if (!gallerySettingNamesInGallery.Contains(gsTmpl.SettingName))
                    {
                        // This gallery is missing an entry for a gallery setting. Create one by copying it from the template gallery.
                        repo.Add(new GallerySettingDto()
                        {
                            FKGalleryId  = GalleryId,
                            SettingName  = gsTmpl.SettingName,
                            SettingValue = gsTmpl.SettingValue
                        });

                        needToClearCache = true;
                    }
                }

                repo.Save();
            }

            if (!foundTmplGallerySettings)
            {
                // If there weren't *any* template gallery settings, insert the seed data. Generally this won't be necessary, but it
                // can help recover from certain conditions, such as when a SQL Server connection is accidentally specified without
                // the MultipleActiveResultSets keyword (or it was false). In this situation the galleries are inserted but an error
                // prevents the remaining data from being inserted. Once the user corrects this and tries again, this code can run to
                // finish inserting the seed data.
                using (var ctx = new GalleryDb())
                {
                    SeedController.InsertSeedData(ctx);
                }
                Factory.ValidateGalleries();
            }

            return(needToClearCache);
        }