Skip to content

Commit aa50fdb

Browse files
committed
add future migration logic to ProcessingHistoryStorage
1 parent 34f27b8 commit aa50fdb

1 file changed

Lines changed: 40 additions & 34 deletions

File tree

stixcore/io/ProcessingHistoryStorage.py

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
class ProcessingHistoryStorage:
1111
"""Persistent handler for meta data on already processed Products"""
1212

13+
DB_VERSION = 1
14+
1315
def __init__(self, filename):
1416
"""Create a new persistent handler. Will open or create the given sqlite DB file.
1517
@@ -22,6 +24,7 @@ def __init__(self, filename):
2224
self.cur = None
2325
self.filename = filename
2426
self._connect_database()
27+
self._migrate_database()
2528

2629
def _connect_database(self):
2730
"""Connects to the sqlite file or creates an empty one if not present."""
@@ -30,45 +33,48 @@ def _connect_database(self):
3033
self.cur = self.conn.cursor()
3134
logger.info(f"ProcessingHistoryStorage DB loaded from {self.filename}")
3235

33-
# TODO reactivate later
34-
# self.cur.execute('''CREATE TABLE if not exists processed_flare_products (
35-
# id INTEGER PRIMARY KEY AUTOINCREMENT,
36-
# flareid TEXT NOT NULL,
37-
# flarelist TEXT NOT NULL,
38-
# version INTEGER NOT NULL,
39-
# name TEXT NOT NULL,
40-
# level TEXT NOT NULL,
41-
# type TEXT NOT NULL,
42-
# fitspath TEXT NOT NULL,
43-
# p_date FLOAT NOT NULL
44-
# )
45-
# ''')
46-
# self.cur.execute('''CREATE INDEX if not exists processed_flare_products_idx ON
47-
# processed_flare_products (flareid, flarelist, version, name,
48-
# level, type)''')
49-
50-
self.cur.execute("""CREATE TABLE if not exists processed_fits_products (
51-
id INTEGER PRIMARY KEY AUTOINCREMENT,
52-
name TEXT NOT NULL,
53-
level TEXT NOT NULL,
54-
type TEXT NOT NULL,
55-
version INTEGER NOT NULL,
56-
fits_in_path TEXT NOT NULL,
57-
fits_out_path TEXT NOT NULL,
58-
p_date TEXT NOT NULL
59-
)
60-
""")
61-
62-
self.cur.execute("""CREATE INDEX if not exists processed_fits_products_idx ON
63-
processed_fits_products
64-
(name, level, type, version, fits_in_path)""")
65-
66-
self.conn.commit()
6736
except sqlite3.Error:
6837
logger.error(f"Failed load DB from {self.filename}")
6938
self.close()
7039
raise
7140

41+
def _migrate_database(self):
42+
"""Migrate the database to the latest version if needed."""
43+
try:
44+
curent_DB_version = self.cur.execute("PRAGMA user_version;").fetchone()[0]
45+
46+
if curent_DB_version < self.DB_VERSION:
47+
logger.info(f"Migrating DB from version {curent_DB_version} to {self.DB_VERSION}")
48+
49+
if curent_DB_version < 1:
50+
self.cur.execute("""CREATE TABLE if not exists processed_fits_products (
51+
id INTEGER PRIMARY KEY AUTOINCREMENT,
52+
name TEXT NOT NULL,
53+
level TEXT NOT NULL,
54+
type TEXT NOT NULL,
55+
version INTEGER NOT NULL,
56+
fits_in_path TEXT NOT NULL,
57+
fits_out_path TEXT NOT NULL,
58+
p_date TEXT NOT NULL
59+
)
60+
""")
61+
62+
self.cur.execute("""CREATE INDEX if not exists processed_fits_products_idx ON
63+
processed_fits_products
64+
(name, level, type, version, fits_in_path)""")
65+
if curent_DB_version < 2:
66+
# future migrations here
67+
pass
68+
self.cur.execute(f"PRAGMA user_version = {self.DB_VERSION};")
69+
self.conn.commit()
70+
logger.info(f"DB migration done up to version {self.DB_VERSION}")
71+
else:
72+
logger.info(f"DB is already at version {curent_DB_version} no migration to do")
73+
except sqlite3.Error:
74+
logger.error(f"Failed to migrate DB to version {self.DB_VERSION}")
75+
self.close()
76+
raise
77+
7278
def close(self):
7379
"""Close the IDB connection."""
7480
if self.conn:

0 commit comments

Comments
 (0)