Esempio n. 1
0
        public SettingsMigrator(InSearchObjectContext ctx)
        {
            Guard.ArgumentNotNull(() => ctx);

            _ctx      = ctx;
            _settings = _ctx.Set <Setting>();
        }
Esempio n. 2
0
        public LocaleResourcesMigrator(InSearchObjectContext ctx)
        {
            Guard.ArgumentNotNull(() => ctx);

            _ctx       = ctx;
            _languages = _ctx.Set <Language>();
            _resources = _ctx.Set <LocaleStringResource>();
        }
        public static void MigrateSettings(this InSearchObjectContext ctx, Action <SettingsBuilder> fn)
        {
            Guard.ArgumentNotNull(() => ctx);
            Guard.ArgumentNotNull(() => fn);

            var builder = new SettingsBuilder();

            fn(builder);
            var entries = builder.Build();

            var migrator = new SettingsMigrator(ctx);

            migrator.Migrate(entries);
        }
        public static void MigrateLocaleResources(this InSearchObjectContext ctx, Action <LocaleResourcesBuilder> fn, bool updateTouchedResources = false)
        {
            Guard.ArgumentNotNull(() => ctx);
            Guard.ArgumentNotNull(() => fn);

            var builder = new LocaleResourcesBuilder();

            fn(builder);
            var entries = builder.Build();

            var migrator = new LocaleResourcesMigrator(ctx);

            migrator.Migrate(entries, updateTouchedResources);
        }
Esempio n. 5
0
        public static void ExecutePendingResourceMigrations(string resPath, InSearchObjectContext dbContext)
        {
            Guard.ArgumentNotNull(() => dbContext);

            string headPath = Path.Combine(resPath, "head.txt");

            if (!File.Exists(headPath))
            {
                return;
            }

            string resHead = File.ReadAllText(headPath).Trim();

            if (!MigratorUtils.IsValidMigrationId(resHead))
            {
                return;
            }

            var migrator   = new DbMigrator(new MigrationsConfiguration());
            var migrations = GetPendingResourceMigrations(migrator, resHead);

            foreach (var id in migrations)
            {
                if (IsAutomaticMigration(id))
                {
                    continue;
                }

                if (!IsValidMigrationId(id))
                {
                    continue;
                }

                // Resolve and instantiate the DbMigration instance from the assembly
                var migration = CreateMigrationInstanceByMigrationId(id, migrator.Configuration);

                var provider = migration as ILocaleResourcesProvider;
                if (provider == null)
                {
                    continue;
                }

                var builder = new LocaleResourcesBuilder();
                provider.MigrateLocaleResources(builder);

                var resEntries  = builder.Build();
                var resMigrator = new LocaleResourcesMigrator(dbContext);
                resMigrator.Migrate(resEntries);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Migrates the database to the latest version
        /// </summary>
        /// <returns>The number of applied migrations</returns>
        public int RunPendingMigrations(TContext context)
        {
            var pendingMigrations = GetPendingMigrations().ToList();

            if (!pendingMigrations.Any())
            {
                return(0);
            }

            var coreSeeders             = new List <SeederEntry>();
            var externalSeeders         = new List <SeederEntry>();
            var isCoreMigration         = context is InSearchObjectContext;
            var initialMigration        = this.GetDatabaseMigrations().LastOrDefault() ?? "[Initial]";
            var lastSuccessfulMigration = initialMigration;

            IDataSeeder <InSearchObjectContext> coreSeeder = null;
            IDataSeeder <TContext> externalSeeder          = null;

            int result = 0;

            // Apply migrations
            foreach (var migrationId in pendingMigrations)
            {
                if (MigratorUtils.IsAutomaticMigration(migrationId))
                {
                    continue;
                }

                if (!MigratorUtils.IsValidMigrationId(migrationId))
                {
                    continue;
                }

                // Resolve and instantiate the DbMigration instance from the assembly
                var migration = MigratorUtils.CreateMigrationInstanceByMigrationId(migrationId, Configuration);

                // Seeders for the core DbContext must be run in any case
                // (e.g. for Resource or Setting updates even from external plugins)
                coreSeeder     = migration as IDataSeeder <InSearchObjectContext>;
                externalSeeder = null;

                if (!isCoreMigration)
                {
                    // Context specific seeders should only be resolved
                    // when origin is external (e.g. a Plugin)
                    externalSeeder = migration as IDataSeeder <TContext>;
                }

                try
                {
                    // Call the actual Update() to execute this migration
                    Update(migrationId);
                    result++;
                }
                catch (AutomaticMigrationsDisabledException)
                {
                    if (context is InSearchObjectContext)
                    {
                        throw;
                    }

                    // DbContexts in plugin assemblies tend to produce
                    // this error, but obviously without any negative side-effect.
                    // Therefore catch and forget!
                    // TODO: (MC) investigate this and implement a cleaner solution
                }
                catch (Exception ex)
                {
                    result = 0;
                    throw new DbMigrationException(lastSuccessfulMigration, migrationId, ex.InnerException ?? ex, false);
                }

                if (coreSeeder != null)
                {
                    coreSeeders.Add(new SeederEntry
                    {
                        DataSeeder          = coreSeeder,
                        MigrationId         = migrationId,
                        PreviousMigrationId = lastSuccessfulMigration,
                    });
                }

                if (externalSeeder != null)
                {
                    externalSeeders.Add(new SeederEntry
                    {
                        DataSeeder          = externalSeeder,
                        MigrationId         = migrationId,
                        PreviousMigrationId = lastSuccessfulMigration,
                    });
                }

                lastSuccessfulMigration = migrationId;
            }

            // Apply core data seeders first
            InSearchObjectContext coreContext = null;

            if (coreSeeders.Any())
            {
                coreContext = isCoreMigration ? context as InSearchObjectContext : new InSearchObjectContext();
                RunSeeders <InSearchObjectContext>(coreSeeders, coreContext);
            }

            // Apply external data seeders
            RunSeeders <TContext>(externalSeeders, context);

            Logger.Information("Database migration successful: {0} >> {1}".FormatInvariant(initialMigration, lastSuccessfulMigration));

            return(result);
        }