Update migration script to handle offset
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
|
import argparse
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
SOURCE_DB = 'backend/data/upload.db'
|
SOURCE_DB = 'backend/data/upload.db'
|
||||||
DEST_DB = 'backend/data/sleep.db'
|
DEST_DB = 'backend/data/sleep.db'
|
||||||
|
|
||||||
def migrate():
|
def migrate(offset_minutes: int = 0):
|
||||||
try:
|
try:
|
||||||
src_conn = sqlite3.connect(SOURCE_DB)
|
src_conn = sqlite3.connect(SOURCE_DB)
|
||||||
src_cur = src_conn.cursor()
|
src_cur = src_conn.cursor()
|
||||||
@@ -13,6 +14,8 @@ def migrate():
|
|||||||
dest_cur = dest_conn.cursor()
|
dest_cur = dest_conn.cursor()
|
||||||
|
|
||||||
print(f"Connecting to {SOURCE_DB} and {DEST_DB}...")
|
print(f"Connecting to {SOURCE_DB} and {DEST_DB}...")
|
||||||
|
if offset_minutes != 0:
|
||||||
|
print(f"Applying {offset_minutes} minute offset to all timestamps.")
|
||||||
|
|
||||||
# 1. Truncate the destination table
|
# 1. Truncate the destination table
|
||||||
print("Truncating sleep_session table...")
|
print("Truncating sleep_session table...")
|
||||||
@@ -38,6 +41,9 @@ def migrate():
|
|||||||
# Convert Unix timestamp to datetime object
|
# Convert Unix timestamp to datetime object
|
||||||
try:
|
try:
|
||||||
start_time = datetime.fromtimestamp(timestamp_seconds)
|
start_time = datetime.fromtimestamp(timestamp_seconds)
|
||||||
|
# Apply offset
|
||||||
|
if offset_minutes != 0:
|
||||||
|
start_time += timedelta(minutes=offset_minutes)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error converting timestamp {timestamp_seconds}: {e}")
|
print(f"Error converting timestamp {timestamp_seconds}: {e}")
|
||||||
error_count += 1
|
error_count += 1
|
||||||
@@ -105,4 +111,8 @@ def migrate():
|
|||||||
if 'dest_conn' in locals(): dest_conn.close()
|
if 'dest_conn' in locals(): dest_conn.close()
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
migrate()
|
parser = argparse.ArgumentParser(description="Migrate sleep data from source DB to destination DB.")
|
||||||
|
parser.add_argument("--offset", type=int, default=0, help="Minute offset to apply to all timestamps.")
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
migrate(offset_minutes=args.offset)
|
||||||
|
|||||||
Reference in New Issue
Block a user