protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            LinqToDBForEFTools.Initialize();
            var connStr = "Host=localhost;Port=5432;Database=stub_db;Username=stub_user;Password=stub_password";

            base.OnConfiguring(optionsBuilder.UseNpgsql(connStr));
        }
Esempio n. 2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ClemBotContext context)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ClemBot.Api 1.0.0"));
            }
            app.UseSerilogRequestLogging();

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints => endpoints.MapControllers());

            // Apply any new migrations
            context.Database.Migrate();

            // Load linq2db for bulk copies
            LinqToDBForEFTools.Initialize();


            // Reload enum types after a migration
            using var conn = (NpgsqlConnection)context.Database.GetDbConnection();
            conn.Open();
            conn.ReloadTypes();
        }
Esempio n. 3
0
        private async Task <string> ProcessAsync(string s3BucketName, string s3ObjectKey)
        {
            var apiServerLogKeyword       = Environment.GetEnvironmentVariable("apiServerLogKeyword");
            var dedicatedServerLogKeyword = Environment.GetEnvironmentVariable("dedicatedServerLogKeyword");

            if (!s3ObjectKey.ToLower().Contains(apiServerLogKeyword) &&
                !s3ObjectKey.ToLower().Contains(dedicatedServerLogKeyword))
            {
                return(string.Empty);
            }

            var readCount    = 0;
            var processCount = 0;
            var insertCount  = 0;

            var sw = Stopwatch.StartNew();

            LinqToDBForEFTools.Initialize();

            var optBuilder = new LinqToDbConnectionOptionsBuilder();

            optBuilder.UseMySql(GetConnectionString());

            using (var dataConnection = new DataConnection(optBuilder.Build()))
            {
                // todo: 1つのeventに2つ以上のファイルが含まれることがないか確認する
                var request = new GetObjectRequest
                {
                    BucketName = s3BucketName,
                    Key        = s3ObjectKey,
                };

                using (var getResponse = await S3Client.GetObjectAsync(request))
                    using (var streamReader = new StreamReader(getResponse.ResponseStream))
                        using (var tran = dataConnection.BeginTransaction())
                        {
                            try
                            {
                                if (s3ObjectKey.ToLower().Contains(apiServerLogKeyword))
                                {
                                    (readCount, processCount, insertCount) = await new ApiServerLogProcessor(dataConnection).ProcessAsync(streamReader);
                                }
                                else if (s3ObjectKey.ToLower().Contains(dedicatedServerLogKeyword))
                                {
                                    (readCount, processCount, insertCount) = await new DedicatedServerLogProcessor(dataConnection).ProcessAsync(streamReader);
                                }

                                tran.Commit();
                            }
                            catch
                            {
                                tran.Rollback();
                                throw;
                            }
                        }

                return($"{sw.Elapsed.TotalMilliseconds:N0} msec. read: {readCount:N0} lines. process: {processCount:N0} lines. insert: {insertCount:N0} rows.");
            }
        }
Esempio n. 4
0
 public BaseNativeTests(NativeDbContext dbContext)
 {
     DbContext = dbContext;
     LinqToDBForEFTools.Initialize();
     InitializeDbContext();
     ClearTables();
     CreateUser();
 }
