pgtoolkit/pgdatadiff.py
changeset 27 5fb4883604d6
parent 14 a900bc629ecc
child 31 c2e6e24b83d9
equal deleted inserted replaced
26:7f219da7ab71 27:5fb4883604d6
    32 class DiffData:
    32 class DiffData:
    33     COLORS = {
    33     COLORS = {
    34         '+' : BOLD | GREEN,
    34         '+' : BOLD | GREEN,
    35         '-' : BOLD | RED,
    35         '-' : BOLD | RED,
    36         '*' : BOLD | YELLOW,
    36         '*' : BOLD | YELLOW,
    37         'V' : BOLD | WHITE}
    37         'V' : BOLD | WHITE,
       
    38         'K' : BOLD | BLUE}
    38     
    39     
    39     def __init__(self, change, cols1, cols2, id=None):
    40     def __init__(self, change, cols1, cols2, id=None):
    40         self.change = change
    41         self.change = change
    41         self.cols1 = cols1
    42         self.cols1 = cols1
    42         self.cols2 = cols2
    43         self.cols2 = cols2
    43         self.id = id
    44         self.id = id
    44     
    45     
    45     def format(self):
    46     def format(self):
    46         out = []
    47         out = []
    47         
    48                 
    48         out.append(highlight(1, self.COLORS[self.change]))
    49         out.append(highlight(1, self.COLORS[self.change]))
    49         out.extend([self.change, ' '])
    50         out.extend([self.change, ' '])
    50         
    51         
    51         out.extend(self._format_changes())
    52         out.extend(self._format_changes())
    52         
    53         
    60             '-' : self._format_delete,
    61             '-' : self._format_delete,
    61             '*' : self._format_update}
    62             '*' : self._format_update}
    62         
    63         
    63         return method[self.change](table)
    64         return method[self.change](table)
    64 
    65 
    65     def _format_changes(self):
    66     def _format_changes(self):        
    66         if self.cols1 and not self.cols2:
    67         if self.cols1 and not self.cols2:
    67             return [', '.join([self._format_value_del(*x) for x in self.cols1.items()])]
    68             return [', '.join([self._format_value_del(*x) for x in self.cols1.items()])]
    68         if not self.cols1 and self.cols2:
    69         if not self.cols1 and self.cols2:
    69             return [', '.join([self._format_value_add(*x) for x in self.cols2.items()])]
    70             return [', '.join([self._format_value_add(*x) for x in self.cols2.items()])]
    70         
    71         
       
    72         out = []        
       
    73         if self.id:
       
    74             out.extend([highlight(1, self.COLORS['*']), self.id[0], ': ', highlight(0), self.id[1], ', '])
       
    75 
    71         items = []
    76         items = []
    72         for i in range(len(self.cols1)):
    77         for i in range(len(self.cols1)):
    73             items.append((
    78             items.append((
    74                 list(self.cols1.keys())[i],
    79                 list(self.cols1.keys())[i],
    75                 list(self.cols1.values())[i],
    80                 list(self.cols1.values())[i],
    76                 list(self.cols2.values())[i]))
    81                 list(self.cols2.values())[i]))
    77             
    82         out.extend([', '.join([self._format_value_change(*x) for x in items])])
    78         return [', '.join([self._format_value_change(*x) for x in items])]
    83         
       
    84         return out
    79 
    85 
    80     def _format_value_del(self, k, v):
    86     def _format_value_del(self, k, v):
    81         fs = (highlight(1, self.COLORS['-']) + '{}: ' + highlight(0) + '{}')
    87         fs = (highlight(1, self.COLORS['-']) + '{}: ' + highlight(0) + '{}')
    82         return fs.format(k, v)
    88         return fs.format(k, v)
    83 
    89 
   189             print(ln.format_patch(self.fulltable1))
   195             print(ln.format_patch(self.fulltable1))
   190 
   196 
   191     def _select(self):
   197     def _select(self):
   192         browser = pgbrowser.PgBrowser(self.conn1)
   198         browser = pgbrowser.PgBrowser(self.conn1)
   193         columns = browser.list_columns(schema=self.schema1, table=self.table1, order=1)
   199         columns = browser.list_columns(schema=self.schema1, table=self.table1, order=1)
       
   200         if not columns:
       
   201             raise Exception('Table %s.%s not found.' % (self.schema1, self.table1))
   194         columns_sel = ', '.join(['"' + x['name'] + '"' for x in columns])
   202         columns_sel = ', '.join(['"' + x['name'] + '"' for x in columns])
   195         self.colnames = [x['name'] for x in columns]
   203         self.colnames = [x['name'] for x in columns]
   196         
   204         
   197         query1 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable1 + ' ORDER BY 1;'
   205         query1 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable1 + ' ORDER BY 1;'
   198         query2 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable2 + ' ORDER BY 1;'
   206         query2 = 'SELECT ' + columns_sel + ' FROM ' + self.fulltable2 + ' ORDER BY 1;'