-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
1177 lines (991 loc) · 50.8 KB
/
database.py
File metadata and controls
1177 lines (991 loc) · 50.8 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import duckdb
import sqlite3
from datetime import datetime
import logging
import pprint
# Get logger (configuration is handled in config.py)
logger = logging.getLogger(__name__)
class Database:
"""
Base database class.
"""
def __init__(self, config):
self.config = config
self.conn = None
# Get debug_writes flag from config, default to False if not present
# Check both direct access and nested access to handle different config structures
if isinstance(config, dict) and "database" in config and isinstance(config["database"], dict):
self.debug_writes = config["database"].get("debug_writes", False)
else:
self.debug_writes = config.get("debug_writes", False)
# Log the debug_writes setting
logger.info(f"Database debug_writes flag initialized to: {self.debug_writes}")
def connect(self):
"""
Connect to the database.
"""
raise NotImplementedError("Subclasses must implement connect()")
def create_table(self, table_name, schema, recreate=False):
"""
Create a table if it doesn't exist.
Args:
table_name: The name of the table to create
schema: The schema definition for the table
recreate: If True, drop the table if it exists before creating it
"""
raise NotImplementedError("Subclasses must implement create_table()")
def upsert(self, table_name, data, primary_key=None):
"""
Upsert data into a table.
"""
raise NotImplementedError("Subclasses must implement upsert()")
def insert(self, table_name, data):
"""
Insert data into a table.
"""
raise NotImplementedError("Subclasses must implement insert()")
def query(self, sql, params=None):
"""
Execute a query and return the results.
"""
raise NotImplementedError("Subclasses must implement query()")
def close(self):
"""
Close the database connection.
"""
if self.conn:
self.conn.close()
self.conn = None
def _debug_print_data(self, operation, table_name, data, primary_key=None):
"""
Print debug information about data being written to the database.
Args:
operation: The operation being performed (e.g., "INSERT", "UPSERT")
table_name: The name of the table
data: The data being written
primary_key: The primary key column (for upserts)
"""
if not self.debug_writes:
return
logger.info(f"DEBUG WRITES: {operation} to table '{table_name}'")
if primary_key:
logger.info(f"Primary key: {primary_key}")
logger.info(f"Number of rows: {len(data)}")
# Print a sample of the data (up to 5 rows)
sample_size = min(5, len(data))
logger.info(f"Sample data (showing {sample_size} of {len(data)} rows):")
pp = pprint.PrettyPrinter(indent=2)
for i, item in enumerate(data[:sample_size]):
logger.info(f"Row {i+1}:")
logger.info(pp.pformat(item))
if len(data) > sample_size:
logger.info(f"... and {len(data) - sample_size} more rows")
class MotherDuckDatabase(Database):
"""
MotherDuck database implementation.
"""
def connect(self):
"""
Connect to MotherDuck.
"""
motherduck_token = os.getenv("MOTHERDUCK_TOKEN")
if not motherduck_token:
raise ValueError("MOTHERDUCK_TOKEN environment variable is not set")
db_name = self.config["database"]["connection"]["motherduck"]["database"]
connection_string = f"md:{db_name}?motherduck_token={motherduck_token}"
logger.info(f"Connecting to MotherDuck database: {db_name}")
self.conn = duckdb.connect(database=connection_string, read_only=False)
return self.conn
def create_table(self, table_name, schema, recreate=False):
"""
Create a table if it doesn't exist.
Args:
table_name: The name of the table to create
schema: The schema definition for the table
recreate: If True, drop the table if it exists before creating it
"""
if not self.conn:
self.connect()
# If recreate is True, drop the table if it exists
if recreate:
logger.info(f"Dropping table {table_name} if it exists (recreate=True)")
self.conn.execute(f"""
DROP TABLE IF EXISTS {table_name}
""")
# Create the table
self.conn.execute(f"""
CREATE TABLE IF NOT EXISTS {table_name} (
{schema}
)
""")
# Check if updated_at column exists, if not add it
column_check = self.conn.execute(f"""
SELECT column_name
FROM information_schema.columns
WHERE table_name = '{table_name}' AND column_name = 'updated_at'
""").fetchall()
if not column_check:
logger.info(f"Adding updated_at column to table {table_name}")
self.conn.execute(f"""
ALTER TABLE {table_name} ADD COLUMN updated_at TIMESTAMP
""")
def upsert(self, table_name, data, primary_key=None):
"""
Upsert data into a table using a true merge approach.
This implementation updates existing records and inserts new ones without deleting any data.
"""
if not self.conn:
self.connect()
if not data:
return 0
# Debug print the data being upserted
self._debug_print_data("UPSERT", table_name, data, primary_key)
# Create a temporary table for the new data
columns = list(data[0].keys())
if "updated_at" not in columns:
columns.append("updated_at")
column_defs = ", ".join([f"{col} {self._get_column_type(data[0][col]) if col in data[0] else 'TIMESTAMP'}" for col in columns])
self.conn.execute(f"""
CREATE TEMPORARY TABLE temp_data (
{column_defs}
)
""")
# Get table schema to check column types
try:
schema_query = f"""
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = '{table_name}'
"""
schema_info = self.conn.execute(schema_query).fetchall()
column_types = {col[0]: col[1] for col in schema_info}
logger.info(f"DEBUG: Table schema for {table_name}: {column_types}")
except Exception as e:
logger.warning(f"DEBUG: Could not get schema info: {e}")
column_types = {}
# Insert data into the temporary table
current_time = datetime.utcnow().isoformat()
logger.info(f"DEBUG: Using UTC current_time for updated_at: {current_time}")
for item in data:
try:
# Prepare values with updated_at and convert timestamps
values = []
for col in columns:
if col == "updated_at":
values.append(current_time)
elif col in column_types and "TIMESTAMP" in column_types.get(col, "").upper() and item.get(col):
# Convert string timestamps to datetime objects for TIMESTAMP columns
try:
timestamp_str = item.get(col)
logger.info(f"DEBUG: Converting timestamp: {timestamp_str}, type: {type(timestamp_str)}")
# If it's already a datetime object, use it directly
if isinstance(timestamp_str, datetime):
values.append(timestamp_str)
# If it's a string in ISO format, parse it
elif isinstance(timestamp_str, str):
# Try to parse the timestamp string
try:
# Try ISO format with Z (UTC)
dt = datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%SZ")
values.append(dt)
logger.info(f"DEBUG: Converted to datetime: {dt}")
except ValueError:
try:
# Try ISO format without Z
dt = datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%S")
values.append(dt)
logger.info(f"DEBUG: Converted to datetime: {dt}")
except ValueError:
# If all parsing fails, pass the string as is
logger.warning(f"DEBUG: Could not parse timestamp: {timestamp_str}")
values.append(timestamp_str)
else:
# If it's neither a datetime nor a string, use as is
values.append(timestamp_str)
except Exception as e:
logger.error(f"DEBUG: Error converting timestamp {item.get(col)}: {e}")
values.append(item.get(col))
else:
values.append(item.get(col))
placeholders = ", ".join(["?" for _ in range(len(values))])
insert_sql = f"""
INSERT INTO temp_data ({', '.join(columns)})
VALUES ({placeholders})
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {insert_sql}")
logger.info(f"DEBUG: With values: {values}")
self.conn.execute(insert_sql, values)
except Exception as e:
logger.error(f"DEBUG: Error inserting row into temp table: {e}")
logger.error(f"DEBUG: Row data: {item}")
raise
# Check if the target table exists
table_exists_query = f"""
SELECT COUNT(*) FROM information_schema.tables
WHERE table_name = '{table_name}'
"""
table_exists = self.conn.execute(table_exists_query).fetchone()[0] > 0
if not table_exists:
# If table doesn't exist, just insert all records
logger.info(f"Table {table_name} does not exist yet, creating and inserting all records")
# Create the table with the same schema as temp_data
create_table_sql = f"""
CREATE TABLE {table_name} AS SELECT * FROM temp_data WHERE 1=0
"""
self.conn.execute(create_table_sql)
# Insert all records
insert_all_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
SELECT {', '.join(columns)} FROM temp_data
"""
self.conn.execute(insert_all_sql)
else:
# If primary key is provided, use it for true upserting (merge)
if primary_key:
# Get list of primary keys in the new data
pk_values = [item[primary_key] for item in data]
pk_list = ", ".join([f"'{pk}'" for pk in pk_values])
if self.debug_writes:
# Log the records being updated
select_sql = f"SELECT * FROM {table_name} WHERE {primary_key} IN ({pk_list})"
logger.info(f"DEBUG: Executing SQL: {select_sql}")
existing_records = self.query(select_sql)
if existing_records:
logger.info(f"DEBUG WRITES: Found {len(existing_records)} existing records in '{table_name}' with {primary_key} IN ({pk_list})")
logger.info(f"DEBUG: Will update these records and insert new ones")
# Log the first existing record
if len(existing_records) > 0:
logger.info(f"DEBUG: First existing record: {existing_records[0]}")
if hasattr(existing_records[0], 'keys'):
logger.info(f"DEBUG: First existing record updated_at: {existing_records[0].get('updated_at', 'N/A')}")
# Create a temporary table with existing records that match the primary keys
create_existing_sql = f"""
CREATE TEMPORARY TABLE existing_records AS
SELECT * FROM {table_name} WHERE {primary_key} IN ({pk_list})
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {create_existing_sql}")
self.conn.execute(create_existing_sql)
# Update existing records
non_pk_columns = [col for col in columns if col != primary_key]
set_clauses = [f"{col} = temp_data.{col}" for col in non_pk_columns]
update_sql = f"""
UPDATE {table_name}
SET {', '.join(set_clauses)}
FROM temp_data
WHERE {table_name}.{primary_key} = temp_data.{primary_key}
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {update_sql}")
self.conn.execute(update_sql)
# Insert new records that don't exist in the target table
insert_new_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
SELECT {', '.join(columns)} FROM temp_data
WHERE NOT EXISTS (
SELECT 1 FROM {table_name}
WHERE {table_name}.{primary_key} = temp_data.{primary_key}
)
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {insert_new_sql}")
self.conn.execute(insert_new_sql)
# Drop the temporary table with existing records
self.conn.execute("DROP TABLE existing_records")
else:
# If no primary key is provided, just insert all records
# This might create duplicates, but that's expected behavior without a primary key
insert_all_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
SELECT {', '.join(columns)} FROM temp_data
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {insert_all_sql}")
self.conn.execute(insert_all_sql)
# Drop the temporary table
self.conn.execute("DROP TABLE temp_data")
return len(data)
def insert(self, table_name, data):
"""
Insert data into a table without upserting.
"""
if not self.conn:
self.connect()
if not data:
return 0
# Debug print the data being inserted
self._debug_print_data("INSERT", table_name, data)
# Get column names from the first data item
columns = list(data[0].keys())
if "updated_at" not in columns:
columns.append("updated_at")
# Get table schema to check column types
try:
schema_query = f"""
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = '{table_name}'
"""
schema_info = self.conn.execute(schema_query).fetchall()
column_types = {col[0]: col[1] for col in schema_info}
logger.info(f"DEBUG: Table schema for {table_name}: {column_types}")
except Exception as e:
logger.warning(f"DEBUG: Could not get schema info: {e}")
column_types = {}
# Insert records
count = 0
current_time = datetime.utcnow().isoformat()
for item in data:
try:
# Prepare values with updated_at and convert timestamps
values = []
for col in columns:
if col == "updated_at":
values.append(current_time)
elif col in column_types and "TIMESTAMP" in column_types[col].upper() and item.get(col):
# Convert string timestamps to datetime objects for TIMESTAMP columns
try:
timestamp_str = item.get(col)
logger.info(f"DEBUG: Converting timestamp: {timestamp_str}, type: {type(timestamp_str)}")
# If it's already a datetime object, use it directly
if isinstance(timestamp_str, datetime):
values.append(timestamp_str)
# If it's a string in ISO format, parse it
elif isinstance(timestamp_str, str):
# Try to parse the timestamp string
try:
# Try ISO format with Z (UTC)
dt = datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%SZ")
values.append(dt)
logger.info(f"DEBUG: Converted to datetime: {dt}")
except ValueError:
try:
# Try ISO format without Z
dt = datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%S")
values.append(dt)
logger.info(f"DEBUG: Converted to datetime: {dt}")
except ValueError:
# If all parsing fails, pass the string as is
logger.warning(f"DEBUG: Could not parse timestamp: {timestamp_str}")
values.append(timestamp_str)
else:
# If it's neither a datetime nor a string, use as is
values.append(timestamp_str)
except Exception as e:
logger.error(f"DEBUG: Error converting timestamp {item.get(col)}: {e}")
values.append(item.get(col))
else:
values.append(item.get(col))
placeholders = ", ".join(["?" for _ in range(len(values))])
insert_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {insert_sql}")
logger.info(f"DEBUG: With values: {values}")
self.conn.execute(insert_sql, values)
count += 1
except Exception as e:
logger.error(f"DEBUG: Error inserting row {count+1}: {e}")
logger.error(f"DEBUG: Row data: {item}")
raise
return count
def query(self, sql, params=None):
"""
Execute a query and return the results.
"""
if not self.conn:
self.connect()
if params:
return self.conn.execute(sql, params).fetchall()
else:
return self.conn.execute(sql).fetchall()
def _get_column_type(self, value):
"""
Get the SQL type for a value.
"""
if isinstance(value, int):
return "INTEGER"
elif isinstance(value, float):
return "FLOAT"
elif isinstance(value, bool):
return "BOOLEAN"
elif isinstance(value, (datetime, str)):
return "TEXT"
else:
return "TEXT"
class SQLiteDatabase(Database):
"""
SQLite database implementation.
"""
def connect(self):
"""
Connect to SQLite.
"""
sqlite_config = self.config["database"]["connection"]["sqlite"]
logger.info(f"Connecting to SQLite database: {sqlite_config['path']}")
self.conn = sqlite3.connect(sqlite_config["path"])
# Enable foreign keys
self.conn.execute("PRAGMA foreign_keys = ON")
# Configure SQLite to return rows as dictionaries
self.conn.row_factory = sqlite3.Row
return self.conn
def create_table(self, table_name, schema, recreate=False):
"""
Create a table if it doesn't exist.
Args:
table_name: The name of the table to create
schema: The schema definition for the table
recreate: If True, drop the table if it exists before creating it
"""
if not self.conn:
self.connect()
# If recreate is True, drop the table if it exists
if recreate:
logger.info(f"Dropping table {table_name} if it exists (recreate=True)")
self.conn.execute(f"""
DROP TABLE IF EXISTS {table_name}
""")
self.conn.commit()
# Create the table
self.conn.execute(f"""
CREATE TABLE IF NOT EXISTS {table_name} (
{schema}
)
""")
# Check if updated_at column exists, if not add it
cursor = self.conn.execute(f"PRAGMA table_info({table_name})")
columns = [row[1] for row in cursor.fetchall()]
if "updated_at" not in columns:
logger.info(f"Adding updated_at column to table {table_name}")
self.conn.execute(f"""
ALTER TABLE {table_name} ADD COLUMN updated_at TEXT
""")
self.conn.commit()
def upsert(self, table_name, data, primary_key=None):
"""
Upsert data into a table using a true merge approach.
This implementation updates existing records and inserts new ones without deleting any data.
"""
if not self.conn:
self.connect()
if not data:
return 0
# Debug print the data being upserted
self._debug_print_data("UPSERT", table_name, data, primary_key)
# Get column names from the first data item
columns = list(data[0].keys())
if "updated_at" not in columns:
columns.append("updated_at")
# Begin transaction
self.conn.execute("BEGIN TRANSACTION")
try:
# Check if the table exists
cursor = self.conn.execute(f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table_name}'")
table_exists = cursor.fetchone() is not None
if not table_exists:
# If table doesn't exist, create it and insert all records
logger.info(f"Table {table_name} does not exist yet, creating and inserting all records")
# Create the table with columns from the data
column_defs = []
for col in columns:
if col == primary_key:
column_defs.append(f"{col} TEXT PRIMARY KEY")
elif col == "updated_at":
column_defs.append(f"{col} TEXT")
else:
column_defs.append(f"{col} TEXT")
create_table_sql = f"""
CREATE TABLE {table_name} (
{', '.join(column_defs)}
)
"""
self.conn.execute(create_table_sql)
# Insert all records
current_time = datetime.utcnow().isoformat()
for item in data:
# Prepare values with updated_at
values = [item.get(col) for col in columns if col != "updated_at"]
values.append(current_time) # Add updated_at
placeholders = ", ".join(["?" for _ in range(len(values))])
self.conn.execute(f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
""", values)
else:
# If primary key is provided, use it for true upserting (merge)
if primary_key:
current_time = datetime.utcnow().isoformat()
for item in data:
# Check if record exists
cursor = self.conn.execute(f"SELECT * FROM {table_name} WHERE {primary_key} = ?", (item[primary_key],))
existing_record = cursor.fetchone()
if existing_record:
# Update existing record
if self.debug_writes:
logger.info(f"DEBUG WRITES: Updating existing record in '{table_name}' with {primary_key} = '{item[primary_key]}'")
# Prepare SET clause for update
set_clauses = []
update_values = []
for col in columns:
if col != primary_key: # Skip primary key in SET clause
if col == "updated_at":
set_clauses.append(f"{col} = ?")
update_values.append(current_time)
else:
set_clauses.append(f"{col} = ?")
update_values.append(item.get(col))
# Add primary key value for WHERE clause
update_values.append(item[primary_key])
update_sql = f"""
UPDATE {table_name}
SET {', '.join(set_clauses)}
WHERE {primary_key} = ?
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {update_sql}")
logger.info(f"DEBUG: With values: {update_values}")
self.conn.execute(update_sql, update_values)
else:
# Insert new record
if self.debug_writes:
logger.info(f"DEBUG WRITES: Inserting new record into '{table_name}' with {primary_key} = '{item[primary_key]}'")
# Prepare values with updated_at
values = [item.get(col) for col in columns if col != "updated_at"]
values.append(current_time) # Add updated_at
placeholders = ", ".join(["?" for _ in range(len(values))])
insert_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {insert_sql}")
logger.info(f"DEBUG: With values: {values}")
self.conn.execute(insert_sql, values)
else:
# If no primary key is provided, just insert all records
# This might create duplicates, but that's expected behavior without a primary key
current_time = datetime.utcnow().isoformat()
for item in data:
# Prepare values with updated_at
values = [item.get(col) for col in columns if col != "updated_at"]
values.append(current_time) # Add updated_at
placeholders = ", ".join(["?" for _ in range(len(values))])
self.conn.execute(f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
""", values)
# Commit transaction
self.conn.commit()
return len(data)
except Exception as e:
# Rollback transaction on error
self.conn.rollback()
logger.error(f"Error upserting data: {e}")
raise
def insert(self, table_name, data):
"""
Insert data into a table without upserting.
"""
if not self.conn:
self.connect()
if not data:
return 0
# Debug print the data being inserted
self._debug_print_data("INSERT", table_name, data)
# Get column names from the first data item
columns = list(data[0].keys())
if "updated_at" not in columns:
columns.append("updated_at")
# Get table schema to check column types
try:
cursor = self.conn.execute(f"PRAGMA table_info({table_name})")
schema_info = cursor.fetchall()
column_types = {row[1]: row[2] for row in schema_info}
logger.info(f"DEBUG: Table schema for {table_name}: {column_types}")
except Exception as e:
logger.warning(f"DEBUG: Could not get schema info: {e}")
column_types = {}
# Begin transaction
self.conn.execute("BEGIN TRANSACTION")
try:
# Insert new records
current_time = datetime.utcnow().isoformat()
count = 0
for item in data:
try:
# Prepare values with updated_at and convert timestamps
values = []
for col in columns:
if col == "updated_at":
values.append(current_time)
elif col in column_types and "TIMESTAMP" in column_types.get(col, "").upper() and item.get(col):
# Convert string timestamps to datetime objects for TIMESTAMP columns
try:
timestamp_str = item.get(col)
logger.info(f"DEBUG: Converting timestamp: {timestamp_str}, type: {type(timestamp_str)}")
# If it's already a datetime object, use it directly
if isinstance(timestamp_str, datetime):
values.append(timestamp_str.isoformat())
# If it's a string in ISO format, parse it
elif isinstance(timestamp_str, str):
# Try to parse the timestamp string
try:
# Try ISO format with Z (UTC)
dt = datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%SZ")
values.append(dt.isoformat())
logger.info(f"DEBUG: Converted to datetime: {dt}")
except ValueError:
try:
# Try ISO format without Z
dt = datetime.strptime(timestamp_str, "%Y-%m-%dT%H:%M:%S")
values.append(dt.isoformat())
logger.info(f"DEBUG: Converted to datetime: {dt}")
except ValueError:
# If all parsing fails, pass the string as is
logger.warning(f"DEBUG: Could not parse timestamp: {timestamp_str}")
values.append(timestamp_str)
else:
# If it's neither a datetime nor a string, use as is
values.append(timestamp_str)
except Exception as e:
logger.error(f"DEBUG: Error converting timestamp {item.get(col)}: {e}")
values.append(item.get(col))
else:
values.append(item.get(col))
placeholders = ", ".join(["?" for _ in range(len(values))])
insert_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {insert_sql}")
logger.info(f"DEBUG: With values: {values}")
self.conn.execute(insert_sql, values)
count += 1
except Exception as e:
logger.error(f"DEBUG: Error inserting row {count+1}: {e}")
logger.error(f"DEBUG: Row data: {item}")
raise
# Commit transaction
self.conn.commit()
return count
except Exception as e:
# Rollback transaction on error
self.conn.rollback()
logger.error(f"Error inserting data: {e}")
raise
def query(self, sql, params=None):
"""
Execute a query and return the results.
"""
if not self.conn:
self.connect()
cursor = self.conn.cursor()
if params:
cursor.execute(sql, params)
else:
cursor.execute(sql)
return cursor.fetchall()
try:
import psycopg2
import psycopg2.extras
class PostgreSQLDatabase(Database):
"""
PostgreSQL database implementation.
"""
def connect(self):
"""
Connect to PostgreSQL.
"""
pg_config = self.config["database"]["connection"]["postgresql"]
logger.info(f"Connecting to PostgreSQL database: {pg_config['host']}/{pg_config['database']}")
self.conn = psycopg2.connect(
host=pg_config["host"],
port=pg_config["port"],
database=pg_config["database"],
user=pg_config["username"],
password=pg_config["password"]
)
return self.conn
def create_table(self, table_name, schema, recreate=False):
"""
Create a table if it doesn't exist.
Args:
table_name: The name of the table to create
schema: The schema definition for the table
recreate: If True, drop the table if it exists before creating it
"""
if not self.conn:
self.connect()
# Create the table
with self.conn.cursor() as cursor:
# If recreate is True, drop the table if it exists
if recreate:
logger.info(f"Dropping table {table_name} if it exists (recreate=True)")
cursor.execute(f"""
DROP TABLE IF EXISTS {table_name}
""")
cursor.execute(f"""
CREATE TABLE IF NOT EXISTS {table_name} (
{schema}
)
""")
# Check if updated_at column exists, if not add it
cursor.execute(f"""
SELECT column_name
FROM information_schema.columns
WHERE table_name = '{table_name}' AND column_name = 'updated_at'
""")
if not cursor.fetchone():
logger.info(f"Adding updated_at column to table {table_name}")
cursor.execute(f"""
ALTER TABLE {table_name} ADD COLUMN updated_at TIMESTAMP
""")
self.conn.commit()
def upsert(self, table_name, data, primary_key=None):
"""
Upsert data into a table using a true merge approach.
This implementation updates existing records and inserts new ones without deleting any data.
"""
if not self.conn:
self.connect()
if not data:
return 0
# Debug print the data being upserted
self._debug_print_data("UPSERT", table_name, data, primary_key)
# Get column names from the first data item
columns = list(data[0].keys())
if "updated_at" not in columns:
columns.append("updated_at")
# Begin transaction
with self.conn.cursor() as cursor:
# Check if the table exists
cursor.execute(f"""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = '{table_name}'
)
""")
table_exists = cursor.fetchone()[0]
if not table_exists:
# If table doesn't exist, create it and insert all records
logger.info(f"Table {table_name} does not exist yet, creating and inserting all records")
# Create the table with columns from the data
column_defs = []
for col in columns:
if col == primary_key:
column_defs.append(f"{col} TEXT PRIMARY KEY")
elif col == "updated_at":
column_defs.append(f"{col} TIMESTAMP")
else:
column_defs.append(f"{col} TEXT")
cursor.execute(f"""
CREATE TABLE {table_name} (
{', '.join(column_defs)}
)
""")
# Insert all records
current_time = datetime.utcnow()
for item in data:
# Prepare values with updated_at
values = [item.get(col) for col in columns if col != "updated_at"]
values.append(current_time) # Add updated_at
placeholders = ", ".join(["%s" for _ in range(len(values))])
cursor.execute(f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
""", values)
else:
# If primary key is provided, use it for true upserting (merge)
if primary_key:
current_time = datetime.utcnow()
# PostgreSQL supports ON CONFLICT DO UPDATE for true upserts
for item in data:
# Prepare values with updated_at
values = [item.get(col) for col in columns if col != "updated_at"]
values.append(current_time) # Add updated_at
# Log the operation if debug_writes is enabled
if self.debug_writes:
cursor.execute(f"SELECT * FROM {table_name} WHERE {primary_key} = %s", (item[primary_key],))
existing_record = cursor.fetchone()
if existing_record:
logger.info(f"DEBUG WRITES: Updating existing record in '{table_name}' with {primary_key} = '{item[primary_key]}'")
else:
logger.info(f"DEBUG WRITES: Inserting new record into '{table_name}' with {primary_key} = '{item[primary_key]}'")
# Prepare the ON CONFLICT clause
non_pk_columns = [col for col in columns if col != primary_key]
set_clauses = [f"{col} = EXCLUDED.{col}" for col in non_pk_columns]
placeholders = ", ".join(["%s" for _ in range(len(values))])
upsert_sql = f"""
INSERT INTO {table_name} ({', '.join(columns)})
VALUES ({placeholders})
ON CONFLICT ({primary_key}) DO UPDATE
SET {', '.join(set_clauses)}
"""
if self.debug_writes:
logger.info(f"DEBUG: Executing SQL: {upsert_sql}")
logger.info(f"DEBUG: With values: {values}")
cursor.execute(upsert_sql, values)
else:
# If no primary key is provided, just insert all records
# This might create duplicates, but that's expected behavior without a primary key
current_time = datetime.utcnow()
for item in data:
# Prepare values with updated_at