diff --git a/src/components/auth/login-form.tsx b/src/components/auth/login-form.tsx index f7199a48..54f25c6b 100644 --- a/src/components/auth/login-form.tsx +++ b/src/components/auth/login-form.tsx @@ -4,6 +4,7 @@ import { useEffect, useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { Fingerprint } from "lucide-react"; import { authClient } from "@/lib/auth/client"; +import { authErrorMessage } from "@/lib/auth/error-message"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; @@ -64,7 +65,8 @@ export function LoginForm({ callbackUrl }: LoginFormProps) { }); if (error) { - setError(AUTH_ERRORS[error.code ?? ""] ?? "Something went wrong. Please try again."); + console.error("Sign-in failed", error); + setError(authErrorMessage(error, AUTH_ERRORS)); return; } diff --git a/src/components/auth/signup-form.tsx b/src/components/auth/signup-form.tsx index 41c7a360..eeaf17dd 100644 --- a/src/components/auth/signup-form.tsx +++ b/src/components/auth/signup-form.tsx @@ -3,6 +3,7 @@ import { useState, useTransition } from "react"; import { useRouter } from "next/navigation"; import { authClient } from "@/lib/auth/client"; +import { authErrorMessage } from "@/lib/auth/error-message"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; @@ -37,7 +38,11 @@ export function SignupForm() { }); if (error) { - setError(AUTH_ERRORS[error.code ?? ""] ?? "Something went wrong. Please try again."); + // Keep the full detail in the browser console — the visible copy is + // deliberately short, but a self-hoster debugging a 500 wants both this + // and the matching server log line. + console.error("Sign-up failed", error); + setError(authErrorMessage(error, AUTH_ERRORS)); return; } diff --git a/src/db/migrations/0009_numerous_monster_badoon.sql b/src/db/migrations/0009_numerous_monster_badoon.sql new file mode 100644 index 00000000..52043123 --- /dev/null +++ b/src/db/migrations/0009_numerous_monster_badoon.sql @@ -0,0 +1,11 @@ +-- better-auth 1.7 added `account.issuer`, which namespaces an identity to the +-- authority that issued it. It is not optional: sign-in looks up the credential +-- account with `issuer = 'local:credential'` in the WHERE clause, so an existing +-- row left NULL here is an account nobody can log into any more. Add nullable, +-- backfill every existing row, then enforce NOT NULL. +-- +-- Ledgr configures no social providers, so in practice every row is a credential +-- account; the CASE covers a fork that added one before upgrading. +ALTER TABLE "account" ADD COLUMN "issuer" text;--> statement-breakpoint +UPDATE "account" SET "issuer" = CASE WHEN "provider_id" = 'credential' THEN 'local:credential' ELSE 'local:oauth:' || "provider_id" END WHERE "issuer" IS NULL;--> statement-breakpoint +ALTER TABLE "account" ALTER COLUMN "issuer" SET NOT NULL; diff --git a/src/db/migrations/meta/0009_snapshot.json b/src/db/migrations/meta/0009_snapshot.json new file mode 100644 index 00000000..b58493dd --- /dev/null +++ b/src/db/migrations/meta/0009_snapshot.json @@ -0,0 +1,3605 @@ +{ + "id": "284b9eb5-35f4-440a-adf1-670968956b17", + "prevId": "d79f58c1-23d0-4ba6-bf8a-455177205ea3", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.account": { + "name": "account", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "issuer": { + "name": "issuer", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "access_token": { + "name": "access_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "id_token": { + "name": "id_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_token_expires_at": { + "name": "access_token_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "refresh_token_expires_at": { + "name": "refresh_token_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "account_userId_idx": { + "name": "account_userId_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "account_user_id_user_id_fk": { + "name": "account_user_id_user_id_fk", + "tableFrom": "account", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.passkey": { + "name": "passkey", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "public_key": { + "name": "public_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "credential_id": { + "name": "credential_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "counter": { + "name": "counter", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "device_type": { + "name": "device_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "backed_up": { + "name": "backed_up", + "type": "boolean", + "primaryKey": false, + "notNull": true + }, + "transports": { + "name": "transports", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "aaguid": { + "name": "aaguid", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "passkey_userId_idx": { + "name": "passkey_userId_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "passkey_credentialID_idx": { + "name": "passkey_credentialID_idx", + "columns": [ + { + "expression": "credential_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "passkey_user_id_user_id_fk": { + "name": "passkey_user_id_user_id_fk", + "tableFrom": "passkey", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.session": { + "name": "session", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "ip_address": { + "name": "ip_address", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_agent": { + "name": "user_agent", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "session_userId_idx": { + "name": "session_userId_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "session_user_id_user_id_fk": { + "name": "session_user_id_user_id_fk", + "tableFrom": "session", + "tableTo": "user", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "session_token_unique": { + "name": "session_token_unique", + "nullsNotDistinct": false, + "columns": [ + "token" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user": { + "name": "user", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email_verified": { + "name": "email_verified", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "image": { + "name": "image", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "user_email_unique": { + "name": "user_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.verification": { + "name": "verification", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "verification_identifier_idx": { + "name": "verification_identifier_idx", + "columns": [ + { + "expression": "identifier", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.household_members": { + "name": "household_members", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "uq_household_user": { + "name": "uq_household_user", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "household_members_household_id_households_id_fk": { + "name": "household_members_household_id_households_id_fk", + "tableFrom": "household_members", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.households": { + "name": "households", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.user_settings": { + "name": "user_settings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "theme": { + "name": "theme", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'system'" + }, + "currency": { + "name": "currency", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'USD'" + }, + "mcp_enabled": { + "name": "mcp_enabled", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "dashboard_layout": { + "name": "dashboard_layout", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "demo_mode": { + "name": "demo_mode", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.bank_connections": { + "name": "bank_connections", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "credential": { + "name": "credential", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "plaid_institution_id": { + "name": "plaid_institution_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "plaid_item_id": { + "name": "plaid_item_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "institution_name": { + "name": "institution_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sync_cursor": { + "name": "sync_cursor", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'active'" + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "primary_color": { + "name": "primary_color", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_bank_connections_household": { + "name": "idx_bank_connections_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_bank_connections_household_institution": { + "name": "idx_bank_connections_household_institution", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "plaid_institution_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_bank_connections_plaid_item_id": { + "name": "idx_bank_connections_plaid_item_id", + "columns": [ + { + "expression": "plaid_item_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "bank_connections_household_id_households_id_fk": { + "name": "bank_connections_household_id_households_id_fk", + "tableFrom": "bank_connections", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.institution_logos": { + "name": "institution_logos", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "connection_id": { + "name": "connection_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "logo": { + "name": "logo", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "idx_institution_logos_connection": { + "name": "idx_institution_logos_connection", + "columns": [ + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "institution_logos_connection_id_bank_connections_id_fk": { + "name": "institution_logos_connection_id_bank_connections_id_fk", + "tableFrom": "institution_logos", + "tableTo": "bank_connections", + "columnsFrom": [ + "connection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.sync_log": { + "name": "sync_log", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "connection_id": { + "name": "connection_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "synced_at": { + "name": "synced_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "cursor_before": { + "name": "cursor_before", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "cursor_after": { + "name": "cursor_after", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "added_count": { + "name": "added_count", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "modified_count": { + "name": "modified_count", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "removed_count": { + "name": "removed_count", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_sync_log_connection_id": { + "name": "idx_sync_log_connection_id", + "columns": [ + { + "expression": "connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "sync_log_connection_id_bank_connections_id_fk": { + "name": "sync_log_connection_id_bank_connections_id_fk", + "tableFrom": "sync_log", + "tableTo": "bank_connections", + "columnsFrom": [ + "connection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.accounts": { + "name": "accounts", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "bank_connection_id": { + "name": "bank_connection_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "external_account_id": { + "name": "external_account_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "official_name": { + "name": "official_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "subtype": { + "name": "subtype", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "current_balance": { + "name": "current_balance", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "available_balance": { + "name": "available_balance", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "credit_limit": { + "name": "credit_limit", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "currency": { + "name": "currency", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'USD'" + }, + "is_manual": { + "name": "is_manual", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "is_hidden": { + "name": "is_hidden", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_accounts_household": { + "name": "idx_accounts_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_accounts_bank_connection": { + "name": "idx_accounts_bank_connection", + "columns": [ + { + "expression": "bank_connection_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_accounts_resurrection": { + "name": "idx_accounts_resurrection", + "columns": [ + { + "expression": "external_account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "where": "deleted_at IS NOT NULL", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "accounts_household_id_households_id_fk": { + "name": "accounts_household_id_households_id_fk", + "tableFrom": "accounts", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "accounts_bank_connection_id_bank_connections_id_fk": { + "name": "accounts_bank_connection_id_bank_connections_id_fk", + "tableFrom": "accounts", + "tableTo": "bank_connections", + "columnsFrom": [ + "bank_connection_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.balance_history": { + "name": "balance_history", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "date": { + "name": "date", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "balance": { + "name": "balance", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "uq_balance_account_date": { + "name": "uq_balance_account_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_balance_history_account_date": { + "name": "idx_balance_history_account_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "balance_history_account_id_accounts_id_fk": { + "name": "balance_history_account_id_accounts_id_fk", + "tableFrom": "balance_history", + "tableTo": "accounts", + "columnsFrom": [ + "account_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.categories": { + "name": "categories", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "group_id": { + "name": "group_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_income": { + "name": "is_income", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "is_system": { + "name": "is_system", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_categories_household": { + "name": "idx_categories_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_categories_group": { + "name": "idx_categories_group", + "columns": [ + { + "expression": "group_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "categories_household_id_households_id_fk": { + "name": "categories_household_id_households_id_fk", + "tableFrom": "categories", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "categories_group_id_category_groups_id_fk": { + "name": "categories_group_id_category_groups_id_fk", + "tableFrom": "categories", + "tableTo": "category_groups", + "columnsFrom": [ + "group_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.category_groups": { + "name": "category_groups", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sort_order": { + "name": "sort_order", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "is_system": { + "name": "is_system", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_catgroups_household": { + "name": "idx_catgroups_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "category_groups_household_id_households_id_fk": { + "name": "category_groups_household_id_households_id_fk", + "tableFrom": "category_groups", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.category_rules": { + "name": "category_rules", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "match_field": { + "name": "match_field", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'name'" + }, + "match_pattern": { + "name": "match_pattern", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "priority": { + "name": "priority", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_catrules_household": { + "name": "idx_catrules_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "priority", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "category_rules_household_id_households_id_fk": { + "name": "category_rules_household_id_households_id_fk", + "tableFrom": "category_rules", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "category_rules_category_id_categories_id_fk": { + "name": "category_rules_category_id_categories_id_fk", + "tableFrom": "category_rules", + "tableTo": "categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.merchants": { + "name": "merchants", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "raw_names": { + "name": "raw_names", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "logo_url": { + "name": "logo_url", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_merchants_household": { + "name": "idx_merchants_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_merchants_household_name": { + "name": "idx_merchants_household_name", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "name", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "merchants_household_id_households_id_fk": { + "name": "merchants_household_id_households_id_fk", + "tableFrom": "merchants", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "merchants_category_id_categories_id_fk": { + "name": "merchants_category_id_categories_id_fk", + "tableFrom": "merchants", + "tableTo": "categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.transaction_splits": { + "name": "transaction_splits", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "transaction_id": { + "name": "transaction_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "amount": { + "name": "amount", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_splits_txn": { + "name": "idx_splits_txn", + "columns": [ + { + "expression": "transaction_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_splits_category": { + "name": "idx_splits_category", + "columns": [ + { + "expression": "category_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "transaction_splits_transaction_id_transactions_id_fk": { + "name": "transaction_splits_transaction_id_transactions_id_fk", + "tableFrom": "transaction_splits", + "tableTo": "transactions", + "columnsFrom": [ + "transaction_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "transaction_splits_category_id_categories_id_fk": { + "name": "transaction_splits_category_id_categories_id_fk", + "tableFrom": "transaction_splits", + "tableTo": "categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.transactions": { + "name": "transactions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "pending_transaction_id": { + "name": "pending_transaction_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "merchant_id": { + "name": "merchant_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "recurring_transaction_id": { + "name": "recurring_transaction_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "transfer_pair_id": { + "name": "transfer_pair_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "date": { + "name": "date", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "original_name": { + "name": "original_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "amount": { + "name": "amount", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "normalized_amount": { + "name": "normalized_amount", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "currency": { + "name": "currency", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'USD'" + }, + "pending": { + "name": "pending", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "reviewed": { + "name": "reviewed", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "notes": { + "name": "notes", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "tags": { + "name": "tags", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_transfer": { + "name": "is_transfer", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "transfer_source": { + "name": "transfer_source", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "deleted_at": { + "name": "deleted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "external_id": { + "name": "external_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "provider": { + "name": "provider", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "ai_categorization_attempted_at": { + "name": "ai_categorization_attempted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "merchant_resolution_attempted_at": { + "name": "merchant_resolution_attempted_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "pfc_primary": { + "name": "pfc_primary", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "pfc_detailed": { + "name": "pfc_detailed", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "category_source": { + "name": "category_source", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "idx_txn_account_date": { + "name": "idx_txn_account_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_category_date": { + "name": "idx_txn_category_date", + "columns": [ + { + "expression": "category_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_household_date": { + "name": "idx_txn_household_date", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_date": { + "name": "idx_txn_date", + "columns": [ + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_merchant": { + "name": "idx_txn_merchant", + "columns": [ + { + "expression": "merchant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_household_merchant_resolution": { + "name": "idx_txn_household_merchant_resolution", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "merchant_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "merchant_resolution_attempted_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_transfer": { + "name": "idx_txn_transfer", + "columns": [ + { + "expression": "transfer_pair_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_external_id": { + "name": "idx_txn_external_id", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "external_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "where": "external_id IS NOT NULL", + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_household_reviewed_date": { + "name": "idx_txn_household_reviewed_date", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "reviewed", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_household_transfer_date": { + "name": "idx_txn_household_transfer_date", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "is_transfer", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_txn_household_date_id": { + "name": "idx_txn_household_date_id", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "transactions_account_id_accounts_id_fk": { + "name": "transactions_account_id_accounts_id_fk", + "tableFrom": "transactions", + "tableTo": "accounts", + "columnsFrom": [ + "account_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "transactions_household_id_households_id_fk": { + "name": "transactions_household_id_households_id_fk", + "tableFrom": "transactions", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "transactions_merchant_id_merchants_id_fk": { + "name": "transactions_merchant_id_merchants_id_fk", + "tableFrom": "transactions", + "tableTo": "merchants", + "columnsFrom": [ + "merchant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "transactions_category_id_categories_id_fk": { + "name": "transactions_category_id_categories_id_fk", + "tableFrom": "transactions", + "tableTo": "categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "transactions_recurring_transaction_id_recurring_transactions_id_fk": { + "name": "transactions_recurring_transaction_id_recurring_transactions_id_fk", + "tableFrom": "transactions", + "tableTo": "recurring_transactions", + "columnsFrom": [ + "recurring_transaction_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": { + "household_isolation": { + "name": "household_isolation", + "as": "PERMISSIVE", + "for": "ALL", + "to": [ + "public" + ], + "using": "household_id = current_setting('app.household_id', true)", + "withCheck": "household_id = current_setting('app.household_id', true)" + } + }, + "checkConstraints": {}, + "isRLSEnabled": true + }, + "public.budget_categories": { + "name": "budget_categories", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "budget_id": { + "name": "budget_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "limit_amount": { + "name": "limit_amount", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "rollover": { + "name": "rollover", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "is_fixed": { + "name": "is_fixed", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "uq_budgetcat_budget_category": { + "name": "uq_budgetcat_budget_category", + "columns": [ + { + "expression": "budget_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "category_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_budgetcat_budget": { + "name": "idx_budgetcat_budget", + "columns": [ + { + "expression": "budget_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "budget_categories_budget_id_budgets_id_fk": { + "name": "budget_categories_budget_id_budgets_id_fk", + "tableFrom": "budget_categories", + "tableTo": "budgets", + "columnsFrom": [ + "budget_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "budget_categories_category_id_categories_id_fk": { + "name": "budget_categories_category_id_categories_id_fk", + "tableFrom": "budget_categories", + "tableTo": "categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.budgets": { + "name": "budgets", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "month": { + "name": "month", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'category'" + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "uq_budget_household_month": { + "name": "uq_budget_household_month", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "month", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "budgets_household_id_households_id_fk": { + "name": "budgets_household_id_households_id_fk", + "tableFrom": "budgets", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.recurring_transactions": { + "name": "recurring_transactions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "plaid_stream_id": { + "name": "plaid_stream_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "merchant_id": { + "name": "merchant_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "category_id": { + "name": "category_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "average_amount": { + "name": "average_amount", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "last_amount": { + "name": "last_amount", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "frequency": { + "name": "frequency", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "last_date": { + "name": "last_date", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "next_date": { + "name": "next_date", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "is_active": { + "name": "is_active", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": true + }, + "is_income": { + "name": "is_income", + "type": "boolean", + "primaryKey": false, + "notNull": false, + "default": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_recurring_household": { + "name": "idx_recurring_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_recurring_next": { + "name": "idx_recurring_next", + "columns": [ + { + "expression": "next_date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_recurring_plaid_stream_id": { + "name": "idx_recurring_plaid_stream_id", + "columns": [ + { + "expression": "plaid_stream_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "recurring_transactions_household_id_households_id_fk": { + "name": "recurring_transactions_household_id_households_id_fk", + "tableFrom": "recurring_transactions", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "recurring_transactions_account_id_accounts_id_fk": { + "name": "recurring_transactions_account_id_accounts_id_fk", + "tableFrom": "recurring_transactions", + "tableTo": "accounts", + "columnsFrom": [ + "account_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "recurring_transactions_merchant_id_merchants_id_fk": { + "name": "recurring_transactions_merchant_id_merchants_id_fk", + "tableFrom": "recurring_transactions", + "tableTo": "merchants", + "columnsFrom": [ + "merchant_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + }, + "recurring_transactions_category_id_categories_id_fk": { + "name": "recurring_transactions_category_id_categories_id_fk", + "tableFrom": "recurring_transactions", + "tableTo": "categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "set null", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.holdings_history": { + "name": "holdings_history", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "plaid_security_id": { + "name": "plaid_security_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "security_name": { + "name": "security_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "ticker": { + "name": "ticker", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "quantity": { + "name": "quantity", + "type": "double precision", + "primaryKey": false, + "notNull": false + }, + "value": { + "name": "value", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "date": { + "name": "date", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_holdingshistory_account_date": { + "name": "idx_holdingshistory_account_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_holdingshistory_security": { + "name": "idx_holdingshistory_security", + "columns": [ + { + "expression": "plaid_security_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "uq_holdingshistory_account_security_date": { + "name": "uq_holdingshistory_account_security_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "plaid_security_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "holdings_history_account_id_accounts_id_fk": { + "name": "holdings_history_account_id_accounts_id_fk", + "tableFrom": "holdings_history", + "tableTo": "accounts", + "columnsFrom": [ + "account_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.investment_holdings": { + "name": "investment_holdings", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "plaid_security_id": { + "name": "plaid_security_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "security_name": { + "name": "security_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "ticker": { + "name": "ticker", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "quantity": { + "name": "quantity", + "type": "double precision", + "primaryKey": false, + "notNull": false + }, + "cost_basis": { + "name": "cost_basis", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "current_value": { + "name": "current_value", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "sector": { + "name": "sector", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "currency": { + "name": "currency", + "type": "text", + "primaryKey": false, + "notNull": false, + "default": "'USD'" + }, + "as_of_date": { + "name": "as_of_date", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_holdings_account": { + "name": "idx_holdings_account", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_holdings_date": { + "name": "idx_holdings_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "as_of_date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "idx_holdings_security": { + "name": "idx_holdings_security", + "columns": [ + { + "expression": "plaid_security_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "investment_holdings_account_id_accounts_id_fk": { + "name": "investment_holdings_account_id_accounts_id_fk", + "tableFrom": "investment_holdings", + "tableTo": "accounts", + "columnsFrom": [ + "account_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.investment_transactions": { + "name": "investment_transactions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "plaid_investment_transaction_id": { + "name": "plaid_investment_transaction_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "security_name": { + "name": "security_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "ticker": { + "name": "ticker", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "quantity": { + "name": "quantity", + "type": "double precision", + "primaryKey": false, + "notNull": false + }, + "price": { + "name": "price", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "amount": { + "name": "amount", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "fees": { + "name": "fees", + "type": "integer", + "primaryKey": false, + "notNull": false, + "default": 0 + }, + "date": { + "name": "date", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_invtxn_account_date": { + "name": "idx_invtxn_account_date", + "columns": [ + { + "expression": "account_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "date", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "uq_invtxn_plaid_id": { + "name": "uq_invtxn_plaid_id", + "columns": [ + { + "expression": "plaid_investment_transaction_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "investment_transactions_account_id_accounts_id_fk": { + "name": "investment_transactions_account_id_accounts_id_fk", + "tableFrom": "investment_transactions", + "tableTo": "accounts", + "columnsFrom": [ + "account_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.saved_reports": { + "name": "saved_reports", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "report_type": { + "name": "report_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "filters": { + "name": "filters", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "idx_saved_reports_household": { + "name": "idx_saved_reports_household", + "columns": [ + { + "expression": "household_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "saved_reports_household_id_households_id_fk": { + "name": "saved_reports_household_id_households_id_fk", + "tableFrom": "saved_reports", + "tableTo": "households", + "columnsFrom": [ + "household_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.oauth_clients": { + "name": "oauth_clients", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "client_id": { + "name": "client_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "client_name": { + "name": "client_name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "redirect_uris": { + "name": "redirect_uris", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "oauth_clients_client_id_unique": { + "name": "oauth_clients_client_id_unique", + "nullsNotDistinct": false, + "columns": [ + "client_id" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.oauth_codes": { + "name": "oauth_codes", + "schema": "", + "columns": { + "code": { + "name": "code", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "client_id": { + "name": "client_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "code_challenge": { + "name": "code_challenge", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "code_challenge_method": { + "name": "code_challenge_method", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'S256'" + }, + "redirect_uri": { + "name": "redirect_uri", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "used": { + "name": "used", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.oauth_consents": { + "name": "oauth_consents", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "client_id": { + "name": "client_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "granted_at": { + "name": "granted_at", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "uq_consent_user_client": { + "name": "uq_consent_user_client", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "client_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.oauth_refresh_tokens": { + "name": "oauth_refresh_tokens", + "schema": "", + "columns": { + "token": { + "name": "token", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "client_id": { + "name": "client_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "household_id": { + "name": "household_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "revoked": { + "name": "revoked", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json index b07f57b1..3a028a1b 100644 --- a/src/db/migrations/meta/_journal.json +++ b/src/db/migrations/meta/_journal.json @@ -64,6 +64,13 @@ "when": 1788026129238, "tag": "0008_deep_robbie_robertson", "breakpoints": true + }, + { + "idx": 9, + "version": "7", + "when": 1788305486484, + "tag": "0009_numerous_monster_badoon", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/db/schema/auth.test.ts b/src/db/schema/auth.test.ts new file mode 100644 index 00000000..73f6cab8 --- /dev/null +++ b/src/db/schema/auth.test.ts @@ -0,0 +1,66 @@ +import { describe, it, expect } from "vitest"; +import { getAuthTables } from "better-auth/db"; +import { getTableColumns } from "drizzle-orm"; +import { auth } from "@/lib/auth"; +import * as schema from "./index"; + +/** + * Better Auth validates its models against the Drizzle schema at request time, + * not at build time — a field the library expects but the schema lacks surfaces + * as a runtime 500 on the first request touching that model. Issue #133 was + * exactly that: better-auth 1.7 added `account.issuer` and every signup started + * 500ing with `The field "issuer" does not exist in the "account" Drizzle + * schema.` + * + * The drizzle adapter resolves a field as `schema[modelName][fieldName]` — the + * JS export key on the schema object and the property key on the table, not the + * SQL column name — so that is what this asserts. It covers the core models + * plus every configured plugin's, and is driven by `auth.options`, so a plugin + * added in `src/lib/auth/index.ts` is checked here without editing this file. + */ + +const expectedTables = getAuthTables(auth.options); +const schemaExports = schema as Record; + +describe("Better Auth model shape vs. Drizzle schema", () => { + for (const [model, definition] of Object.entries(expectedTables)) { + const modelName = definition.modelName; + + describe(`${model} -> "${modelName}"`, () => { + const table = schemaExports[modelName]; + + it("is exported from src/db/schema", () => { + expect(table, `no schema export named "${modelName}"`).toBeDefined(); + }); + + if (!table) return; + + const columns = getTableColumns( + table as Parameters[0], + ); + + for (const [field, attr] of Object.entries(definition.fields)) { + // `fieldName` is the property Better Auth reads off the table object; + // it only differs from the model field when explicitly remapped. + const property = attr.fieldName ?? field; + + it(`has field "${property}"`, () => { + expect( + columns[property], + `Better Auth expects "${modelName}"."${property}"`, + ).toBeDefined(); + }); + + // A field Better Auth always writes must not be nullable — a NULL there + // is a row its own lookups can never match. `account.issuer` is the + // case that matters: sign-in filters on it, so a NULL issuer is an + // account nobody can log into. + if (attr.required && attr.defaultValue === undefined) { + it(`has field "${property}" NOT NULL`, () => { + expect(columns[property]?.notNull).toBe(true); + }); + } + } + }); + } +}); diff --git a/src/db/schema/auth.ts b/src/db/schema/auth.ts index 7862005e..a3529c08 100644 --- a/src/db/schema/auth.ts +++ b/src/db/schema/auth.ts @@ -77,6 +77,11 @@ export const account = pgTable( id: text("id").primaryKey(), accountId: text("account_id").notNull(), providerId: text("provider_id").notNull(), + // Namespaces an identity to the authority that issued it, so a provider ID + // can't collide across authentication methods. Better Auth writes it on + // every account and *filters sign-in on it*, so it is never null: + // "local:credential" for email/password, "local:oauth:" for OAuth. + issuer: text("issuer").notNull(), userId: text("user_id") .notNull() .references(() => user.id, { onDelete: "cascade" }), diff --git a/src/lib/auth/error-message.test.ts b/src/lib/auth/error-message.test.ts new file mode 100644 index 00000000..311333bf --- /dev/null +++ b/src/lib/auth/error-message.test.ts @@ -0,0 +1,52 @@ +import { describe, it, expect } from "vitest"; +import { + authErrorMessage, + GENERIC_ERROR, + SERVER_ERROR, +} from "./error-message"; + +describe("authErrorMessage", () => { + const known = { USER_ALREADY_EXISTS: "An account with this email already exists." }; + + it("prefers form-specific copy for a code it knows", () => { + expect( + authErrorMessage({ code: "USER_ALREADY_EXISTS", status: 422 }, known), + ).toBe(known.USER_ALREADY_EXISTS); + }); + + // The #133 case: a 500 carrying no code used to render "please try again", + // which is advice that can never work. + it("names a 500 as a server-side problem and points at the logs", () => { + expect( + authErrorMessage({ status: 500, statusText: "Internal Server Error" }), + ).toBe(SERVER_ERROR); + expect(SERVER_ERROR).not.toBe(GENERIC_ERROR); + }); + + it("does not echo the server's message on a 5xx", () => { + expect( + authErrorMessage({ + status: 500, + message: 'The field "issuer" does not exist in the "account" Drizzle schema', + }), + ).toBe(SERVER_ERROR); + }); + + it("surfaces a 4xx message, which Better Auth writes for end users", () => { + expect( + authErrorMessage({ status: 400, message: "Password is too short" }), + ).toBe("Password is too short"); + }); + + it("falls back to the generic message when there is nothing specific", () => { + expect(authErrorMessage({ status: 400 })).toBe(GENERIC_ERROR); + expect(authErrorMessage({})).toBe(GENERIC_ERROR); + expect(authErrorMessage(null)).toBe(GENERIC_ERROR); + }); + + it("still maps a known code when it arrives on a 5xx", () => { + expect( + authErrorMessage({ code: "USER_ALREADY_EXISTS", status: 500 }, known), + ).toBe(known.USER_ALREADY_EXISTS); + }); +}); diff --git a/src/lib/auth/error-message.ts b/src/lib/auth/error-message.ts new file mode 100644 index 00000000..03394cee --- /dev/null +++ b/src/lib/auth/error-message.ts @@ -0,0 +1,49 @@ +/** + * Turns a Better Auth client error into something a self-hoster can act on. + * + * Both auth forms used to collapse every failure into "Something went wrong. + * Please try again." — which is actively misleading for a 500, where retrying + * can never work. Issue #133 sat unnoticed behind that message: the server was + * logging a precise schema error while the only person who could fix it was + * told to try again. + */ + +/** The shape Better Auth's client returns in `{ error }`. */ +export interface AuthClientError { + code?: string; + message?: string; + status?: number; + statusText?: string; +} + +export const GENERIC_ERROR = "Something went wrong. Please try again."; + +export const SERVER_ERROR = + "The server couldn't complete that request. This is a problem with the Ledgr server, not your details — check the server logs for the cause."; + +/** + * @param error the `error` from a Better Auth client call + * @param known context-specific copy for error codes this form expects + */ +export function authErrorMessage( + error: AuthClientError | null | undefined, + known: Record = {}, +): string { + if (!error) return GENERIC_ERROR; + + // A code we have written copy for always wins — it is the most specific. + const mapped = known[error.code ?? ""]; + if (mapped) return mapped; + + // 5xx is never the user's fault and never fixed by retrying. Deliberately + // does not echo the server's message: point at the logs, which hold the + // detail, rather than leaking internals into the browser. + if (error.status !== undefined && error.status >= 500) return SERVER_ERROR; + + // 4xx messages from Better Auth are written for end users ("Password is too + // short"), so they beat the generic fallback. + const message = error.message?.trim(); + if (message) return message; + + return GENERIC_ERROR; +} diff --git a/tests/integration/account-deletion.test.ts b/tests/integration/account-deletion.test.ts index 306edf2c..6d1d25f2 100644 --- a/tests/integration/account-deletion.test.ts +++ b/tests/integration/account-deletion.test.ts @@ -61,6 +61,7 @@ async function seedHousehold(db: LedgrDb, s: string) { id: `authacc-${s}`, accountId: `acc-${s}`, providerId: "credential", + issuer: "local:credential", userId: uid, password: "hashed", updatedAt: new Date(),