-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathimport_messages.py
More file actions
76 lines (62 loc) · 2.04 KB
/
Copy pathimport_messages.py
File metadata and controls
76 lines (62 loc) · 2.04 KB
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
import sqlite3
import sys
import os
def log_message(message):
print(f"[LOG] {message}")
if len(sys.argv) != 3:
print("Usage: python import_messages.py <source_db> <destination_db>")
sys.exit(1)
source_db = sys.argv[1]
destination_db = sys.argv[2]
if not os.path.exists(source_db):
print(f"Error: Source database '{source_db}' does not exist.")
sys.exit(1)
try:
conn = sqlite3.connect(destination_db)
cursor = conn.cursor()
# Create the messages table if it doesn't exist
cursor.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source TEXT,
sourceName TEXT,
timestamp INTEGER,
message TEXT,
groupId TEXT,
groupName TEXT,
attachmentPaths TEXT,
attachmentDescriptions TEXT,
processedAt INTEGER,
quoteId INTEGER,
quoteAuthor TEXT,
quoteText TEXT
);
""")
conn.execute(f"ATTACH DATABASE '{source_db}' AS src")
# Get the count of messages in the source database
src_count = cursor.execute("SELECT COUNT(*) FROM src.messages").fetchone()[0]
log_message(f"Source database has {src_count} messages")
# Insert into the messages table only new rows based on the primary key
cursor.execute("""
INSERT INTO messages
SELECT * FROM src.messages
WHERE NOT EXISTS (
SELECT 1 FROM messages WHERE messages.id = src.messages.id
);
""")
# Get the count of inserted messages
inserted_count = cursor.rowcount
log_message(f"Inserted {inserted_count} new messages")
conn.commit()
conn.execute("DETACH DATABASE src")
# Get the final count of messages in the destination database
dest_count = cursor.execute("SELECT COUNT(*) FROM messages").fetchone()[0]
log_message(f"Destination database now has {dest_count} messages")
except sqlite3.Error as e:
print(f"SQLite error: {e}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if conn:
conn.close()
log_message("Import process completed")