From 7fb690afa4348e906adb8c4ac8055589776b6850 Mon Sep 17 00:00:00 2001 From: Ananya Date: Wed, 3 Jun 2026 03:49:07 +0530 Subject: [PATCH 1/3] Add D1 migrations and migrate.sh for notifications tables --- migrate.sh | 0 migrations/0001_initial_schema.sql | 81 +++++++++++++++++++++++++++ migrations/0002_add_notifications.sql | 0 wrangler.toml | 6 +- 4 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 migrate.sh create mode 100644 migrations/0001_initial_schema.sql create mode 100644 migrations/0002_add_notifications.sql diff --git a/migrate.sh b/migrate.sh new file mode 100644 index 0000000..e69de29 diff --git a/migrations/0001_initial_schema.sql b/migrations/0001_initial_schema.sql new file mode 100644 index 0000000..9294e2d --- /dev/null +++ b/migrations/0001_initial_schema.sql @@ -0,0 +1,81 @@ +-- Migration 0001: Initial schema + +CREATE TABLE IF NOT EXISTS users ( + id TEXT PRIMARY KEY, + username_hash TEXT NOT NULL UNIQUE, + email_hash TEXT NOT NULL UNIQUE, + name TEXT NOT NULL, + username TEXT NOT NULL, + email TEXT NOT NULL, + password_hash TEXT NOT NULL, + role TEXT NOT NULL, + created_at TEXT NOT NULL DEFAULT (datetime('now')) +); + +CREATE TABLE IF NOT EXISTS activities ( + id TEXT PRIMARY KEY, + title TEXT NOT NULL, + description TEXT, + type TEXT NOT NULL DEFAULT 'course', + format TEXT NOT NULL DEFAULT 'self_paced', + schedule_type TEXT NOT NULL DEFAULT 'ongoing', + host_id TEXT NOT NULL, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + FOREIGN KEY (host_id) REFERENCES users(id) +); + +CREATE TABLE IF NOT EXISTS sessions ( + id TEXT PRIMARY KEY, + activity_id TEXT NOT NULL, + title TEXT, + description TEXT, + start_time TEXT, + end_time TEXT, + location TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + FOREIGN KEY (activity_id) REFERENCES activities(id) +); + +CREATE TABLE IF NOT EXISTS enrollments ( + id TEXT PRIMARY KEY, + activity_id TEXT NOT NULL, + user_id TEXT NOT NULL, + role TEXT NOT NULL DEFAULT 'participant', + status TEXT NOT NULL DEFAULT 'active', + created_at TEXT NOT NULL DEFAULT (datetime('now')), + UNIQUE (activity_id, user_id), + FOREIGN KEY (activity_id) REFERENCES activities(id), + FOREIGN KEY (user_id) REFERENCES users(id) +); + +CREATE TABLE IF NOT EXISTS session_attendance ( + id TEXT PRIMARY KEY, + session_id TEXT NOT NULL, + user_id TEXT NOT NULL, + status TEXT NOT NULL DEFAULT 'registered', + created_at TEXT NOT NULL DEFAULT (datetime('now')), + UNIQUE (session_id, user_id), + FOREIGN KEY (session_id) REFERENCES sessions(id), + FOREIGN KEY (user_id) REFERENCES users(id) +); + +CREATE TABLE IF NOT EXISTS tags ( + id TEXT PRIMARY KEY, + name TEXT UNIQUE NOT NULL +); + +CREATE TABLE IF NOT EXISTS activity_tags ( + activity_id TEXT NOT NULL, + tag_id TEXT NOT NULL, + PRIMARY KEY (activity_id, tag_id), + FOREIGN KEY (activity_id) REFERENCES activities(id), + FOREIGN KEY (tag_id) REFERENCES tags(id) +); + +CREATE INDEX IF NOT EXISTS idx_activities_host ON activities(host_id); +CREATE INDEX IF NOT EXISTS idx_enrollments_activity ON enrollments(activity_id); +CREATE INDEX IF NOT EXISTS idx_enrollments_user ON enrollments(user_id); +CREATE INDEX IF NOT EXISTS idx_sessions_activity ON sessions(activity_id); +CREATE INDEX IF NOT EXISTS idx_sa_session ON session_attendance(session_id); +CREATE INDEX IF NOT EXISTS idx_sa_user ON session_attendance(user_id); +CREATE INDEX IF NOT EXISTS idx_at_activity ON activity_tags(activity_id); \ No newline at end of file diff --git a/migrations/0002_add_notifications.sql b/migrations/0002_add_notifications.sql new file mode 100644 index 0000000..e69de29 diff --git a/wrangler.toml b/wrangler.toml index e606b91..761e130 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -16,6 +16,7 @@ not_found_handling = "404-page" binding = "DB" database_name = "education_db" database_id = "a0021f2e-a8cc-4e20-8910-3c7290ba47a6" +migrations_dir = "migrations" # Virtual Classroom Durable Object @@ -49,4 +50,7 @@ invocation_logs = true [observability.traces] enabled = false persist = true -head_sampling_rate = 1 \ No newline at end of file +head_sampling_rate = 1 + +[build] +command = "bash migrate.sh" \ No newline at end of file From efcd0e36831370c35b6ce1499aca76bd97761f3c Mon Sep 17 00:00:00 2001 From: Ananya Date: Wed, 3 Jun 2026 03:51:35 +0530 Subject: [PATCH 2/3] Add D1 migrations and migrate.sh for notifications tables --- migrate.sh | 5 +++++ migrations/0002_add_notifications.sql | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/migrate.sh b/migrate.sh index e69de29..2edf7e1 100644 --- a/migrate.sh +++ b/migrate.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e +echo "Applying D1 migrations to education_db..." +npx wrangler d1 migrations apply education_db --remote +echo "Done." \ No newline at end of file diff --git a/migrations/0002_add_notifications.sql b/migrations/0002_add_notifications.sql index e69de29..9d87eec 100644 --- a/migrations/0002_add_notifications.sql +++ b/migrations/0002_add_notifications.sql @@ -0,0 +1,26 @@ +-- Migration 0002: Add notifications and notification_preferences tables + +CREATE TABLE IF NOT EXISTS notifications ( + id TEXT PRIMARY KEY, + user_id TEXT NOT NULL, + type TEXT NOT NULL, + title TEXT NOT NULL, + message TEXT NOT NULL, + is_read INTEGER NOT NULL DEFAULT 0, + related_id TEXT, + created_at TEXT NOT NULL DEFAULT (datetime('now')), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE +); + +CREATE INDEX IF NOT EXISTS idx_notif_user ON notifications(user_id); +CREATE INDEX IF NOT EXISTS idx_notif_unread ON notifications(user_id, is_read); +CREATE INDEX IF NOT EXISTS idx_notif_created ON notifications(user_id, created_at DESC); + +CREATE TABLE IF NOT EXISTS notification_preferences ( + user_id TEXT PRIMARY KEY, + enrollment_notify INTEGER NOT NULL DEFAULT 1, + session_notify INTEGER NOT NULL DEFAULT 1, + system_notify INTEGER NOT NULL DEFAULT 1, + updated_at TEXT NOT NULL DEFAULT (datetime('now')), + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE +); \ No newline at end of file From c8e15e0b1c961ac9b07958d8fcfde6261bf2ab2b Mon Sep 17 00:00:00 2001 From: Ananya Date: Wed, 3 Jun 2026 04:04:24 +0530 Subject: [PATCH 3/3] fixes --- migrate.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/migrate.sh b/migrate.sh index 2edf7e1..f290dd7 100644 --- a/migrate.sh +++ b/migrate.sh @@ -1,5 +1,5 @@ #!/bin/bash -set -e +set -euo pipefail echo "Applying D1 migrations to education_db..." -npx wrangler d1 migrations apply education_db --remote +npx --yes wrangler d1 migrations apply education_db --remote echo "Done." \ No newline at end of file