Skip to content

Commit 65621e8

Browse files
committed
table builder CLI options
1 parent 674403b commit 65621e8

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed

main.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,17 @@ def __init__(self):
3333
# mp.set_start_method('forkserver')
3434

3535
def run(self):
36-
pumper = Pumper(
37-
reader=self.config.reader(),
38-
writer=self.config.writer(),
39-
)
40-
pumper.run()
36+
if self.config.is_table_templates():
37+
templates = self.config.table_builder().templates()
38+
for db in templates:
39+
for table in templates[db]:
40+
print(db, ':', table, ':', templates[db][table])
41+
else:
42+
pumper = Pumper(
43+
reader=self.config.reader(),
44+
writer=self.config.writer(),
45+
)
46+
pumper.run()
4147

4248
def start(self):
4349
if self.config.is_daemon():

src/cliopts.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ def config():
146146
action='store_true',
147147
help='Keep CSV pool files. Useful for debugging'
148148
)
149+
argparser.add_argument(
150+
'--table-templates',
151+
action='store_true',
152+
help='Prepare table templates.'
153+
)
149154

150155
argparser.add_argument(
151156
'--src-server-id',
@@ -262,12 +267,14 @@ def config():
262267

263268
# build options
264269
return Config ({
270+
265271
'app-config': {
266272
'config-file': args.config_file,
267273
'log-file': args.log_file,
268274
'log-level': CLIOpts.log_level_from_string(args.log_level),
269275
'dry': args.dry,
270276
'daemon': args.daemon,
277+
'table-templates': args.table_templates,
271278
'pid_file': args.pid_file,
272279
'mempool': args.mempool or args.csvpool, # csvpool assumes mempool to be enabled
273280
'mempool-max-events-num': args.mempool_max_events_num,
@@ -284,6 +291,15 @@ def config():
284291
},
285292
},
286293

294+
'tablebuilder-config': {
295+
'host': args.src_host,
296+
'port': args.src_port,
297+
'user': args.src_user,
298+
'password': args.src_password,
299+
'dbs': [x for x in args.src_only_schemas.split(',') if x] if args.src_only_schemas else None,
300+
'tables': [x for x in args.src_only_tables.split(',') if x] if args.src_only_tables else None,
301+
},
302+
287303
'reader-config': {
288304
'mysql': {
289305
'connection_settings': {

src/config.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from .converter.csvwriteconverter import CSVWriteConverter
1515
from .converter.chwriteconverter import CHWriteConverter
16-
16+
from .tablebuilder import TableBuilder
1717

1818
class Config(object):
1919

@@ -46,6 +46,12 @@ def is_daemon(self):
4646
def is_pool(self):
4747
return self.config['app-config']['mempool']
4848

49+
def is_table_templates(self):
50+
return self.config['app-config']['table-templates']
51+
52+
def table_builder(self):
53+
return TableBuilder(**self.config['tablebuilder-config'])
54+
4955
def reader(self):
5056
if self.config['reader-config']['file']['csv_file_path']:
5157
return CSVReader(**self.config['reader-config']['file'])

src/tablebuilder.py

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,74 @@ class TableBuilder(object):
1010
connection = None
1111
cursor = None
1212

13-
def templates(self, host, user, password=None, db=None, tables=None):
13+
host = None
14+
port = None
15+
user = None
16+
password = None
17+
dbs = None
18+
tables = None
19+
20+
def __init__(self, host, port, user, password=None, dbs=None, tables=None):
21+
self.host = host
22+
self.port = port
23+
self.user = user
24+
self.password = password
25+
self.dbs = dbs
26+
self.tables = tables
27+
28+
def templates(self):
1429
"""
1530
Create templates for specified MySQL tables. In case no tables specified all tables from specified db are templated
1631
1732
:param host: string MySQL host
1833
:param user: string MySQL user
1934
:param password: string MySQL password
20-
:param db: string MySQL datatabse/ May be omitted, in this case tables has to contain full table names, Ex.: db.table1
21-
:param tables: string|list either comma-separated string or list of table names. May be short (in case db specified) or full (in the form db.table, in case no db specified)
35+
:param dbs: list of string MySQL datatabse/ May be omitted, in this case tables has to contain full table names, Ex.: db.table1
36+
:param tables: list of string list of table names. May be short (in case db specified) or full (in the form db.table, in case no db specified)
2237
:return: dict of CREATE TABLE () templates
2338
"""
2439
res = {}
2540

41+
db = None
42+
43+
try:
44+
db = self.dbs[0]
45+
except:
46+
pass
47+
2648
# sanity check
27-
if db is None and tables is None:
49+
if db is None and self.tables is None:
2850
return res
2951

3052
# MySQL connections
3153
self.connection = MySQLdb.connect(
32-
host=host,
33-
user=user,
34-
passwd=password,
54+
host=self.host,
55+
user=self.user,
56+
passwd=self.password,
3557
db=db,
3658
)
3759
self.cursor = self.connection.cursor()
3860

3961
# in case to tables specified - list all tables of the DB specified
40-
if db is not None and tables is None:
62+
if db is not None and self.tables is None:
4163
self.cursor.execute("USE " + db)
42-
tables = []
64+
self.tables = []
4365
self.cursor.execute("SHOW TABLES") # execute 'SHOW TABLES' (but data is not returned)
4466
for (table_name,) in self.cursor:
45-
tables.append(table_name)
46-
47-
# tables can be something like 'db1, db2, db3'
48-
# make [db1, db2, db3]
49-
if isinstance(tables, str):
50-
tables = [table.strip() for table in tables.split(',')]
67+
self.tables.append(table_name)
5168

5269
# create dict of table templates
53-
for table in tables:
54-
res[table] = self.create_table_template(table, db)
55-
56-
# {'table1': 'CREATE TABLE(...)...', 'table2': 'CREATE TABLE(...)...'}
70+
for table in self.tables:
71+
if not db in res:
72+
res[db] = {}
73+
res[db][table] = self.create_table_template(table, db)
74+
75+
# {
76+
# 'db': {
77+
# 'table1': 'CREATE TABLE(...)...',
78+
# 'table2': 'CREATE TABLE(...)...',
79+
# }
80+
# }
5781
return res
5882

5983
def create_table_template(self, table_name, db=None):

0 commit comments

Comments
 (0)