Skip to content
Snippets Groups Projects
Select Git revision
  • 6d56dc30a37f599f97b7362a78a7db1afb68ce71
  • master default protected
  • gitkeep
  • dev protected
  • Issue/2464-invalidateMeta
  • Issue/2309-docs
  • Issue/2462-removeTraces
  • Hotfix/2459-EncodingPath
  • Hotfix/2452-linkedDeletion
  • Issue/1792-newMetadataStructure
  • Hotfix/2371-fixGitLabinRCV
  • Fix/xxxx-activateGitlab
  • Issue/2349-gitlabHttps
  • Issue/2287-guestRole
  • Issue/2102-gitLabResTypeRCV
  • Hotfix/2254-fixContentLenghtCalculation
  • Fix/xxxx-resourceVisibility
  • Issue/1951-quotaImplementation
  • Issue/2162-fixFolderResponse
  • Issue/2158-emailServicedesk
  • Hotfix/2141-fileUploadErrors
  • v3.3.4
  • v3.3.3
  • v3.3.2
  • v3.3.1
  • v3.3.0
  • v3.2.3
  • v3.2.2
  • v3.2.1
  • v3.2.0
  • v3.1.2
  • v3.1.1
  • v3.1.0
  • v3.0.6
  • v3.0.5
  • v3.0.4
  • v3.0.3
  • v3.0.2
  • v3.0.1
  • v3.0.0
  • v2.8.2
41 results

nunit3-junit.xslt

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    DatabaseMasterHelper.cs 3.47 KiB
    using Coscine.Configuration;
    using System.Data.SqlClient;
    
    namespace Coscine.Database.Helpers
    {
        public class DatabaseMasterHelper
        {
            private readonly IConfiguration _configuration;
    
            public ConnectionSettings ConnectionSettings { get; set; }
    
            public DatabaseMasterHelper()
            {
                _configuration = new ConsulConfiguration();
            }
    
            public DatabaseMasterHelper(IConfiguration configuration)
            {
                _configuration = configuration;
            }
    
            public void KillConnectionsToDatabase(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
    
                    command.CommandText = $"alter database {sqlCommandBuilder.QuoteIdentifier(dbDatabase)} set single_user with rollback immediate; alter database {sqlCommandBuilder.QuoteIdentifier(dbDatabase)} set MULTI_USER; ";
                    command.ExecuteNonQuery();
                }
            }
    
            public void CreateDatabase(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = $"CREATE DATABASE {sqlCommandBuilder.QuoteIdentifier(dbDatabase)};";
                    command.ExecuteNonQuery();
                }
            }
    
            public void DropDatabase(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    command.CommandText = $"DROP DATABASE {sqlCommandBuilder.QuoteIdentifier(dbDatabase)};";
                    command.ExecuteNonQuery();
                }
            }
    
            public bool DatabaseExists(string dbDatabase)
            {
                var sqlCommandBuilder = new SqlCommandBuilder();
                using (var connection = new SqlConnection(ConnectionSettings.GetConnectionString(false)))
                {
                    connection.Open();
                    var command = connection.CreateCommand();
                    var dbName = sqlCommandBuilder.QuoteIdentifier(dbDatabase);
                    dbName = dbName.StartsWith("[") ? dbName.Substring(1) : dbName;
                    dbName = dbName.EndsWith("]") ? dbName.Remove(dbName.Length - 1) : dbName;
                    command.CommandText = $"SELECT DB_ID('{dbName}') AS RESULT;"; ;
                    using (var reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            var result = reader["RESULT"].ToString();
                            return !string.IsNullOrWhiteSpace(result);
                        }
                    }
                    return false;
                }
            }
    
            public void EnsureDatabase(string dbDatabase)
            {
                if (!DatabaseExists(dbDatabase))
                {
                    CreateDatabase(dbDatabase);
                }
            }
    
            public string GetConsulDatabaseName()
            {
                return _configuration.GetString(ConfigurationConnectionSettings.DatabaseKey);
            }
        }
    }