Skip to content

Commit 2de9be2

Browse files
committed
Created dump_commands function for external use
1 parent d627537 commit 2de9be2

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

mysql/toolkit/components/execute.py

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -66,27 +66,33 @@ def execute_commands(self):
6666
print('\t' + str(self.success), 'successful commands')
6767

6868
def dump_fails(self):
69-
# Re-add semi-colon separator
70-
fails = [com + ';\n' for com in self.fail]
71-
print('\t' + str(len(fails)), 'failed commands')
72-
73-
# Create a directory to save fail SQL scripts
74-
fails_dir = os.path.join(os.path.dirname(self.sql_script), 'fails')
75-
if not os.path.exists(fails_dir):
76-
os.mkdir(fails_dir)
77-
fails_dir = os.path.join(fails_dir, datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H-%M-%S'))
78-
if not os.path.exists(fails_dir):
79-
os.mkdir(fails_dir)
80-
print('\tDumping failed commands to', fails_dir)
81-
82-
# Dump failed commands to text file in the same directory as the script
83-
for count, fail in tqdm(enumerate(fails), total=len(fails), desc='Dumping failed SQL commands to text'):
84-
fails_fname = str(os.path.basename(self.sql_script).rsplit('.')[0]) + str(count) + '.sql'
85-
txt_file = os.path.join(fails_dir, fails_fname)
86-
87-
# Dump to text file
88-
with open(txt_file, 'w') as txt:
89-
txt.writelines(fail)
69+
"""Dump failed commands to .sql files in the fails directory."""
70+
dump_commands(self.fail, self.sql_script)
71+
72+
73+
def dump_commands(commands, sql_script):
74+
"""Dump SQL commands to .sql files."""
75+
# Re-add semi-colon separator
76+
fails = [com + ';\n' for com in commands]
77+
print('\t' + str(len(fails)), 'failed commands')
78+
79+
# Create a directory to save fail SQL scripts
80+
fails_dir = os.path.join(os.path.dirname(sql_script), 'fails')
81+
if not os.path.exists(fails_dir):
82+
os.mkdir(fails_dir)
83+
fails_dir = os.path.join(fails_dir, datetime.fromtimestamp(time()).strftime('%Y-%m-%d %H-%M-%S'))
84+
if not os.path.exists(fails_dir):
85+
os.mkdir(fails_dir)
86+
print('\tDumping failed commands to', fails_dir)
87+
88+
# Dump failed commands to text file in the same directory as the script
89+
for count, fail in tqdm(enumerate(fails), total=len(fails), desc='Dumping failed SQL commands to text'):
90+
fails_fname = str(os.path.basename(sql_script).rsplit('.')[0]) + str(count) + '.sql'
91+
txt_file = os.path.join(fails_dir, fails_fname)
92+
93+
# Dump to text file
94+
with open(txt_file, 'w') as txt:
95+
txt.writelines(fail)
9096

9197

9298
def split_sql_commands(text):

0 commit comments

Comments
 (0)