# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():


import requests
import tempfile
from logging import getLogger
from plenario.database import postgres_engine
        self.handle = tempfile.NamedTemporaryFile()

        # Download and write to disk in 1MB chunks.
        for chunk in file_stream_request.iter_content(chunk_size=1024*1024):
            if chunk:
                self._handle.write(chunk)
                self._handle.flush()
        logger.info('End.')


def add_unique_hash(table_name):
    """
    Adds an md5 hash column of the preexisting columns
    and removes duplicate rows from a table.
    :param table_name: Name of table to add hash to.
    """

    logger.info('Begin (table_name: {})'.format(table_name))
    add_hash = '''
    DROP TABLE IF EXISTS temp;
    CREATE TABLE temp AS
      SELECT DISTINCT *,
             md5(CAST(("{table_name}".*)AS text))
                AS hash FROM "{table_name}";
    DROP TABLE "{table_name}";
    ALTER TABLE temp RENAME TO "{table_name}";
    ALTER TABLE "{table_name}" ADD PRIMARY KEY (hash);
    '''.format(table_name=table_name)

    try:

# Path: plenario/database.py
# def create_database(bind: Engine, database: str) -> None:
# def drop_database(bind: Engine, database: str) -> None:
# def create_extension(bind: Engine, extension: str) -> None:
# def psql(path: str) -> None:
# def postgres_session_context():
# def redshift_session_context():


# Path: plenario/etl/common.py
import requests
import tempfile
from logging import getLogger
from plenario.database import postgres_engine

        self.handle = tempfile.NamedTemporaryFile()

        # Download and write to disk in 1MB chunks.
        for chunk in file_stream_request.iter_content(chunk_size=1024*1024):
            if chunk:
                self._handle.write(chunk)
                self._handle.flush()
        logger.info('End.')


def add_unique_hash(table_name):
    """
    Adds an md5 hash column of the preexisting columns
    and removes duplicate rows from a table.
    :param table_name: Name of table to add hash to.
    """

    logger.info('Begin (table_name: {})'.format(table_name))
    add_hash = '''
    DROP TABLE IF EXISTS temp;
    CREATE TABLE temp AS
      SELECT DISTINCT *,
             md5(CAST(("{table_name}".*)AS text))
                AS hash FROM "{table_name}";
    DROP TABLE "{table_name}";
    ALTER TABLE temp RENAME TO "{table_name}";
    ALTER TABLE "{table_name}" ADD PRIMARY KEY (hash);
    '''.format(table_name=table_name)

    try:

        postgres_engine.execute(add_hash)