File size: 5,193 Bytes
9fa456d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
"""
μ
λ‘λλ νμΌκ³Ό λ°μ΄ν°λ² μ΄μ€ μ 보λ₯Ό μμ νλ μ€ν¬λ¦½νΈ
"""
import os
import sys
import sqlite3
from pathlib import Path
# UTF-8 μΈμ½λ© μ€μ
if sys.platform == 'win32':
sys.stdout.reconfigure(encoding='utf-8')
sys.stderr.reconfigure(encoding='utf-8')
def reset_upload_data():
"""μ
λ‘λλ νμΌκ³Ό DB λ°μ΄ν° μμ """
try:
# 1. λ°μ΄ν°λ² μ΄μ€ κ²½λ‘ νμΈ
db_path = os.path.join('instance', 'finance_analysis.db')
if not os.path.exists(db_path):
# μλ κ²½λ‘λ‘λ μλ
db_path = 'finance_analysis.db'
if not os.path.exists(db_path):
print(f"λ°μ΄ν°λ² μ΄μ€ νμΌμ μ°Ύμ μ μμ΅λλ€: {db_path}")
return False
print(f"λ°μ΄ν°λ² μ΄μ€ κ²½λ‘: {db_path}")
# 2. λ°μ΄ν°λ² μ΄μ€ μ°κ²°
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 3. μ
λ‘λλ νμΌ λͺ©λ‘ μ‘°ν (μμ μ μ νμΌ κ²½λ‘ μ μ₯)
cursor.execute("SELECT id, file_path FROM uploaded_file")
files_to_delete = cursor.fetchall()
print(f"\nμμ ν νμΌ μ: {len(files_to_delete)}κ°")
# 4. νμΌ μμ€ν
μμ νμΌ μμ
deleted_files = 0
failed_files = []
for file_id, file_path in files_to_delete:
if file_path and os.path.exists(file_path):
try:
os.remove(file_path)
deleted_files += 1
print(f" β νμΌ μμ : {os.path.basename(file_path)}")
except Exception as e:
failed_files.append((file_path, str(e)))
print(f" β νμΌ μμ μ€ν¨: {os.path.basename(file_path)} - {str(e)}")
# 5. λ°μ΄ν°λ² μ΄μ€μμ λ°μ΄ν° μμ
print(f"\nλ°μ΄ν°λ² μ΄μ€ λ°μ΄ν° μμ μ€...")
# document_chunk ν
μ΄λΈ μμ (RAGμ© μ²ν¬)
cursor.execute("DELETE FROM document_chunk")
chunk_count = cursor.rowcount
print(f" β document_chunk: {chunk_count}κ° μ²ν¬ μμ ")
# uploaded_file ν
μ΄λΈ μμ
cursor.execute("DELETE FROM uploaded_file")
file_count = cursor.rowcount
print(f" β uploaded_file: {file_count}κ° νμΌ μ 보 μμ ")
# chat_message ν
μ΄λΈ μμ (μ νμ¬ν - μ±ν
κΈ°λ‘λ μμ )
cursor.execute("DELETE FROM chat_message")
message_count = cursor.rowcount
print(f" β chat_message: {message_count}κ° λ©μμ§ μμ ")
# chat_session ν
μ΄λΈ μμ (μ νμ¬ν - μ±ν
μΈμ
λ μμ )
cursor.execute("DELETE FROM chat_session")
session_count = cursor.rowcount
print(f" β chat_session: {session_count}κ° μΈμ
μμ ")
# λ³κ²½μ¬ν 컀λ°
conn.commit()
conn.close()
print(f"\n{'='*60}")
print(f"μμ μλ£!")
print(f" - νμΌ μμ€ν
: {deleted_files}κ° νμΌ μμ ")
print(f" - λ°μ΄ν°λ² μ΄μ€: {file_count}κ° νμΌ μ 보, {chunk_count}κ° μ²ν¬ μμ ")
if failed_files:
print(f"\nμμ μ€ν¨ν νμΌ ({len(failed_files)}κ°):")
for file_path, error in failed_files:
print(f" - {file_path}: {error}")
print(f"{'='*60}\n")
return True
except Exception as e:
print(f"\nμ€λ₯ λ°μ: {str(e)}")
import traceback
traceback.print_exc()
return False
if __name__ == '__main__':
import sys
print("="*60)
print("μ
λ‘λ λ°μ΄ν° μ΄κΈ°ν μ€ν¬λ¦½νΈ")
print("="*60)
print("\nλ€μ λ°μ΄ν°κ° μμ λ©λλ€:")
print(" - μ
λ‘λλ λͺ¨λ νμΌ (uploads ν΄λ)")
print(" - λ°μ΄ν°λ² μ΄μ€μ uploaded_file ν
μ΄λΈ")
print(" - λ°μ΄ν°λ² μ΄μ€μ document_chunk ν
μ΄λΈ (RAG μ²ν¬)")
print(" - λ°μ΄ν°λ² μ΄μ€μ chat_message ν
μ΄λΈ (μ±ν
κΈ°λ‘)")
print(" - λ°μ΄ν°λ² μ΄μ€μ chat_session ν
μ΄λΈ (μ±ν
μΈμ
)")
print("\nμ¬μ©μ μ 보λ μ μ§λ©λλ€.")
print("="*60)
# λͺ
λ Ήμ€ μΈμλ‘ --yesκ° μμΌλ©΄ νμΈ μμ΄ μ€ν
auto_confirm = '--yes' in sys.argv or '-y' in sys.argv
if not auto_confirm:
try:
confirm = input("\nμ λ§ μμ νμκ² μ΅λκΉ? (yes/no): ")
except (EOFError, KeyboardInterrupt):
print("\nμ
λ ₯μ΄ μ·¨μλμμ΅λλ€. μλ μ€ν λͺ¨λλ‘ μ§νν©λλ€.")
confirm = 'yes'
else:
confirm = 'yes'
print("\nμλ μ€ν λͺ¨λ: νμΈ μμ΄ μμ λ₯Ό μ§νν©λλ€.")
if confirm.lower() in ['yes', 'y', 'μ', 'γ
']:
success = reset_upload_data()
if success:
print("\nμ΄κΈ°νκ° μλ£λμμ΅λλ€. μ΄μ μλ‘ νμΌμ μ
λ‘λν μ μμ΅λλ€.")
else:
print("\nμ΄κΈ°ν μ€ μ€λ₯κ° λ°μνμ΅λλ€.")
else:
print("\nμ·¨μλμμ΅λλ€.")
|