From 1941ba0a415b87dc957e5cd9abd0768bf4cbedca Mon Sep 17 00:00:00 2001 From: Vince Grassia <593223+vgrassia@users.noreply.github.com> Date: Fri, 9 Jul 2021 18:51:28 -0400 Subject: [PATCH] Fix minor bug when testing if database already exists (#1452) * Fix minor bug when testing if database already exists * Add newline at end of file * Remove unused import --- .gitignore | 1 + util/Migrator/DbMigrator.cs | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 365c4bff06..c644054a45 100644 --- a/.gitignore +++ b/.gitignore @@ -215,3 +215,4 @@ bitwarden_license/src/Sso/wwwroot/lib bitwarden_license/src/Sso/wwwroot/css .github/test/build.secrets **/CoverageOutput/ +.idea/* diff --git a/util/Migrator/DbMigrator.cs b/util/Migrator/DbMigrator.cs index ddfa19a004..adfef0e6a6 100644 --- a/util/Migrator/DbMigrator.cs +++ b/util/Migrator/DbMigrator.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using System.Data.SqlClient; using System.Reflection; using System.Threading; @@ -39,18 +40,17 @@ namespace Bit.Migrator { databaseName = "vault"; } - var commandBuilder = new SqlCommandBuilder(); + var databaseNameQuoted = new SqlCommandBuilder().QuoteIdentifier(databaseName); var command = new SqlCommand( - "IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = '@DatabaseName') = 0) " + + "IF ((SELECT COUNT(1) FROM sys.databases WHERE [name] = @DatabaseName) = 0) " + "CREATE DATABASE " + databaseNameQuoted + ";", connection); - command.Parameters.Add("@DatabaseName", System.Data.SqlDbType.VarChar); - command.Parameters["@DatabaseName"].Value = databaseName; + command.Parameters.Add("@DatabaseName", SqlDbType.VarChar).Value = databaseName; command.Connection.Open(); command.ExecuteNonQuery(); command.CommandText = "IF ((SELECT DATABASEPROPERTYEX([name], 'IsAutoClose') " + - "FROM sys.databases WHERE [name] = '@DatabaseName') = 1) " + + "FROM sys.databases WHERE [name] = @DatabaseName) = 1) " + "ALTER DATABASE " + databaseNameQuoted + " SET AUTO_CLOSE OFF;"; command.ExecuteNonQuery(); }