Schemas

The schema module defines two classes, Schema and SchemaDict, derived from DbObject and DbObjectDict, respectively.

Schema

Schema is derived from DbObject and represents a database schema, i.e., a collection of tables and other objects. The ‘public’ schema is special as in most contexts an unqualified object is assumed to be part of it, e.g., table “t” is usually shorthand for table “public.t.”

For now, the schema name is the only attribute and is of course the identifying attribute in the Schema keylist.

class pyrseas.dbobject.schema.Schema(name, description=None, owner=None, privileges=[], oldname=None, oid=None)

A database schema definition, i.e., a named collection of tables, views, triggers and other schema objects.

static Schema.from_map(name, inobj)

Initialize a schema instance from a YAML map

Parameters:
  • name – schema name
  • inobj – YAML map of the schema
Returns:

schema instance

Schema.extern_dir(root='.')

Return the path to a directory to hold the schema objects.

Returns:directory path
Schema.to_map(db, dbschemas, opts)

Convert tables, etc., dictionaries to a YAML-suitable format

Parameters:
  • dbschemas – dictionary of schemas
  • opts – options to include/exclude schemas/tables, etc.
Returns:

dictionary

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

Return SQL statements to CREATE the schema

Returns:SQL statements
Schema.drop()
Schema.data_import(opts)

Generate SQL to import data from the tables in this schema

Parameters:opts – options to include/exclude schemas/tables, etc.
Returns:list of SQL statements

Schema Dictionary

SchemaDict is derived from DbObjectDict. It is a dictionary that represents the collection of schemas in a database. Certain internal schemas (information_schema, pg_catalog, etc.) owned by the ‘postgres’ user are excluded.

class pyrseas.dbobject.schema.SchemaDict(dbconn=None)

The collection of schemas in a database. Minimally, the ‘public’ schema.

Method from_map() is called from Database from_map() to start a recursive interpretation of the input map. The inmap argument is the same as input to the diff_map() method of Database. The newdb argument is the holder of DbObjectDict-derived dictionaries which is filled in as the recursive interpretation proceeds.

SchemaDict.from_map(inmap, newdb)

Initialize the dictionary of schemas by converting the input map

Parameters:
  • inmap – the input YAML map defining the schemas
  • newdb – collection of dictionaries defining the database

Starts the recursive analysis of the input map and construction of the internal collection of dictionaries describing the database objects.

SchemaDict.to_map(db, opts)

Convert the schema dictionary to a regular dictionary

Parameters:opts – options to include/exclude schemas/tables, etc.
Returns:dictionary

Invokes the to_map method of each schema to construct a dictionary of schemas.

SchemaDict.data_import(opts)

Iterate over schemas with tables to be imported

Parameters:opts – options to include/exclude schemas/tables, etc.
Returns:list of SQL statements