kvesteri / sqlalchemy-continuum

Versioning extension for SQLAlchemy.
BSD 3-Clause "New" or "Revised" License
575 stars 127 forks source link

Suggestion: Adding parent_table_map/version_table_map #286

Open AbdealiLoKo opened 2 years ago

AbdealiLoKo commented 2 years ago

I have been using the variables:

A lot. And they helps me to use sqla-continuum effectively for various logic I need for my application.

But recently I came across a case where I was doing:

isinstance(mytable, MyMixinClass)

But with sqla-continuum, I wanted to be able to change it to:

isinstance(mytable, MyMixinClass)
    or (
        mytable in versioning_manager.association_version_tables
        and isinstance(parent_table(mytable), MyMixinClass
    )

I couldn't find any good way to figure out this mapping

AbdealiLoKo commented 2 years ago

Having a parent_table() function would also be great.

I have created something like this in my own projects:

from sqlalchemy_continuum import versioning_manager, parent_class

def parent_table(table):
    if table in versioning_manager.association_version_tables:
        parent_table = next(
            iter(
                t
                for t in versioning_manager.association_tables
                if versioning_manager.options['table_name'] % t.name == table.name
            ),
            None,
        )
        return parent_table

    parent_table = next(
        iter(
            parent_class(m).__table__
            for m in versioning_manager.version_class_map
            if m.__table__ == table
        ),
        None,
    )
    return parent_table
AbdealiLoKo commented 2 years ago

@marksteward I was hoping to create a PR for this - if there is interest on your side

I can think of 2 approaches here:

  1. Create a helper utils.parent_table() which uses the logic I wrote above
  2. Change the current VersioningManager.association_tables -> VersioningManager.parent_association_table_map and VersioningManager.association_version_tables -> VersioningManager.version_association_table_map ?

I feel solution 2 is a bit better and more consistent with VersioningManager.version_class_map VersioningManager.parent_class_map

What would you recommend ?