-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
343 lines (300 loc) · 11.7 KB
/
database.py
File metadata and controls
343 lines (300 loc) · 11.7 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import sqlite3
import pandas as pd
import json
from datetime import datetime
from config import DB_FILE
import uuid
def get_connection():
"""Creates a database connection to the SQLite database specified by DB_FILE."""
return sqlite3.connect(DB_FILE)
def ensure_config_histories_table():
"""Ensures config_histories table exists with correct schema."""
conn = get_connection()
c = conn.cursor()
try:
# Check if old table exists and migrate
c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='config_history'")
old_exists = c.fetchone()
if old_exists:
# Migrate old data to new table
try:
c.execute("ALTER TABLE config_history RENAME TO config_histories")
except:
# If rename fails, just drop old table
c.execute("DROP TABLE IF EXISTS config_history")
# Create table with correct schema
c.execute('''CREATE TABLE IF NOT EXISTS config_histories
(id TEXT PRIMARY KEY,
config_id TEXT,
version INTEGER,
json_data TEXT,
created_at TIMESTAMP,
FOREIGN KEY(config_id) REFERENCES configs(id) ON DELETE CASCADE)''')
conn.commit()
except Exception as e:
print(f"Error ensuring config_histories table: {e}")
finally:
conn.close()
def init_db():
"""Initializes the database tables if they do not exist."""
conn = get_connection()
c = conn.cursor()
# Table: Datasources
c.execute('''CREATE TABLE IF NOT EXISTS datasources
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE,
db_type TEXT,
host TEXT,
port TEXT,
dbname TEXT,
username TEXT,
password TEXT)''')
# Table: Configs
c.execute('''CREATE TABLE IF NOT EXISTS configs
(id TEXT PRIMARY KEY,
config_name TEXT UNIQUE,
table_name TEXT,
json_data TEXT,
updated_at TIMESTAMP)''')
# Table: Config Histories (renamed from config_history)
c.execute('''CREATE TABLE IF NOT EXISTS config_histories
(id TEXT PRIMARY KEY,
config_id TEXT,
version INTEGER,
json_data TEXT,
created_at TIMESTAMP,
FOREIGN KEY(config_id) REFERENCES configs(id) ON DELETE CASCADE)''')
conn.commit()
conn.close()
# --- Datasource CRUD Operations ---
def get_datasources():
"""Retrieves all datasources from the database."""
conn = get_connection()
try:
# Select specific columns to display in the UI
df = pd.read_sql_query("SELECT id, name, db_type, host, dbname, username FROM datasources", conn)
except:
df = pd.DataFrame()
finally:
conn.close()
return df
def get_datasource_by_id(id):
"""Retrieves a specific datasource by its ID."""
conn = get_connection()
c = conn.cursor()
c.execute("SELECT * FROM datasources WHERE id=?", (id,))
row = c.fetchone()
conn.close()
if row:
return {
"id": row[0], "name": row[1], "db_type": row[2],
"host": row[3], "port": row[4], "dbname": row[5],
"username": row[6], "password": row[7]
}
return None
def get_datasource_by_name(name):
"""Retrieves a specific datasource by its unique name."""
conn = get_connection()
c = conn.cursor()
c.execute("SELECT * FROM datasources WHERE name=?", (name,))
row = c.fetchone()
conn.close()
if row:
return {
"id": row[0], "name": row[1], "db_type": row[2],
"host": row[3], "port": row[4], "dbname": row[5],
"username": row[6], "password": row[7]
}
return None
def save_datasource(name, db_type, host, port, dbname, username, password):
"""Saves a new datasource to the database."""
conn = get_connection()
c = conn.cursor()
try:
c.execute('''INSERT INTO datasources (name, db_type, host, port, dbname, username, password)
VALUES (?, ?, ?, ?, ?, ?, ?)''',
(name, db_type, host, port, dbname, username, password))
conn.commit()
return True, "Saved successfully"
except sqlite3.IntegrityError:
return False, f"Datasource name '{name}' already exists."
except Exception as e:
return False, str(e)
finally:
conn.close()
def update_datasource(id, name, db_type, host, port, dbname, username, password):
"""Updates an existing datasource in the database."""
conn = get_connection()
c = conn.cursor()
try:
c.execute('''UPDATE datasources
SET name=?, db_type=?, host=?, port=?, dbname=?, username=?, password=?
WHERE id=?''',
(name, db_type, host, port, dbname, username, password, id))
conn.commit()
return True, "Updated successfully"
except sqlite3.IntegrityError:
return False, f"Datasource name '{name}' already exists."
except Exception as e:
return False, str(e)
finally:
conn.close()
def delete_datasource(id):
"""Deletes a datasource from the database by ID."""
conn = get_connection()
c = conn.cursor()
c.execute("DELETE FROM datasources WHERE id=?", (id,))
conn.commit()
conn.close()
# --- Config CRUD Operations ---
def save_config_to_db(config_name, table_name, json_data):
"""Saves or updates a JSON configuration in the database and tracks history."""
# Ensure config_histories table exists with correct schema
ensure_config_histories_table()
conn = get_connection()
c = conn.cursor()
try:
json_str = json.dumps(json_data)
# Check if config already exists
c.execute("SELECT id FROM configs WHERE config_name=?", (config_name,))
existing = c.fetchone()
config_id = existing[0] if existing else str(uuid.uuid4())
# Get next version number
c.execute("SELECT MAX(version) FROM config_histories WHERE config_id=?", (config_id,))
max_version = c.fetchone()[0]
next_version = (max_version + 1) if max_version else 1
# Save to configs table (INSERT OR REPLACE)
c.execute('''INSERT OR REPLACE INTO configs (id, config_name, table_name, json_data, updated_at)
VALUES (?, ?, ?, ?, ?)''',
(config_id, config_name, table_name, json_str, datetime.now()))
# Save to config_histories table
history_id = str(uuid.uuid4())
c.execute('''INSERT INTO config_histories (id, config_id, version, json_data, created_at)
VALUES (?, ?, ?, ?, ?)''',
(history_id, config_id, next_version, json_str, datetime.now()))
conn.commit()
return True, "Config saved!"
except Exception as e:
return False, str(e)
finally:
conn.close()
def get_configs_list():
"""Retrieves a list of saved configurations, sorted by update time."""
conn = get_connection()
try:
# Fetch json_data to extract target table info
df = pd.read_sql_query("SELECT config_name, table_name, json_data, updated_at FROM configs ORDER BY updated_at DESC", conn)
# Helper to extract target table from JSON string
def extract_target(json_str):
try:
data = json.loads(json_str)
return data.get('target', {}).get('table', '-')
except:
return '-'
if not df.empty:
df['destination_table'] = df['json_data'].apply(extract_target)
# Remove json_data column to keep DF lightweight for UI
df = df.drop(columns=['json_data'])
# Rename for clarity
df = df.rename(columns={'table_name': 'source_table'})
except Exception as e:
# Return empty DF with expected columns if error
df = pd.DataFrame(columns=['config_name', 'source_table', 'destination_table', 'updated_at'])
finally:
conn.close()
return df
def get_config_content(config_name):
"""Retrieves the JSON content of a specific configuration."""
conn = get_connection()
c = conn.cursor()
c.execute("SELECT json_data FROM configs WHERE config_name=?", (config_name,))
row = c.fetchone()
conn.close()
if row:
return json.loads(row[0])
return None
def delete_config(config_name):
"""Deletes a configuration from the database by name."""
conn = get_connection()
c = conn.cursor()
try:
c.execute("DELETE FROM configs WHERE config_name=?", (config_name,))
conn.commit()
return True, "Config deleted successfully"
except Exception as e:
return False, str(e)
finally:
conn.close()
def get_config_history(config_name):
"""Retrieves all versions of a configuration."""
# Ensure config_histories table exists with correct schema
ensure_config_histories_table()
conn = get_connection()
try:
# Get config_id from config_name first
c = conn.cursor()
c.execute("SELECT id FROM configs WHERE config_name=?", (config_name,))
result = c.fetchone()
if result:
config_id = result[0]
df = pd.read_sql_query(
"SELECT id, version, created_at FROM config_histories WHERE config_id=? ORDER BY version DESC",
conn,
params=(config_id,)
)
else:
df = pd.DataFrame(columns=['id', 'version', 'created_at'])
except:
df = pd.DataFrame(columns=['id', 'version', 'created_at'])
finally:
conn.close()
return df
def get_config_version(config_name, version):
"""Retrieves a specific version of a configuration."""
# Ensure config_histories table exists with correct schema
ensure_config_histories_table()
conn = get_connection()
c = conn.cursor()
try:
# Get config_id first
c.execute("SELECT id FROM configs WHERE config_name=?", (config_name,))
result = c.fetchone()
if result:
config_id = result[0]
c.execute("SELECT json_data FROM config_histories WHERE config_id=? AND version=?", (config_id, version))
row = c.fetchone()
if row:
return json.loads(row[0])
return None
except:
return None
finally:
conn.close()
def compare_config_versions(config_name, version1, version2):
"""Compares two versions of a configuration and returns the differences."""
config_v1 = get_config_version(config_name, version1)
config_v2 = get_config_version(config_name, version2)
if not config_v1 or not config_v2:
return None
diff = {
'mappings_added': [],
'mappings_removed': [],
'mappings_modified': []
}
mappings_v1 = {m['source']: m for m in config_v1.get('mappings', [])}
mappings_v2 = {m['source']: m for m in config_v2.get('mappings', [])}
# Find added and modified mappings
for source, mapping_v2 in mappings_v2.items():
if source not in mappings_v1:
diff['mappings_added'].append(mapping_v2)
elif mappings_v1[source] != mapping_v2:
diff['mappings_modified'].append({
'source': source,
'old': mappings_v1[source],
'new': mapping_v2
})
# Find removed mappings
for source, mapping_v1 in mappings_v1.items():
if source not in mappings_v2:
diff['mappings_removed'].append(mapping_v1)
return diff