Skip to content

Commit

Permalink
SQLAlchemy 2.0 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Dec 1, 2024
1 parent aee8039 commit c0a0d9f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ python_requires = >= 3.9

install_requires =
numpy~=1.13
sqlalchemy~=1.1
sqlalchemy>=1.4
# Seq stores data as bytes
biopython~=1.79
attrs>=20
Expand Down
17 changes: 3 additions & 14 deletions src/gambit/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import sqlalchemy as sa
from sqlalchemy import Column, Integer, String, Boolean, Float
from sqlalchemy import ForeignKey, UniqueConstraint
from sqlalchemy.orm import Session, relationship, backref, deferred
from sqlalchemy.orm import Session, relationship, backref, deferred, declarative_base
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.exc import MultipleResultsFound, NoResultFound

from .sqla import JsonString
Expand Down Expand Up @@ -68,16 +67,11 @@ class Genome(Base):
"""

__tablename__ = 'genomes'
__table_args__= (UniqueConstraint('ncbi_db', 'ncbi_id'),)

#: Attributes which serve as unique IDs.
ID_ATTRS = ('key', 'genbank_acc', 'refseq_acc', 'ncbi_id')

@declared_attr
def __table_args__(cls):
return (
UniqueConstraint('ncbi_db', 'ncbi_id'),
)

id = Column(Integer(), primary_key=True)
key = Column(String(), unique=True, nullable=False)
description = Column(String(), nullable=False)
Expand Down Expand Up @@ -136,12 +130,7 @@ class ReferenceGenomeSet(Base):
for this genome set.
"""
__tablename__ = 'genome_sets'

@declared_attr
def __table_args__(cls):
return (
UniqueConstraint('key', 'version'),
)
__table_args__ = (UniqueConstraint('key', 'version'),)

id = Column(Integer(), primary_key=True)
key = Column(String(), index=True, nullable=False)
Expand Down
3 changes: 2 additions & 1 deletion src/gambit/db/sqla.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ def default_sessionmaker(bind, *, readonly: bool = True, class_: Optional[type]
"""
if class_ is None:
class_ = ReadOnlySession if readonly else Session
return sessionmaker(bind, class_=class_, **kw)
# future=True - forwards compatibility with SQLAlchemy 2.0
return sessionmaker(bind, class_=class_, future=True, **kw)


def file_sessionmaker(path: 'FilePath', **kw) -> sessionmaker:
Expand Down

0 comments on commit c0a0d9f

Please sign in to comment.