Giter VIP home page Giter VIP logo

Comments (4)

olirice avatar olirice commented on May 27, 2024

is there a way I can have each module register their own entities?

you'll want to keep the call to register_entities in env.py because

  • it should only be called once
  • due to python's import system, if you accidentally didn't import the location where register_entities was called, none of those entities would be visible to alembic

Or should each module expose a list of entities, and then I need to import those in my env.py?

that would work. it doesn't have any opinion about how you would want to expose the entities to env.py

Perhaps it would be possible to add a similar mechanism that registers an entity on each subclass instantiation, e.g. using the init_subclass mechanism

that would be an option, but requiring a user to collect all entities in env.py explicitly & deciding which ones to add in register_entities, was an intentional design decision. It allows user's to put arbitrary rules in env.py for configuration (e.g. changing the schema name dynamically). Its also the least confusing for newcomers.


If your project has no import side-effects, there is helper function available that will traverse your package and collect all instances of an arbitrary class into a list.

# env.py
from alembic_utils.experimental import collect_instance
from alembic_utils.pg_view import PGView
from alembic_utils.replaceable_entity import register_entities

import myapp

instances: List[PGView] = collect_instances(myapp, PGView)

register_entities(*instances)

that's your best bet if you want to avoid plumbing lots of imports

from alembic_utils.

tdamsma avatar tdamsma commented on May 27, 2024

from alembic_utils.

ddemidov avatar ddemidov commented on May 27, 2024

Would it be possible to keep track of the instantiated functions/views inside PGView/PGFunction constructors? Something like

_pg_functions = []

class PGFunction:
  def __init__(self, *args, **kwargs):
    ...
    _pg_functions.append(self)

Then I guess at the point of the migration generation register_entities(*_pg_functions, *_pg_views) could be called behind the scenes? Of course, this can also be done on the user side, by extending the PGFunction and PGView classes.

EDIT: If there is an auto_register() function that just calls register_entities(*_pg_functions, *_pg_views) and has to be called explicitly by the user inside env.py, then the users could still omit the call and apply their arbitrary rules for registration.

from alembic_utils.

olirice avatar olirice commented on May 27, 2024

My concerns with any automatic registration approach are:

  • they fail unless the ReplaceableEntity is on an import path in env.py
  • less configurable / explicit

So long as you don't have import side effects, the snippet using collect_instances above should be enough to get the behavior you're looking for

from alembic_utils.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.