Esempio n. 1
0
        public static async Task <HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, ILogger log, ExecutionContext context)
        {
            log.LogInformation("MigrateDatabaseShard triggered");

            var services = new ServiceCollection()
                           .AddLogging()
                           .AddSingleton <IServiceFactory, LocalServiceFactory>();

            var config = new ConfigurationBuilder()
                         .SetBasePath(context.FunctionAppDirectory)
                         .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                         .AddJsonFile("connectionStrings.json", optional: true, reloadOnChange: true)
                         .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                         .AddJsonFile($"appsettings.Development.json", optional: true, reloadOnChange: true)
                         .AddJsonFile($"connectionStrings.Development.json", optional: true, reloadOnChange: true)
                         .AddEnvironmentVariables()
                         .Build();

            var jsonContent = await req.Content.ReadAsStringAsync();

            var shardInfo = JsonConvert.DeserializeObject <ShardInfo>(jsonContent);

            log.LogInformation($"Database Name: {shardInfo.ShardName}");

            var shardSettings         = config.GetSection("AppSettings:ShardSettings");
            var shardingConfiguration = new ShardingConfiguration
            {
                Server              = shardSettings.GetValue <string>("Server"),
                Database            = shardSettings.GetValue <string>("Map"),
                ShardMapName        = "ReflexMap",
                IntegrationSecurity = shardSettings.GetValue <bool>("IntegratedSecurity"),
                ConnectTimeout      = shardSettings.GetValue <int>("ConnectTimeout", 120),
                Pooling             = shardSettings.GetValue <bool>("Pooling", true),
                MaxPoolSize         = shardSettings.GetValue <int>("MaxPoolSize", 100),
                ApplicationName     = shardSettings.GetValue <string>("ApplicationName", "Export-MigrateDatabaseShard")
            };

            if (!shardingConfiguration.IntegrationSecurity)
            {
                shardingConfiguration.Username = shardSettings.GetValue <string>("Username");
                shardingConfiguration.Password = shardSettings.GetValue <string>("Password");
            }

            services.AddSingleton <ISharding>(x => new Sharding(shardingConfiguration));

            var serviceProvider = services.BuildServiceProvider();

            var serviceFactory = serviceProvider.GetService <IServiceFactory>();

            log.LogInformation("Initializing...");
            new TenantedDbConfiguration(serviceFactory, log).DbInitialization(shardInfo.ShardName);
            log.LogInformation("Initializing finished");

            return(req.CreateResponse(HttpStatusCode.OK));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            var services = new ServiceCollection()
                           .AddLogging()
                           .AddSingleton <IServiceFactory, LocalServiceFactory>();

            var builder = new ConfigurationBuilder()
                          .SetBasePath(Directory.GetCurrentDirectory())
                          .AddJsonFile("appsettings.json", optional: false)
                          .AddJsonFile($"appsettings.Development.json", optional: true)
                          .AddEnvironmentVariables();

            var config = builder.Build();

            var shardSettings         = config.GetSection("AppSettings:ShardSettings");
            var shardingConfiguration = new ShardingConfiguration
            {
                Server              = shardSettings.GetValue <string>("Server"),
                Database            = shardSettings.GetValue <string>("Map"),
                ShardMapName        = "ReflexMap",
                IntegrationSecurity = shardSettings.GetValue <bool>("IntegratedSecurity"),
                ConnectTimeout      = shardSettings.GetValue <int>("ConnectTimeout", 120),
                Pooling             = shardSettings.GetValue <bool>("Pooling", true),
                MaxPoolSize         = shardSettings.GetValue <int>("MaxPoolSize", 100),
                ApplicationName     = "Export-DatabaseMigrationTool"
            };

            if (!shardingConfiguration.IntegrationSecurity)
            {
                shardingConfiguration.Username = shardSettings.GetValue <string>("Username");
                shardingConfiguration.Password = shardSettings.GetValue <string>("Password");
            }

            services.AddSingleton <ISharding>(x => new Sharding(shardingConfiguration));

            var serviceProvider = services.BuildServiceProvider();

            serviceProvider.GetService <ILoggerFactory>().AddConsole(LogLevel.Debug);

            var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger("DatabaseMigrationTool");

            logger.LogInformation("Running");

            var serviceFactory = serviceProvider.GetService <IServiceFactory>();

            logger.LogInformation("Initializing...");
            new TenantedDbConfiguration(serviceFactory).DbInitialization();
            logger.LogInformation("Initializing finished");
        }