private async Task MaybeCreateDatabase() { var masterConnection = new Connection(new ConnectionFactory(ConnectionStringBuilder.For("master").ConnectionString, Output)); try { var dbExists = Honestly.DontKnow; if (!Parameters.Optional(new InputBool("skip-create", "will not create database if set"), false).Get()) { dbExists = (await masterConnection.ExecuteScalarAsync <int>("SELECT 1 FROM sys.databases WHERE name = @dbName", new { dbName = DatabaseName })) == 1; if (!dbExists) { Logger.Log($"Database '{DatabaseName}' not found. It will be created.", "important"); Logger.Log($"Creating database '{DatabaseName}'.", "important"); await masterConnection.ExecuteAsync($@"IF(DB_ID(N'{DatabaseName}') IS NULL) BEGIN CREATE DATABASE [{DatabaseName}] END"); dbExists = true; } } if (!dbExists.IsKnown && Parameters.Optional(new InputBool("ensure-db-exists", "halts execution if db does not exist, requires access to master db"), false).Get()) { dbExists = (await masterConnection.ExecuteScalarAsync <int>("SELECT 1 FROM sys.databases WHERE name = @dbName", new { dbName = DatabaseName })) == 1; if (!dbExists) { throw new Exception($"Database {DatabaseName} does not exist"); } } } finally { masterConnection.Dispose(); } }