Database Objects

The dbobject module defines two low-level classes and an intermediate class. Most Pyrseas classes are derived from either DbObject or DbObjectDict.

Database Object

A DbObject represents a database object such as a schema, table, or column, defined in a PostgreSQL system catalog. It is initialized from a dictionary of attributes. Derived classes should define a keylist that is a list of attribute names that uniquely identify each object instance within the database.

class pyrseas.dbobject.DbObject(name=None, description=None, owner=None, privileges=None, **attrs)

A single object in a database catalog, e.g., a schema, a table, a column

DbObject.objtype = ''

Type of object as an uppercase string, for SQL syntax generation

This is used in most CREATE, ALTER and DROP statements. It is also used by extern_key() in lowercase form.

DbObject.keylist = ['name']

List of attributes that uniquely identify the object in the catalogs

See description of key() for further details.


Return a tuple that identifies the database object

Returns:a single string or a tuple of strings

This is used as key for all internal maps. The first-level objects (schemas, languages and casts) use the object name as the key. Second-level (schema-owned) objects usually use the schema name and the object name as the key. Some object types need longer keys, e.g., operators need schema name, operator symbols, left argument and right argument.

Each class implementing an object type specifies a keylist attribute, i.e., a list giving the names of attributes making up the key.

The following methods are generally used to map objects for external output:


Return the key to be used in external maps for this object


This is used for the first two levels of external maps. The first level is the one that includes schemas, as well as extensions, languages, casts and FDWs. The second level includes all schema-owned objects, i.e., tables, functions, operators, etc. All subsequent levels, e.g., primary keys, indexes, etc., currently use the object name as the external identifier, appearing in the map after an object grouping header, such as primary_key.

The common format for an external key is object-type non-schema-qualified-name, where object-type is the lowercase version of objtype, e.g., table tablename. Some object types require more, e.g., functions need the signature, so they override this implementation.

DbObject.extern_filename(ext='yaml', truncate=False)

Return a filename to be used to output external files

  • ext – file extension
  • truncate – truncate filename to MAX_IDENT_LEN

filename string

This is used for the first two levels of external maps. The first level is the one that includes schemas, as well as extensions, languages, casts and FDWs. The second level includes all schema-owned objects, i.e., tables, functions, operators, etc.

The common format for the filename is objtype.objname.yaml, e.g., for a table t1 the filename is “table.t1.yaml”. For an object name that has characters not allowed in filesystems, the characters are replaced by underscores.


Returns a full identifier for the database object


This is used by comment(), alter_owner() and drop() to generate SQL syntax referring to the object. It does not include the object type, but it may include (in overriden methods) other elements, e.g., the arguments to a function.

DbObject.to_map(no_owner=False, no_privs=False)

Convert an object to a YAML-suitable format

  • no_owner – exclude object owner information
  • no_privs – exclude privilege information


This base implementation simply copies the internal Python dictionary, removes the keylist attributes, and returns a new dictionary using the extern_key() result as the key.


Return a list of access privileges on the current object


The following methods generate SQL statements from the object properties and sometimes from a second object:


Return SQL statement to create a COMMENT on the object

Returns:SQL statement

Return ALTER statement to set the OWNER of an object

Returns:SQL statement

Return SQL statement to DROP the object

Returns:SQL statement

Return SQL statement to RENAME the object

Parameters:newname – the new name for the object
Returns:SQL statement
DbObject.diff_map(inobj, no_owner=False)

Generate SQL to transform an existing object

  • inobj – a YAML map defining the new object
  • no_owner – exclude object owner information

list of SQL statements

Compares the object to an input object and generates SQL statements to transform it into the one represented by the input. This base implementation simply deals with owners and comments.


Generate SQL statements to grant or revoke privileges

Parameters:inobj – a YAML map defining the input object
Returns:list of SQL statements

Generate SQL statements to add or change COMMENTs

Parameters:inobj – a YAML map defining the input object
Returns:list of SQL statements

Database Object Dictionary

A DbObjectDict represents a collection of DbObject‘s and is derived from the Python built-in type dict. If a DbConnection object is used for initialization, an internal method is called to initialize the dictionary from the database catalogs. The DbObjectDict fetch() method fetches all objects using the query defined by derived classes. Derived classes should also define a cls attribute for the associated DbObject class, e.g., SchemaDict sets cls to Schema.

class pyrseas.dbobject.DbObjectDict(dbconn=None)

A dictionary of database objects, all of the same type

DbObjectDict.cls = <class 'pyrseas.dbobject.DbObject'>

The class, derived from DbObject that the objects belong to.

DbObjectDict.query = ''

The SQL SELECT query to fetch object instances from the catalogs

This is used by the method fetch().


Convert the object dictionary to a regular dictionary

Parameters:opts – options to include/exclude information, etc.

Invokes the to_map method of each object to construct the dictionary. If opts specifies a directory, the objects are written to files in that directory.


Fetch all objects from the catalogs using the class query

Returns:list of self.cls objects

Schema Object

A DbSchemaObject is derived from DbObject. It is used as a base class for objects owned by a schema and to define certain common methods. This is different from the Schema that represents the schema itself.

class pyrseas.dbobject.DbSchemaObject(schema, name, description=None, owner=None, privileges=None, **attrs)

A database object that is owned by a certain schema


Return a full identifier for a schema object


Return the schema-qualified name of self or a related object


No qualification is used if the schema is ‘public’.


Adjust the schema and table name if the latter is qualified


Return a SQL DROP statement for the schema object

Returns:SQL statement

Return a SQL ALTER statement to RENAME the schema object

Parameters:newname – the new name of the object
Returns:SQL statement