rattail.db.util
¶
Database Utilities
- class rattail.db.util.CounterMagic(config)[source]¶
Provides magic counter values, to simulate PostgreSQL sequence.
- class rattail.db.util.PhoneValidator(error=False)[source]¶
Simple validator, used to ensure a phone number matches the general expected pattern.
- class rattail.db.util.QuerySequence(query)[source]¶
Simple wrapper for a SQLAlchemy (or Django, or other?) query, to make it sort of behave like a normal sequence, as much as needed to e.g. make an importer happy.
- rattail.db.util.finalize_session(session, dry_run=False, success=True)[source]¶
Wrap up the given session, per the given arguments. This is meant to provide a simple convenience, for commands which must do work within a DB session, but would like to support a “dry run” mode.
- rattail.db.util.format_phone_number(number)[source]¶
Returns a phone number in
(XXX) XXX-XXXX
format if possible; otherwise returns the argument unaltered.
- rattail.db.util.get_fieldnames(config, obj, columns=True, proxies=True, relations=False)[source]¶
Produce a simple list of fieldnames for the given class, reflecting its table columns as well as any association proxies, and optionally, relationships.
- Parameters:
obj¶ – Either a class or instance of a class, which derives from the base model class.
columns¶ – Whether or not to include simple columns.
relations¶ – Whether or not to include fields which represent relationships to other models. If
False
(the default) then only “simple” fields will be included.proxies¶ – Whether or not to include association proxy fields.
- rattail.db.util.make_full_description(brand_name, description, size)[source]¶
Combine the given field values into a complete description.
- rattail.db.util.make_topo_sortkey(model, metadata=None)[source]¶
Returns a function suitable for use as a
key
kwarg to a standard Python sorting call. This key function will expect a single class mapper and return a sequence number associated with that model. The sequence is determined by SQLAlchemy’s topological table sorting.
- rattail.db.util.normalize_full_name(first_name, last_name)[source]¶
Normalize the given first and last name to a “full” name value. The fallback return value is an empty string.
- rattail.db.util.short_session(session=None, Session=None, commit=False, factory=None, config=None)[source]¶
Compatibility wrapper around
wuttjamaican.db.sess.short_session
.Note that this wrapper is a function whereas the upsream version is a proper context manager (class). So calling this function will return a new instance of the upsream class.
You should always specify keyword arguments when calling this function, since the arg order is different between this function and the upstream class. And note that this function will eventually be deprecated and removed, so new code should call upstream directly.
- rattail.db.util.uuid_column(*args, **kwargs)[source]¶
Returns a UUID column for use as a table’s primary key.
See also
wuttjamaican.db.util.uuid_column()
.We override the upstream logic (for now) to ensure we stick with
String(length=32)
for the column data type, whereas upstream may deviate in the future.
- rattail.db.util.uuid_fk_column(target_column, *args, **kwargs)[source]¶
Returns a UUID column for use as a foreign key to another table.
- Parameters:
target_column¶ – Name of the table column on the remote side, e.g.
'user.uuid'
.
See also
wuttjamaican.db.util.uuid_fk_column()
.We override the upstream logic (for now) to ensure we stick with
String(length=32)
for the column data type, whereas upstream may deviate in the future.