This commit is contained in:
lelo 2025-04-02 09:54:58 +00:00
parent a44e61e9c7
commit 7c63e12128
2 changed files with 0 additions and 90 deletions

View File

@ -1,48 +0,0 @@
#!/usr/bin/env python3
import sqlite3
from datetime import datetime, timezone
# Name of your SQLite database file
DB_NAME = 'access_log.db'
def convert_naive_to_timezone_aware(naive_ts):
"""
Convert a naive ISO timestamp (assumed to be in local time) into a timezone-aware timestamp.
"""
try:
dt = datetime.fromisoformat(naive_ts)
except Exception as e:
print(f"Error parsing timestamp {naive_ts}: {e}")
return naive_ts # If parsing fails, return the original value
# If the timestamp is naive (i.e., no tzinfo), make it timezone-aware by assuming it's local time.
if dt.tzinfo is None:
# Get the local timezone info using the current time's offset.
local_tz = datetime.now(timezone.utc).astimezone().tzinfo
dt = dt.replace(tzinfo=local_tz)
return dt.isoformat()
else:
# Timestamp is already timezone-aware
return naive_ts
def update_database():
conn = sqlite3.connect(DB_NAME)
cursor = conn.cursor()
# Fetch all records' IDs and timestamps
cursor.execute("SELECT id, timestamp FROM file_access_log")
rows = cursor.fetchall()
updated_count = 0
for rec_id, ts in rows:
new_ts = convert_naive_to_timezone_aware(ts)
if new_ts != ts:
cursor.execute("UPDATE file_access_log SET timestamp = ? WHERE id = ?", (new_ts, rec_id))
updated_count += 1
conn.commit()
conn.close()
print(f"Updated {updated_count} records to timezone-aware timestamps.")
if __name__ == "__main__":
update_database()

View File

@ -1,42 +0,0 @@
import sqlite3
def transform_database(db_path):
# Connect to the SQLite database
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 1. Create the new table with the desired schema.
cursor.execute('''
CREATE TABLE IF NOT EXISTS file_access_log_new (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT,
rel_path TEXT,
ip_address TEXT,
user_agent TEXT,
device_id TEXT
)
''')
# 2. Copy data from the old table, applying the following transformations:
# - Rename full_path to rel_path and remove '/mnt/' from its entries.
# - Omit the referrer column.
# - Copy ip_address into the new device_id column.
cursor.execute('''
INSERT INTO file_access_log_new (id, timestamp, rel_path, ip_address, user_agent, device_id)
SELECT id, timestamp, REPLACE(full_path, '/mnt/', ''), ip_address, user_agent, ip_address
FROM file_access_log
''')
# 3. Drop the old table.
cursor.execute('DROP TABLE file_access_log')
# 4. Rename the new table to use the original table's name.
cursor.execute('ALTER TABLE file_access_log_new RENAME TO file_access_log')
# Commit the changes and close the connection.
conn.commit()
conn.close()
if __name__ == "__main__":
# Replace 'your_database.db' with the path to your SQLite database file.
transform_database("access_log.db")