11import argparse
2+ import subprocess
23import time
34from contextlib import closing
45
@@ -51,21 +52,25 @@ def main() -> None:
5152 parser .add_argument (
5253 "--password" , dest = "password" , action = "store" , help = "mysql root password" , default = ""
5354 )
55+ parser .add_argument (
56+ "--backup" , dest = "backup" , action = "store" , help = "backup data before deleting to file"
57+ )
5458 parser .add_argument ("--dry-run" , dest = "dry_run" , action = "store_true" , help = "dry run" )
5559
5660 args = parser .parse_args ()
61+ sample_id = None
62+ count_sample_id = None
5763
5864 # ignore pyright checking as oracle bug in type signature of close() method
5965 with closing (
6066 mysql .connector .connect (
61- user = "root " , password = args . password , host = args .host , database = "archive"
67+ user = "report " , password = "$report" , host = args .host , database = "archive"
6268 ) # pyright: ignore
6369 ) as conn :
6470 # this is so we don't cache query results and keep getting the same answer
6571 conn .autocommit = True
6672
6773 with closing (conn .cursor (prepared = True )) as c :
68- c .execute ("SET SQL_LOG_BIN=0" ) # disable any binary logging for this session
6974 print (f"Looking for sample_id corresponding to { args .history } days ago" )
7075 c .execute (
7176 "SELECT MAX(sample_id) FROM sample WHERE smpl_time < TIMESTAMPADD(DAY, -?, NOW())" ,
@@ -82,19 +87,62 @@ def main() -> None:
8287 f"ID of last row to delete is { sample_id } and there are { count_sample_id } rows "
8388 f"-> { int (1 + count_sample_id / args .limit )} delete operations"
8489 )
90+
91+ if args .backup :
92+ command = [
93+ r"C:\Instrument\Apps\MySQL\bin\mysqldump.exe" ,
94+ "--user=root" ,
95+ f"--password={ args .password } " ,
96+ f"--host={ args .host } " ,
97+ "--single-transaction" ,
98+ f"--result-file={ args .backup } " ,
99+ "--no-create-db" ,
100+ "--no-create-info" ,
101+ "--skip-triggers" ,
102+ "--quick" ,
103+ f'--where="WHERE sample_id < { sample_id } "' ,
104+ "archive" ,
105+ "sample" ,
106+ ]
107+ if args .dry_run :
108+ print (command )
109+ else :
110+ subprocess .run (command , check = True )
111+
112+ # ignore pyright checking as oracle bug in type signature of close() method
113+ with closing (
114+ mysql .connector .connect (
115+ user = "root" , password = args .password , host = args .host , database = "archive"
116+ ) # pyright: ignore
117+ ) as conn :
118+ # this is so we don't cache query results and keep getting the same answer
119+ conn .autocommit = True
120+
121+ with closing (conn .cursor (prepared = True )) as c :
122+ c .execute ("SET SQL_LOG_BIN=0" ) # disable any binary logging for this session
123+ delete_ops = int (1 + count_sample_id / args .limit )
124+
125+ print (
126+ f"ID of last row to delete is { sample_id } and there are { count_sample_id } rows "
127+ f"-> { delete_ops } delete operations"
128+ )
85129 print (
86- f"This will take at least { args .sleep * count_sample_id / args . limit :.1f} "
130+ f"This will take at least { args .sleep * delete_ops :.1f} "
87131 "seconds based on sleep time alone"
88132 )
89133 if args .dry_run :
90134 print ("Exiting as dry-run" )
91135 return
92136 rowcount = 1
93137 it = 0
138+ progress = 0
94139 while rowcount > 0 :
95140 c .execute (f"DELETE FROM sample WHERE sample_id < { sample_id } LIMIT { args .limit } " )
96141 rowcount = c .rowcount
97- print (f"{ it % 10 } " , end = "" , flush = True )
142+ so_far = 100.0 * it / delete_ops
143+ if so_far > progress :
144+ print (f"{ progress } % " , end = "" , flush = True )
145+ progress += 5
98146 it += 1
99147 time .sleep (args .sleep )
100148 print ("" )
0 commit comments