-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbackup.py
806 lines (608 loc) · 26.1 KB
/
backup.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
"""
POSTGRES backup script.
- Creates and restores backups.
- Check against data corruption with analyze, vacuum and reindex
What to do:
- run vacuum, which shows problems with data
- if any table is affected run reindex on it
- run reindex from time to time
"""
import sys
import os
import subprocess
import argparse
from pathlib import Path
import time
from datetime import datetime
from sqlalchemy import create_engine, Column, String, Integer, MetaData, Table, text, LargeBinary, DateTime, select
from sqlalchemy.dialects.postgresql.types import BYTEA
from sqlalchemy.orm import sessionmaker
from utils import ReflectedTable
from workspace import get_workspaces
parent_directory = Path(__file__).parents[1]
def get_backup_directory(export_type):
return parent_directory / "data" / ("backup_" + export_type)
def get_workspace_backup_directory(export_type, workspace):
return get_backup_directory(export_type) / workspace
def run_pg_dump_backup(run_info):
workspace = run_info["workspace"]
tables = run_info["tables"]
output_file = run_info["output_file"]
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
if "format" not in run_info:
run_info["format"] = "custom"
if run_info["format"] == "custom":
format_args = "c"
elif run_info["format"] == "plain" or run_info["format"] == "sql":
format_args = "p"
command_input = [
"pg_dump",
"-h", host,
"-U", user,
"-d", database,
"-F", format_args,
"-f", output_file,
"--data-only",
]
if "format" in run_info and run_info["format"] == "sql":
command_input.append("--inserts")
for table in tables:
command_input.append("-t")
command_input.append(table)
operating_dir = get_workspace_backup_directory(run_info["format"], workspace)
operating_dir.mkdir(parents=True, exist_ok=True)
print("Running: {} @ {}".format(command_input, operating_dir))
try:
result = subprocess.run(command_input, cwd=str(operating_dir), check=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
print("Backup completed successfully.")
except subprocess.CalledProcessError as e:
print("An error occurred:", e)
print("Standard Output:", e.stdout)
print("Standard Error:", e.stderr)
return False
return True
def truncate_table(run_info, table):
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
tables = run_info["tables"]
print("Truncating {}".format(table))
sql = f'TRUNCATE TABLE {table} CASCADE;'
command = [
'psql',
"-h", host,
"-U", user,
"-d", database,
'-c', sql,
]
try:
subprocess.run(command, check=True)
print("Table truncated successfully.")
except subprocess.CalledProcessError as e:
print("An error occurred:", e)
return False
return True
def reset_table_index_sequence(run_info, table):
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
tables = run_info["tables"]
print("Resetting sequence for table {}".format(table))
sql = f"SELECT setval('{table}_id_seq', COALESCE((SELECT MAX(id) FROM {table}), 1));"
command = [
'psql',
"-h", host,
"-U", user,
"-d", database,
'-c', sql,
]
try:
subprocess.run(command, check=True)
print("Reset index sequence successfully.")
except subprocess.CalledProcessError as e:
print("An error occurred:", e)
return False
return True
def reset_tables_index_sequence(tablemapping, run_info):
workspace = run_info["workspace"]
# after restore we need to reset sequences
for item in tablemapping:
key = item[0]
tables = item[1]
run_info["output_file"] = key
run_info["tables"] = tables
for table in tables:
table = table.replace("instance_", workspace+"_")
if not reset_table_index_sequence(run_info, table):
print("Could not reset index in table")
return False
return True
def run_table_sql(run_info, table, sql):
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
print("Call {} initiating".format(table))
command = [
'psql',
"-h", host,
"-U", user,
"-d", database,
'-c', sql,
]
try:
subprocess.run(command, check=True)
print("Call {} successful.".format(sql))
except subprocess.CalledProcessError as e:
print("An error occurred:", e)
return False
return True
def truncate_all(run_info):
workspace = run_info["workspace"]
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
tables = run_info["tables"]
for table in tables:
table = table.replace("instance_", workspace+"_")
if not truncate_table(run_info, table):
return False
return True
def run_pg_restore(run_info):
workspace = run_info["workspace"]
tables = run_info["tables"]
output_file = run_info["output_file"]
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
if "format" not in run_info:
run_info["format"] = "custom"
if run_info["format"] == "custom":
format_args = "c"
elif run_info["format"] == "plain":
format_args = "p"
command_input = [
"pg_restore",
"-h", host,
"-U", user,
"-d", database,
"-F", format_args,
"--data-only",
output_file,
]
for table in tables:
command_input.append("-t")
command_input.append(table)
operating_dir = get_workspace_backup_directory(run_info["format"], workspace)
operating_dir.mkdir(parents=True, exist_ok=True)
print("Running: {} @ {}".format(command_input, operating_dir))
try:
subprocess.run(command_input, cwd = str(operating_dir), check=True)
print("Restore completed successfully.")
except subprocess.CalledProcessError as e:
print("An error occurred:", e)
return False
return True
### SQLite code
def create_destionation_table(table_name, source_table, destination_engine):
"""
Copy columns from postgres to sqlite
BYTEA is not represented in sqlite
"""
with destination_engine.connect() as connection:
if not destination_engine.dialect.has_table(connection, table_name):
columns = []
for column in source_table.columns:
# Mapping bytea to LargeBinary (or appropriate type)
if column.type.__class__ == BYTEA:
columns.append(Column(column.name, LargeBinary, nullable=column.nullable))
else:
columns.append(Column(column.name, column.type, nullable=column.nullable))
# For debugging purposes, you can print column details
# print(column.name)
# print(column.type.__class__)
# print(f"Nullable: {column.nullable}")
destination_metadata = MetaData()
destination_table = Table(table_name, destination_metadata, *columns)
destination_table.create(destination_engine)
def get_engine_table(workspace, table_name, engine, with_workspace=True):
if with_workspace:
this_tables_name = "{}_{}".format(workspace, table_name)
else:
this_tables_name = table_name
engine_metadata = MetaData()
engine_table = Table(this_tables_name, engine_metadata, autoload_with=engine)
return engine_table
def get_table_row_values(row, source_table):
data = {}
for column in source_table.columns:
value = getattr(row, column.name)
data[column.name] = value
return data
def is_row_with_id(connection, table, id_value):
try:
existing = connection.execute(
select(table.c.id).where(table.c.id == id_value)
).first()
return existing
except Exception as E:
connection.rollback()
return False
def copy_table(instance, table_name, source_engine, destination_engine, override=False, to_sqlite=True, commit_every_row=False):
"""
Copies table from postgres to destination
@param override If true, will work faster, since it does not check if row with id exists
@param to_sqlite If to SQLlite then destination table names will not include workspace. If from SQLite then
source tables will not include
"""
Session = sessionmaker(bind=source_engine)
session = Session()
source_table = get_engine_table(instance, table_name, source_engine, with_workspace=to_sqlite)
destination_table = get_engine_table(instance, table_name, destination_engine, with_workspace=not to_sqlite)
print(f"Copying from {source_table} to {destination_table}")
with destination_engine.connect() as destination_connection:
with source_engine.connect() as connection:
result = connection.execute(source_table.select())
index = 0
for row in result:
index += 1
sys.stdout.write("{}\r".format(index))
data = get_table_row_values(row, source_table)
# Check if the ID already exists
if not override:
id_value = data.get("id")
if id_value is not None:
existing = is_row_with_id(destination_connection, destination_table, id_value)
if existing:
continue
try:
destination_connection.execute(destination_table.insert(), data)
except Exception as e:
print(f"Skipping row {index} due to insert error {e}")
destination_connection.rollback()
continue
if commit_every_row:
try:
destination_connection.commit()
except Exception as e:
print(f"Skipping row {index} due to insert error {e}")
destination_connection.rollback()
continue
if not commit_every_row:
destination_connection.commit()
session.close()
def obfuscate_user_table(table_name, destination_engine):
"""
Remove passwords from the database
"""
destination_metadata = MetaData()
destination_table = Table(table_name, destination_metadata, autoload_with=destination_engine)
columns = destination_table.columns.keys()
is_superuser_index = columns.index('is_superuser')
with destination_engine.connect() as destination_connection:
result = destination_connection.execute(destination_table.select())
for row in result:
update_stmt = destination_table.update().where(destination_table.c.id == row[0]).values(password='')
destination_connection.execute(update_stmt)
if is_superuser_index and row[is_superuser_index]:
update_stmt = destination_table.update().where(destination_table.c.id == row[0]).values(username='admin')
destination_connection.commit()
def create_indexes(destination_engine, table_name, column_name):
destination_metadata = MetaData()
destination_table = Table(table_name, destination_metadata, autoload_with=destination_engine)
r = ReflectedTable(destination_engine)
#r.create_index(destination_table, "link")
#r.create_index(destination_table, "title")
#r.create_index(destination_table, "date_published")
def obfuscate_all(destination_engine):
r = ReflectedTable(destination_engine)
obfuscate_user_table("user", destination_engine)
r.truncate_table("dataexport")
r.truncate_table("usersearchhistory")
#### SQLite
def get_local_engine(run_info):
workspace = run_info["workspace"]
user = run_info["user"]
database = run_info["database"]
host = run_info["host"]
password = run_info["password"]
# Create the database engine
SOURCE_DATABASE_URL = f"postgresql://{user}:{password}@{host}/{database}"
source_engine = create_engine(SOURCE_DATABASE_URL)
return source_engine
def get_sqlite_engine(run_info):
workspace = run_info["workspace"]
file_name = workspace+".db"
DESTINATION_DATABASE_URL = "sqlite:///" + file_name
destination_engine = create_engine(DESTINATION_DATABASE_URL)
return destination_engine
def run_db_copy_backup(run_info):
workspace = run_info["workspace"]
tables = run_info["tables"]
empty = run_info["empty"]
# Create the database engine
source_engine = get_local_engine(run_info)
operating_dir = get_workspace_backup_directory(run_info["format"], workspace)
operating_dir.mkdir(parents=True, exist_ok=True)
os.chdir(operating_dir)
destination_engine = get_sqlite_engine(run_info)
for table in tables:
table = table.replace(workspace + "_", "")
source_table = get_engine_table(workspace, table, source_engine)
create_destionation_table(table, source_table, destination_engine)
if not empty:
copy_table(workspace, table, source_engine, destination_engine, override=True, to_sqlite=True)
return True
def run_db_copy_restore(run_info):
workspace = run_info["workspace"]
tables = run_info["tables"]
empty = run_info["empty"]
append = run_info["append"]
destination_engine = get_local_engine(run_info)
operating_dir = get_workspace_backup_directory(run_info["format"], workspace)
os.chdir(operating_dir)
source_engine = get_sqlite_engine(run_info)
for table in tables:
table = table.replace(workspace + "_", "")
copy_table(workspace, table, source_engine, destination_engine, override=False, to_sqlite=False, commit_every_row=True)
return True
def run_db_copy_backup_auth(run_info):
workspace = run_info["workspace"]
# Create the database engine
source_engine = get_local_engine(run_info)
operating_dir = get_workspace_backup_directory(run_info["format"], workspace)
operating_dir.mkdir(parents=True, exist_ok=True)
os.chdir(operating_dir)
destination_engine = get_sqlite_engine(run_info)
source_table = get_engine_table("auth", "user", source_engine)
create_destionation_table("user", source_table, destination_engine)
copy_table("auth", "user", source_engine, destination_engine, override=True, to_sqlite=True)
return True
def backup_workspace(run_info):
"""
@note table order is important
mapping:
file : tables
"""
print("--------------------")
print(run_info["workspace"])
print("--------------------")
tablemapping = {
"./instance_entries" : ["instance_linkdatamodel"],
"./instance_domains" : ["instance_domains"],
"./instance_sourcecategories" : ["instance_sourcecategories"],
"./instance_sourcessubcategories" : ["instance_sourcesubcategories"],
"./instance_sources" : ["instance_sourcedatamodel"],
"./instance_usertags" : ["instance_usertags"],
"./instance_compactedtags" : ["instance_compactedtags"],
"./instance_usercompactedtags" : ["instance_usercompactedtags"],
"./instance_entrycompactedtags" : ["instance_entrycompactedtags"],
"./instance_compactedtags" : ["instance_compactedtags"],
"./instance_usercompactedtags" : ["instance_usercompactedtags"],
"./instance_votes" : ["instance_uservotes"],
"./instance_browser" : ["instance_browser"],
"./instance_entryrules" : ["instance_entryrules"],
"./instance_dataexport" : ["instance_dataexport"],
"./instance_gateway" : ["instance_gateway"],
"./instance_modelfiles" : ["instance_modelfiles"],
"./instance_readlater" : ["instance_readlater"],
"./instance_blockentrylist" : ["instance_blockentrylist"],
"./instance_comments" : ["instance_usercomments"],
"./instance_userbookmarks" : ["instance_userbookmarks"],
"./instance_usersearchhistory" : ["instance_usersearchhistory"],
"./instance_userentrytransitionhistory" : ["instance_userentrytransitionhistory"],
"./instance_userentryvisithistory" : ["instance_userentryvisithistory"],
"./instance_userconfig" : ["instance_userconfig"],
"./instance_configurationentry" : ["instance_configurationentry"],
}
workspace = run_info["workspace"]
for key in tablemapping:
new_run_info = dict(run_info)
new_key = key.replace("instance", workspace)
new_run_info["tables"] = []
new_run_info["output_file"] = new_key
for item in tablemapping[key]:
table_name = item.replace("instance", workspace)
new_run_info["tables"].append(table_name)
if new_run_info["format"] == "sqlite":
if not run_db_copy_backup(new_run_info):
return False
else:
if not run_pg_dump_backup(new_run_info):
return False
if run_info["format"] == "sqlite":
run_db_copy_backup_auth(run_info)
destination_engine = get_sqlite_engine(run_info)
create_indexes(destination_engine, "linkdatamodel", "link")
create_indexes(destination_engine, "linkdatamodel", "title")
create_indexes(destination_engine, "linkdatamodel", "date_published")
obfuscate_all(destination_engine)
return True
def restore_workspace(run_info):
"""
@note table order is important
"""
workspace = run_info["workspace"]
print("--------------------")
print(run_info["workspace"])
print("--------------------")
# order is important
tablemapping = [
["./instance_sourcecategories" , ["instance_sourcecategories"]],
["./instance_sourcessubcategories" , ["instance_sourcesubcategories"]],
["./instance_sources" , ["instance_sourcedatamodel"]],
["./instance_domains" , ["instance_domains"]],
["./instance_entries" , ["instance_linkdatamodel"]],
["./instance_usertags" , ["instance_usertags"]],
["./instance_compactedtags" , ["instance_compactedtags"]],
["./instance_usercompactedtags" , ["instance_usercompactedtags"]],
["./instance_entrycompactedtags" , ["instance_entrycompactedtags"]],
["./instance_votes" , ["instance_uservotes"]],
["./instance_comments" , ["instance_usercomments"]],
["./instance_userbookmarks" , ["instance_userbookmarks"]],
["./instance_browser" , ["instance_browser"]],
["./instance_entryrules" , ["instance_entryrules"]],
["./instance_dataexport" , ["instance_dataexport"]],
["./instance_gateway" , ["instance_gateway"]],
["./instance_blockentrylist" , ["instance_blockentrylist"]],
["./instance_modelfiles" , ["instance_modelfiles"]],
["./instance_readlater" , ["instance_readlater"]],
["./instance_userconfig" , ["instance_userconfig"]],
["./instance_configurationentry" , ["instance_configurationentry"]],
["./instance_usersearchhistory" , ["instance_usersearchhistory"]],
["./instance_userentrytransitionhistory" , ["instance_userentrytransitionhistory"]],
["./instance_userentryvisithistory" , ["instance_userentryvisithistory"]],
]
if not run_info["append"]:
for item in tablemapping:
key = item[0]
tables = item[1]
run_info["output_file"] = key
run_info["tables"] = tables
if not truncate_all(run_info):
print("Could not truncate table")
return
for item in tablemapping:
key = item[0]
tables = item[1]
new_run_info = dict(run_info)
new_key = key.replace("instance", workspace)
new_run_info["output_file"] = new_key
new_run_info["tables"] = []
for item in tables:
table_name = item.replace("instance", workspace)
new_run_info["tables"].append(table_name)
if new_run_info["format"] == "sqlite":
if not run_db_copy_restore(new_run_info):
return False
else:
if not run_pg_restore(new_run_info):
return False
reset_tables_index_sequence(tablemapping, run_info)
return True
def run_sql_for_workspaces(run_info, sql_command):
print("--------------------")
print(run_info["workspace"])
print("--------------------")
# order is important
tablemapping = [
"instance_apikeys",
"instance_applogging",
"instance_backgroundjob",
"instance_blockentry",
"instance_blockentrylist",
"instance_browser",
"instance_configurationentry",
"instance_domains",
"instance_dataexport",
"instance_entryrules",
"instance_gateway",
"instance_keywords",
"instance_linkdatamodel",
"instance_modelfiles",
"instance_readlater",
"instance_sourcecategories",
"instance_sourcesubcategories",
"instance_sourcedatamodel",
"instance_userconfig",
"instance_usercomments",
"instance_userbookmarks",
"instance_usersearchhistory",
"instance_userentrytransitionhistory",
"instance_userentryvisithistory",
"instance_usertags",
"instance_compactedtags",
"instance_usercompactedtags",
"instance_uservotes",
]
workspace = run_info["workspace"]
for table in tablemapping:
call_table = table.replace("instance", workspace)
call_sql_command = sql_command.replace("{table}", call_table)
if not run_table_sql(run_info, call_table, call_sql_command):
return False
return True
def parse_backup():
parser = argparse.ArgumentParser(prog="Backup", description="Backup manager. Please provide .pgpass file, and define your password there.")
parser.add_argument("-b", "--backup", action="store_true", help="Perform a backup")
parser.add_argument("-r", "--restore", action="store_true", help="Restore from a backup")
parser.add_argument("-a", "--analyze", action="store_true", help="Analyze the database")
parser.add_argument("--vacuum", action="store_true", help="Vacuum the database")
parser.add_argument("--reindex", action="store_true", help="Reindex the database. Useful to detect errors in consistency")
parser.add_argument("-s", "--sequence-update", action="store_true", help="Updates sequence numbers")
parser.add_argument("-U", "--user", default="user", help="Username for the database (default: 'user')")
parser.add_argument("-d", "--database", default="db", help="Database name (default: 'db')")
parser.add_argument("-p", "--password", default="", help="Password. Necessary for sqlite format")
parser.add_argument("-w", "--workspace", help="Workspace for which to perform backup/restore. If not specified - all")
parser.add_argument("-D", "--debug", help="Enable debug output") # TODO implement debug
parser.add_argument("-i", "--ignore-errors", action="store_true", help="Ignore errors during the operation")
parser.add_argument("--empty", action="store_true", help="Creates empty table version during backup")
parser.add_argument("--append", action="store_true", help="Appends data during restore, does not clear tables")
parser.add_argument("-f", "--format", default="custom", choices=["custom", "plain", "sql", "sqlite"],
help="Format of the backup (default: 'custom'). Choices: 'custom', 'plain', or 'sql'.")
parser.add_argument("--host", default="127.0.0.1", help="Host address for the database (default: 127.0.0.1)")
return parser, parser.parse_args()
def main():
parser, args = parse_backup()
if not args.backup and not args.restore and not args.analyze and not args.vacuum and not args.reindex and not args.sequence_update:
parser.print_help()
workspaces = []
if args.workspace:
all = get_workspaces()
if args.workspace in all:
workspaces = [args.workspace]
else:
print("No such workspace!")
else:
workspaces = get_workspaces()
start_time = time.time()
errors = False
for workspace in workspaces:
run_info = {}
run_info["workspace"] = workspace
run_info["user"] = args.user
run_info["database"] = args.database
run_info["host"] = args.host
run_info["format"] = args.format
run_info["password"] = args.password
run_info["empty"] = args.empty
run_info["append"] = args.append
if args.ignore_errors:
run_info["ignore_errors"] = True
if args.backup and not backup_workspace(run_info):
print("Leaving because of errors")
errors = True
break
if args.restore and not restore_workspace(run_info):
print("Leaving because of errors")
errors = True
break
if args.analyze and not run_sql_for_workspaces(run_info, "ANALYZE {table};"):
print("Leaving because of errors")
errors = True
break
if args.vacuum and not run_sql_for_workspaces(run_info, "VACUUM {table};"):
print("Leaving because of errors")
errors = True
break
if args.reindex and not run_sql_for_workspaces(run_info, "REINDEX TABLE {table};"):
print("Leaving because of errors")
errors = True
break
sql_text = "SELECT setval('{table}_id_seq', COALESCE((SELECT MAX(id) FROM {table}), 1));"
if args.sequence_update and not run_sql_for_workspaces(run_info, sql_text):
print("Leaving because of errors")
errors = True
break
if errors:
print("There were errors")
else:
print("All calls were successful")
elapsed_time_seconds = time.time() - start_time
elapsed_minutes = int(elapsed_time_seconds // 60)
elapsed_seconds = int(elapsed_time_seconds % 60)
print(f"Time: {elapsed_minutes}:{elapsed_seconds}")
if __name__ == "__main__":
main()