Files
dougal-software/bin/system_imports.py
D. Berge b5b91d41c9 Reset event serial ids after re-import.
When the database is recreated, the sequences
used in the events_timed and events_seq tables
will be at their initial values, which will
almost certainly conflict with existing data
when it is imported via COPY.

With this commit, we set the current value for
those sequences to something usable.

Fixes #33.
2020-09-14 01:36:40 +02:00

53 lines
1.1 KiB
Python
Executable File

#!/usr/bin/python3
"""
Re-import Dougal-exported data created by
system_exports.py
"""
import os
from glob import glob
import configuration
import preplots
from datastore import Datastore
exportables = [
"events_seq",
"events_seq_labels",
"events_timed",
"events_timed_labels"
]
if __name__ == '__main__':
print("Reading configuration")
surveys = configuration.surveys()
print("Connecting to database")
db = Datastore()
print("Reading surveys")
for survey in surveys:
print(f'Survey: {survey["id"]} ({survey["schema"]})')
db.set_survey(survey["schema"])
with db.conn.cursor() as cursor:
try:
pathPrefix = survey["exports"]["machine"]["path"]
except KeyError:
print("Survey does not define an export path for machine data")
continue
for table in exportables:
path = os.path.join(pathPrefix, table)
print("", path, "", table)
with open(path, "rb") as fd:
cursor.copy_from(fd, table);
# If we don't commit the data does not actually get copied
db.conn.commit()
# Update the sequences that generate event ids
cur.execute("SELECT reset_events_serials();")
print("Done")