-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace alchy with sqlservice and unfreeze SQLAlchemy #255
Changes from all commits
fc69035
9e8cfac
c7995ff
f2d7320
e1ba4d5
e33c372
75669ed
2af3942
8ae70e4
dab536e
76ce1ed
a5fa98c
ab23676
18839a3
ae47d24
cdc10dd
13c1d51
4126ae8
e09a869
dc943ae
1af7e9b
5f72e17
02aafcb
72bbe15
5276700
f945a65
3790ac7
f8d3e60
1d9f08f
81a5e56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,16 +47,16 @@ def load(context, sample, group, name, group_name, threshold, bed_stream): | |
result.sample.name = name | ||
result.sample.group_name = group_name | ||
try: | ||
chanjo_db.add(result.sample) | ||
with click.progressbar(result.models, length=result.count, | ||
label='loading transcripts') as bar: | ||
for tx_model in bar: | ||
chanjo_db.add(tx_model) | ||
chanjo_db.save() | ||
with chanjo_db.begin() as session: | ||
session.add(result.sample) | ||
with click.progressbar(result.models, length=result.count, | ||
label='loading transcripts') as bar: | ||
for tx_model in bar: | ||
session.add(tx_model) | ||
|
||
except IntegrityError as error: | ||
LOG.error('sample already loaded, rolling back') | ||
LOG.debug(error.args[0]) | ||
chanjo_db.session.rollback() | ||
context.abort() | ||
|
||
|
||
|
@@ -68,14 +68,14 @@ def link(context, bed_stream): | |
"""Link related genomic elements.""" | ||
chanjo_db = ChanjoDB(uri=context.obj['database']) | ||
result = link_elements(bed_stream) | ||
with click.progressbar(result.models, length=result.count, | ||
label='adding transcripts') as bar: | ||
for tx_model in bar: | ||
chanjo_db.add(tx_model) | ||
try: | ||
chanjo_db.save() | ||
with chanjo_db.begin() as session: | ||
with click.progressbar(result.models, length=result.count, | ||
label='adding transcripts') as bar: | ||
for tx_model in bar: | ||
session.add(tx_model) | ||
|
||
except IntegrityError: | ||
LOG.exception('elements already linked?') | ||
chanjo_db.session.rollback() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rollback is handled automatically by the session, see here |
||
click.echo("use 'chanjo db setup --reset' to re-build") | ||
context.abort() | ||
LOG.exception('elements already linked?') | ||
click.echo("use 'chanjo db setup --reset' to re-build") | ||
context.abort() |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,16 @@ | |
import logging | ||
import os | ||
|
||
from alchy import Manager | ||
from sqlservice import Database | ||
from chanjo.calculate import CalculateMixin | ||
from .models import BASE | ||
from .fetch import FetchMixin | ||
from .delete import DeleteMixin | ||
|
||
log = logging.getLogger(__name__) | ||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class ChanjoDB(Manager, CalculateMixin, DeleteMixin, FetchMixin): | ||
class ChanjoDB(Database, CalculateMixin, DeleteMixin, FetchMixin): | ||
"""SQLAlchemy-based database object. | ||
|
||
Bundles functionality required to setup and interact with various | ||
|
@@ -45,16 +45,14 @@ class ChanjoDB(Manager, CalculateMixin, DeleteMixin, FetchMixin): | |
""" | ||
|
||
def __init__(self, uri=None, debug=False, base=BASE): | ||
self.Model = base | ||
self.uri = uri | ||
self.model_class = base | ||
if uri: | ||
self.connect(uri, debug=debug) | ||
|
||
|
||
def connect(self, db_uri, debug=False): | ||
"""Configure connection to a SQL database. | ||
|
||
.. versionadded:: 2.1.0 | ||
|
||
Args: | ||
db_uri (str): path/URI to the database to connect to | ||
debug (Optional[bool]): whether to output logging information | ||
|
@@ -66,12 +64,9 @@ def connect(self, db_uri, debug=False): | |
# expect only a path to a sqlite database | ||
db_path = os.path.abspath(os.path.expanduser(db_uri)) | ||
db_uri = "sqlite:///{}".format(db_path) | ||
self.uri = db_uri | ||
|
||
config['SQLALCHEMY_DATABASE_URI'] = db_uri | ||
|
||
# connect to the SQL database | ||
super(ChanjoDB, self).__init__(config=config, Model=self.Model) | ||
super(ChanjoDB, self).__init__(db_uri, model_class=BASE) | ||
|
||
@property | ||
def dialect(self): | ||
|
@@ -92,8 +87,8 @@ def set_up(self): | |
""" | ||
# create the tables | ||
self.create_all() | ||
tables = self.Model.metadata.tables.keys() | ||
log.info("created tables: %s", ', '.join(tables)) | ||
tables = self.model_class.metadata.tables.keys() | ||
LOG.info("created tables: %s", ', '.join(tables)) | ||
return self | ||
|
||
def tear_down(self): | ||
|
@@ -105,21 +100,3 @@ def tear_down(self): | |
# drop/delete the tables | ||
self.drop_all() | ||
return self | ||
|
||
def save(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is no longer required because sessions handle commits and rollbacks automatically |
||
"""Manually persist changes made to various elements. Chainable. | ||
|
||
.. versionchanged:: 2.1.2 | ||
Flush session before commit. | ||
|
||
Returns: | ||
Store: ``self`` for chainability | ||
""" | ||
try: | ||
# commit/persist dirty changes to the database | ||
self.commit() | ||
except Exception as error: | ||
log.debug('rolling back failed transaction') | ||
self.session.rollback() | ||
raise error | ||
return self |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,13 @@ | |
from collections import namedtuple | ||
from datetime import datetime | ||
|
||
from alchy import ModelBase, make_declarative_base | ||
from sqlservice import declarative_base | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 |
||
from sqlalchemy import Column, types, ForeignKey, UniqueConstraint, orm | ||
|
||
Exon = namedtuple('Exon', ['chrom', 'start', 'end', 'completeness']) | ||
|
||
# base for declaring a mapping | ||
BASE = make_declarative_base(Base=ModelBase) | ||
|
||
BASE = declarative_base() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✅ |
||
|
||
class Transcript(BASE): | ||
|
||
|
@@ -59,7 +58,6 @@ class Sample(BASE): | |
sample = orm.relationship('TranscriptStat', cascade='all,delete', | ||
backref='sample') | ||
|
||
|
||
class TranscriptStat(BASE): | ||
|
||
"""Statistics on transcript level, related to sample and transcript. | ||
|
@@ -89,7 +87,7 @@ class TranscriptStat(BASE): | |
threshold = Column(types.Integer) | ||
_incomplete_exons = Column(types.Text) | ||
|
||
sample_id = Column(types.String(32), ForeignKey('sample.id'), | ||
sample_id = Column(types.String(32), ForeignKey('sample.id', ondelete='CASCADE'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same there, nice! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without that param if you removed a sample it didin't remove the linked records in TranscriptStat any more. I guess something has changed in SQLAlchemy when switching to version 2? |
||
nullable=False) | ||
transcript_id = Column(types.String(32), ForeignKey('transcript.id'), | ||
nullable=False) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
coloredlogs | ||
alchy | ||
click | ||
path.py | ||
toolz | ||
pyyaml | ||
importlib_metadata | ||
pymysql | ||
sqlalchemy==1.3.* | ||
sqlalchemy>=2.0 | ||
sqlservice>=3.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
store.save() is no longer required, read below