Clinical-Genomics / cgstats

Models and connecting to clinstatsdb
0 stars 0 forks source link

Separate config #1

Closed robinandeer closed 8 years ago

robinandeer commented 8 years ago

Use a function to setup a session explicitly No longer reads a config file

I don't know how to marry this with the automatic version check of the database - if you have any idea about that I'm happy yo listen :)

ingkebil commented 8 years ago

I keep on getting the following error. Is there another connection string I should be using?

Traceback (most recent call last):
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/mnt/hds/proj/bioinfo/git/kenny/clinstatsdb/clinstatsdb/xparseunaligned.py", line 52, in <module>
    main(sys.argv[1:])
  File "/mnt/hds/proj/bioinfo/git/kenny/clinstatsdb/clinstatsdb/xparseunaligned.py", line 14, in main
    SQL = connect('mysql://tralalala@127.0.0.1:3306/csdb_test')
  File "clinstatsdb/db/store.py", line 10, in connect
    engine = create_engine(connection_string)
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 386, in create_engine
    return strategy.create(*args, **kwargs)
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 75, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 92, in dbapi
    return __import__('MySQLdb')
ImportError: No module named MySQLdb
robinandeer commented 8 years ago

Probably, I like the pure Python implementation: "pymysql"

I hope you don't mind I made it an explicit dependency over the default MySQL one :)

ingkebil commented 8 years ago

Well, yes, I did remove Python-MySQL and I installed pymysql instead. As is described in the requirements. Ok, the connectionstring becomes mysql+pymysql:// :)

robinandeer commented 8 years ago

Yeah, sorry that ought to do it!

ingkebil commented 8 years ago
Traceback (most recent call last):
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/kenny.billiau/miniconda/envs/clinstatsdb/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/mnt/hds/proj/bioinfo/git/kenny/clinstatsdb/clinstatsdb/xparseunaligned.py", line 52, in <module>
    main(sys.argv[1:])
  File "/mnt/hds/proj/bioinfo/git/kenny/clinstatsdb/clinstatsdb/xparseunaligned.py", line 16, in main
    print(Supportparams.exists('/home/clinical/DEMUX//150703_D00134_0206_AH5HGFBCXX/Unaligned4/support.txt')) # 515
  File "clinstatsdb/db/models.py", line 113, in exists
    rs = (cls.query(cls.supportparams_id.label('id'))
TypeError: 'Query' object is not callable

Any idea why class methods calling cls.query don't work anymore?

ingkebil commented 8 years ago

I might have to declare my models in a different way?

robinandeer commented 8 years ago

I didn't test that, sorry. I can have a look but it seems it should still work...

robinandeer commented 8 years ago

it seems like e.g. Project.query already is setup to return Project rows (full)

you could rewrite it as:

project_obj = cls.query.filter_by(projectname=project_name).first()
return project_obj.id if project_obj else False

would that work for you?

robinandeer commented 8 years ago

I mean project_obj will be none if it isn't found in the database so you could return the object or None and handle other stuff downstream maybe?

ingkebil commented 8 years ago

but this will still fall over the fact that cls.query is not callable, no?

db = declarative_base()

class Supportparams(db):
    __tablename__ = 'supportparams'

    supportparams_id = Column(Integer, primary_key=True)
    document_path = Column(String(255), nullable=False)

    @classmethod
    def exists(cls, document_path):
        """Checks if the supportparams entry already exists

        Args:
            document_path (str): Full path to the Unaligned directory

        Returns:
            int: supportparams_id on exists
            False: on not exists

        """
        try:
            rs = (cls.query(cls.supportparams_id.label('id'))
                     .filter(cls.document_path == document_path).one())
            return rs.id
        except NoResultFound:
            return False
robinandeer commented 8 years ago

yeah, I believe cls.query is something like session.query(cls), no? But that's not a problem, is it?

ingkebil commented 8 years ago

According to http://stackoverflow.com/a/14553324/322188 session.query instead of session.query_property should work. And it does :)

robinandeer commented 8 years ago

cool!

ingkebil commented 8 years ago

About the version check of the database: The whole point here was to remove the config file no? So the responsibility to check if this is the latest version of the database now relies on the calling script, not this package. We can remove the Version.latest classmethod and whatever script or package that uses this package will have to check themselves according to their specs/config.

ingkebil commented 8 years ago

haha, so far for me waiting for the :+1: ;)

robinandeer commented 8 years ago

:+1: