/// <summary> /// Verifies whether a it is possible to connect to a database. /// </summary> public bool CanConnect(string databaseType, string connectionString, string server, string database, string login, string password, bool integratedAuth) { // we do not test SqlCE or LocalDB connections if (databaseType.InvariantContains("SqlCe") || databaseType.InvariantContains("SqlLocalDb")) { return(true); } string providerName; if (string.IsNullOrWhiteSpace(connectionString) == false) { providerName = ConfigConnectionString.ParseProviderName(connectionString); } else if (integratedAuth) { // has to be Sql Server providerName = Constants.DbProviderNames.SqlServer; connectionString = GetIntegratedSecurityDatabaseConnectionString(server, database); } else { connectionString = GetDatabaseConnectionString( server, database, login, password, databaseType, out providerName); } var factory = _dbProviderFactoryCreator.CreateFactory(providerName); return(DbConnectionExtensions.IsConnectionAvailable(connectionString, factory)); }
public void ShouldReturnConnectionStringSpecifiedInConfig() { const string expectedConnectionString = @"filesystem=MobileDB.FileSystem.PhysicalFileSystem;path=C:\Development\database"; var connectionString = ConfigConnectionString.FromAppConfig("Default"); connectionString.Should().Be(expectedConnectionString); }
public void Customize(IFixture fixture) { fixture.Customize <BackOfficeIdentityUser>( u => u.FromFactory <string, string, string>( (a, b, c) => BackOfficeIdentityUser.CreateNew(new GlobalSettings(), a, b, c))); fixture .Customize(new ConstructorCustomization(typeof(UsersController), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(InstallController), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(PreviewController), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(MemberController), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(BackOfficeController), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(BackOfficeUserManager), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(MemberManager), new GreedyConstructorQuery())) .Customize(new ConstructorCustomization(typeof(DatabaseSchemaCreatorFactory), new GreedyConstructorQuery())); // When requesting an IUserStore ensure we actually uses a IUserLockoutStore fixture.Customize <IUserStore <BackOfficeIdentityUser> >(cc => cc.FromFactory(Mock.Of <IUserLockoutStore <BackOfficeIdentityUser> >)); fixture.Customize <ConfigConnectionString>( u => u.FromFactory <string, string, string>( (a, b, c) => new ConfigConnectionString(a, b, c))); fixture.Customize <IUmbracoVersion>( u => u.FromFactory( () => new UmbracoVersion())); fixture.Customize <HostingSettings>(x => x.With(settings => settings.ApplicationVirtualPath, string.Empty)); fixture.Customize <BackOfficeAreaRoutes>(u => u.FromFactory( () => new BackOfficeAreaRoutes( Options.Create(new GlobalSettings()), Mock.Of <IHostingEnvironment>(x => x.ToAbsolute(It.IsAny <string>()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty), Mock.Of <IRuntimeState>(x => x.Level == RuntimeLevel.Run), new UmbracoApiControllerTypeCollection(Enumerable.Empty <Type>)))); fixture.Customize <PreviewRoutes>(u => u.FromFactory( () => new PreviewRoutes( Options.Create(new GlobalSettings()), Mock.Of <IHostingEnvironment>(x => x.ToAbsolute(It.IsAny <string>()) == "/umbraco" && x.ApplicationVirtualPath == string.Empty), Mock.Of <IRuntimeState>(x => x.Level == RuntimeLevel.Run)))); var configConnectionString = new ConfigConnectionString( "ss", "Data Source=(localdb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\Umbraco.mdf;Integrated Security=True"); fixture.Customize <ConfigConnectionString>(x => x.FromFactory(() => configConnectionString)); var httpContextAccessor = new HttpContextAccessor { HttpContext = new DefaultHttpContext() }; fixture.Customize <HttpContext>(x => x.FromFactory(() => httpContextAccessor.HttpContext)); fixture.Customize <IHttpContextAccessor>(x => x.FromFactory(() => httpContextAccessor)); fixture.Customize <WebRoutingSettings>(x => x.With(settings => settings.UmbracoApplicationUrl, "http://localhost:5000")); }
public void ShouldReturnConnectionStringInstanceFromSpecifiedConfiguration() { const string connectionString = @"filesystem=MobileDB.FileSystem.PhysicalFileSystem;path=C:\Development\database"; var configConnectionStringInstance = new ConfigConnectionString(connectionString); var filesystem = configConnectionStringInstance.GetPart("filesystem"); var path = configConnectionStringInstance.GetPart("path"); filesystem.Should().Be("MobileDB.FileSystem.PhysicalFileSystem"); path.Should().Be(@"C:\Development\database"); configConnectionStringInstance.As <ConnectionString>().Should().NotBeNull(); }
private void Configure(string connectionString, string providerName, bool installMissingDatabase) { // Update existing connection string var umbracoConnectionString = new ConfigConnectionString(Constants.System.UmbracoConnectionName, connectionString, providerName); _connectionStrings.CurrentValue.UmbracoConnectionString = umbracoConnectionString; _databaseFactory.Configure(umbracoConnectionString.ConnectionString, umbracoConnectionString.ProviderName); if (installMissingDatabase) { CreateDatabase(); } }
/// <summary> /// Initializes a new instance of the <see cref="UmbracoDatabaseFactory"/>. /// </summary> /// <remarks>Used by the other ctor and in tests.</remarks> internal UmbracoDatabaseFactory( ILogger <UmbracoDatabaseFactory> logger, ILoggerFactory loggerFactory, IOptions <GlobalSettings> globalSettings, IMapperCollection mappers, IDbProviderFactoryCreator dbProviderFactoryCreator, DatabaseSchemaCreatorFactory databaseSchemaCreatorFactory, NPocoMapperCollection npocoMappers, string connectionString) { _globalSettings = globalSettings; _mappers = mappers ?? throw new ArgumentNullException(nameof(mappers)); _dbProviderFactoryCreator = dbProviderFactoryCreator ?? throw new ArgumentNullException(nameof(dbProviderFactoryCreator)); _databaseSchemaCreatorFactory = databaseSchemaCreatorFactory ?? throw new ArgumentNullException(nameof(databaseSchemaCreatorFactory)); _npocoMappers = npocoMappers; _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _loggerFactory = loggerFactory; if (connectionString is null) { logger.LogDebug("Missing connection string, defer configuration."); return; // not configured } var configConnectionString = new ConfigConnectionString("Custom", connectionString); // could as well be <add name="umbracoDbDSN" connectionString="" providerName="" /> // so need to test the values too if (configConnectionString.IsConnectionStringConfigured() == false) { logger.LogDebug("Empty connection string or provider name, defer configuration."); return; // not configured } Configure(configConnectionString.ConnectionString, configConnectionString.ProviderName); }
public static bool IsConnectionStringConfigured(this ConfigConnectionString databaseSettings) => databaseSettings != null && !string.IsNullOrWhiteSpace(databaseSettings.ConnectionString) && !string.IsNullOrWhiteSpace(databaseSettings.ProviderName);