예제 #1
0
        public async Task Execute()
        {
            var migrationLogDb = GetMigrationLogDatabase();

            var fileEntries = await GetFiles(migrationLogDb);

            foreach (var jsonFile in fileEntries)
            {
                Console.WriteLine("Start processing " + jsonFile.FilePath);
                using (var file = File.OpenText(jsonFile.FilePath))
                    using (var reader = new JsonTextReader(file))
                    {
                        try
                        {
                            var allData = (JObject)JToken.ReadFrom(reader);

                            var collectionName = allData.SelectToken("CollectionName").Value <string>();
                            var setting        = _dbSettings.Settings.First(x => x.CollectionName == collectionName);

                            var jsonData = allData.SelectToken("Data");
                            var client   = new MongoClient(setting.ConnectionString);
                            var database = client.GetDatabase(setting.DatabaseName);

                            var migrationType = allData.SelectToken("MigrationType").Value <string>();

                            switch (migrationType)
                            {
                            case "data":
                                Console.WriteLine("Upserting Data");
                                await ScriptHelper.Execute(database, jsonData, collectionName, setting.Identifier);

                                break;

                            case "index":
                                Console.WriteLine("Upserting Indexes");
                                var indexHelper = new IndexHelper();
                                await indexHelper.Execute(database, jsonData, collectionName);

                                break;

                            default:
                                Console.WriteLine("Migration type not supported.");
                                break;
                            }

                            await SaveLogEntry(migrationLogDb, jsonFile);
                        }
                        catch (MongoException me)
                        {
                            Console.WriteLine("An Error related to MongoDb occurred: " + me.Message);
                            return;
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("An Error occurred: " + e.Message);
                            return;
                        }
                    }
                Console.WriteLine("Done processing " + jsonFile.FilePath);
            }
            Console.WriteLine("MongoReadyRoll completed!");
        }