Skip to content

Commit abf8087

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 6ef37d7 + a07b0b3 commit abf8087

11 files changed

Lines changed: 192 additions & 39 deletions

File tree

src/apps/activity_assignments/db/schemas.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from sqlalchemy import Column, ForeignKey, Index
2+
from sqlalchemy.dialects.postgresql import UUID
23

34
from infrastructure.database import Base
45

@@ -8,10 +9,10 @@
89
class ActivityAssigmentSchema(Base):
910
__tablename__ = "activity_assignments"
1011

11-
activity_flow_id = Column(ForeignKey("flows.id", ondelete="RESTRICT"), nullable=True)
12-
activity_id = Column(ForeignKey("activities.id", ondelete="RESTRICT"), nullable=True)
13-
respondent_subject_id = Column(ForeignKey("subjects.id", ondelete="RESTRICT"), nullable=False)
14-
target_subject_id = Column(ForeignKey("subjects.id", ondelete="RESTRICT"), nullable=False)
12+
activity_flow_id = Column(UUID(as_uuid=True), nullable=True)
13+
activity_id = Column(UUID(as_uuid=True), nullable=True)
14+
respondent_subject_id = Column(ForeignKey("subjects.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)
15+
target_subject_id = Column(ForeignKey("subjects.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)
1516

1617
__table_args__ = (
1718
Index(

src/apps/alerts/db/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AlertSchema(Base):
2020
nullable=True,
2121
)
2222
subject_id = Column(
23-
ForeignKey("subjects.id", ondelete="RESTRICT"),
23+
ForeignKey("subjects.id", ondelete="RESTRICT", onupdate="CASCADE"),
2424
nullable=True,
2525
)
2626
is_watched = Column(Boolean(), nullable=False, default=False)

src/apps/answers/db/schemas.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
Text,
1010
Time,
1111
Unicode,
12+
UniqueConstraint,
1213
and_,
1314
asc,
1415
false,
@@ -31,9 +32,9 @@ class AnswerSchema(HistoryAware, Base):
3132
version = Column(Text())
3233
submit_id = Column(UUID(as_uuid=True))
3334
client = Column(JSONB())
34-
applet_history_id = Column(Text(), nullable=False, index=True)
35-
flow_history_id = Column(Text(), nullable=True, index=True)
36-
activity_history_id = Column(Text(), nullable=False, index=True)
35+
applet_history_id = Column(String(), nullable=False, index=True)
36+
flow_history_id = Column(String(), nullable=True, index=True)
37+
activity_history_id = Column(String(), nullable=False, index=True)
3738
respondent_id = Column(UUID(as_uuid=True), nullable=True, index=True)
3839
is_flow_completed = Column(Boolean(), nullable=True)
3940
migrated_data = Column(JSONB())
@@ -42,8 +43,8 @@ class AnswerSchema(HistoryAware, Base):
4243
input_subject_id = Column(UUID(as_uuid=True), nullable=True, index=True)
4344
relation = Column(String(length=20), nullable=True)
4445
consent_to_share = Column(Boolean(), default=False)
45-
event_history_id = Column(String(), nullable=True, index=True)
46-
device_id = Column(Text(), nullable=True, index=True)
46+
event_history_id = Column(String(), nullable=True)
47+
device_id = Column(Text(), nullable=True)
4748

4849
answer_item = relationship(
4950
"AnswerItemSchema",
@@ -101,8 +102,10 @@ def is_identifier_encrypted(cls):
101102
class AnswerEHRSchema(Base):
102103
__tablename__ = "answers_ehr"
103104

104-
submit_id = Column(UUID(as_uuid=True), index=True)
105-
activity_id = Column(UUID(as_uuid=True), index=True)
105+
submit_id = Column(UUID(as_uuid=True))
106+
activity_id = Column(UUID(as_uuid=True))
106107
ehr_storage_uri = Column(Text())
107108
ehr_ingestion_status = Column(Text())
108109
meta = Column(JSONB())
110+
111+
__table_args__ = (UniqueConstraint("submit_id", "activity_id", name="answers_ehr_submit_activity_key"),)

src/apps/authentication/db/schemas.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TokenBlacklistSchema(Base):
1111
jti = Column(Text(), index=True, unique=True, nullable=False)
1212
user_id = Column(UUID(as_uuid=True), nullable=False)
1313
exp = Column(DateTime(), index=True, nullable=False)
14-
type = Column(Enum(TokenPurpose), nullable=False)
14+
type = Column(Enum(TokenPurpose, name="token_purpose"), nullable=False)
1515
rjti = Column(Text(), index=True, nullable=True)
1616

1717

src/apps/schedule/db/schemas.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ class _BaseEventSchema:
1616
timer_type = Column(String(10), nullable=False) # NOT_SET, TIMER, IDLE
1717
version = Column(
1818
String(13),
19+
nullable=False,
1920
default=lambda: datetime.datetime.now(datetime.UTC).strftime("%Y%m%d") + "-1",
2021
server_default=text("TO_CHAR(timezone('utc', now()), 'YYYYMMDD') || '-1'"),
2122
)
2223

2324
# Periodicity columns
24-
periodicity = Column(String(10)) # Options: ONCE, DAILY, WEEKLY, WEEKDAYS, MONTHLY, ALWAYS
25+
periodicity = Column(String(10), nullable=False) # Options: ONCE, DAILY, WEEKLY, WEEKDAYS, MONTHLY, ALWAYS
2526
start_date = Column(Date, nullable=True)
2627
end_date = Column(Date, nullable=True)
2728
selected_date = Column(Date, nullable=True)
@@ -50,7 +51,7 @@ class AppletEventsSchema(Base):
5051
__tablename__ = "applet_events"
5152

5253
applet_id = Column(ForeignKey("applet_histories.id_version", ondelete="CASCADE"), nullable=False)
53-
event_id = Column(ForeignKey("event_histories.id_version", ondelete="CASCADE"), nullable=False)
54+
event_id = Column(ForeignKey("event_histories.id_version", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)
5455

5556
__table_args__ = (
5657
UniqueConstraint(
@@ -73,15 +74,15 @@ class _BaseNotificationSchema:
7374
class NotificationSchema(_BaseNotificationSchema, Base):
7475
__tablename__ = "notifications"
7576

76-
event_id = Column(ForeignKey("events.id", ondelete="CASCADE"), nullable=False)
77+
event_id = Column(ForeignKey("events.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)
7778

7879

7980
class NotificationHistorySchema(_BaseNotificationSchema, HistoryAware, Base):
8081
__tablename__ = "notification_histories"
8182

8283
id_version = Column(String(), primary_key=True)
8384
id = Column(UUID(as_uuid=True))
84-
event_id = Column(ForeignKey("event_histories.id_version", ondelete="RESTRICT"), nullable=False)
85+
event_id = Column(ForeignKey("event_histories.id_version", ondelete="RESTRICT", onupdate="CASCADE"), nullable=False)
8586

8687

8788
class _BaseReminderSchema:
@@ -92,15 +93,15 @@ class _BaseReminderSchema:
9293
class ReminderSchema(_BaseReminderSchema, Base):
9394
__tablename__ = "reminders"
9495

95-
event_id = Column(ForeignKey("events.id", ondelete="CASCADE"), nullable=False)
96+
event_id = Column(ForeignKey("events.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=False)
9697

9798

9899
class ReminderHistorySchema(_BaseReminderSchema, HistoryAware, Base):
99100
__tablename__ = "reminder_histories"
100101

101102
id_version = Column(String(), primary_key=True)
102103
id = Column(UUID(as_uuid=True))
103-
event_id = Column(ForeignKey("event_histories.id_version", ondelete="RESTRICT"), nullable=False)
104+
event_id = Column(ForeignKey("event_histories.id_version", ondelete="RESTRICT", onupdate="CASCADE"), nullable=False)
104105

105106

106107
class UserDeviceEventsHistorySchema(Base):

src/apps/subjects/db/schemas.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from sqlalchemy import Column, ForeignKey, Index, String, Unicode
1+
from sqlalchemy import Boolean, Column, ForeignKey, Index, String, Unicode
22
from sqlalchemy.dialects.postgresql import JSONB
33
from sqlalchemy_utils import StringEncryptedType
44

@@ -10,6 +10,7 @@
1010

1111
class SubjectSchema(Base):
1212
__tablename__ = "subjects"
13+
is_deleted = Column(Boolean(), nullable=False, default=False)
1314
applet_id = Column(ForeignKey("applets.id", ondelete="RESTRICT"), nullable=False)
1415
creator_id = Column(ForeignKey("users.id", ondelete="RESTRICT"), nullable=False)
1516
user_id = Column(ForeignKey("users.id", ondelete="RESTRICT"), nullable=True)
@@ -19,29 +20,25 @@ class SubjectSchema(Base):
1920
nickname = Column(StringEncryptedType(Unicode, get_key), default=None, nullable=True)
2021
tag = Column(String, default=None, nullable=True)
2122
secret_user_id = Column(String, nullable=False)
22-
language = Column(String(length=5))
23+
language = Column(String(length=20))
2324
integration_source = Column(String(length=20), nullable=True, default=None)
2425
meta = Column(JSONB(), nullable=True)
2526

2627
__table_args__ = (
27-
Index(
28-
None,
29-
"user_id",
30-
"applet_id",
31-
unique=True,
32-
),
28+
Index(None, "user_id", "applet_id", unique=True),
29+
Index("idx_subjects_meta", "meta", postgresql_using="gin"),
3330
)
3431

3532

3633
class SubjectRelationSchema(Base):
3734
__tablename__ = "subject_relations"
3835
source_subject_id = Column(
39-
ForeignKey("subjects.id", ondelete="RESTRICT"),
36+
ForeignKey("subjects.id", ondelete="RESTRICT", onupdate="CASCADE"),
4037
nullable=False,
4138
index=True,
4239
)
4340
target_subject_id = Column(
44-
ForeignKey("subjects.id", ondelete="RESTRICT"),
41+
ForeignKey("subjects.id", ondelete="RESTRICT", onupdate="CASCADE"),
4542
nullable=False,
4643
index=True,
4744
)

src/apps/users/db/schemas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
class UserSchema(Base):
1212
__tablename__ = "users"
1313

14-
email = Column(String(length=56), unique=True)
14+
email = Column(String(length=100), unique=True)
1515
email_encrypted = Column(StringEncryptedType(Unicode, get_key), default=None)
1616
first_name = Column(StringEncryptedType(Unicode, get_key))
1717
last_name = Column(StringEncryptedType(Unicode, get_key))
1818
hashed_password = Column(String(length=100))
1919
last_seen_at = Column(DateTime(), default=lambda: datetime.now(timezone.utc).replace(tzinfo=None))
2020
is_super_admin = Column(Boolean(), default=False, server_default="false")
21-
mfa_enabled = Column(Boolean(), default=False, server_default="false")
21+
mfa_enabled = Column(Boolean(), nullable=False, default=False, server_default="false")
2222
mfa_secret = Column(Text(), nullable=True) # Changed from String(100) to Text() for encrypted values
2323
pending_mfa_secret = Column(Text(), nullable=True)
2424
pending_mfa_created_at = Column(DateTime(), nullable=True)

src/apps/workspaces/db/schemas/user_applet_access.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,6 @@ class UserPinSchema(Base):
111111

112112
user_id = Column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
113113
pinned_user_id = Column(ForeignKey("users.id", ondelete="CASCADE"), nullable=True)
114-
pinned_subject_id = Column(ForeignKey("subjects.id", ondelete="CASCADE"), nullable=True)
114+
pinned_subject_id = Column(ForeignKey("subjects.id", ondelete="CASCADE", onupdate="CASCADE"), nullable=True)
115115
owner_id = Column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
116116
role = Column(Enum(UserPinRole, name="user_pin_role"), nullable=False)

src/config/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ def uploads_dir(self):
121121
"applets",
122122
"activities",
123123
"activity_flows",
124+
"activity_assignments",
124125
"themes",
125126
"logs",
126127
"schedule",
127128
"answers",
128129
"folders",
129-
"answers",
130130
"invitations",
131131
"workspaces",
132132
"transfer_ownership",
@@ -136,6 +136,7 @@ def uploads_dir(self):
136136
"job",
137137
"subjects",
138138
"integrations",
139+
"integrations.loris",
139140
],
140141
)
141142

src/infrastructure/database/migrations/base.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
Import all SQLAlchemy models here
88
"""
99

10-
import os
11-
1210
from config import settings
1311
from infrastructure.database import Base # noqa: F401, F403
1412

@@ -18,10 +16,7 @@ def import_db_schemas():
1816
of database schemas that are used by SQLAlchemy.
1917
"""
2018

21-
for app in os.listdir(settings.apps_dir):
22-
if app not in settings.migrations_apps:
23-
continue
24-
19+
for app in settings.migrations_apps:
2520
__import__(f"apps.{app}.db.schemas")
2621

2722

0 commit comments

Comments
 (0)