From 25e6b7599aa384950b1874a956c925cce3c702eb Mon Sep 17 00:00:00 2001 From: lorentz Date: Thu, 7 May 2026 07:35:51 -0400 Subject: [PATCH] feat(07.1-01): add user timezone column migration - Adds `timezone TEXT NOT NULL DEFAULT 'UTC'` to "user" table (TZ-01) - Backfills any NULL rows defensively - Idempotent: ADD COLUMN IF NOT EXISTS, no destructive ops - Storage timezone of existing TIMESTAMP columns unchanged --- migrations/083_add_user_timezone.sql | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 migrations/083_add_user_timezone.sql diff --git a/migrations/083_add_user_timezone.sql b/migrations/083_add_user_timezone.sql new file mode 100644 index 0000000..f1721f9 --- /dev/null +++ b/migrations/083_add_user_timezone.sql @@ -0,0 +1,26 @@ +-- ============================================================================= +-- Per-user IANA timezone (Phase 7.1 — TZ-01) +-- ============================================================================= +-- Adds a `timezone` column to the Better Auth "user" table so day/week +-- boundary math (dashboards, ticket filters, finance, engagement) can be +-- computed against the viewer's zone instead of server UTC. +-- +-- Storage zone for every existing TIMESTAMP / TIMESTAMPTZ column is unchanged. +-- Only display/range-bucketing logic in subsequent plans reads this column. +-- +-- The SQL default here is the literal 'UTC'. The application-level default +-- (process.env.DEFAULT_TIMEZONE || 'UTC') is enforced by Better Auth's +-- additionalField `defaultValue` in lib/auth.ts so new sessions see the env- +-- driven value even if a row was created without it. +-- ============================================================================= + +ALTER TABLE "user" + ADD COLUMN IF NOT EXISTS timezone TEXT NOT NULL DEFAULT 'UTC'; + +-- Backfill any rows that may have been created with NULL (defensive — the +-- DEFAULT clause above covers new inserts, but on managed Postgres a column +-- added with DEFAULT may briefly show NULL in flight on some replicas). +UPDATE "user" SET timezone = 'UTC' WHERE timezone IS NULL; + +COMMENT ON COLUMN "user".timezone IS + 'IANA timezone string (e.g. America/New_York). Storage timezone for all date columns remains UTC; this column only affects display and range-bucketing.';