Esempio n. 5
0
        public void EnsureCreatedAndConnectedWithLinq2Db(ITestOutputHelper output)
        {
            Database.OpenConnection();
            Database.EnsureCreated();

            // Linq2Db
            LinqToDBForEFTools.Initialize();
        }
        public void TestJsonConvert()
        {
            LinqToDBForEFTools.Initialize();

            // // converting from string, because usually JSON is stored as string, but it depends on DataProvider
            // Mapping.MappingSchema.Default.SetConverter<string, LocalizedString>(v => JsonConvert.DeserializeObject<LocalizedString>(v));
            //
            // // here we told linq2db how to pass converted value as DataParameter.
            // Mapping.MappingSchema.Default.SetConverter<LocalizedString, DataParameter>(v => new DataParameter("", JsonConvert.SerializeObject(v), LinqToDB.DataType.NVarChar));

            using (var ctx = new JsonConvertContext(_options))
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();

                ctx.EventScheduleItems.Delete();

                ctx.EventScheduleItems.Add(new EventScheduleItem()
                {
                    NameLocalized = new LocalizedString()
                    {
                        English = "English", German = "German", Slovak = "Slovak"
                    },
                    GuidColumn = Guid.NewGuid()
                });
                ctx.SaveChanges();

                var queryable = ctx.EventScheduleItems
                                .Where(p => p.Id < 10).ToLinqToDB();

                var path = "some";

                var items = queryable
                            .Select(p => new
                {
                    p.Id,
                    p.NameLocalized,
                    p.CrashEnum,
                    p.GuidColumn,
                    JsonValue = JsonValue(p.JsonColumn, path)
                });

                var item = items.FirstOrDefault();

                Assert.That(item.NameLocalized.English, Is.EqualTo("English"));
                Assert.That(item.NameLocalized.German, Is.EqualTo("German"));
                Assert.That(item.NameLocalized.Slovak, Is.EqualTo("Slovak"));

                //TODO: make it work
                // var concrete = queryable.Select(p => new
                // {
                //  p.Id,
                //  English = p.NameLocalized.English
                // }).FirstOrDefault();
                //
                // Assert.That(concrete.English, Is.EqualTo("English"));
            }
        }
        public static void AddEFRepositories(this IServiceCollection services, bool selfHosted, string connectionString,
                                             SupportedDatabaseProviders provider)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new Exception($"Database provider type {provider} was selected but no connection string was found.");
            }
            LinqToDBForEFTools.Initialize();
            services.AddAutoMapper(typeof(UserRepository));
            services.AddDbContext <DatabaseContext>(options =>
            {
                if (provider == SupportedDatabaseProviders.Postgres)
                {
                    options.UseNpgsql(connectionString);
                    // Handle NpgSql Legacy Support for `timestamp without timezone` issue
                    AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
                }
                else if (provider == SupportedDatabaseProviders.MySql)
                {
                    options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
                }
            });
            services.AddSingleton <ICipherRepository, CipherRepository>();
            services.AddSingleton <ICollectionCipherRepository, CollectionCipherRepository>();
            services.AddSingleton <ICollectionRepository, CollectionRepository>();
            services.AddSingleton <IDeviceRepository, DeviceRepository>();
            services.AddSingleton <IEmergencyAccessRepository, EmergencyAccessRepository>();
            services.AddSingleton <IFolderRepository, FolderRepository>();
            services.AddSingleton <IGrantRepository, GrantRepository>();
            services.AddSingleton <IGroupRepository, GroupRepository>();
            services.AddSingleton <IInstallationRepository, InstallationRepository>();
            services.AddSingleton <IMaintenanceRepository, MaintenanceRepository>();
            services.AddSingleton <IOrganizationRepository, OrganizationRepository>();
            services.AddSingleton <IOrganizationApiKeyRepository, OrganizationApiKeyRepository>();
            services.AddSingleton <IOrganizationConnectionRepository, OrganizationConnectionRepository>();
            services.AddSingleton <IOrganizationSponsorshipRepository, OrganizationSponsorshipRepository>();
            services.AddSingleton <IOrganizationUserRepository, OrganizationUserRepository>();
            services.AddSingleton <IPolicyRepository, PolicyRepository>();
            services.AddSingleton <ISendRepository, SendRepository>();
            services.AddSingleton <ISsoConfigRepository, SsoConfigRepository>();
            services.AddSingleton <ISsoUserRepository, SsoUserRepository>();
            services.AddSingleton <ITaxRateRepository, TaxRateRepository>();
            services.AddSingleton <ITransactionRepository, TransactionRepository>();
            services.AddSingleton <IUserRepository, UserRepository>();
            services.AddSingleton <IProviderRepository, ProviderRepository>();
            services.AddSingleton <IProviderUserRepository, ProviderUserRepository>();
            services.AddSingleton <IProviderOrganizationRepository, ProviderOrganizationRepository>();

            if (selfHosted)
            {
                services.AddSingleton <IEventRepository, EventRepository>();
            }
        }
Esempio n. 8
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            string connection = configuration.GetConnectionString("DefaultConnection");

            services
            .AddEntityFrameworkNpgsql()
            .AddDbContext <IssueContext>(options =>
                                         options.UseNpgsql(connection)
                                         );

            LinqToDBForEFTools.Initialize();
        }
Esempio n. 9
0
        public EntityUpdateTest()
        {
            //       System.Diagnostics.DiagnosticListener.AllListeners.Subscribe(new CommandListener());
            LinqToDBForEFTools.Initialize();

            //BatchUpdateManager.BatchUpdateBuilder = builder =>
            //{
            //    builder.Executing = (command) =>
            //    {
            //        Trace.TraceInformation("\r\n执行时间:{0} 毫秒 \r\n -->CommandExecuted.Command:\r\n{1}\r\nParamter:{2}", "", command.CommandText,
            //           string.Join(",", command.Parameters.Cast<IDbDataParameter>().Select(t => string.Format("{0}:{1}:{2};", t.ParameterName, t.DbType, t.Value)))
            //           );
            //    };

            //};
        }
