Skip to content
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

Removes sqlalchemy_utils dependency #356

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Changelog

Here you can see the full list of changes between each SQLAlchemy-Continuum release.

2.0.0 (Unreleased)
^^^^^^^^^^^^^^^^^

- Removed direct dependency on SQLAlchemy-Utils to improve initialization times


1.3.14 (2023-01-04)
^^^^^^^^^^^^^^^^^^^

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def get_version():
platforms='any',
install_requires=[
'SQLAlchemy>=1.4.0',
'SQLAlchemy-Utils>=0.41.2',
],
extras_require=extras_require,
classifiers=[
Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_continuum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)


__version__ = '1.4.2'
__version__ = '2.0.0'


versioning_manager = VersioningManager()
Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_continuum/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
from functools import wraps

import sqlalchemy as sa
from sqlalchemy_utils.functions import get_declarative_base
from sqlalchemy.orm.descriptor_props import ConcreteInheritedProperty

from .dialects.postgresql import create_versioning_trigger_listeners
from .model_builder import ModelBuilder
from .relationship_builder import RelationshipBuilder
from .sa_utils import get_declarative_base
from .table_builder import TableBuilder


Expand Down
2 changes: 1 addition & 1 deletion sqlalchemy_continuum/dialects/postgresql.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sqlalchemy as sa

from sqlalchemy_continuum.plugins import PropertyModTrackerPlugin
from ..plugins.property_mod_tracker import PropertyModTrackerPlugin


trigger_sql = """
Expand Down
7 changes: 4 additions & 3 deletions sqlalchemy_continuum/fetcher.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import operator
import sqlalchemy as sa
from sqlalchemy_utils import get_primary_keys, identity

from .sa_utils import get_primary_key_columns, identity
from .utils import tx_column_name, end_tx_column_name


def parent_identity(obj_or_class):
return tuple(
getattr(obj_or_class, column_key)
for column_key in get_primary_keys(obj_or_class).keys()
for column_key in get_primary_key_columns(obj_or_class)
if column_key != tx_column_name(obj_or_class)
)

Expand Down Expand Up @@ -83,7 +84,7 @@ def _transaction_id_subquery(self, obj, next_or_prev='next', alias=None):
),
*[
getattr(attrs, pk) == getattr(obj, pk)
for pk in get_primary_keys(obj.__class__)
for pk in get_primary_key_columns(obj.__class__)
if pk != tx_column_name(obj)
]
)
Expand Down
40 changes: 36 additions & 4 deletions sqlalchemy_continuum/manager.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import re
from functools import wraps

import sqlalchemy as sa
from sqlalchemy.orm import object_session
from sqlalchemy_utils import get_column_key

from .builder import Builder
from .fetcher import SubqueryFetcher, ValidityFetcher
Expand All @@ -14,6 +12,41 @@
from .utils import is_modified, is_versioned, version_table


def _get_column_key(model, column):
"""
Return the key for given column in given model.

:param model: SQLAlchemy declarative model object

::

class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column('_name', sa.String)


get_column_key(User, User.__table__.c._name) # 'name'

.. versionadded: 0.26.5

.. versionchanged: 0.27.11
Throws UnmappedColumnError instead of ValueError when no property was
found for given column. This is consistent with how SQLAlchemy works.
"""
mapper = sa.inspect(model)
try:
return mapper.get_property_by_column(column).key
except sa.orm.exc.UnmappedColumnError:
for key, c in mapper.columns.items():
if c.name == column.name and c.table is column.table:
return key
raise sa.orm.exc.UnmappedColumnError(
'No column %s is configured on mapper %s...' %
(column, mapper)
)


def tracked_operation(func):
@wraps(func)
def wrapper(self, mapper, connection, target):
Expand Down Expand Up @@ -156,7 +189,7 @@ def create_transaction_model(self):

def is_excluded_column(self, model, column):
try:
key = get_column_key(model, column)
key = _get_column_key(model, column)
except sa.orm.exc.UnmappedColumnError:
return False

Expand Down Expand Up @@ -454,4 +487,3 @@ def track_association_operations(
'operation_type': op,
})
uow.pending_statements.append(stmt)

2 changes: 1 addition & 1 deletion sqlalchemy_continuum/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.orm import column_property
from sqlalchemy_utils.functions import get_declarative_base

from .sa_utils import get_declarative_base
from .utils import adapt_columns, option
from .version import VersionClassBase

Expand Down
3 changes: 2 additions & 1 deletion sqlalchemy_continuum/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from collections import OrderedDict

import sqlalchemy as sa
from sqlalchemy_utils import identity

from .sa_utils import identity


class Operation(object):
Expand Down
Loading