public static async Task EnsureSeedData(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var env = scope.ServiceProvider.GetRequiredService <IWebHostEnvironment>();

                var configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>();
                var ssoContext    = scope.ServiceProvider.GetRequiredService <SsoContext>();
                var userManager   = scope.ServiceProvider.GetRequiredService <UserManager <UserIdentity> >();
                var roleManager   = scope.ServiceProvider.GetRequiredService <RoleManager <RoleIdentity> >();

                await DbHealthChecker.TestConnection(ssoContext);

                if (!env.IsDevelopment())
                {
                    return;
                }

                ssoContext.Database.EnsureCreated();

                await EnsureSeedIdentityServerData(ssoContext, configuration);
                await EnsureSeedIdentityData(userManager, roleManager, configuration);
                await EnsureSeedGlobalConfigurationData(ssoContext, configuration, env);
            }
        }
Example #2
0
        public static async Task EnsureSeedData(IServiceProvider serviceProvider)
        {
            using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                var env = scope.ServiceProvider.GetRequiredService <IWebHostEnvironment>();

                var configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>();
                var ssoContext    = scope.ServiceProvider.GetRequiredService <SsoContext>();
                var userManager   = scope.ServiceProvider.GetRequiredService <UserManager <UserIdentity> >();
                var roleManager   = scope.ServiceProvider.GetRequiredService <RoleManager <RoleIdentity> >();
                var configOptions = scope.ServiceProvider.GetRequiredService <IConfigurationOptions>();

                await DbHealthChecker.TestConnection(ssoContext);

                var ssoVersion = ssoContext.GlobalConfigurationSettings.FirstOrDefault(w => w.Key == "SSO:Version");
                SsoVersion.Current = new Version(ssoVersion?.Value ?? "3.1.0");

                //if (!env.IsDevelopment())
                //    return;

                ssoContext.Database.EnsureCreated();
                ssoContext.Database.Migrate();

                await EnsureSeedIdentityServerData(ssoContext, configuration, configOptions);
                await EnsureSeedIdentityData(userManager, roleManager, configuration);
                await EnsureSeedGlobalConfigurationData(ssoContext, configuration, env);
            }
        }
Example #3
0
        public static async Task EnsureDatabaseIsReady(IServiceProvider serviceProvider)
        {
            using var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var env      = scope.ServiceProvider.GetRequiredService <IWebHostEnvironment>();
            var services = scope.ServiceProvider;
            var context  = services.GetRequiredService <FreteContext>();

            Log.Information("Testing conection with database");
            await DbHealthChecker.TestConnection(context);

            Log.Information("Connection successfull");


            Log.Information("Aguarde, carregando fretes");
            if (!context.Fretes.Any())
            {
                await context.Fretes.AddRangeAsync(new[]
                {
                    new Frete("Standard", "Entrega em até 15 dias uteis", 9, 1),
                    new Frete("Fast", "Entrega em até 7 dias uteis", 15, 2m),
                    new Frete("Ultra", "Entrega em até 2 dias uteis", 20, 1.5m)
                });

                await context.SaveChangesAsync();
            }



            Log.Information("Dados carregados");
        }
        private static async Task ConfigureEventStoreContext(EventStoreContext storeDb)
        {
            var storeDbExist = await DbHealthChecker.CheckTableExists <StoredEvent>(storeDb);

            if (!storeDbExist)
            {
                await storeDb.Database.MigrateAsync();
            }
        }
        public static async Task CheckDatabases(IServiceProvider serviceProvider, JpDatabaseOptions options)
        {
            using var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var id4Context = scope.ServiceProvider.GetRequiredService <JpProjectAdminUiContext>();

            await DbHealthChecker.TestConnection(id4Context);

            await ValidateIs4Context(options, id4Context);
        }
        private static async Task ValidateIs4Context(JpDatabaseOptions options, JPProjectAdminUIContext id4AdminUiContext)
        {
            var configurationDatabaseExist = await DbHealthChecker.CheckTableExists <Client>(id4AdminUiContext);

            var operationalDatabaseExist = await DbHealthChecker.CheckTableExists <PersistedGrant>(id4AdminUiContext);

            var isDatabaseExist = configurationDatabaseExist && operationalDatabaseExist;

            if (!isDatabaseExist && options.MustThrowExceptionIfDatabaseDontExist)
            {
                throw new DatabaseNotFoundException("IdentityServer4 Database doesn't exist. Ensure it was created before.'");
            }
        }