Esempio n. 10
0
        public NadekoBot(int shardId, int parentProcessId)
        {
            if (shardId < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(shardId));
            }

            LogSetup.SetupLogger(shardId);
            TerribleElevatedPermissionCheck();

            Credentials = new BotCredentials();
            Cache       = new RedisCache(Credentials, shardId);
            LinqToDBForEFTools.Initialize();
            _db = new DbService(Credentials);

            if (shardId == 0)
            {
                _db.Setup();
            }

            Client = new DiscordSocketClient(new DiscordSocketConfig
            {
                MessageCacheSize    = 50,
                LogLevel            = LogSeverity.Warning,
                ConnectionTimeout   = int.MaxValue,
                TotalShards         = Credentials.TotalShards,
                ShardId             = shardId,
                AlwaysDownloadUsers = false,
                ExclusiveBulkDelete = true,
            });

            CommandService = new CommandService(new CommandServiceConfig()
            {
                CaseSensitiveCommands = false,
                DefaultRunMode        = RunMode.Sync,
            });

            SetupShard(parentProcessId);

#if GLOBAL_NADEKO || DEBUG
            Client.Log += Client_Log;
#endif
        }
Esempio n. 11
0
        public override void PreInitialize()
        {
            //System.Diagnostics.DiagnosticListener.AllListeners.Subscribe(new CommandListener());
            LinqToDBForEFTools.Initialize();
            //BatchUpdateManager.BatchUpdateBuilder = builder =>
            //{
            //    builder.Executing = (command) =>
            //    {
            //        Trace.TraceInformation("\r\n执行时间:{0} 毫秒 \r\n -->CommandExecuted.Command:\r\n{1}\r\nParamter:{2}", "", command.CommandText,
            //           string.Join(",", command.Parameters.Cast<IDbDataParameter>().Select(t => string.Format("{0}:{1}:{2};", t.ParameterName, t.DbType, t.Value)))
            //           );
            //    };

            //};

            // Database.SetInitializer<BaseBlocksDbContext>(null);
            // Configuration.ReplaceService<IEfTransactionStrategy, DefaultTransactionStrategy>(DependencyLifeStyle.Transient);

            //#if DEBUG //TODO
            //            DbInterception.Add(new EFIntercepterLogging());
            //#endif
        }
Esempio n. 12
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            LinqToDBForEFTools.Initialize();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <AppDbContext>();
                    DbInitializer.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }

            host.Run();
        }
Esempio n. 13
0
 public Linq2DbProvider(ILogger <Linq2DbProvider> logger)
 {
     LinqToDBForEFTools.Initialize();
     DataConnection.TurnTraceSwitchOn();
     DataConnection.WriteTraceLine = (msg, category, level) => logger.Log(GetLogLevel(level), msg);
 }
