tablecopy.py
changeset 34 98c7809af415
child 35 e7f79c4a27ce
equal deleted inserted replaced
33:bd0beda49bcb 34:98c7809af415
       
     1 #!/usr/bin/env python3.2
       
     2 #
       
     3 # Copy data from one table to another table with same schema.
       
     4 #
       
     5 
       
     6 import io
       
     7 
       
     8 from pgtoolkit import toolbase, pgmanager, pgdatacopy
       
     9 from pgtoolkit.progresswrapper import ProgressWrapper
       
    10 
       
    11 
       
    12 class TableCopyTool(toolbase.SrcDstTablesTool):
       
    13     def __init__(self):
       
    14         toolbase.SrcDstTablesTool.__init__(self, name='tablecopy', desc='Table copy tool.')
       
    15         
       
    16         self.parser.add_argument('-n', '--no-action', dest='noaction', action='store_true',
       
    17             help="Do nothing, just print tables to be copied. Useful in combination with --regex.")
       
    18         
       
    19         self.init()
       
    20 
       
    21     def main(self):
       
    22         srcconn = self.pgm.get_conn('src')
       
    23         dstconn = self.pgm.get_conn('dst')
       
    24         
       
    25         dc = pgdatacopy.PgDataCopy(srcconn, dstconn)
       
    26         
       
    27         for srcschema, srctable, dstschema, dsttable in self.tables():
       
    28             print('Copying [%s] %s.%s --> [%s] %s.%s' % (
       
    29                 self.args.src, srcschema, srctable,
       
    30                 self.args.dst, dstschema, dsttable))
       
    31             
       
    32             if self.args.noaction:
       
    33                 continue
       
    34             
       
    35             dc.set_source(srctable, srcschema)
       
    36             dc.set_destination(dsttable, dstschema)
       
    37         
       
    38             dc.check()
       
    39         
       
    40             buf = io.BytesIO()
       
    41             wrapped = ProgressWrapper(buf)
       
    42             dc.read(wrapped)
       
    43             data = buf.getvalue()
       
    44             buf.close()
       
    45             
       
    46             buf = io.BytesIO(data)
       
    47             wrapped = ProgressWrapper(buf, len(data))
       
    48             dc.write(wrapped)
       
    49             buf.close()
       
    50         
       
    51             dc.analyze()
       
    52 
       
    53 
       
    54 tool = TableCopyTool()
       
    55 tool.main()
       
    56