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μ·¨μ†Œλ˜μ—ˆμŠ΅λ‹ˆλ‹€.")