from openpyxl import load_workbook

DB_FILE = 'database.xlsx'

def migrate_db():
    try:
        wb = load_workbook(DB_FILE)
        if 'Devices' in wb.sheetnames:
            ws = wb['Devices']
            
            # Check if 'Purchased From' column already exists
            headers = [cell.value for cell in ws[1]]
            if 'Purchased From' in headers:
                print("'Purchased From' column already exists.")
            else:
                # Add new column
                ws.cell(row=1, column=len(headers) + 1, value='Purchased From')
                print("Added 'Purchased From' column to Devices sheet.")
                
            wb.save(DB_FILE)
            print("Migration completed successfully.")
    except Exception as e:
        print(f"Migration Failed: {e}")

if __name__ == "__main__":
    migrate_db()
