sqlalchemy.inspect.columns

Here are the examples of the python api sqlalchemy.inspect.columns taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

2 Examples 7

3 Source : db.py
with MIT License
from Jarrott

    def __prune_fields(self):
        columns = inspect(self.__class__).columns
        if not self._fields:
            all_columns = set([column.name for column in columns])
            self._fields = list(all_columns - set(self._exclude))

    def hide(self, *args):

0 Source : upgrades.py
with MIT License
from OneGov

def migrate_to_jsonb(connection, schemas):
    """ Migrates all text base json columns to jsonb. """

    def json_columns(cls):
        try:
            for column in inspect(cls).columns:
                if isinstance(column.type, JSON):
                    yield column
        except NoInspectionAvailable:
            pass

    classes = list(find_models(Base, is_match=lambda cls: True))

    # XXX onegov.libres (but not libres itself) uses json with a different orm
    # base - so we need to included it manually
    try:
        from libres.db.models import ORMBase
        classes.extend(find_models(ORMBase, is_match=lambda cls: True))
    except ImportError:
        pass

    columns = list(c for cls in classes for c in json_columns(cls))

    if not columns:
        return False

    text_columns = list(
        r.identity for r in connection.execute(text("""
            SELECT concat_ws(':', table_schema, table_name, column_name)
                AS identity
              FROM information_schema.columns
             WHERE data_type != 'jsonb'
               AND table_schema IN :schemas
               AND column_name IN :names
        """), schemas=tuple(schemas), names=tuple(c.name for c in columns))
    )

    for schema in schemas:
        for column in columns:
            identity = ':'.join((schema, column.table.name, column.name))

            if identity not in text_columns:
                continue

            # XXX do not use this kind of statement outside upgrades!
            connection.execute("""
                ALTER TABLE "{schema}".{table}
                ALTER COLUMN {column}
                TYPE JSONB USING {column}::jsonb
            """.format(
                schema=schema,
                table=column.table.name,
                column=column.name
            ))

            # commits/rolls back the current transaction (to keep the number
            # of required locks to a minimum)
            yield True


@upgrade_task('Rename associated tables')