Skip to content

Instantly share code, notes, and snippets.

@danallison
Last active August 29, 2024 10:08
Show Gist options
  • Select an option

  • Save danallison/7217d76d944ea4d8dabd0ba3041ebefc to your computer and use it in GitHub Desktop.

Select an option

Save danallison/7217d76d944ea4d8dabd0ba3041ebefc to your computer and use it in GitHub Desktop.

Revisions

  1. danallison revised this gist May 16, 2018. 1 changed file with 20 additions and 6 deletions.
    26 changes: 20 additions & 6 deletions with_sql_session.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    from sshtunnel import SSHTunnelForwarder
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    from functools import wraps

    # secrets.py contains credentials, etc.
    import secrets
    @@ -14,21 +15,21 @@ def get_engine_for_port(port):
    db=secrets.db
    ))

    def with_sql_session(function, engine=None):
    def with_sql_session(function, args, kwargs, engine=None):
    if engine is None:
    # Default to local port
    engine = get_engine_for_port(5432)
    Session = sessionmaker(bind=engine)
    session = Session()
    try:
    return function(session)
    return function(session, *args, **kwargs)
    finally:
    session.close()

    def with_local_sql_session(function):
    return with_sql_session(function)
    def with_local_sql_session(function, *args, **kwargs):
    return with_sql_session(function, args, kwargs)

    def with_remote_sql_session(function):
    def with_remote_sql_session(function, *args, **kwargs):
    # Hat tip: https://stackoverflow.com/a/38001815
    with SSHTunnelForwarder(
    (secrets.server_ip_address, 22),
    @@ -39,4 +40,17 @@ def with_remote_sql_session(function):
    ) as tunnel:
    tunnel.start()
    engine = get_engine_for_port(tunnel.local_bind_port)
    return with_sql_session(function, engine=engine)
    return with_sql_session(function, args, kwargs, engine=engine)

    # Decorators
    def local_sql_session(function):
    @wraps(function)
    def wrapper(*args, **kwargs):
    return with_local_sql_session(function, *args, **kwargs)
    return wrapper

    def remote_sql_session(function):
    @wraps(function)
    def wrapper(*args, **kwargs):
    return with_remote_sql_session(function, *args, **kwargs)
    return wrapper
  2. danallison revised this gist May 10, 2018. 2 changed files with 42 additions and 33 deletions.
    33 changes: 0 additions & 33 deletions with_remote_sql_session.py
    Original file line number Diff line number Diff line change
    @@ -1,33 +0,0 @@
    # Hat tip to https://stackoverflow.com/a/38001815
    from sshtunnel import SSHTunnelForwarder
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    # secrets.py contains credentials, etc.
    import secrets

    def with_remote_sql_session(function):
    with SSHTunnelForwarder(
    (secrets.server_ip_address, 22),
    ssh_username=secrets.ssh_username,
    ssh_pkey=secrets.ssh_private_key_path,
    ssh_private_key_password=secrets.ssh_private_key_password,
    remote_bind_address=('127.0.0.1', 5432)
    ) as tunnel:

    tunnel.start()

    engine = create_engine('postgresql://{user}:{password}@{host}:{port}/{db}'.format(
    user=secrets.pg_user,
    password=secrets.pg_password,
    host='127.0.0.1',
    port=tunnel.local_bind_port,
    db='my_database'
    ))

    Session = sessionmaker(bind=engine)
    session = Session()
    try:
    return function(session)
    finally:
    session.close()
    42 changes: 42 additions & 0 deletions with_sql_session.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    from sshtunnel import SSHTunnelForwarder
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    # secrets.py contains credentials, etc.
    import secrets

    def get_engine_for_port(port):
    return create_engine('postgresql://{user}:{password}@{host}:{port}/{db}'.format(
    user=secrets.pg_user,
    password=secrets.pg_password,
    host='127.0.0.1',
    port=port,
    db=secrets.db
    ))

    def with_sql_session(function, engine=None):
    if engine is None:
    # Default to local port
    engine = get_engine_for_port(5432)
    Session = sessionmaker(bind=engine)
    session = Session()
    try:
    return function(session)
    finally:
    session.close()

    def with_local_sql_session(function):
    return with_sql_session(function)

    def with_remote_sql_session(function):
    # Hat tip: https://stackoverflow.com/a/38001815
    with SSHTunnelForwarder(
    (secrets.server_ip_address, 22),
    ssh_username=secrets.ssh_username,
    ssh_pkey=secrets.ssh_private_key_path,
    ssh_private_key_password=secrets.ssh_private_key_password,
    remote_bind_address=('127.0.0.1', 5432)
    ) as tunnel:
    tunnel.start()
    engine = get_engine_for_port(tunnel.local_bind_port)
    return with_sql_session(function, engine=engine)
  3. danallison revised this gist May 4, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions with_remote_sql_session.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,6 @@
    # Hat tip to https://stackoverflow.com/a/38001815
    from sshtunnel import SSHTunnelForwarder
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker

    # secrets.py contains credentials, etc.
  4. danallison revised this gist May 4, 2018. 1 changed file with 5 additions and 8 deletions.
    13 changes: 5 additions & 8 deletions with_remote_sql_session.py
    Original file line number Diff line number Diff line change
    @@ -12,24 +12,21 @@ def with_remote_sql_session(function):
    ssh_pkey=secrets.ssh_private_key_path,
    ssh_private_key_password=secrets.ssh_private_key_password,
    remote_bind_address=('127.0.0.1', 5432)
    ) as server:
    ) as tunnel:

    server.start() #start ssh sever
    tunnel.start()

    #connect to PostgreSQL
    local_port = str(server.local_bind_port)
    engine = create_engine('postgresql://{user}:{password}@{host}:{port}/{db}'.format(
    user=secrets.pg_user,
    password=secrets.pg_password,
    host='localhost',
    port=local_port,
    host='127.0.0.1',
    port=tunnel.local_bind_port,
    db='my_database'
    ))

    Session = sessionmaker(bind=engine)
    session = Session()
    try:
    return_val = function(session)
    return function(session)
    finally:
    session.close()
    return return_val
  5. danallison created this gist May 3, 2018.
    35 changes: 35 additions & 0 deletions with_remote_sql_session.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # Hat tip to https://stackoverflow.com/a/38001815
    from sshtunnel import SSHTunnelForwarder
    from sqlalchemy.orm import sessionmaker

    # secrets.py contains credentials, etc.
    import secrets

    def with_remote_sql_session(function):
    with SSHTunnelForwarder(
    (secrets.server_ip_address, 22),
    ssh_username=secrets.ssh_username,
    ssh_pkey=secrets.ssh_private_key_path,
    ssh_private_key_password=secrets.ssh_private_key_password,
    remote_bind_address=('127.0.0.1', 5432)
    ) as server:

    server.start() #start ssh sever

    #connect to PostgreSQL
    local_port = str(server.local_bind_port)
    engine = create_engine('postgresql://{user}:{password}@{host}:{port}/{db}'.format(
    user=secrets.pg_user,
    password=secrets.pg_password,
    host='localhost',
    port=local_port,
    db='my_database'
    ))

    Session = sessionmaker(bind=engine)
    session = Session()
    try:
    return_val = function(session)
    finally:
    session.close()
    return return_val