Types and Domains

The dbtype module defines six classes, DbType derived from DbSchemaObject, BaseType, Composite, Enum and Domain derived from DbType, and TypeDict derived from and DbObjectDict.

Database Type

Class DbType is derived from DbSchemaObject and represents a SQL type or domain as defined in the PostgreSQL pg_type catalog. Note: Only enumerated types are implemented currently.

class pyrseas.dbobject.dbtype.DbType(name, schema='public', description=None, **attrs)

A composite, domain or enum type

Base Type

BaseType is derived from DbType and represents a PostgreSQL user-defined base type.

The map returned by to_map() and expected as argument by diff_map() has the following structure (not all fields need be present):

{'type t1':
    {'alignment': 'double',
     'analyze': 'analyze_func',
     'input': 'input_func',
     'internallength': 'variable',
     'output': 'output_func',
     'receive': 'receive_func',
     'send': 'send_func',
     'storage': 'plain'
     'typmod_in': 'typmod_in_func',
     'typmod_out': 'typmod_out_func'
    }
}
class pyrseas.dbobject.dbtype.BaseType(name, schema='public', description=None, **attrs)

A composite type

BaseType.to_map(db, no_owner)

Convert a type to a YAML-suitable format

Parameters:no_owner – exclude type owner information
Returns:dictionary
BaseType.create(obj, *args, **kwargs)

Return SQL statements to CREATE the base type

Returns:SQL statements
BaseType.drop()

Generate SQL to drop the type

Returns:list of SQL statements

The CASCADE thing is mandatory to drop the functions too. There is a cyclic dependency so the dependency graph cannot be used. The functions will not be explicitly dropped.

Composite

Composite is derived from DbType and represents a standalone composite type.

class pyrseas.dbobject.dbtype.Composite(name, schema='public', description=None, **attrs)

A composite type

Composite.to_map(db, no_owner)

Convert a type to a YAML-suitable format

Parameters:no_owner – exclude type owner information
Returns:dictionary
Composite.create(obj, *args, **kwargs)

Return SQL statements to CREATE the composite type

Returns:SQL statements

Enum

Enum is derived from DbType and represents an enumerated type.

class pyrseas.dbobject.dbtype.Enum(name, schema='public', description=None, **attrs)

An enumerated type definition

Enum.create(obj, *args, **kwargs)

Return SQL statements to CREATE the enum

Returns:SQL statements

Domain

Domain is derived from DbType and represents a domain.

class pyrseas.dbobject.dbtype.Domain(name, schema='public', description=None, **attrs)

A domain definition

Domain.to_map(db, no_owner)

Convert a domain to a YAML-suitable format

Parameters:no_owner – exclude domain owner information
Returns:dictionary
Domain.create(obj, *args, **kwargs)

Return SQL statements to CREATE the domain

Returns:SQL statements

Type Dictionary

TypeDict is derived from DbObjectDict. It is a dictionary that represents the collection of domains and enums in a database.

class pyrseas.dbobject.dbtype.TypeDict(dbconn=None)

The collection of domains and enums in a database

TypeDict.from_map(schema, inobjs, newdb)

Initalize the dictionary of types by converting the input map

Parameters:
  • schema – schema owning the types
  • inobjs – YAML map defining the schema objects
  • newdb – collection of dictionaries defining the database

Connect various objects to their corresponding types or domains

Parameters:
  • dbcolumns – dictionary of columns
  • dbconstrs – dictionary of constraints
  • dbfuncs – dictionary of functions

Fills the check_constraints dictionaries for each domain by traversing the dbconstrs dictionary. Fills the attributes list for composite types. Fills the dependent functions dictionary for base types.