On 07/06/2016 12:04 AM, Jon Davidson wrote:
In my current employer's setup, we use SQLAlchemy core and ORM, but we
use a homegrown migration system instead of alembic. When possible, we
want to write code so that it can work equally well with the old DB
schema and the new DB schema.
In this case, I am adding a column, and if the column doesn't exist, it
is possible to fake it using a property.
My current thought is to introspect the DB and see if the column exists,
and if it does not exist remove it from the table and mapped class and
add the property instead. Does this sound reasonable?
so yeah this use case is a big deal now, because all of openstack wants
to do it.
However in openstack, they at least know what version the target schema
is at, so while they need to run code that talks to the "old" and "new"
columns simultaneously, the code itself doesn't have to guess because
it's tied to a "version" of the schema that may include only the old,
the old and new, or only the new.
That is, they have a multi-step versioning approach, if that makes sense.
If you are trying to get around all that and just have something that
automagically figures things out, then yes you'd have to use
introspection but this is expensive (at least make sure the whole thing
finishes its work upon startup) and kind of hacky (also hard to test,
that is, your database column and your python property will likely not
behave the same under all circumstances). Removing columns is also
not a well supported use case, neither in Table nor in a mapped class.
If the column is involved in any kind of constraints (and relationships
at the ORM level), removal wreaks havoc with that which is why SQLA has
just never supported that.
I'd look to arrange things at least so that the "version" that's in the
schema is definitely known before your mappings ever start getting set up.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.