diff options
author | Marko Kreen | 2009-10-14 14:34:40 +0000 |
---|---|---|
committer | Marko Kreen | 2009-10-14 14:34:40 +0000 |
commit | 8d36578a23d6ea1a17199765b412d963da5fd34f (patch) | |
tree | 4a8c7533ce1c0855f8a0ca58a0f04da8118a3a5a /python/skytools/dbstruct.py | |
parent | 9dcab958bdaadb2787027b9b10af362a0bc2f861 (diff) |
londiste: make copy unlink inherited table from it's parents
Otherwise we cannot drop constraints.
Unlink from childs is not needed, so they are left as-is.
Patch by Hannu Krosing
Diffstat (limited to 'python/skytools/dbstruct.py')
-rw-r--r-- | python/skytools/dbstruct.py | 31 |
1 files changed, 28 insertions, 3 deletions
diff --git a/python/skytools/dbstruct.py b/python/skytools/dbstruct.py index 2a1c8e47..916de131 100644 --- a/python/skytools/dbstruct.py +++ b/python/skytools/dbstruct.py @@ -9,7 +9,7 @@ from skytools.quoting import quote_ident, quote_fqident, quote_literal, unquote_ __all__ = ['TableStruct', 'SeqStruct', 'T_TABLE', 'T_CONSTRAINT', 'T_INDEX', 'T_TRIGGER', 'T_RULE', 'T_GRANT', 'T_OWNER', 'T_PKEY', 'T_ALL', - 'T_SEQUENCE'] + 'T_SEQUENCE', 'T_PARENT'] T_TABLE = 1 << 0 T_CONSTRAINT = 1 << 1 @@ -19,6 +19,7 @@ T_RULE = 1 << 4 T_GRANT = 1 << 5 T_OWNER = 1 << 6 T_SEQUENCE = 1 << 7 +T_PARENT = 1 << 8 T_PKEY = 1 << 20 # special, one of constraints T_ALL = ( T_TABLE | T_CONSTRAINT | T_INDEX | T_SEQUENCE | T_TRIGGER | T_RULE | T_GRANT | T_OWNER ) @@ -195,6 +196,27 @@ class TTrigger(TElem): def get_drop_sql(self, curs): return 'DROP TRIGGER %s ON %s' % (quote_ident(self.name), quote_fqident(self.table_name)) +class TParent(TElem): + """Info about trigger.""" + type = T_PARENT + SQL = """ + SELECT n.nspname||'.'||c.relname AS name + FROM pg_inherits i + JOIN pg_class c ON i.inhparent = c.oid + JOIN pg_namespace n ON c.relnamespace = n.oid + WHERE i.inhrelid = %(oid)s + """ + def __init__(self, table_name, row): + self.name = table_name + self.parent_name = row['name'] + + def get_create_sql(self, curs, new_table_name = None): + return 'ALTER TABLE ONLY %s INHERIT %s' % (quote_fqident(self.name), quote_fqident(self.parent_name)) + + def get_drop_sql(self, curs): + return 'ALTER TABLE ONLY %s NO INHERIT %s' % (quote_fqident(self.name), quote_fqident(self.parent_name)) + + class TOwner(TElem): """Info about table owner.""" type = T_OWNER @@ -405,7 +427,10 @@ class BaseStruct(object): def drop(self, curs, objs, log = None): """Issues DROP statements for requested set of objects.""" - for o in self.object_list: + # make sure the creating & dropping happen in reverse order + olist = self.object_list[:] + olist.reverse() + for o in olist: if o.type & objs: sql = o.get_drop_sql(curs) if not sql: @@ -462,7 +487,7 @@ class TableStruct(BaseStruct): self.object_list += self.seq_list # load additional objects - to_load = [TConstraint, TIndex, TTrigger, TRule, TGrant, TOwner] + to_load = [TConstraint, TIndex, TTrigger, TRule, TGrant, TOwner, TParent] for eclass in to_load: self.object_list += self._load_elem(curs, self.name, args, eclass) |