Example #7
0
        public static async Task CheckDatabases(IServiceProvider serviceProvider)
        {
            using var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var storeDb = scope.ServiceProvider.GetRequiredService <EventStoreContext>();

            if (storeDb.Database.IsInMemory())
            {
                return;
            }

            await DbHealthChecker.TestConnection(storeDb);

            await ConfigureEventStoreContext(storeDb);
        }
Example #8
0
        /// <summary>
        /// Generate migrations before running this method, you can use command bellow:
        /// Nuget package manager: Add-Migration DbInit -context ApplicationIdentityContext -output Data/Migrations333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333
        /// Dotnet CLI: dotnet ef migrations add DbInit -c ApplicationIdentityContext -o Data/Migrations
        /// </summary>
        public static async Task EnsureSeedData(IServiceScope serviceScope)
        {
            var services   = serviceScope.ServiceProvider;
            var ssoContext = services.GetRequiredService <JpProjectAdminUiContext>();

            Log.Information("Check if database contains Client (ConfigurationDbStore) table");
            await DbHealthChecker.WaitForTable <Client>(ssoContext);

            Log.Information("Check if database contains PersistedGrant (PersistedGrantDbStore) table");
            await DbHealthChecker.WaitForTable <PersistedGrant>(ssoContext);

            Log.Information("Checks done");

            var eventStoreDb = serviceScope.ServiceProvider.GetRequiredService <EventStoreContext>();
            await eventStoreDb.Database.EnsureCreatedAsync();
        }
        public static async Task EnsureSeedData(IServiceProvider serviceProvider)
        {
            await DbMigrationHelpers.CheckDatabases(serviceProvider, new JpDatabaseOptions()
            {
                MustThrowExceptionIfDatabaseDontExist = true
            });

            using var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var eventStoreDb = scope.ServiceProvider.GetRequiredService <EventStoreContext>();
            var storeDbExist = await DbHealthChecker.CheckTableExists <StoredEvent>(eventStoreDb);

            if (!storeDbExist)
            {
                await eventStoreDb.Database.MigrateAsync();
            }
        }
        public static async Task EnsureDatabaseIsReady(IServiceScope serviceScope)
        {
            var services   = serviceScope.ServiceProvider;
            var ssoContext = services.GetRequiredService <SsoContext>();

            Log.Information("Testing conection with database");
            await DbHealthChecker.TestConnection(ssoContext);

            Log.Information("Connection successfull");

            Log.Information("Check if database contains Client (ConfigurationDbStore) table");
            await DbHealthChecker.WaitForTable <Client>(ssoContext);

            Log.Information("Check if database contains PersistedGrant (PersistedGrantDbStore) table");
            await DbHealthChecker.WaitForTable <PersistedGrant>(ssoContext);

            Log.Information("Checks done");
        }
        public static async Task CheckDatabases(IServiceProvider serviceProvider, JpDatabaseOptions options)
        {
            using var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var id4Context = scope.ServiceProvider.GetRequiredService <JPProjectAdminUIContext>();
            var storeDb    = scope.ServiceProvider.GetRequiredService <EventStoreContext>();

            if (id4Context.Database.IsInMemory() || storeDb.Database.IsInMemory())
            {
                return;
            }

            await DbHealthChecker.TestConnection(id4Context);

            await ValidateIs4Context(options, id4Context);

            await ConfigureEventStoreContext(storeDb);
        }
        public static async Task EnsureSeedData(IServiceProvider serviceProvider)
        {
            using var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope();

            var configuration = scope.ServiceProvider.GetRequiredService <IConfiguration>();
            var ssoContext    = scope.ServiceProvider.GetRequiredService <ApplicationSsoContext>();
            var userManager   = scope.ServiceProvider.GetRequiredService <UserManager <UserIdentity> >();
            var roleManager   = scope.ServiceProvider.GetRequiredService <RoleManager <IdentityRole> >();

            await DbHealthChecker.TestConnection(ssoContext);

            await ssoContext.Database.MigrateAsync();

            scope.ServiceProvider.GetRequiredService <EventStoreContext>().Database.Migrate();

            await EnsureSeedIdentityServerData(ssoContext, configuration);
            await EnsureSeedIdentityData(userManager, roleManager, configuration);
        }
Example #13
0
        private static async Task WaitForDb(DbContext context)
        {
            var maxAttemps = 12;
            var delay      = 5000;

            var healthChecker = new DbHealthChecker();

            for (int i = 0; i < maxAttemps; i++)
            {
                if (healthChecker.TestConnection(context))
                {
                    return;
                }
                await Task.Delay(delay);
            }

            // after a few attemps we give up
            throw new Exception("Error wating database");
        }