public bool Execute(IMongoDatabase database, MongoStorageOptions storageOptions, IMongoMigrationBag migrationBag)
        {
            var jobIdMapping = migrationBag.GetItem <Dictionary <int, string> >("JobIdMapping");

            // Update jobQueue to reflect new job id
            var jobQueueCollection   = database.GetCollection <BsonDocument>($@"{storageOptions.Prefix}.jobQueue");
            var migratedJobQueueList = jobQueueCollection.Find(_ => true).ToList().Select(jq =>
            {
                // NOTE:
                // This is a "hack". We actually migrate to schema version 7,
                // where we should have migrated to schema version 6.
                // But there is a issue in schema version 6 that will
                // cause us to loose information.
                // Since any version of Hangfire.Mongo running schema version 6
                // does not have migration, it is save to do this.
                jq["_id"]   = new BsonObjectId(ObjectId.GenerateNewId());
                jq["JobId"] = jobIdMapping[jq["JobId"].AsInt32];
                return(jq);
            }).ToList();

            if (migratedJobQueueList.Any())
            {
                jobQueueCollection.InsertMany(migratedJobQueueList);
            }

            var filter = Builders <BsonDocument> .Filter.In("JobId", jobIdMapping.Keys);

            jobQueueCollection.DeleteMany(filter);

            return(true);
        }
Exemplo n.º 2
0
        public bool Execute(IMongoDatabase database, MongoStorageOptions storageOptions, IMongoMigrationBag migrationBag)
        {
            var jobIdMapping = migrationBag.GetItem <Dictionary <int, string> >("JobIdMapping");

            var setCollection = database.GetCollection <BsonDocument>($@"{storageOptions.Prefix}.set");
            var filter        = Builders <BsonDocument> .Filter.Eq("Key", "schedule");

            var migratedSetList = setCollection.Find(filter).ToList().Select(s =>
            {
                s["Value"] = jobIdMapping[int.Parse(s["Value"].AsString)];
                s["_t"]    = new BsonArray(new[] { "KeyValueDto", "ExpiringKeyValueDto", "SetDto" });
                return(s);
            }).ToList();

            if (migratedSetList.Any())
            {
                var stateDataCollection = database.GetCollection <BsonDocument>($@"{storageOptions.Prefix}.statedata");
                stateDataCollection.InsertMany(migratedSetList);
            }
            return(true);
        }