cliche.orm — Object-relational mapping

Cliche uses the relational database and data on the database are mapped to objects. It widely uses SQLAlchemy as its ORM (object-relational mapping) framework.

In order to define a persist model class, just subclass Base:

from sqlalchemy import Column, Integer, UnicodeText

from .orm import Base


class Thing(Base):
    '''A something object-relationally mapped.'''

    id = Column(Integer, primary_key=True)
    value = Column(UnicodeText, nullable=False)
    __repr_columns = id, value
    __tablename__ = 'things'
class cliche.orm.Base(**kwargs)

SQLAlchemy declarative base class.

__repr_columns__

(collections.abc.Sequence) This columns will be printed to repr() string of its instances if __repr_columns__ is defined.

See also

SQLAlchemy — Declarative
Declarative allows all three to be expressed at once within the class declaration.
cliche.orm.Session = sessionmaker(class_='Session',bind=None, expire_on_commit=True, autoflush=True, autocommit=True)

SQLAlchemy session class.

See also

SQLAlchemy — Using the Session
Session is the primary usage interface for persistence operations.
cliche.orm.downgrade_database(engine, revision)

Reverts to a previous revision.

Parameters:
  • engine (sqlalchemy.engine.base.Engine) – the database engine to revert
  • revision (str) – the previous revision to revert to
cliche.orm.get_alembic_config(engine)

Creates a configuration for alembic. You can pass an Engine object or a string of database url either. So:

from sqlalchemy import create_engine
engine = create_engine('postgresql://localhost/cliche')
alembic_cfg = get_alembic_config(engine)

is equivalent to:

db_url = 'postgresql://localhost/cliche'
alembic_cfg = get_alembic_config(db_url)
Parameters:engine (sqlalchemy.engine.base.Engine, str) – the database engine to use
Returns:an alembic config
Return type:alembic.config.Config
cliche.orm.get_database_revision(engine)

Gets the current revision of the database.

Parameters:engine (sqlalchemy.engine.base.Engine) – the database engine to get the current revision
Returns:the script of the current revision
Return type:alembic.script.Script
cliche.orm.import_all_modules(dry_run=False)

Import all submodules of cliche to ensure every ORM entity classes are ready to use. It’s useful for being ready to auto-generate a migration script.

Returns:the set of module names
Return type:collections.abc.Set
cliche.orm.initialize_database(engine)

Creates all database schemas and stamps it as the head of versions.

Parameters:engine (sqlalchemy.engine.base.Engine) – the database engine to initialize
cliche.orm.upgrade_database(engine, revision='head')

Upgrades the database schema to the chosen revision (default is head).

Parameters:
  • engine (sqlalchemy.engine.base.Engine) – the database engine to upgrade
  • revision (str) – the revision to upgrade to. default is 'head'