private async Task CreateDatabaseIfNotExists(string databaseName, CancellationToken cancellationToken) { using (var connection = await _sqlConnectionFactory.GetSqlConnectionAsync(cancellationToken: cancellationToken)) { await connection.TryOpenAsync(cancellationToken); bool doesDatabaseExist = await SchemaInitializer.DoesDatabaseExistAsync(connection, databaseName, cancellationToken); if (!doesDatabaseExist) { _logger.LogInformation("The database does not exists."); bool created = await SchemaInitializer.CreateDatabaseAsync(connection, databaseName, cancellationToken); if (created) { _logger.LogInformation("The database is created."); } else { throw new SchemaManagerException(Resources.InsufficientDatabasePermissionsMessage); } } } }
public virtual async Task InitializeAsync() { SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder(Config.ConnectionString); connectionBuilder.InitialCatalog = "master"; Connection = new SqlConnection(connectionBuilder.ToString()); await Connection.OpenAsync(); await SchemaInitializer.CreateDatabaseAsync(Connection, DatabaseName, CancellationToken.None); await Connection.ChangeDatabaseAsync(DatabaseName); Output.WriteLine($"Using database '{DatabaseName}'."); }
private async Task InitializeAsync(CancellationToken cancellationToken) { var configuredConnectionBuilder = new SqlConnectionStringBuilder(_sqlServerDataStoreConfiguration.ConnectionString); string databaseName = configuredConnectionBuilder.InitialCatalog; SchemaInitializer.ValidateDatabaseName(databaseName); SqlConnectionStringBuilder connectionBuilder = new SqlConnectionStringBuilder(_sqlServerDataStoreConfiguration.ConnectionString) { InitialCatalog = string.Empty }; using (var connection = new SqlConnection(connectionBuilder.ToString())) { bool doesDatabaseExist = await SchemaInitializer.DoesDatabaseExistAsync(connection, databaseName, cancellationToken); // database creation is allowed if (!doesDatabaseExist) { Console.WriteLine("The database does not exists."); bool created = await SchemaInitializer.CreateDatabaseAsync(connection, databaseName, cancellationToken); if (created) { Console.WriteLine("The database is created."); } else { throw new SchemaManagerException(Resources.InsufficientDatabasePermissionsMessage); } connection.Close(); } } bool canInitialize = false; // now switch to the target database using (var connection = await _sqlConnectionFactory.GetSqlConnectionAsync(cancellationToken: cancellationToken)) { canInitialize = await SchemaInitializer.CheckDatabasePermissionsAsync(connection, cancellationToken); } if (!canInitialize) { throw new SchemaManagerException(Resources.InsufficientTablesPermissionsMessage); } }
public async Task DatabaseExists_DoesDatabaseExistAsync_ReturnsTrue() { const string dbName = "willexist"; try { Assert.False(await SchemaInitializer.DoesDatabaseExistAsync(Connection, dbName, CancellationToken.None)); Assert.True(await SchemaInitializer.CreateDatabaseAsync(Connection, dbName, CancellationToken.None)); Assert.True(await SchemaInitializer.DoesDatabaseExistAsync(Connection, dbName, CancellationToken.None)); } finally { await DeleteDatabaseAsync(dbName); } }
public async Task InvalidDatabaseName_CreateDatabaseAsync_ThrowsException() { await Assert.ThrowsAsync <ArgumentException>( () => SchemaInitializer.CreateDatabaseAsync(Connection, "[something] DROP DATABASE Production --", CancellationToken.None)); }