/// <summary>
        /// Opens the component.
        /// </summary>
        /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param>
        public async virtual Task OpenAsync(string correlationId)
        {
            if (IsOpen())
            {
                return;
            }

            if (_connection == null)
            {
                _connection      = CreateLocalConnection();
                _localConnection = true;
            }

            if (_localConnection)
            {
                await _connection.OpenAsync(correlationId);
            }

            if (_connection.IsOpen() == false)
            {
                throw new InvalidStateException(correlationId, "CONNECTION_NOT_OPENED", "Database connection is not opened");
            }

            _databaseName = _connection.GetDatabaseName();

            // Define database schema
            DefineSchema();

            // Recreate objects
            await CreateSchemaAsync(correlationId);

            _opened = true;
        }
        private PostgresConnection CreateLocalConnection()
        {
            var connection = new PostgresConnection();

            if (_config != null)
            {
                connection.Configure(_config);
            }

            if (_references != null)
            {
                connection.SetReferences(_references);
            }

            return(connection);
        }
        /// <summary>
        /// Sets references to dependent components.
        /// </summary>
        /// <param name="references">references to locate the component dependencies.</param>
        public virtual void SetReferences(IReferences references)
        {
            _references = references;

            _logger.SetReferences(references);
            _dependencyResolver.SetReferences(references);

            // Get connection
            _connection      = _dependencyResolver.GetOneOptional("connection") as PostgresConnection;
            _localConnection = _connection == null;

            // Or create a local one
            if (_connection == null)
            {
                _connection = CreateLocalConnection();
            }
        }
        public PostgresConnectionTest()
        {
            Db = new PostgresConnection();

            postgresUri      = Environment.GetEnvironmentVariable("POSTGRES_URI");
            postgresHost     = Environment.GetEnvironmentVariable("POSTGRES_HOST") ?? "localhost";
            postgresPort     = Environment.GetEnvironmentVariable("POSTGRES_PORT") ?? "5432";
            postgresDatabase = Environment.GetEnvironmentVariable("POSTGRES_DB") ?? "test";
            postgresUsername = Environment.GetEnvironmentVariable("POSTGRES_USERNAME") ?? "postgres";
            postgresPassword = Environment.GetEnvironmentVariable("POSTGRES_PASSWORD") ?? "postgres";
            if (postgresUri == null && postgresHost == null)
            {
                return;
            }

            if (Db == null)
            {
                return;
            }
        }
 /// <summary>
 /// Unsets (clears) previously set references to dependent components.
 /// </summary>
 public virtual void UnsetReferences()
 {
     _connection = null;
 }