author | Radek Brich <brich.radek@ifortuna.cz> |
Tue, 06 May 2014 18:37:43 +0200 | |
changeset 101 | 2a2d0d5df03b |
parent 100 | d6088dba8fea |
child 102 | fda45bdfd68d |
permissions | -rw-r--r-- |
100
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
1 |
from pgtoolkit import pgmanager, pgbrowser |
2 | 2 |
|
83
515fadd3d286
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
3 |
from pycolib.configparser import ConfigParser |
515fadd3d286
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
4 |
from pycolib.coloredformatter import ColoredFormatter |
515fadd3d286
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
5 |
from pycolib.ansicolor import highlight |
515fadd3d286
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
6 |
|
100
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
7 |
import argparse |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
8 |
import logging |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
9 |
import re |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
10 |
import textwrap |
2 | 11 |
|
12 |
||
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
13 |
class ConnectionInfoNotFound(Exception): |
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
14 |
pass |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
15 |
|
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
16 |
|
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
17 |
class BadArgsError(Exception): |
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
18 |
pass |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
19 |
|
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
20 |
|
100
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
21 |
class ToolDescriptionFormatter(argparse.HelpFormatter): |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
22 |
"""Help message formatter which retains any formatting in descriptions.""" |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
23 |
|
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
24 |
def _fill_text(self, text, width, indent): |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
25 |
return textwrap.dedent(text) |
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
26 |
|
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
27 |
|
2 | 28 |
class ToolBase: |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
29 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
30 |
def __init__(self, name, desc=None, **kwargs): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
31 |
self.config = ConfigParser() |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
32 |
self.parser = argparse.ArgumentParser(prog=name, description=desc or self.__doc__, |
100
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
33 |
formatter_class=ToolDescriptionFormatter) |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
34 |
self.pgm = pgmanager.get_instance() |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
35 |
self.target_isolation_level = None |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
36 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
37 |
def setup(self, args=None): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
38 |
self.specify_args() |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
39 |
self.load_args(args) |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
40 |
self.init_logging() |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
41 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
42 |
def specify_args(self): |
84 | 43 |
self.config.add_option('databases', dict) |
83
515fadd3d286
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
44 |
self.config.add_option('meta_db') |
515fadd3d286
Add dependency on pycolib. Move common modules to pycolib. Add example table schema for meta DB.
Radek Brich <radek.brich@devl.cz>
parents:
78
diff
changeset
|
45 |
self.config.add_option('meta_query') |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
46 |
self.parser.add_argument('-Q', dest='queries', action='store_true', |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
47 |
help='Print database queries.') |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
48 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
49 |
def load_args(self, args=None, config_file=None): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
50 |
self.config.load(config_file or 'pgtoolkit.conf') |
100
d6088dba8fea
Add pgtool wrapper for all tools. Only this script will be installed into system bin.
Radek Brich <brich.radek@ifortuna.cz>
parents:
84
diff
changeset
|
51 |
self.args = self.parser.parse_args(args) |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
52 |
|
14
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
53 |
def init_logging(self): |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
54 |
# logging |
62
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
55 |
format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S') |
14
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
56 |
handler = logging.StreamHandler() |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
57 |
handler.setFormatter(format) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
58 |
handler.setLevel(logging.DEBUG) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
59 |
self.log = logging.getLogger('main') |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
60 |
self.log.addHandler(handler) |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
61 |
self.log.setLevel(logging.DEBUG) |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
62 |
|
62
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
63 |
log_notices = logging.getLogger('pgmanager_notices') |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
64 |
log_notices.addHandler(handler) |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
65 |
log_notices.setLevel(logging.DEBUG) |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
66 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
67 |
if self.args.queries: |
62
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
68 |
log_sql = logging.getLogger('pgmanager_sql') |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
69 |
log_sql.addHandler(handler) |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
70 |
log_sql.setLevel(logging.DEBUG) |
2 | 71 |
|
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
72 |
def prepare_conn_from_metadb(self, name, lookup_name): |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
73 |
"""Create connection in pgmanager using meta DB. |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
74 |
|
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
75 |
name -- Name for connection in pgmanager. |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
76 |
lookup_name -- Name of connection in meta DB. |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
77 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
78 |
""" |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
79 |
if not self.pgm.knows_conn('meta'): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
80 |
self.pgm.create_conn(name='meta', dsn=self.config.meta_db) |
2 | 81 |
with self.pgm.cursor('meta') as curs: |
17
f768a3529ee7
ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
82 |
curs.execute(self.config.meta_query, [lookup_name]) |
2 | 83 |
row = curs.fetchone_dict() |
84 |
curs.connection.commit() |
|
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
85 |
if row: |
30
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
86 |
self.pgm.create_conn(name=name, |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
87 |
isolation_level=self.target_isolation_level, |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
88 |
**row) |
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
89 |
return True |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
90 |
self.pgm.close_conn('meta') |
2 | 91 |
|
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
92 |
def prepare_conn_from_config(self, name, lookup_name): |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
93 |
"""Create connection in pgmanager using info in config.databases.""" |
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
94 |
if self.config.databases: |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
95 |
if lookup_name in self.config.databases: |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
96 |
dsn = self.config.databases[lookup_name] |
30
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
97 |
self.pgm.create_conn(name=name, |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
98 |
isolation_level=self.target_isolation_level, |
a8b7cd92f39f
Fix analyzeall tool. Add user output formating to batchquery tool. Add isolation_level setting support to ToolBase.
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
99 |
dsn=dsn) |
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
100 |
return True |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
101 |
|
62
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
102 |
def prepare_conns(self, **kwargs): |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
103 |
"""Create connections in PgManager. |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
104 |
|
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
105 |
Keyword arguments meaning: |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
106 |
key: connection name for use in PgManager |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
107 |
value: connection name in config or meta DB |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
108 |
|
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
109 |
""" |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
110 |
for name in kwargs: |
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
111 |
lookup_name = kwargs[name] |
20
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
112 |
found = self.prepare_conn_from_config(name, lookup_name) |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
113 |
if not found and self.config.meta_db: |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
114 |
found = self.prepare_conn_from_metadb(name, lookup_name) |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
115 |
if not found: |
73f0d53fef6b
PgManager: Do not add NullHandler to logger. Rewrite get_instance(). ToolBase: fix prepare_conns() method.
Radek Brich <radek.brich@devl.cz>
parents:
17
diff
changeset
|
116 |
raise ConnectionInfoNotFound('Connection name "%s" not found in config nor in meta DB.' % lookup_name) |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
117 |
|
5 | 118 |
|
119 |
class SimpleTool(ToolBase): |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
120 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
121 |
def __init__(self, name, desc=None, **kwargs): |
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
122 |
ToolBase.__init__(self, name, desc, **kwargs) |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
123 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
124 |
def specify_args(self): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
125 |
ToolBase.specify_args(self) |
5 | 126 |
self.parser.add_argument('target', metavar='target', type=str, help='Target database') |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
127 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
128 |
def setup(self, args=None): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
129 |
ToolBase.setup(self, args) |
62
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
130 |
self.prepare_conns(target=self.args.target) |
5 | 131 |
|
132 |
||
133 |
class SrcDstTool(ToolBase): |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
134 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
135 |
def __init__(self, name, desc=None, *, allow_reverse=False, force_reverse=False, **kwargs): |
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
136 |
ToolBase.__init__(self, name, desc, **kwargs) |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
137 |
self.allow_reverse = allow_reverse |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
138 |
self.force_reverse = force_reverse |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
139 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
140 |
def specify_args(self): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
141 |
ToolBase.specify_args(self) |
5 | 142 |
self.parser.add_argument('src', metavar='source', type=str, help='Source database') |
143 |
self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database') |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
144 |
if self.allow_reverse: |
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
145 |
self.parser.add_argument('-r', '--reverse', action='store_true', help='Reverse operation. Swap source and destination.') |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
146 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
147 |
def load_args(self, args=None, config_file=None): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
148 |
ToolBase.load_args(self, args, config_file) |
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
149 |
if self.is_reversed(): |
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
150 |
self.args.src, self.args.dst = self.args.dst, self.args.src |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
151 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
152 |
def setup(self, args=None): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
153 |
ToolBase.setup(self, args) |
62
af637235ca81
Update loopquery: allow any number of queries, support reading parameters from config file.
Radek Brich <radek.brich@devl.cz>
parents:
56
diff
changeset
|
154 |
self.prepare_conns(src=self.args.src, dst=self.args.dst) |
5 | 155 |
|
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
156 |
def is_reversed(self): |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
157 |
return ('reverse' in self.args and self.args.reverse) or self.force_reverse |
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
158 |
|
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
159 |
|
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
160 |
class SrcDstTablesTool(SrcDstTool): |
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
161 |
|
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
162 |
def specify_args(self): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
163 |
SrcDstTool.specify_args(self) |
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
164 |
self.parser.add_argument('-t', '--src-table', metavar='source_table', |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
165 |
dest='srctable', type=str, default='', help='Source table name.') |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
166 |
self.parser.add_argument('-s', '--src-schema', metavar='source_schema', |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
167 |
dest='srcschema', type=str, default='', help='Source schema name (default=public).') |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
168 |
self.parser.add_argument('--dst-table', metavar='destination_table', |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
169 |
dest='dsttable', type=str, default='', help='Destination table name (default=source_table).') |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
170 |
self.parser.add_argument('--dst-schema', metavar='destination_schema', |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
171 |
dest='dstschema', type=str, default='', help='Destination schema name (default=source_schema).') |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
172 |
self.parser.add_argument('--regex', action='store_true', help="Use RE in schema or table name.") |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
173 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
174 |
def load_args(self, args=None, config_file=None): |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
175 |
SrcDstTool.load_args(self, args, config_file) |
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
176 |
self.load_table_names() |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
177 |
|
101
2a2d0d5df03b
Refactor ToolBase to allow tool composition. Add TableSync tool (composited). Move more tools under pgtool.
Radek Brich <brich.radek@ifortuna.cz>
parents:
100
diff
changeset
|
178 |
def load_table_names(self): |
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
179 |
self.schema1 = self.args.srcschema |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
180 |
self.table1 = self.args.srctable |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
181 |
self.schema2 = self.args.dstschema |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
182 |
self.table2 = self.args.dsttable |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
183 |
|
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
184 |
# check regex - it applies to source name, dest name must not be specified |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
185 |
# applies to only one - schema or table name |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
186 |
if self.args.regex: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
187 |
if self.table2 or (self.schema2 and not self.table1): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
188 |
raise BadArgsError('Cannot specify both --regex and --dst-schema, --dst-table.') |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
189 |
# schema defaults to public |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
190 |
if self.table1 and not self.schema1: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
191 |
self.schema1 = 'public' |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
192 |
# dest defaults to source |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
193 |
if not self.schema2: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
194 |
self.schema2 = self.schema1 |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
195 |
if not self.table2: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
196 |
self.table2 = self.table1 |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
197 |
|
56
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
198 |
# swap src, dst when in reverse mode |
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
199 |
if self.is_reversed(): |
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
200 |
self.schema1, self.schema2 = self.schema2, self.schema1 |
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
201 |
self.table1, self.table2 = self.table2, self.table1 |
94e091c23ebb
Add reverse parameter for diff tools.
Radek Brich <radek.brich@devl.cz>
parents:
51
diff
changeset
|
202 |
|
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
203 |
def tables(self): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
204 |
'''Generator. Yields schema1, table1, schema2, table2.''' |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
205 |
srcconn = self.pgm.get_conn('src') |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
206 |
try: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
207 |
srcbrowser = pgbrowser.PgBrowser(srcconn) |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
208 |
if self.args.regex: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
209 |
if not self.table1: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
210 |
# all tables from schemas defined by regex |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
211 |
for item in self._iter_schemas_regex(srcbrowser, self.schema1): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
212 |
yield item |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
213 |
else: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
214 |
# all tables defined by regex |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
215 |
for item in self._iter_tables_regex(srcbrowser, self.schema1, self.schema2, self.table1): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
216 |
yield item |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
217 |
else: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
218 |
if not self.table1: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
219 |
if not self.schema1: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
220 |
# all tables from all schemas |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
221 |
for item in self._iter_schemas_regex(srcbrowser, self.schema1): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
222 |
yield item |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
223 |
else: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
224 |
# all tables from specified schema |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
225 |
for item in self._iter_tables_regex(srcbrowser, self.schema1, self.schema2, self.table1): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
226 |
yield item |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
227 |
else: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
228 |
# one table |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
229 |
yield (self.schema1, self.table1, self.schema2, self.table2) |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
230 |
finally: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
231 |
self.pgm.put_conn(srcconn, 'src') |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
232 |
|
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
233 |
def _iter_schemas_regex(self, browser, regex): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
234 |
for schema in browser.list_schemas(): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
235 |
if schema['system']: |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
236 |
continue |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
237 |
schemaname = schema['name'] |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
238 |
if re.match(regex, schemaname): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
239 |
for item in self._iter_tables_regex(browser, schemaname, schemaname, ''): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
240 |
yield item |
51
bdc44f96cb0b
Add loopquery tool. Log notices to console from all tools (toolbase).
Radek Brich <radek.brich@devl.cz>
parents:
34
diff
changeset
|
241 |
|
34
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
242 |
def _iter_tables_regex(self, browser, schema1, schema2, regex): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
243 |
for table in browser.list_tables(schema1): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
244 |
tablename = table['name'] |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
245 |
if re.match(regex, tablename): |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
246 |
yield (schema1, tablename, schema2, tablename) |
98c7809af415
Add PgDataCopy. Add TableCopyTool.Add SrcDstTablesTool class to toolbase, use in tablecopy, tablediff.
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
247 |