public async Task CreateAndInitializeDatabase(string databaseName, bool forceIncrementalSchemaUpgrade, SchemaInitializer schemaInitializer = null, CancellationToken cancellationToken = default) { var testConnectionString = new SqlConnectionStringBuilder(_initialConnectionString) { InitialCatalog = databaseName }.ToString(); schemaInitializer = schemaInitializer ?? CreateSchemaInitializer(testConnectionString); // Create the database. using (var connection = await _sqlConnectionFactory.GetSqlConnectionAsync(_masterDatabaseName, cancellationToken)) { await connection.OpenAsync(cancellationToken); using (SqlCommand command = connection.CreateCommand()) { command.CommandTimeout = 600; command.CommandText = $"CREATE DATABASE {databaseName}"; await command.ExecuteNonQueryAsync(cancellationToken); } } // Verify that we can connect to the new database. This sometimes does not work right away with Azure SQL. await Policy .Handle <SqlException>() .WaitAndRetryAsync( retryCount: 7, sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))) .ExecuteAsync(async() => { using (var connection = await _sqlConnectionFactory.GetSqlConnectionAsync(databaseName, cancellationToken)) { await connection.OpenAsync(cancellationToken); using (SqlCommand sqlCommand = connection.CreateCommand()) { sqlCommand.CommandText = "SELECT 1"; await sqlCommand.ExecuteScalarAsync(cancellationToken); } } }); await schemaInitializer.InitializeAsync(forceIncrementalSchemaUpgrade, cancellationToken); await _searchParameterDefinitionManager.StartAsync(CancellationToken.None); _sqlServerFhirModel.Initialize(SchemaVersionConstants.Max, true); }
public async Task InitializeAsync(bool forceIncrementalSchemaUpgrade) { // Create the database using (var sqlConnection = new SqlConnection(_masterConnectionString)) { await sqlConnection.OpenAsync(); using (SqlCommand command = sqlConnection.CreateCommand()) { command.CommandTimeout = 600; command.CommandText = $"CREATE DATABASE {DatabaseName}"; await command.ExecuteNonQueryAsync(); } } // verify that we can connect to the new database. This sometimes does not work right away with Azure SQL. await Policy .Handle <SqlException>() .WaitAndRetryAsync( retryCount: 7, sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))) .ExecuteAsync(async() => { using (var sqlConnection = new SqlConnection(TestConnectionString)) { await sqlConnection.OpenAsync(); using (SqlCommand sqlCommand = sqlConnection.CreateCommand()) { sqlCommand.CommandText = "SELECT 1"; await sqlCommand.ExecuteScalarAsync(); } } }); await _schemaInitializer.InitializeAsync(forceIncrementalSchemaUpgrade); }