PgDiff: Add support for indexes.
authorRadek Brich <radek.brich@devl.cz>
Tue, 24 Sep 2013 13:34:15 +0200
changeset 87 3ef717ee9253
parent 86 b61b54aa9f96
child 88 b8b2d28a7f35
PgDiff: Add support for indexes.
pgtoolkit/pgbrowser.py
pgtoolkit/pgdiff.py
--- a/pgtoolkit/pgbrowser.py	Fri Aug 16 13:19:26 2013 +0200
+++ b/pgtoolkit/pgbrowser.py	Tue Sep 24 13:34:15 2013 +0200
@@ -30,7 +30,7 @@
 
 class Column:
     def __init__(self, browser, table,
-        name, type, notnull, hasdefault, default, description):
+            name, type, notnull, hasdefault, default, description):
         self.browser = browser  # Browser instance
         self.table = table  # Table instance
         self.name = name
@@ -54,7 +54,8 @@
 
 class Index:
     def __init__(self, browser, table,
-        name, primary, unique, clustered, valid, definition):
+            name, primary, unique, clustered, valid, definition,
+            columns):
         self.browser = browser
         self.table = table
         self.name = name
@@ -63,6 +64,7 @@
         self.clustered = clustered
         self.valid = valid
         self.definition = definition
+        self.columns = columns
 
 
 class Table:
--- a/pgtoolkit/pgdiff.py	Fri Aug 16 13:19:26 2013 +0200
+++ b/pgtoolkit/pgdiff.py	Tue Sep 24 13:34:15 2013 +0200
@@ -252,6 +252,31 @@
         return out
 
 
+class DiffIndex(DiffBase):
+    def __init__(self, change, schema, table, index, definition, changes=None):
+        DiffBase.__init__(self)
+        self.level = 2
+        self.type = 'index'
+        self.change = change
+        self.schema = schema
+        self.table = table
+        self.index = index
+        self.name = index
+        self.definition = definition
+        self.changes = changes
+
+    def format_patch(self):
+        q_drop = 'DROP INDEX %s;' % (self.index,)
+        q_add = '%s;' % (self.definition,)
+        if self.change == '*':
+            out = [q_drop, q_add]
+        if self.change == '+':
+            out = [q_add]
+        if self.change == '-':
+            out = [q_drop]
+        return out
+
+
 class DiffType(DiffBase):
     def __init__(self, change, schema, name):
         DiffBase.__init__(self)
@@ -319,6 +344,12 @@
             diff.append(('definition', a.definition, b.definition))
         return diff
 
+    def _compare_indexes(self, a, b):
+        diff = []
+        if a.definition != b.definition:
+            diff.append(('definition', a.definition, b.definition))
+        return diff
+
     def _compare_functions(self, a, b):
         diff = []
         if a.result != b.result:
@@ -387,6 +418,23 @@
             else:
                 yield cdo
 
+    def _diff_indexes(self, schema, table, src_indexes, dst_indexes):
+        for nd in self._diff_names(src_indexes, dst_indexes):
+            if nd[1] in dst_indexes:
+                dst_definition = dst_indexes[nd[1]].definition
+            else:
+                dst_definition = None
+            ido = DiffIndex(change=nd[0], schema=schema, table=table, index=nd[1],
+                definition=dst_definition)
+            if nd[0] == '*':
+                a = src_indexes[nd[1]]
+                b = dst_indexes[nd[1]]
+                ido.changes = self._compare_indexes(a, b)
+                if ido.changes:
+                    yield ido
+            else:
+                yield ido
+
     def _diff_tables(self, schema, src_tables, dst_tables):
         for nd in self._diff_names(src_tables, dst_tables):
             if not self._test_table(nd[1]):
@@ -409,6 +457,14 @@
                         yield tdo
                         tdo = None
                     yield cdo
+                # indexes
+                src_indexes = src_tables[nd[1]].indexes
+                dst_indexes = dst_tables[nd[1]].indexes
+                for ido in self._diff_indexes(schema, nd[1], src_indexes, dst_indexes):
+                    if tdo:
+                        yield tdo
+                        tdo = None
+                    yield ido
             else:
                 yield tdo