Esempio n. 14
0
 static NpgSqlTests()
 {
     LinqToDBForEFTools.Initialize();
     DataConnection.TurnTraceSwitchOn();
 }
        public static void AddSqlServerRepositories(this IServiceCollection services, GlobalSettings globalSettings)
        {
            var selectedDatabaseProvider = globalSettings.DatabaseProvider;
            var provider         = SupportedDatabaseProviders.SqlServer;
            var connectionString = string.Empty;

            if (!string.IsNullOrWhiteSpace(selectedDatabaseProvider))
            {
                switch (selectedDatabaseProvider.ToLowerInvariant())
                {
                case "postgres":
                case "postgresql":
                    provider         = SupportedDatabaseProviders.Postgres;
                    connectionString = globalSettings.PostgreSql.ConnectionString;
                    break;

                case "mysql":
                case "mariadb":
                    provider         = SupportedDatabaseProviders.MySql;
                    connectionString = globalSettings.MySql.ConnectionString;
                    break;

                default:
                    break;
                }
            }

            var useEf = (provider != SupportedDatabaseProviders.SqlServer);

            if (useEf)
            {
                if (string.IsNullOrWhiteSpace(connectionString))
                {
                    throw new Exception($"Database provider type {provider} was selected but no connection string was found.");
                }
                LinqToDBForEFTools.Initialize();
                services.AddAutoMapper(typeof(EntityFrameworkRepos.UserRepository));
                services.AddDbContext <EntityFrameworkRepos.DatabaseContext>(options =>
                {
                    if (provider == SupportedDatabaseProviders.Postgres)
                    {
                        options.UseNpgsql(connectionString);
                    }
                    else if (provider == SupportedDatabaseProviders.MySql)
                    {
                        options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
                    }
                });
                services.AddSingleton <ICipherRepository, EntityFrameworkRepos.CipherRepository>();
                services.AddSingleton <ICollectionCipherRepository, EntityFrameworkRepos.CollectionCipherRepository>();
                services.AddSingleton <ICollectionRepository, EntityFrameworkRepos.CollectionRepository>();
                services.AddSingleton <IDeviceRepository, EntityFrameworkRepos.DeviceRepository>();
                services.AddSingleton <IEmergencyAccessRepository, EntityFrameworkRepos.EmergencyAccessRepository>();
                services.AddSingleton <IFolderRepository, EntityFrameworkRepos.FolderRepository>();
                services.AddSingleton <IGrantRepository, EntityFrameworkRepos.GrantRepository>();
                services.AddSingleton <IGroupRepository, EntityFrameworkRepos.GroupRepository>();
                services.AddSingleton <IInstallationRepository, EntityFrameworkRepos.InstallationRepository>();
                services.AddSingleton <IMaintenanceRepository, EntityFrameworkRepos.MaintenanceRepository>();
                services.AddSingleton <IOrganizationRepository, EntityFrameworkRepos.OrganizationRepository>();
                services.AddSingleton <IOrganizationUserRepository, EntityFrameworkRepos.OrganizationUserRepository>();
                services.AddSingleton <IPolicyRepository, EntityFrameworkRepos.PolicyRepository>();
                services.AddSingleton <ISendRepository, EntityFrameworkRepos.SendRepository>();
                services.AddSingleton <ISsoConfigRepository, EntityFrameworkRepos.SsoConfigRepository>();
                services.AddSingleton <ISsoUserRepository, EntityFrameworkRepos.SsoUserRepository>();
                services.AddSingleton <ITaxRateRepository, EntityFrameworkRepos.TaxRateRepository>();
                services.AddSingleton <ITransactionRepository, EntityFrameworkRepos.TransactionRepository>();
                services.AddSingleton <IU2fRepository, EntityFrameworkRepos.U2fRepository>();
                services.AddSingleton <IUserRepository, EntityFrameworkRepos.UserRepository>();
                services.AddSingleton <IProviderRepository, EntityFrameworkRepos.ProviderRepository>();
                services.AddSingleton <IProviderUserRepository, EntityFrameworkRepos.ProviderUserRepository>();
                services.AddSingleton <IProviderOrganizationRepository, EntityFrameworkRepos.ProviderOrganizationRepository>();
            }
            else
            {
                services.AddSingleton <ICipherRepository, SqlServerRepos.CipherRepository>();
                services.AddSingleton <ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
                services.AddSingleton <ICollectionRepository, SqlServerRepos.CollectionRepository>();
                services.AddSingleton <IDeviceRepository, SqlServerRepos.DeviceRepository>();
                services.AddSingleton <IEmergencyAccessRepository, SqlServerRepos.EmergencyAccessRepository>();
                services.AddSingleton <IFolderRepository, SqlServerRepos.FolderRepository>();
                services.AddSingleton <IGrantRepository, SqlServerRepos.GrantRepository>();
                services.AddSingleton <IGroupRepository, SqlServerRepos.GroupRepository>();
                services.AddSingleton <IInstallationRepository, SqlServerRepos.InstallationRepository>();
                services.AddSingleton <IMaintenanceRepository, SqlServerRepos.MaintenanceRepository>();
                services.AddSingleton <IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
                services.AddSingleton <IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
                services.AddSingleton <IPolicyRepository, SqlServerRepos.PolicyRepository>();
                services.AddSingleton <ISendRepository, SqlServerRepos.SendRepository>();
                services.AddSingleton <ISsoConfigRepository, SqlServerRepos.SsoConfigRepository>();
                services.AddSingleton <ISsoUserRepository, SqlServerRepos.SsoUserRepository>();
                services.AddSingleton <ITaxRateRepository, SqlServerRepos.TaxRateRepository>();
                services.AddSingleton <IEmergencyAccessRepository, SqlServerRepos.EmergencyAccessRepository>();
                services.AddSingleton <IProviderRepository, SqlServerRepos.ProviderRepository>();
                services.AddSingleton <IProviderUserRepository, SqlServerRepos.ProviderUserRepository>();
                services.AddSingleton <IProviderOrganizationRepository, SqlServerRepos.ProviderOrganizationRepository>();
                services.AddSingleton <ITransactionRepository, SqlServerRepos.TransactionRepository>();
                services.AddSingleton <IU2fRepository, SqlServerRepos.U2fRepository>();
                services.AddSingleton <IUserRepository, SqlServerRepos.UserRepository>();
            }

            if (globalSettings.SelfHosted)
            {
                if (useEf)
                {
                    services.AddSingleton <IEventRepository, EntityFrameworkRepos.EventRepository>();
                }
                else
                {
                    services.AddSingleton <IEventRepository, SqlServerRepos.EventRepository>();
                }
                services.AddSingleton <IInstallationDeviceRepository, NoopRepos.InstallationDeviceRepository>();
                services.AddSingleton <IMetaDataRepository, NoopRepos.MetaDataRepository>();
            }
            else
            {
                services.AddSingleton <IEventRepository, TableStorageRepos.EventRepository>();
                services.AddSingleton <IInstallationDeviceRepository, TableStorageRepos.InstallationDeviceRepository>();
                services.AddSingleton <IMetaDataRepository, TableStorageRepos.MetaDataRepository>();
            }
        }