Fix bug
Browse files- database_manager.py +38 -1
database_manager.py
CHANGED
|
@@ -99,6 +99,10 @@ class DatabaseManager:
|
|
| 99 |
for table in tables:
|
| 100 |
table_name = table[0]
|
| 101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
# Get table schema
|
| 103 |
cursor.execute(f"PRAGMA table_info({table_name})")
|
| 104 |
columns = [col[1] for col in cursor.fetchall()]
|
|
@@ -123,6 +127,25 @@ class DatabaseManager:
|
|
| 123 |
'data': table_data
|
| 124 |
}
|
| 125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
conn.close()
|
| 127 |
return data
|
| 128 |
|
|
@@ -144,10 +167,14 @@ class DatabaseManager:
|
|
| 144 |
cursor = conn.cursor()
|
| 145 |
|
| 146 |
for table_name, table_info in json_data.items():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
columns = table_info['columns']
|
| 148 |
data = table_info['data']
|
| 149 |
|
| 150 |
-
if not columns
|
| 151 |
continue
|
| 152 |
|
| 153 |
# Create table
|
|
@@ -170,6 +197,16 @@ class DatabaseManager:
|
|
| 170 |
values = [row.get(col) for col in columns]
|
| 171 |
cursor.execute(insert_sql, values)
|
| 172 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
conn.commit()
|
| 174 |
conn.close()
|
| 175 |
print(f"✅ Successfully restored database to {db_path}")
|
|
|
|
| 99 |
for table in tables:
|
| 100 |
table_name = table[0]
|
| 101 |
|
| 102 |
+
# Skip system tables
|
| 103 |
+
if table_name in ['sqlite_sequence', 'sqlite_master']:
|
| 104 |
+
continue
|
| 105 |
+
|
| 106 |
# Get table schema
|
| 107 |
cursor.execute(f"PRAGMA table_info({table_name})")
|
| 108 |
columns = [col[1] for col in cursor.fetchall()]
|
|
|
|
| 127 |
'data': table_data
|
| 128 |
}
|
| 129 |
|
| 130 |
+
# Handle sqlite_sequence separately
|
| 131 |
+
try:
|
| 132 |
+
cursor.execute("SELECT * FROM sqlite_sequence")
|
| 133 |
+
sequence_rows = cursor.fetchall()
|
| 134 |
+
if sequence_rows:
|
| 135 |
+
sequence_data = []
|
| 136 |
+
for row in sequence_rows:
|
| 137 |
+
sequence_data.append({
|
| 138 |
+
'name': row[0],
|
| 139 |
+
'seq': row[1]
|
| 140 |
+
})
|
| 141 |
+
data['sqlite_sequence'] = {
|
| 142 |
+
'columns': ['name', 'seq'],
|
| 143 |
+
'data': sequence_data
|
| 144 |
+
}
|
| 145 |
+
except Exception:
|
| 146 |
+
# sqlite_sequence might not exist, that's ok
|
| 147 |
+
pass
|
| 148 |
+
|
| 149 |
conn.close()
|
| 150 |
return data
|
| 151 |
|
|
|
|
| 167 |
cursor = conn.cursor()
|
| 168 |
|
| 169 |
for table_name, table_info in json_data.items():
|
| 170 |
+
# Skip system tables
|
| 171 |
+
if table_name in ['sqlite_sequence', 'sqlite_master']:
|
| 172 |
+
continue
|
| 173 |
+
|
| 174 |
columns = table_info['columns']
|
| 175 |
data = table_info['data']
|
| 176 |
|
| 177 |
+
if not columns:
|
| 178 |
continue
|
| 179 |
|
| 180 |
# Create table
|
|
|
|
| 197 |
values = [row.get(col) for col in columns]
|
| 198 |
cursor.execute(insert_sql, values)
|
| 199 |
|
| 200 |
+
# Handle sqlite_sequence separately if it exists in backup
|
| 201 |
+
if 'sqlite_sequence' in json_data:
|
| 202 |
+
sequence_data = json_data['sqlite_sequence']['data']
|
| 203 |
+
for seq_row in sequence_data:
|
| 204 |
+
table_name = seq_row.get('name')
|
| 205 |
+
seq_value = seq_row.get('seq')
|
| 206 |
+
if table_name and seq_value:
|
| 207 |
+
cursor.execute(f"INSERT INTO sqlite_sequence (name, seq) VALUES (?, ?)",
|
| 208 |
+
(table_name, seq_value))
|
| 209 |
+
|
| 210 |
conn.commit()
|
| 211 |
conn.close()
|
| 212 |
print(f"✅ Successfully restored database to {db_path}")
|