public void ReInitConnectionString(InfrastructureSettings infrastructureSettings)
 {
     _settingsManager.FullSettings.DbFactorySettings.ConnectionString = infrastructureSettings.ConnectonString;
 }
        public async Task <InitializationStatesEnums> InitializeDataStorage(dynamic settings)
        {
            IDbBase db = null;

            try
            {
                InfrastructureSettings infrastructureSettings = settings;

                db = _dbFactory.GetDbBase(infrastructureSettings.ConnectonString);

                await db.ConnectAsync();

                var databaseInitializationState = await _timescaleInfrastructureHelpers.InitializationState(db);

                if (databaseInitializationState == InitializationStatesEnums.DATABASE_INITIALIZED)
                {
                    _timescaleInfrastructureHelpers.ReInitConnectionString(infrastructureSettings);

                    return(databaseInitializationState);
                }

                await _timescaleInfrastructureHelpers.CreateSchema(db, "server");

                await _timescaleInfrastructureHelpers.CreateSchema(db, "monitoring");

                await _timescaleInfrastructureHelpers.CreateExtension(db, "timescaledb", "monitoring");

                await _timescaleInfrastructureHelpers.CreateSchema(db, "resources");

                await _timescaleInfrastructureHelpers.CreateSchema(db, "tenants");

                await _timescaleInfrastructureHelpers.DropSchema(db, "public");

                await _timescaleInfrastructureHelpers.CreateExtension(db, "pgcrypto", "tenants");

                var pathToInitializationFile = Path.Combine(
                    _settingsManager.FullSettings.ServerSettings.BasePath,
                    PATH_TO_DATABASE_INITIALIZATION_FILES_FOLDER,
                    TABLES_AND_FUNCTIONS_FILE);

                var tablesAndFunctionsInitialization = _timescaleInfrastructureHelpers.LoadFile(
                    pathToInitializationFile
                    );

                await _timescaleInfrastructureHelpers.RunNonQuery(db, tablesAndFunctionsInitialization);

                await _settingsManager.ReloadSettingsAsync();

                _timescaleInfrastructureHelpers.ReInitConnectionString(infrastructureSettings);

                await _serversManager.ServerInitizalizationStateSet(InitializationStatesEnums.DATABASE_INITIALIZED);

                return(InitializationStatesEnums.DATABASE_INITIALIZED);
            }
            catch (OutputException)
            {
                throw;
            }
            catch (Exception ex)
            {
                await _logsManager.ErrorAsync(new ErrorLogStructure(ex).WithErrorSource());

                throw new OutputException(ex, StatusCodes.Status500InternalServerError, MnemoxStatusCodes.INTERNAL_SERVER_ERROR);
            }
            finally
            {
                await db?.DisconnectAsync();
            }
        }