-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
807 lines (724 loc) · 30.2 KB
/
database.py
File metadata and controls
807 lines (724 loc) · 30.2 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
"""Database configuration."""
import uuid
import datetime
from typing import Set, List, Any
from contextvars import ContextVar
import enum
import sqlalchemy
from sqlalchemy.ext.mutable import MutableDict, MutableList
from sqlalchemy import (
Column,
Uuid,
DateTime,
ForeignKey,
Table,
String,
UniqueConstraint,
Text,
Enum,
Boolean,
JSON,
Integer,
BigInteger,
Index,
CheckConstraint,
event,
)
from sqlalchemy.orm import (
DeclarativeBase,
sessionmaker,
Session,
relationship,
mapped_column,
Mapped,
Mapper,
)
from sqlalchemy.sql import func
from sqlalchemy.pool import StaticPool
from .config import engine_vars, ssl_env_vars, setup_database_vars
from .authn import get_password_hash, get_api_key_hash
class Base(DeclarativeBase):
pass
LocalSession = None
local_session: ContextVar[Session] = ContextVar("local_session")
db_engine = None
# GCP MYSQL will throw an error if we don't specify the length for any varchar
# fields. So we can't use mapped_column in string cases.
VAR_CHAR_LENGTH = 36
VAR_CHAR_STANDARD_LENGTH = 255
# Constants for the local env
LOCAL_INST_UUID = uuid.UUID("14c81c50-935e-4151-8561-c2fc3bdabc0f")
LOCAL_USER_UUID = uuid.UUID("f21a3e53-c967-404e-91fd-f657cb922c39")
LOCAL_APIKEY_UUID = uuid.UUID("cd65804d-c597-40c2-bc89-0b0810e66346")
LOCAL_USER_EMAIL = "tester@datakind.org"
LOCAL_PASSWORD = "tester_password"
DATETIME_TESTING = datetime.datetime(2024, 12, 26, 19, 37, 59, 753357)
@event.listens_for(Mapper, "before_insert")
@event.listens_for(Mapper, "before_update")
def validate_string_lengths(mapper, connection, target):
for column in mapper.columns:
col_type = column.type
if isinstance(col_type, String) and col_type.length:
val = getattr(target, column.name, None)
if isinstance(val, str) and len(val) > col_type.length:
raise ValueError(
f"Value for '{column.name}' exceeds max length "
f"{col_type.length}: {len(val)} characters provided"
)
def init_db(env: str) -> None:
"""Initialize the database for LOCAL and DEV environemtns for ease of use."""
# add some sample users to the database for development utility.
if env not in ("LOCAL", "DEV"):
# this init db setup only applies for Local and Dev.
return
try:
with sqlalchemy.orm.Session(db_engine) as session:
# Only create a fake user in the local environment. The backend doesn't allow creating users, so
# in dev, users should be created in the frontend.
if env == "LOCAL":
session.merge(
AccountTable(
id=LOCAL_USER_UUID,
inst_id=None,
name="Tester S",
email=LOCAL_USER_EMAIL,
email_verified_at=None,
password=get_password_hash(LOCAL_PASSWORD),
access_type="DATAKINDER",
created_at=DATETIME_TESTING,
updated_at=DATETIME_TESTING,
)
)
session.merge(
InstTable(
id=LOCAL_INST_UUID,
name="Foobar University",
created_at=DATETIME_TESTING,
updated_at=DATETIME_TESTING,
)
)
session.merge(
ApiKeyTable(
id=LOCAL_APIKEY_UUID,
hashed_key_value=get_api_key_hash("key_1"),
allows_enduser=True,
created_at=DATETIME_TESTING,
updated_at=DATETIME_TESTING,
created_by=LOCAL_USER_UUID,
access_type="DATAKINDER",
valid=True,
)
)
# Create test files and batches for LOCAL environment
if env == "LOCAL":
# Create test files
test_file_1 = FileTable(
id=uuid.UUID("f0bb3a20-6d92-4254-afed-6a72f43c562a"),
inst_id=LOCAL_INST_UUID,
name="test_course_file.csv",
source="MANUAL_UPLOAD",
uploader=LOCAL_USER_UUID,
sst_generated=False,
valid=True,
schemas=["COURSE"], # Using string literal to avoid circular import
created_at=DATETIME_TESTING,
updated_at=DATETIME_TESTING,
)
test_file_2 = FileTable(
id=uuid.UUID("cb02d06c-2a59-486a-9bdd-d394a4fcb833"),
inst_id=LOCAL_INST_UUID,
name="test_cohort_file.csv",
source="MANUAL_UPLOAD",
uploader=LOCAL_USER_UUID,
sst_generated=False,
valid=True,
schemas=[
"STUDENT"
], # Using string literal to avoid circular import
created_at=DATETIME_TESTING,
updated_at=DATETIME_TESTING,
)
# Create test batch for LOCAL_INST_UUID (using a different ID)
test_batch = BatchTable(
id=uuid.UUID("f0bb3a20-6d92-4254-afed-6a72f43c562b"),
inst_id=LOCAL_INST_UUID,
name="test_batch_1",
created_by=LOCAL_USER_UUID,
created_at=DATETIME_TESTING,
updated_at=DATETIME_TESTING,
)
# Associate files with batch
test_batch.files.add(test_file_1)
test_batch.files.add(test_file_2)
session.merge(test_file_1)
session.merge(test_file_2)
session.merge(test_batch)
session.commit()
except Exception as e:
session.rollback()
raise e
finally:
session.close()
class InstTable(Base):
"""The institution overview table that maps ids to names. The parent table to
all other tables except for AccountHistory and JobTable."""
__tablename__ = "inst"
id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4
)
# Linked children tables.
accounts: Mapped[Set["AccountTable"]] = relationship(back_populates="inst")
apikeys: Mapped[Set["ApiKeyTable"]] = relationship(back_populates="inst")
files: Mapped[Set["FileTable"]] = relationship(back_populates="inst")
batches: Mapped[Set["BatchTable"]] = relationship(back_populates="inst")
account_histories: Mapped[List["AccountHistoryTable"]] = relationship(
back_populates="inst"
)
models: Mapped[Set["ModelTable"]] = relationship(back_populates="inst")
schemas_registry: Mapped[List["SchemaRegistryTable"]] = relationship(
"SchemaRegistryTable", back_populates="inst", cascade="all, delete-orphan"
)
name: Mapped[str] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False, unique=True
)
# If retention unset, the Datakind default is used. File-level retentions overrides
# this value.
retention_days: Mapped[int] = mapped_column(nullable=True)
# The emails for which self sign up will be allowed for this institution and will automatically be assigned to this institution.
# The dict structure is {email: AccessType string}
allowed_emails: Mapped[dict[str, str]] = mapped_column(
MutableDict.as_mutable(JSON()),
nullable=False,
default=dict,
)
# Schemas that are allowed for validation.
schemas: Mapped[list[str]] = mapped_column(
MutableList.as_mutable(JSON()), nullable=False, default=list
)
state: Mapped[str | None] = mapped_column(String(VAR_CHAR_LENGTH), nullable=True)
# Only populated for PDP schools.
pdp_id: Mapped[str | None] = mapped_column(String(VAR_CHAR_LENGTH), nullable=True)
# Only populated for Edvise schools.
edvise_id: Mapped[str | None] = mapped_column(
String(VAR_CHAR_LENGTH), nullable=True
)
# Only populated for Legacy schools (any-format uploads).
legacy_id: Mapped[str | None] = mapped_column(
String(VAR_CHAR_LENGTH), nullable=True
)
# Only populated for GenAI onboarding schools (loose uploads; mapping pipeline).
genai_id: Mapped[str | None] = mapped_column(
String(VAR_CHAR_LENGTH), nullable=True, unique=True
)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
onupdate=func.now(),
nullable=False,
default=func.now(),
)
created_by: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=True)
# Within the institutions, the set of name + state should be unique
__table_args__ = (UniqueConstraint("name", "state", name="inst_name_state_uc"),)
class ApiKeyTable(Base):
"""API KEYS should match the format generated by `openssl rand -hex 32`"""
__tablename__ = "apikey"
id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4
)
# A hash of the key_value, so the user must store the generated key_value secretly.
hashed_key_value: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False, unique=True
)
# Set the foreign key to link to the institution table.
inst_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("inst.id", ondelete="CASCADE"),
nullable=True,
)
inst: Mapped["InstTable"] = relationship(back_populates="apikeys")
created_by: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=False)
notes: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
# Whether this key allows changing the enduser. ONLY SET FOR THE FRONTEND KEY. Can only be set when the API key has DATAKINDER access type as this allows Datakinder level endusers.
allows_enduser: Mapped[bool] = mapped_column(nullable=True)
access_type: Mapped[str] = mapped_column(String(VAR_CHAR_LENGTH), nullable=False)
created_at = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
onupdate=func.now(),
nullable=False,
default=func.now(),
)
# API key must be valid and not deleted.
deleted: Mapped[bool] = mapped_column(nullable=True)
valid: Mapped[bool] = mapped_column(nullable=True)
# Within the api keys, the set of inst + access type should be unique
__table_args__ = (
UniqueConstraint("inst_id", "access_type", name="apikeys_inst_access_uc"),
)
class AccountTable(Base):
"""
NOTE: only users created by the frontend are accessible through the fronted. Users created by API calls can only directly call API calls. Frontend will not work.
The user accounts table"""
__tablename__ = "users" # Name to be compliant with Laravel.
id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4
)
# Set account histories to be children
account_histories: Mapped[List["AccountHistoryTable"]] = relationship(
back_populates="account"
)
# Set the foreign key to link to the institution table.
inst_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("inst.id", ondelete="CASCADE"),
nullable=True,
)
inst: Mapped["InstTable"] = relationship(back_populates="accounts")
name: Mapped[str] = mapped_column(String(VAR_CHAR_STANDARD_LENGTH), nullable=False)
email: Mapped[str] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False, unique=True
)
google_id: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
azure_id: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
email_verified_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
)
password: Mapped[str] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False
)
two_factor_secret: Mapped[str | None] = mapped_column(Text, nullable=True)
two_factor_recovery_codes: Mapped[str | None] = mapped_column(Text, nullable=True)
two_factor_confirmed_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
)
remember_token: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
# Required for team integration with laravel
current_team_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), nullable=True
)
access_type: Mapped[str | None] = mapped_column(
String(VAR_CHAR_LENGTH), nullable=True
)
# profile_photo_path : Mapped[dict[str, str]] = mapped_column(String(VAR_CHAR_LENGTH), nullable=True)
created_at = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
onupdate=func.now(),
nullable=False,
default=func.now(),
)
class AccountHistoryTable(Base):
"""The user history table"""
__tablename__ = "account_history"
id: Mapped[int] = mapped_column(
Integer, primary_key=True
) # Auto-increment should be default
timestamp: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), server_default=func.now(), nullable=False
)
# Set the parent foreign key to link to the users table.
account_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("users.id", ondelete="CASCADE"),
nullable=False,
)
account: Mapped["AccountTable"] = relationship(back_populates="account_histories")
# This field is nullable if the action was taken by a Datakinder.
inst_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("inst.id", ondelete="CASCADE"),
nullable=True,
)
inst: Mapped["InstTable"] = relationship(back_populates="account_histories")
action: Mapped[str] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False
)
resource_id: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=False)
# An intermediary association table allows bi-directional many-to-many between files and batches.
association_table = Table(
"file_batch_association_table",
Base.metadata,
Column(
"file_val",
Uuid(as_uuid=True),
ForeignKey("file.id", ondelete="CASCADE"),
primary_key=True,
),
Column(
"batch_val",
Uuid(as_uuid=True),
ForeignKey("batch.id", ondelete="CASCADE"),
primary_key=True,
),
)
class FileTable(Base):
"""The file table"""
__tablename__ = "file"
name: Mapped[str] = mapped_column(String(VAR_CHAR_STANDARD_LENGTH), nullable=False)
id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4
)
batches: Mapped[Set["BatchTable"]] = relationship(
secondary=association_table, back_populates="files"
)
# Set the parent foreign key to link to the institution table.
inst_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("inst.id", ondelete="CASCADE"),
nullable=False,
)
inst: Mapped["InstTable"] = relationship(back_populates="files")
# The size to the nearest mb.
# size_mb: Mapped[int] = mapped_column(nullable=False)
# Who uploaded the file. For SST generated files, this field would be null.
uploader: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=True)
# Can be PDP_SFTP, MANUAL_UPLOAD etc. May be empty for generated files.
source: Mapped[str | None] = mapped_column(String(VAR_CHAR_LENGTH), nullable=True)
# The schema type(s) of this file.
schemas: Mapped[list[str]] = mapped_column(
MutableList.as_mutable((JSON())),
nullable=False,
default=list,
)
# If null, the following is non-deleted.
# The deleted field indicates whether there is a pending deletion request on the data.
# The data may stil be available to Datakind debug role in a soft-delete state but for all
# intents and purposes is no longer accessible by the app.
deleted: Mapped[bool] = mapped_column(nullable=True)
# When the deletion request was made
deleted_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
)
retention_days: Mapped[int] = mapped_column(nullable=True)
# Whether the file was generated by SST. (e.g. was it input or output)
sst_generated: Mapped[bool] = mapped_column(nullable=False, default=False)
# Whether the file was approved (in the case of output) or valid for input.
valid: Mapped[bool] = mapped_column(nullable=False, default=False)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
onupdate=func.now(),
nullable=False,
default=func.now(),
)
# Within a given institution, there should be no duplicated file names.
__table_args__ = (UniqueConstraint("name", "inst_id", name="file_name_inst_uc"),)
class BatchTable(Base):
"""The batch table"""
__tablename__ = "batch"
id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4
)
# Set the parent foreign key to link to the institution table.
inst_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("inst.id", ondelete="CASCADE"),
nullable=False,
)
inst: Mapped["InstTable"] = relationship(back_populates="batches")
files: Mapped[Set["FileTable"]] = relationship(
secondary=association_table, back_populates="batches"
)
name: Mapped[str] = mapped_column(String(VAR_CHAR_STANDARD_LENGTH), nullable=False)
created_by: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True))
# If null, the following is non-deleted.
deleted: Mapped[bool] = mapped_column(nullable=True)
# If true, the batch is ready for use.
completed: Mapped[bool] = mapped_column(nullable=True)
# The time the deletion request was set.
deleted_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
onupdate=func.now(),
nullable=False,
default=func.now(),
)
# If a batch is deleted, the uuid of the user in the updated_by section is the deleter.
updated_by: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=True)
# Within a given institution, there should be no duplicated batch names.
__table_args__ = (UniqueConstraint("name", "inst_id", name="batch_name_inst_uc"),)
class ModelTable(Base):
"""The model table"""
__tablename__ = "model"
id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True), primary_key=True, default=uuid.uuid4
)
# Set the parent foreign key to link to the institution table.
inst_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("inst.id", ondelete="CASCADE"),
nullable=False,
)
inst: Mapped["InstTable"] = relationship(back_populates="models")
jobs: Mapped[Set["JobTable"]] = relationship(
back_populates="model",
passive_deletes=True,
)
name: Mapped[str] = mapped_column(String(VAR_CHAR_STANDARD_LENGTH), nullable=False)
# What configuration of schemas are allowed (list of maps e.g. [PDP Course : 1 + PDP Cohort : 1, X_schema :1 + Y_schema: 2])
schema_configs: Mapped[dict[str, str]] = mapped_column(JSON(), nullable=True)
created_by: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=True)
# If null, the following is non-deleted.
deleted: Mapped[bool] = mapped_column(nullable=True)
# If true, the model has been approved and is ready for use.
valid: Mapped[bool] = mapped_column(nullable=True)
# The time the deletion request was set.
deleted_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=True
)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
updated_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
onupdate=func.now(),
nullable=False,
default=func.now(),
)
# version is unused. version is not currently supported. The webapp only knows about the name of the model and any usages of a model will only use the live version.
version: Mapped[int] = mapped_column(Integer, default=0)
# Within a given institution, there should be no duplicated model names.
__table_args__ = (UniqueConstraint("name", "inst_id", name="model_name_inst_uc"),)
class JobTable(Base):
"""The job table"""
__tablename__ = "job"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
# Set the parent foreign key to link to the institution table.
model_id: Mapped[uuid.UUID] = mapped_column(
Uuid(as_uuid=True),
ForeignKey("model.id", ondelete="CASCADE"),
nullable=False,
)
model: Mapped["ModelTable"] = relationship(back_populates="jobs")
created_by: Mapped[uuid.UUID] = mapped_column(Uuid(as_uuid=True), nullable=False)
# The time the deletion request was set.
triggered_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True), nullable=False
)
batch_name: Mapped[str] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False
)
# The following will be empty if not completed or if job errored out. Getting additional details will require a call to the Databricks table.
output_filename: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
# Whether the file was approved.
output_valid: Mapped[bool] = mapped_column(nullable=True, default=False)
err_msg: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
completed: Mapped[bool] = mapped_column(nullable=True)
model_version: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
model_run_id: Mapped[str | None] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=True
)
class DocType(enum.Enum):
base = "base"
extension = "extension"
class SchemaRegistryTable(Base):
"""
Stores versioned schema documents:
- Base schema (doc_type=base, is_pdp=False, is_edvise=False, inst_id NULL)
- PDP shared extension (doc_type=extension, is_pdp=True, is_edvise=False, inst_id NULL)
- Edvise shared extension (doc_type=extension, is_pdp=False, is_edvise=True, inst_id NULL)
- Custom institution extension (doc_type=extension, is_pdp=False, is_edvise=False, inst_id=<UUID>)
Layers can reference a parent (extends_schema_id) that they extend.
"""
__tablename__ = "schema_registry"
schema_id: Mapped[int] = mapped_column(
Integer, primary_key=True, autoincrement=True
)
doc_type: Mapped[DocType] = mapped_column(
Enum(DocType, native_enum=False), nullable=False
)
# Nullable: NULL for base, PDP shared extension, and Edvise shared extension
inst_id: Mapped[uuid.UUID | None] = mapped_column(
ForeignKey("inst.id", ondelete="RESTRICT", onupdate="CASCADE"), nullable=True
)
is_pdp: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
is_edvise: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
version_label: Mapped[str] = mapped_column(
String(VAR_CHAR_STANDARD_LENGTH), nullable=False
)
extends_schema_id: Mapped[int | None] = mapped_column(
Integer,
ForeignKey(
"schema_registry.schema_id", ondelete="SET NULL", onupdate="CASCADE"
),
nullable=True,
)
json_doc: Mapped[dict] = mapped_column(
MutableDict.as_mutable(JSON()), nullable=False
)
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
created_at: Mapped[datetime.datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
nullable=False,
default=func.now(),
)
# ---------------- Relationships ----------------
inst: Mapped["InstTable | None"] = relationship(
"InstTable",
back_populates="schemas_registry", # we'll add this new relationship on InstTable (see below)
)
parent_schema: Mapped["SchemaRegistryTable | None"] = relationship(
"SchemaRegistryTable",
remote_side="SchemaRegistryTable.schema_id",
foreign_keys=[extends_schema_id],
back_populates="child_schemas",
)
child_schemas: Mapped[List["SchemaRegistryTable"]] = relationship(
"SchemaRegistryTable",
back_populates="parent_schema",
cascade="all, delete-orphan",
)
__table_args__ = (
UniqueConstraint("doc_type", "version_label", name="uq_base_version"),
UniqueConstraint("is_pdp", "version_label", name="uq_pdp_version"),
UniqueConstraint("inst_id", "version_label", name="uq_inst_version"),
CheckConstraint(
"NOT (is_pdp = 1 AND is_edvise = 1)", name="ck_no_pdp_and_edvise"
),
Index("idx_schema_active_base", "doc_type", "is_active"),
Index("idx_schema_active_pdp", "is_pdp", "is_active"),
Index("idx_schema_active_edvise", "is_edvise", "is_active"),
Index("idx_schema_active_inst", "inst_id", "is_active"),
)
# Convenience: identify logical namespace
@property
def namespace(self) -> str:
if self.doc_type == DocType.base:
return "base"
if self.is_pdp:
return "pdp"
if self.is_edvise:
return "edvise"
if self.inst_id:
return f"inst:{self.inst_id}"
return "unknown"
def get_session():
"""Get the session."""
sess: Session = LocalSession()
try:
yield sess
sess.commit()
except Exception as e:
sess.rollback()
raise e
finally:
sess.close()
def init_connection_pool_local() -> sqlalchemy.engine.base.Engine:
"""Creates a local sqlite db for local env testing."""
return sqlalchemy.create_engine(
"sqlite://",
echo_pool="debug",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
# The following functions are heavily inspired from the GCP open source sample code.
def connect_tcp_socket(
engine_args: dict[str, str], connect_args: dict[str, str]
) -> sqlalchemy.engine.base.Engine:
"""Initializes a TCP connection pool for a Cloud SQL instance of MySQL."""
pool = sqlalchemy.create_engine(
# Equivalent URL:
# mysql+pymysql://<db_user>:<db_pass>@<db_host>:<db_port>/<db_name>
# https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/cloud-sql/postgres/sqlalchemy/connect_tcp.py
sqlalchemy.engine.url.URL.create(
drivername="mysql+pymysql",
username=engine_args["DB_USER"],
password=engine_args["DB_PASS"],
host=engine_args["INSTANCE_HOST"],
port=int(engine_args["DB_PORT"]),
database=engine_args["DB_NAME"],
),
connect_args=connect_args,
# Pool size is the maximum number of permanent connections to keep.
pool_size=5,
# Temporarily exceeds the set pool_size if no connections are available.
max_overflow=2,
# The total number of concurrent connections for your application will be
# a total of pool_size and max_overflow.
# SQLAlchemy automatically uses delays between failed connection attempts,
# but provides no arguments for configuration.
# 'pool_timeout' is the maximum number of seconds to wait when retrieving a
# new connection from the pool. After the specified amount of time, an
# exception will be thrown.
pool_timeout=30, # 30 seconds
# 'pool_recycle' is the maximum number of seconds a connection can persist.
# Connections that live longer than the specified amount of time will be
# re-established
pool_recycle=1800, # 30 minutes
)
return pool
def init_connection_pool() -> sqlalchemy.engine.Engine:
"""Helper function to return SQLAlchemy connection pool."""
setup_database_vars()
# Set up ssl context for the connection args.
ssl_args = {
"ssl_ca": ssl_env_vars["DB_ROOT_CERT"],
"ssl_cert": ssl_env_vars["DB_CERT"],
"ssl_key": ssl_env_vars["DB_KEY"],
}
return connect_tcp_socket(engine_vars, ssl_args)
def setup_db(env: str) -> Any:
"""Setup Database. Called by all environments."""
# initialize connection pool
global db_engine
if env == "LOCAL":
db_engine = init_connection_pool_local()
else:
db_engine = init_connection_pool()
# Integrating FastAPI with SQL DB
# create SQLAlchemy ORM session
global LocalSession
LocalSession = sessionmaker(autocommit=False, autoflush=False, bind=db_engine)
# TODO: instead of create_all, check if exists and only create all if not existing
# https://stackoverflow.com/questions/33053241/sqlalchemy-if-table-does-not-exist
Base.metadata.create_all(db_engine)
if env in ("LOCAL", "DEV"):
# Creates a fake user in the local db
init_db(env)