-
-
Notifications
You must be signed in to change notification settings - Fork 53
fix: enforce database integrity add therapy_type table, unique constraint on assessment_results, and ON DELETE rules on foreign keys #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,8 +49,8 @@ CREATE TABLE session ( | |||||||||||||||||||||||||||||||
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||||||||||||||||||||||||||||||||
| created_at TIMESTAMPTZ DEFAULT NOW(), | ||||||||||||||||||||||||||||||||
| timestamp TIMESTAMPTZ NOT NULL, | ||||||||||||||||||||||||||||||||
| therapist_id UUID REFERENCES therapist(id), | ||||||||||||||||||||||||||||||||
| patient_id UUID REFERENCES patient(id), | ||||||||||||||||||||||||||||||||
| therapist_id UUID REFERENCES therapist(id) ON DELETE SET NULL, | ||||||||||||||||||||||||||||||||
| patient_id UUID REFERENCES patient(id) ON DELETE SET NULL, | ||||||||||||||||||||||||||||||||
| is_consultation BOOLEAN DEFAULT FALSE, | ||||||||||||||||||||||||||||||||
| mode INT2, | ||||||||||||||||||||||||||||||||
| duration INT4, | ||||||||||||||||||||||||||||||||
|
|
@@ -59,21 +59,29 @@ CREATE TABLE session ( | |||||||||||||||||||||||||||||||
| declined_reason TEXT, | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
| declined_reason TEXT, | |
| declined_reason TEXT |
Copilot
AI
Mar 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
daily_activity_logs.date was changed from TIMESTAMPTZ to DATE, but this change isnβt mentioned in the PR description and may break existing app queries/inserts that use ISO timestamps (e.g., .gte('date', startDate.toIso8601String())) and rely on time-of-day semantics. If the column should remain a timestamp, revert the type change; if it should be date-only, update the client code to write/query date-only values consistently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
π Script executed:
#!/bin/bash
# Find all usages of daily_activity_logs date column comparisons
rg -n -A2 -B2 "daily_activity_logs" --type dart | rg -A2 -B2 "date"Repository: AOSSIE-Org/NeuroTrack
Length of output: 1819
π Script executed:
# Check the full context around the date queries and DateTime creation
head -n 230 patient/lib/repository/supabase_patient_repository.dart | tail -n 100Repository: AOSSIE-Org/NeuroTrack
Length of output: 3830
π Script executed:
# Check what type of DateTime is being used (local vs UTC)
rg -B5 "startDate\.toIso8601String" patient/lib/repository/supabase_patient_repository.dartRepository: AOSSIE-Org/NeuroTrack
Length of output: 952
π Script executed:
# Check if there's any timezone handling in the DateTime creation
rg -B10 "DateTime\(dateTime\.year" patient/lib/repository/supabase_patient_repository.dartRepository: AOSSIE-Org/NeuroTrack
Length of output: 369
π Script executed:
# Create a quick test to verify what format Dart produces
cat > /tmp/test_datetime.dart << 'EOF'
void main() {
// Local DateTime
var local = DateTime(2024, 3, 9, 0, 0, 0);
print("Local DateTime.toIso8601String(): ${local.toIso8601String()}");
// UTC DateTime
var utc = DateTime.utc(2024, 3, 9, 0, 0, 0);
print("UTC DateTime.toIso8601String(): ${utc.toIso8601String()}");
// DateTime.now()
var now = DateTime.now();
print("DateTime.now().toIso8601String(): ${now.toIso8601String()}");
}
EOFRepository: AOSSIE-Org/NeuroTrack
Length of output: 47
π Script executed:
# Check the schema change to see what the original type was
git log --oneline -p supabase/schemas/schema.sql | grep -A5 -B5 "daily_activity_logs" | head -50Repository: AOSSIE-Org/NeuroTrack
Length of output: 436
π Script executed:
# Check if there are any test failures or bugs reported related to date queries
rg -i "timezone|utc|date.*issue|date.*bug" --type dart patient/lib/repository/ therapist/lib/repository/Repository: AOSSIE-Org/NeuroTrack
Length of output: 508
Fix the date query format inconsistency and timezone handling.
Client code in supabase_patient_repository.dart queries the date column using local DateTime.toIso8601String() (format: "2024-03-09T00:00:00"), which can cause off-by-one day errors depending on the app's timezone. The codebase uses UTC conversions elsewhere (e.g., supabase_therapist_repository.dart), suggesting this should too.
Update the queries to use .toUtc().toIso8601String() for consistency, or extract date-only strings using startDate.toIso8601String().split('T')[0] (e.g., "2024-03-09").
π€ Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@supabase/schemas/schema.sql` at line 168, The patient repository queries
against the SQL DATE column using local DateTime string which can cause timezone
off-by-one errors; in supabase_patient_repository.dart locate any call that
converts a DateTime to a string for the 'date' column (e.g., calls that use
toIso8601String() in query parameters) and change it to use either
startDate.toUtc().toIso8601String() for full-UTC timestamps or extract a
date-only value via startDate.toIso8601String().split('T')[0] (preferred for
DATE columns); update the query parameter construction so the 'date' filter
consistently uses the UTC or date-only string format to match other repositories
like supabase_therapist_repository.dart.
Copilot
AI
Mar 9, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The daily_activity_logs table definition is syntactically invalid: there is no comma between activity_items JSONB NOT NULL and patient_id ..., and there is also a trailing comma before the closing ). This will prevent schema.sql from executing. Add the missing comma separator and remove the trailing comma.
| activity_items JSONB NOT NULL | |
| patient_id UUID REFERENCES patient(id) ON DELETE CASCADE, | |
| activity_items JSONB NOT NULL, | |
| patient_id UUID REFERENCES patient(id) ON DELETE CASCADE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing comma causes SQL syntax error.
Line 169 is missing a comma after activity_items JSONB NOT NULL, which will cause a PostgreSQL syntax error when executing this schema. This blocks the PR's objective of making the schema executable from scratch.
Proposed fix
CREATE TABLE daily_activity_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE,
date DATE NOT NULL,
- activity_items JSONB NOT NULL
- patient_id UUID REFERENCES patient(id) ON DELETE CASCADE,
+ activity_items JSONB NOT NULL,
+ patient_id UUID REFERENCES patient(id) ON DELETE CASCADE
);π Committable suggestion
βΌοΈ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| CREATE TABLE daily_activity_logs ( | |
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | |
| activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE, | |
| date TIMESTAMPTZ NOT NULL, | |
| date DATE NOT NULL, | |
| activity_items JSONB NOT NULL | |
| patient_id UUID REFERENCES patient(id) ON DELETE CASCADE, | |
| ); | |
| CREATE TABLE daily_activity_logs ( | |
| id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | |
| activity_id UUID REFERENCES daily_activities(id) ON DELETE CASCADE, | |
| date DATE NOT NULL, | |
| activity_items JSONB NOT NULL, | |
| patient_id UUID REFERENCES patient(id) ON DELETE CASCADE | |
| ); |
π€ Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@supabase/schemas/schema.sql` around lines 165 - 171, The CREATE TABLE
daily_activity_logs has a missing comma after the column declaration
activity_items JSONB NOT NULL and also an extra trailing comma before the
closing ); β fix the schema by adding a comma after activity_items JSONB NOT
NULL and removing the trailing comma after patient_id UUID REFERENCES
patient(id) ON DELETE CASCADE so the column list is correctly comma-separated;
locate the CREATE TABLE daily_activity_logs block and update those two
punctuation issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR description says patient-owned data should cascade on patient deletion, but
session.patient_idis set toON DELETE SET NULL. This leaves sessions behind with no patient and doesnβt match the stated integrity rule; consider switching this FK toON DELETE CASCADE(or update the PR description/behavior consistently across patient-owned tables).