author | Radek Brich <radek.brich@devl.cz> |
Mon, 27 Feb 2012 15:12:40 +0100 | |
changeset 30 | a8b7cd92f39f |
parent 29 | 8636719a30f6 |
child 31 | c2e6e24b83d9 |
permissions | -rw-r--r-- |
2 | 1 |
import argparse |
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
2 |
import logging |
2 | 3 |
|
11
bc69eca59041
PgConsole: add app.py, splitted from pgconsole.py.
Radek Brich <radek.brich@devl.cz>
parents:
9
diff
changeset
|
4 |
from pgtoolkit import pgmanager, config |
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
5 |
from pgtoolkit.coloredformatter import ColoredFormatter |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
6 |
from pgtoolkit.highlight import highlight |
2 | 7 |
|
8 |
||
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
|
9 |
class ConnectionInfoNotFound(Exception): |
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
|
10 |
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
|
11 |
|
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
|
12 |
|
2 | 13 |
class ToolBase: |
14 |
def __init__(self, name, desc): |
|
15 |
self.parser = argparse.ArgumentParser(description=desc) |
|
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
16 |
self.parser.add_argument('-d', dest='debug', action='store_true', |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
17 |
help='Debug mode - print database queries.') |
2 | 18 |
|
19 |
self.config = config.ConfigParser() |
|
20 |
self.config.add_argument('databases', type=dict) |
|
21 |
self.config.add_argument('meta_db') |
|
22 |
self.config.add_argument('meta_query') |
|
23 |
||
24 |
self.pgm = pgmanager.get_instance() |
|
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
|
25 |
self.target_isolation_level = None |
2 | 26 |
|
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
|
27 |
def init(self, *args): |
2 | 28 |
self.config.load('pgtoolkit.conf') |
29 |
self.args = self.parser.parse_args() |
|
14
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
30 |
self.init_logging() |
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
|
31 |
self.prepare_conns(*args) |
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
|
32 |
|
14
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
33 |
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
|
34 |
# logging |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
35 |
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
|
36 |
format = ColoredFormatter(highlight(1,7,0)+'%(asctime)s %(levelname)-5s'+highlight(0)+' %(message)s', '%H:%M:%S') |
a900bc629ecc
TableDiffTool: add arguments to set different destination schema and table. PgDataDiff: fixes.
Radek Brich <radek.brich@devl.cz>
parents:
13
diff
changeset
|
37 |
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
|
38 |
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
|
39 |
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
|
40 |
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
|
41 |
self.log.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
|
42 |
|
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
43 |
if self.args.debug: |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
44 |
handler = logging.StreamHandler() |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
45 |
handler.setFormatter(format) |
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
46 |
handler.setLevel(logging.DEBUG) |
29 | 47 |
logger = logging.getLogger('pgmanager_sql') |
13
16dc5dec9c36
ToolBase: add -d parameter, which triggers debug mode (print SQL queries).
Radek Brich <radek.brich@devl.cz>
parents:
11
diff
changeset
|
48 |
logger.addHandler(handler) |
29 | 49 |
logger.setLevel(logging.DEBUG) |
2 | 50 |
|
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
|
51 |
def prepare_conn_from_metadb(self, 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
|
52 |
'''Create connection in pgmanager using 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
|
53 |
|
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
|
54 |
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
|
55 |
lookup_name -- Name of connection in 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
|
56 |
|
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
|
57 |
''' |
2 | 58 |
with self.pgm.cursor('meta') as curs: |
17
f768a3529ee7
ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
59 |
curs.execute(self.config.meta_query, [lookup_name]) |
2 | 60 |
row = curs.fetchone_dict() |
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
**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
|
66 |
return True |
2 | 67 |
|
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
|
68 |
def prepare_conn_from_config(self, 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
|
69 |
'''Create connection in pgmanager using info in 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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
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
|
74 |
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
|
75 |
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
|
76 |
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
|
77 |
|
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
|
78 |
def prepare_conns(self, *pgm_names): |
5 | 79 |
if self.config.meta_db: |
80 |
self.pgm.create_conn(name='meta', dsn=self.config.meta_db) |
|
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
|
81 |
|
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
|
82 |
for name in pgm_names: |
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
|
83 |
lookup_name = self.args.__dict__[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
|
84 |
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
|
85 |
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
|
86 |
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
|
87 |
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
|
88 |
raise ConnectionInfoNotFound('Connection name "%s" not found in config nor in meta DB.' % lookup_name) |
17
f768a3529ee7
ToolBase - implement 'database' config option.
Radek Brich <radek.brich@devl.cz>
parents:
14
diff
changeset
|
89 |
|
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
|
90 |
if 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
|
91 |
self.pgm.close_conn('meta') |
5 | 92 |
|
93 |
||
94 |
class SimpleTool(ToolBase): |
|
95 |
def __init__(self, name, desc): |
|
96 |
ToolBase.__init__(self, name, desc) |
|
97 |
self.parser.add_argument('target', metavar='target', type=str, help='Target database') |
|
98 |
||
99 |
def init(self): |
|
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 |
ToolBase.init(self, 'target') |
5 | 101 |
|
102 |
||
103 |
class SrcDstTool(ToolBase): |
|
104 |
def __init__(self, name, desc): |
|
105 |
ToolBase.__init__(self, name, desc) |
|
106 |
self.parser.add_argument('src', metavar='source', type=str, help='Source database') |
|
107 |
self.parser.add_argument('dst', metavar='destination', type=str, help='Destination database') |
|
108 |
||
109 |
def init(self): |
|
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
|
110 |
ToolBase.init(self, 'src', 'dst') |
5 | 111 |