Skip to content

Commit

Permalink
Merge pull request #40 from HazyResearch/hotfix
Browse files Browse the repository at this point in the history
Support providing a postgres username/pass in connection string
  • Loading branch information
lukehsiao committed Mar 31, 2018
2 parents aba839e + 0d3feba commit 4e035a1
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 29 deletions.
11 changes: 8 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
.. note::
Fonduer is still under active development and APIs may still change
rapidly.
Version 0.1.6
-------------

* `@senwu`_: Fix support for providing a PostgreSQL username and password as
part of the connection string provided to Meta.init()
(`#40 <https://github.com/HazyResearch/fonduer/pull/40>`_)
* `@lukehsiao`_: Switch README from Markdown to reStructuredText

Version 0.1.5
-------------
Expand Down Expand Up @@ -51,3 +55,4 @@ Version 0.1.2
For convenience, all username links for contributors can be listed here
.. _@lukehsiao: https://github.com/lukehsiao
.. _@senwu: https://github.com/senwu
12 changes: 12 additions & 0 deletions docs/dev/changelog.rst
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
Changelog
=========

We are following `Semantic Versioning 2.0.0`_ conventions. The maintainers will
create a git tag for each release and increment the version number found in
`fonduer/\_version.py`_ accordingly. We deploy tags to PyPI automatically using
Travis-CI.

.. note::
Fonduer is still under active development and APIs may still change
rapidly.

.. include:: ../../CHANGELOG.rst

.. _Semantic Versioning 2.0.0: https://semver.org/
.. _fonduer/\_version.py: https://github.com/HazyResearch/fonduer/blob/master/fonduer/_version.py
5 changes: 0 additions & 5 deletions docs/dev/install.rst
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
Installation
============

We are following `Semantic Versioning 2.0.0 <https://semver.org/>`__
conventions. The maintainers will create a git tag for each release and
increment the version number found in `fonduer/\_version.py`_ accordingly. We
deploy tags to PyPI automatically using Travis-CI.

To test changes in the package, you install it in `editable mode`_ locally in
your virtualenv by running::

Expand Down
2 changes: 1 addition & 1 deletion fonduer/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.5"
__version__ = "0.1.6"
27 changes: 12 additions & 15 deletions fonduer/async_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,18 @@ def copy_postgres(segment_file_blob, table_name, tsv_columns):
separated by comma. e.g. "name, age, income"
"""
print('Copying %s to postgres' % table_name)
if _meta.DBPORT:
cmd = ('cat %s | psql -p %s %s -U %s -c "COPY %s(%s) '
'FROM STDIN" --set=ON_ERROR_STOP=true') % (segment_file_blob,
_meta.DBPORT,
_meta.DBNAME,
_meta.DBUSER,
table_name,
tsv_columns)
else:
cmd = ('cat %s | psql %s -U %s -c "COPY %s(%s) '
'FROM STDIN" --set=ON_ERROR_STOP=true') % (segment_file_blob,
_meta.DBNAME,
_meta.DBUSER,
table_name,
tsv_columns)

username = "-U " + _meta.DBUSER if _meta.DBUSER is not None else ""
password = "PGPASSWORD=" + _meta.DBPWD if _meta.DBPWD is not None else ""
port = "-p " + str(_meta.DBPORT) if _meta.DBPORT is not None else ""
cmd = ('cat %s | %s psql %s %s %s -c "COPY %s(%s) '
'FROM STDIN" --set=ON_ERROR_STOP=true') % (segment_file_blob,
password,
_meta.DBNAME,
username,
port,
table_name,
tsv_columns)
_out = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True)
print(_out)

Expand Down
12 changes: 7 additions & 5 deletions fonduer/snorkel/models/meta.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import, division, print_function

import getpass
import logging
from builtins import object
from urllib.parse import urlparse
Expand Down Expand Up @@ -45,6 +44,7 @@ class Meta(object):
DBNAME = None
DBUSER = None
DBPORT = None
DBPWD = None
Session = None
engine = None
Base = declarative_base(name='Base', cls=object)
Expand All @@ -55,11 +55,13 @@ class Meta(object):
def init(cls, conn_string=None):
"""Return the unique Meta class."""
if conn_string and not Meta.ready:
url = urlparse(conn_string)
Meta.conn_string = conn_string
Meta.DBNAME = conn_string.split('/')[-1]
Meta.DBUSER = getpass.getuser()
Meta.DBPORT = urlparse(conn_string).port
Meta.postgres = conn_string.startswith('postgres')
Meta.DBNAME = url.path[1:]
Meta.DBUSER = url.username
Meta.DBPWD = url.password
Meta.DBPORT = url.port
Meta.postgres = url.scheme.startswith('postgres')
# We initialize the engine within the models module because models'
# schema can depend on which data types are supported by the engine
Meta.ready = Meta.postgres
Expand Down

0 comments on commit 4e035a1

Please sign in to comment.