forked from TonicAI/condenser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirect_subset.py
68 lines (53 loc) · 2.45 KB
/
direct_subset.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import uuid, sys
import config_reader, result_tabulator
import time
from subset import Subset
from psql_database_creator import PsqlDatabaseCreator
from mysql_database_creator import MySqlDatabaseCreator
from db_connect import DbConnect
from subset_utils import print_progress
import database_helper
def db_creator(db_type, source, dest):
if db_type == 'postgres':
return PsqlDatabaseCreator(source, dest, False)
elif db_type == 'mysql':
return MySqlDatabaseCreator(source, dest)
else:
raise ValueError('unknown db_type ' + db_type)
if __name__ == '__main__':
if "--stdin" in sys.argv:
config_reader.initialize(sys.stdin)
else:
config_reader.initialize()
db_type = config_reader.get_db_type()
source_dbc = DbConnect(db_type, config_reader.get_source_db_connection_info())
destination_dbc = DbConnect(db_type, config_reader.get_destination_db_connection_info())
database = db_creator(db_type, source_dbc, destination_dbc)
database.teardown()
database.create()
# Get list of tables to operate on
db_helper = database_helper.get_specific_helper()
all_tables = db_helper.list_all_tables(source_dbc)
all_tables = [x for x in all_tables if x not in config_reader.get_excluded_tables()]
subsetter = Subset(source_dbc, destination_dbc, all_tables)
try:
subsetter.prep_temp_dbs()
subsetter.run_middle_out()
print("Beginning pre constraint SQL calls")
start_time = time.time()
for idx, sql in enumerate(config_reader.get_pre_constraint_sql()):
print_progress(sql, idx+1, len(config_reader.get_pre_constraint_sql()))
db_helper.run_query(sql, destination_dbc.get_db_connection())
print("Completed pre constraint SQL calls in {}s".format(time.time()-start_time))
print("Adding database constraints")
if "--no-constraints" not in sys.argv:
database.add_constraints()
print("Beginning post subset SQL calls")
start_time = time.time()
for idx, sql in enumerate(config_reader.get_post_subset_sql()):
print_progress(sql, idx+1, len(config_reader.get_post_subset_sql()))
db_helper.run_query(sql, destination_dbc.get_db_connection())
print("Completed post subset SQL calls in {}s".format(time.time()-start_time))
result_tabulator.tabulate(source_dbc, destination_dbc, all_tables)
finally:
subsetter.unprep_temp_dbs()