80 lines
2.3 KiB
Python
80 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Fix Contact Names
|
|
|
|
This script removes prefixed names like "RG_0x...", "MC_0x...", and "ETH_0x..."
|
|
and replaces them with NULL if they don't have ENS names.
|
|
|
|
Usage:
|
|
python fix_contact_names.py
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import psycopg2
|
|
from psycopg2.extras import RealDictCursor
|
|
from dotenv import load_dotenv
|
|
|
|
# Load environment variables
|
|
load_dotenv()
|
|
|
|
def fix_contact_names():
|
|
"""
|
|
Fix contact names by removing prefixed names and replacing with NULL.
|
|
"""
|
|
# Get database connection string from environment variables
|
|
db_url = os.getenv("PYTHON_DATABASE_URL")
|
|
if not db_url:
|
|
db_url = os.getenv("DATABASE_URL").split("?schema=")[0]
|
|
|
|
# Connect to the database
|
|
conn = psycopg2.connect(db_url)
|
|
conn.autocommit = True
|
|
|
|
try:
|
|
with conn.cursor(cursor_factory=RealDictCursor) as cursor:
|
|
# Find contacts with prefixed names
|
|
query = """
|
|
SELECT id, name, "ethereumAddress", "ensName"
|
|
FROM "Contact"
|
|
WHERE (name LIKE 'RG\\_%' OR name LIKE 'MC\\_%' OR name LIKE 'ETH\\_%'
|
|
OR name LIKE '%\\_0x%' ESCAPE '\\')
|
|
AND "ensName" IS NULL
|
|
"""
|
|
|
|
cursor.execute(query)
|
|
contacts = cursor.fetchall()
|
|
print(f"Found {len(contacts)} contacts with prefixed names")
|
|
|
|
# Update contacts to set name to NULL
|
|
fixed_count = 0
|
|
for contact in contacts:
|
|
update_query = """
|
|
UPDATE "Contact"
|
|
SET name = NULL,
|
|
"updatedAt" = NOW()
|
|
WHERE id = %s
|
|
"""
|
|
|
|
cursor.execute(update_query, (contact["id"],))
|
|
rows_updated = cursor.rowcount
|
|
|
|
if rows_updated > 0:
|
|
print(f"Cleared name for contact {contact['id']} (was '{contact['name']}')")
|
|
fixed_count += 1
|
|
|
|
print(f"Fixed {fixed_count} contacts with prefixed names")
|
|
|
|
finally:
|
|
conn.close()
|
|
|
|
def main():
|
|
"""Main entry point for the script."""
|
|
parser = argparse.ArgumentParser(description="Fix contact names")
|
|
args = parser.parse_args()
|
|
|
|
fix_contact_names()
|
|
|
|
if __name__ == "__main__":
|
|
main() |