Some checks failed
CI - SharePoint Plugin with SonarQube / Test and SonarQube Analysis (push) Has been cancelled
197 lines
5.7 KiB
Python
197 lines
5.7 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Clear all data from the SharePoint connector application.
|
||
This script will:
|
||
1. Drop all vector store tables (PostgreSQL)
|
||
2. Delete all DynamoDB tables (connections, tokens, etc.)
|
||
3. Clear stored credentials
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import boto3
|
||
from storage.credentials_storage import get_credentials_storage
|
||
|
||
def clear_postgresql_vector_store():
|
||
"""Drop all vector store tables from PostgreSQL."""
|
||
try:
|
||
import psycopg2
|
||
|
||
# Get connection params from environment
|
||
db_host = os.getenv("POSTGRES_HOST", "localhost")
|
||
db_port = os.getenv("POSTGRES_PORT", "5432")
|
||
db_name = os.getenv("POSTGRES_DB", "sharepoint_vectors")
|
||
db_user = os.getenv("POSTGRES_USER", "postgres")
|
||
db_password = os.getenv("POSTGRES_PASSWORD", "postgres")
|
||
|
||
print(f"📊 Connecting to PostgreSQL at {db_host}:{db_port}/{db_name}...")
|
||
|
||
conn = psycopg2.connect(
|
||
host=db_host,
|
||
port=db_port,
|
||
database=db_name,
|
||
user=db_user,
|
||
password=db_password
|
||
)
|
||
|
||
cursor = conn.cursor()
|
||
|
||
# Get table prefix
|
||
table_prefix = os.getenv("TABLE_PREFIX", "dev_")
|
||
|
||
# Drop tables in correct order (child tables first)
|
||
tables = [
|
||
f"{table_prefix}document_tags",
|
||
f"{table_prefix}document_chunks",
|
||
f"{table_prefix}documents"
|
||
]
|
||
|
||
for table in tables:
|
||
try:
|
||
cursor.execute(f"DROP TABLE IF EXISTS {table} CASCADE")
|
||
print(f" ✅ Dropped table: {table}")
|
||
except Exception as e:
|
||
print(f" ⚠️ Could not drop {table}: {e}")
|
||
|
||
conn.commit()
|
||
cursor.close()
|
||
conn.close()
|
||
|
||
print("✅ PostgreSQL vector store cleared")
|
||
return True
|
||
|
||
except ImportError:
|
||
print("⚠️ psycopg2 not installed, skipping PostgreSQL cleanup")
|
||
return False
|
||
except Exception as e:
|
||
print(f"❌ Error clearing PostgreSQL: {e}")
|
||
return False
|
||
|
||
|
||
def clear_dynamodb_tables():
|
||
"""Delete all DynamoDB tables."""
|
||
try:
|
||
# Determine if using local or AWS DynamoDB
|
||
endpoint_url = os.getenv("DYNAMODB_ENDPOINT", "http://localhost:8000")
|
||
region = os.getenv("AWS_REGION", "ap-southeast-2")
|
||
table_prefix = os.getenv("TABLE_PREFIX", "dev_")
|
||
|
||
if "localhost" in endpoint_url or "127.0.0.1" in endpoint_url:
|
||
print(f"📊 Connecting to LOCAL DynamoDB at {endpoint_url}...")
|
||
else:
|
||
print(f"📊 Connecting to AWS DynamoDB in {region}...")
|
||
|
||
dynamodb = boto3.client(
|
||
'dynamodb',
|
||
region_name=region,
|
||
endpoint_url=endpoint_url if "localhost" in endpoint_url or "127.0.0.1" in endpoint_url else None
|
||
)
|
||
|
||
# List all tables
|
||
response = dynamodb.list_tables()
|
||
tables = response.get('TableNames', [])
|
||
|
||
# Filter tables with our prefix
|
||
our_tables = [t for t in tables if t.startswith(table_prefix)]
|
||
|
||
if not our_tables:
|
||
print(f" ℹ️ No tables found with prefix '{table_prefix}'")
|
||
return True
|
||
|
||
print(f" Found {len(our_tables)} tables to delete:")
|
||
for table in our_tables:
|
||
print(f" - {table}")
|
||
|
||
# Delete each table
|
||
for table in our_tables:
|
||
try:
|
||
dynamodb.delete_table(TableName=table)
|
||
print(f" ✅ Deleted table: {table}")
|
||
except Exception as e:
|
||
print(f" ⚠️ Could not delete {table}: {e}")
|
||
|
||
print("✅ DynamoDB tables cleared")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"❌ Error clearing DynamoDB: {e}")
|
||
return False
|
||
|
||
|
||
def clear_credentials():
|
||
"""Clear all stored credentials."""
|
||
try:
|
||
print("📊 Clearing stored credentials...")
|
||
|
||
storage = get_credentials_storage()
|
||
|
||
# Get the storage file path
|
||
if hasattr(storage, 'file_path'):
|
||
file_path = storage.file_path
|
||
if os.path.exists(file_path):
|
||
os.remove(file_path)
|
||
print(f" ✅ Deleted credentials file: {file_path}")
|
||
else:
|
||
print(f" ℹ️ No credentials file found at: {file_path}")
|
||
else:
|
||
print(" ℹ️ Credentials storage does not use file system")
|
||
|
||
print("✅ Credentials cleared")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"❌ Error clearing credentials: {e}")
|
||
return False
|
||
|
||
|
||
def main():
|
||
"""Main function to clear all data."""
|
||
print("=" * 60)
|
||
print("🧹 CLEARING ALL SHAREPOINT CONNECTOR DATA")
|
||
print("=" * 60)
|
||
print()
|
||
|
||
# Check for --force flag
|
||
force = '--force' in sys.argv or '-f' in sys.argv
|
||
|
||
if not force:
|
||
try:
|
||
response = input("⚠️ This will DELETE ALL data. Are you sure? (yes/no): ")
|
||
if response.lower() not in ['yes', 'y']:
|
||
print("❌ Aborted")
|
||
return
|
||
except EOFError:
|
||
print("\n❌ No input received. Use --force to skip confirmation.")
|
||
return
|
||
|
||
print()
|
||
print("Starting cleanup...")
|
||
print()
|
||
|
||
# Clear PostgreSQL vector store
|
||
print("1️⃣ Clearing PostgreSQL Vector Store...")
|
||
clear_postgresql_vector_store()
|
||
print()
|
||
|
||
# Clear DynamoDB tables
|
||
print("2️⃣ Clearing DynamoDB Tables...")
|
||
clear_dynamodb_tables()
|
||
print()
|
||
|
||
# Clear credentials
|
||
print("3️⃣ Clearing Stored Credentials...")
|
||
clear_credentials()
|
||
print()
|
||
|
||
print("=" * 60)
|
||
print("✅ ALL DATA CLEARED SUCCESSFULLY")
|
||
print("=" * 60)
|
||
print()
|
||
print("You can now start fresh by running the application:")
|
||
print(" python app_dev.py")
|
||
print()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|