Permalink
Browse files

Add support for comments on views, sequences, indexes and constraints.

 * docs/constraint.rst: Add automethod for Constraint.comment.
 * pyrseas/dbobject/constraint.py: Add Constraint.comment and support
   for mapping and creating comments.
 * pyrseas/dbobject/index.py: Add support for mapping and creating
   comments.
 * pyrseas/dbobject/language.py (Language.diff_map): Replace body by
   call to generic implementation.
 * pyrseas/dbobject/table.py: Add support for mapping and creating
   comments on sequences and views.
 * tests/dbobject/{test_constraint.py, test_index.py,
   test_language.py, test_sequence.py, test_view.py}: Add new tests
   for comment mapping and generation.
  • Loading branch information...
jmafc committed Sep 21, 2011
1 parent 220e935 commit 72c3590e5b01aa7b8c7ba9bedb81e96e8fc229bd
View
@@ -25,6 +25,8 @@ name, the table name and the constraint name.
.. automethod:: Constraint.drop
+.. automethod:: Constraint.comment
+
Check Constraint
----------------
@@ -41,10 +41,13 @@ def add(self):
Works as is for primary keys and unique constraints but has
to be overridden for check constraints and foreign keys.
"""
- return "ALTER TABLE %s ADD CONSTRAINT %s %s (%s)" % (
- DbSchemaObject(schema=self.schema, name=self.table).qualname(),
- quote_id(self.name),
- self.objtype, self.key_columns())
+ stmts = ["ALTER TABLE %s ADD CONSTRAINT %s %s (%s)" % (
+ DbSchemaObject(schema=self.schema, name=self.table).qualname(),
+ quote_id(self.name),
+ self.objtype, self.key_columns())]
+ if hasattr(self, 'description'):
+ stmts.append(self.comment())
+ return stmts
def drop(self):
"""Return string to drop the constraint via ALTER TABLE
@@ -57,6 +60,18 @@ def drop(self):
self._qualtable(), self.name)
return []
+ def comment(self):
+ """Return SQL statement to create COMMENT on constraint
+
+ :return: SQL statement
+ """
+ if hasattr(self, 'description'):
+ descr = "'%s'" % self.description
+ else:
+ descr = 'NULL'
+ return "COMMENT ON CONSTRAINT %s ON %s IS %s" % (
+ self.identifier(), self._qualtable(), descr)
+
class CheckConstraint(Constraint):
"A check constraint definition"
@@ -84,8 +99,11 @@ def add(self):
:return: SQL statement
"""
- return "ALTER TABLE %s ADD CONSTRAINT %s %s (%s)" % (
- self._qualtable(), self.name, self.objtype, self.expression)
+ stmts = ["ALTER TABLE %s ADD CONSTRAINT %s %s (%s)" % (
+ self._qualtable(), self.name, self.objtype, self.expression)]
+ if hasattr(self, 'description'):
+ stmts.append(self.comment())
+ return stmts
def diff_map(self, inchk):
"""Generate SQL to transform an existing CHECK constraint
@@ -99,6 +117,7 @@ def diff_map(self, inchk):
"""
stmts = []
# TODO: to be implemented
+ stmts.append(self.diff_description(inchk))
return stmts
@@ -132,6 +151,7 @@ def diff_map(self, inpk):
"""
stmts = []
# TODO: to be implemented (via ALTER DROP and ALTER ADD)
+ stmts.append(self.diff_description(inpk))
return stmts
@@ -194,6 +214,7 @@ def diff_map(self, infk):
"""
stmts = []
# TODO: to be implemented (via ALTER DROP and ALTER ADD)
+ stmts.append(self.diff_description(infk))
return stmts
@@ -229,6 +250,7 @@ def diff_map(self, inuc):
"""
stmts = []
# TODO: to be implemented (via ALTER DROP and ALTER ADD)
+ stmts.append(self.diff_description(inuc))
return stmts
@@ -245,8 +267,9 @@ class ConstraintDict(DbObjectDict):
contype AS type, conkey AS keycols,
confrelid::regclass AS ref_table, confkey AS ref_cols,
consrc AS expression, confupdtype AS on_update,
- confdeltype AS on_delete, amname AS access_method
- FROM pg_constraint
+ confdeltype AS on_delete, amname AS access_method,
+ obj_description(c.oid, 'pg_constraint') AS description
+ FROM pg_constraint c
JOIN pg_namespace ON (connamespace = pg_namespace.oid)
JOIN pg_roles ON (nspowner = pg_roles.oid)
LEFT JOIN pg_class on (conname = relname)
@@ -313,6 +336,8 @@ def from_map(self, table, inconstrs, target=''):
check.keycols = val['columns']
if target:
check.target = target
+ if 'description' in val:
+ check.description = val['description']
self[(table.schema, table.name, cns)] = check
if 'primary_key' in inconstrs:
cns = inconstrs['primary_key'].keys()[0]
@@ -326,6 +351,8 @@ def from_map(self, table, inconstrs, target=''):
raise
if 'access_method' in val:
pkey.access_method = val['access_method']
+ if 'description' in val:
+ pkey.description = val['description']
self[(table.schema, table.name, cns)] = pkey
if 'foreign_keys' in inconstrs:
fkeys = inconstrs['foreign_keys']
@@ -371,6 +398,8 @@ def from_map(self, table, inconstrs, target=''):
if 'schema' in refs:
sch = refs['schema']
fkey.ref_schema = sch
+ if 'description' in val:
+ fkey.description = val['description']
self[(table.schema, table.name, cns)] = fkey
if 'unique_constraints' in inconstrs:
uconstrs = inconstrs['unique_constraints']
@@ -385,6 +414,8 @@ def from_map(self, table, inconstrs, target=''):
raise
if 'access_method' in val:
unq.access_method = val['access_method']
+ if 'description' in val:
+ unq.description = val['description']
self[(table.schema, table.name, cns)] = unq
def diff_map(self, inconstrs):
@@ -54,6 +54,8 @@ def create(self):
unq and 'UNIQUE ' or '', quote_id(self.name), quote_id(self.table),
acc, hasattr(self, 'keycols') and self.key_columns() or
self.expression))
+ if hasattr(self, 'description'):
+ stmts.append(self.comment())
return stmts
def diff_map(self, inindex):
@@ -76,6 +78,7 @@ def diff_map(self, inindex):
self.unique = inindex.unique
stmts.append(self.create())
# TODO: need to deal with changes in keycols
+ stmts.append(self.diff_description(inindex))
return stmts
@@ -87,7 +90,8 @@ class IndexDict(DbObjectDict):
"""SELECT nspname AS schema, indrelid::regclass AS table,
c.relname AS name, amname AS access_method,
indisunique AS unique, indkey AS keycols,
- pg_get_expr(indexprs, indrelid) AS expression
+ pg_get_expr(indexprs, indrelid) AS expression,
+ obj_description (c.oid, 'pg_class') AS description
FROM pg_index JOIN pg_class c ON (indexrelid = c.oid)
JOIN pg_namespace ON (relnamespace = pg_namespace.oid)
JOIN pg_roles ON (nspowner = pg_roles.oid)
@@ -129,6 +133,8 @@ def from_map(self, table, inindexes):
setattr(idx, attr, val[attr])
if not hasattr(idx, 'unique'):
idx.unique = False
+ if 'description' in val:
+ idx.description = val['description']
self[(table.schema, table.name, i)] = idx
def diff_map(self, inindexes):
@@ -53,18 +53,7 @@ def diff_map(self, inlanguage):
input.
"""
stmts = []
- if hasattr(self, 'description'):
- if hasattr(inlanguage, 'description'):
- if self.description != inlanguage.description:
- self.description = inlanguage.description
- stmts.append(self.comment())
- else:
- del self.description
- stmts.append(self.comment())
- else:
- if hasattr(inlanguage, 'description'):
- self.description = inlanguage.description
- stmts.append(self.comment())
+ stmts.append(self.diff_description(inlanguage))
return stmts
View
@@ -85,19 +85,23 @@ def to_map(self):
def create(self):
"""Return a SQL statement to CREATE the sequence
- :return: SQL statement
+ :return: SQL statements
"""
+ stmts = []
maxval = self.max_value and ("MAXVALUE %d" % self.max_value) \
or "NO MAXVALUE"
minval = self.min_value and ("MINVALUE %d" % self.min_value) \
or "NO MINVALUE"
- return """CREATE SEQUENCE %s
+ stmts.append("""CREATE SEQUENCE %s
START WITH %d
INCREMENT BY %d
%s
%s
CACHE %d""" % (self.qualname(), self.start_value, self.increment_by,
- maxval, minval, self.cache_value)
+ maxval, minval, self.cache_value))
+ if hasattr(self, 'description'):
+ stmts.append(self.comment())
+ return stmts
def add_owner(self):
"""Return statement to ALTER the sequence to indicate its owner table
@@ -123,6 +127,7 @@ def diff_map(self, inseq):
statements to transform it into the one represented by the
input.
"""
+ stmts = []
stmt = ""
if self.start_value != inseq.start_value:
stmt += " START WITH %d" % inseq.start_value
@@ -143,8 +148,9 @@ def diff_map(self, inseq):
if self.cache_value != inseq.cache_value:
stmt += " CACHE %d" % inseq.cache_value
if stmt:
- return "ALTER SEQUENCE %s" % self.qualname() + stmt
- return []
+ stmts.append("ALTER SEQUENCE %s" % self.qualname() + stmt)
+ stmts.append(self.diff_description(inseq))
+ return stmts
class Table(DbClass):
@@ -316,7 +322,10 @@ def to_map(self):
:return: dictionary
"""
- return {self.extern_key(): {'definition': self.definition}}
+ dct = self.__dict__.copy()
+ for k in self.keylist:
+ del dct[k]
+ return {self.extern_key(): dct}
def create(self, newdefn=None):
"""Return SQL statements to CREATE the table
Oops, something went wrong.

0 comments on commit 72c3590

Please sign in to comment.