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")
