Exemplo n.º 1
0
        private async Task ExecuteScriptStrategiesAsync(Assembly migrationAssembly)
        {
            //read all the migration embed in CosmosDB/Migrations
            var ressources = migrationAssembly.GetManifestResourceNames()
                             .Where(r => r.Contains("." + options.Value.MigrationFolder + "."))
                             .OrderBy(r => r)
                             .ToList();

            //for each migration
            foreach (var migration in ressources)
            {
                string migrationContent;
                using (var stream = migrationAssembly.GetManifestResourceStream(migration))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        migrationContent = await reader.ReadToEndAsync();
                    }
                }
                var parsedMigration = new ParsedMigrationName(migration, options);
                var strategy        = scriptStrategies.FirstOrDefault(s => s.Handle(parsedMigration));
                if (strategy == null)
                {
                    throw new InvalidOperationException(string.Format("No strategy found for migration '{0}", migration));
                }

                await client.CreateDatabaseIfNotExistsAsync(parsedMigration.DataBase);

                await client.CreateDocumentCollectionIfNotExistsAsync(UriFactory.CreateDatabaseUri(parsedMigration.DataBase.Id), parsedMigration.Collection);


                await strategy.ApplyMigrationAsync(client, parsedMigration, migrationContent);
            }
        }
 public async Task ApplyMigrationAsync(DocumentClient client, ParsedMigrationName migration, string content)
 {
     await client.UpsertStoredProcedureAsync(
         UriFactory.CreateDocumentCollectionUri(migration.DataBase.Id, migration.Collection.Id),
         new StoredProcedure()
     {
         Body = content,
         Id   = migration.Name
     });
 }
Exemplo n.º 3
0
        public async Task ApplyMigrationAsync(DocumentClient client, ParsedMigrationName migration, string content)
        {
            var nameSplit = migration.Name.Split('-');

            Trigger trigger = new Trigger()
            {
                Body             = content,
                Id               = nameSplit[2],
                TriggerOperation = (TriggerOperation)Enum.Parse(typeof(TriggerOperation), nameSplit[1]),
                TriggerType      = (TriggerType)Enum.Parse(typeof(TriggerType), nameSplit[0])
            };
            await client.UpsertTriggerAsync(
                UriFactory.CreateDocumentCollectionUri(migration.DataBase.Id, migration.Collection.Id),
                trigger);
        }
 public bool Handle(ParsedMigrationName name)
 {
     return(name.Type == "StoredProcedure");
 }
Exemplo n.º 5
0
 public bool Handle(ParsedMigrationName migration)
 {
     return(migration.Type == "Trigger");
 }
Exemplo n.º 6
0
 public bool Handle(ParsedMigrationName name)
 {
     return(name.Type == "Function");
 }