// It's internal because `Umbraco.Core.Persistence.LocalDb` is internal internal LocalDbTestDatabase(TestDatabaseSettings settings, ILoggerFactory loggerFactory, LocalDb localDb, string filesPath, IUmbracoDatabaseFactory dbFactory) { _loggerFactory = loggerFactory; _databaseFactory = dbFactory; _settings = settings; _localDb = localDb; s_filesPath = filesPath; Instance = this; // For GlobalSetupTeardown.cs var counter = 0; var schema = Enumerable.Range(0, _settings.SchemaDatabaseCount) .Select(x => TestDbMeta.CreateWithoutConnectionString($"{DatabaseName}-{++counter}", false)); var empty = Enumerable.Range(0, _settings.EmptyDatabasesCount) .Select(x => TestDbMeta.CreateWithoutConnectionString($"{DatabaseName}-{++counter}", true)); _testDatabases = schema.Concat(empty).ToList(); s_localDbInstance = _localDb.GetInstance(InstanceName); if (s_localDbInstance != null) { return; } if (_localDb.CreateInstance(InstanceName) == false) { throw new Exception("Failed to create a LocalDb instance."); } s_localDbInstance = _localDb.GetInstance(InstanceName); }
/// <summary> /// We cannot continue if the db cannot be connected to /// </summary> private void EnsureDatabaseConnection() { if (ApplicationContext.IsConfigured == false) { return; } if (ApplicationContext.DatabaseContext.IsDatabaseConfigured == false) { return; } // deal with localdb var databaseContext = ApplicationContext.DatabaseContext; var localdbex = new Regex(@"\(localdb\)\\([a-zA-Z0-9-_]+)(;|$)"); var m = localdbex.Match(databaseContext.ConnectionString); if (m.Success) { var instanceName = m.Groups[1].Value; ProfilingLogger.Logger.Info <CoreBootManager>(string.Format("LocalDb instance \"{0}\"", instanceName)); var localDb = new LocalDb(); if (localDb.IsAvailable == false) { throw new UmbracoStartupFailedException("Umbraco cannot start. LocalDb is not available."); } if (localDb.InstanceExists(m.Groups[1].Value) == false) { if (localDb.CreateInstance(instanceName) == false) { throw new UmbracoStartupFailedException(string.Format("Umbraco cannot start. LocalDb cannot create instance \"{0}\".", instanceName)); } if (localDb.StartInstance(instanceName) == false) { throw new UmbracoStartupFailedException(string.Format("Umbraco cannot start. LocalDb cannot start instance \"{0}\".", instanceName)); } } } //try now if (ApplicationContext.DatabaseContext.CanConnect) { return; } var currentTry = 0; while (currentTry < 5) { //first wait, then retry Thread.Sleep(1000); if (ApplicationContext.DatabaseContext.CanConnect) { break; } currentTry++; } if (currentTry == 5) { throw new UmbracoStartupFailedException("Umbraco cannot start. A connection string is configured but Umbraco cannot connect to the database."); } }