86 lines
5.1 KiB
SQL
86 lines
5.1 KiB
SQL
CREATE SCHEMA "habit_tracker";
|
|
--> statement-breakpoint
|
|
CREATE SCHEMA "intake_tracker";
|
|
--> statement-breakpoint
|
|
CREATE TABLE "habit_tracker"."habit" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"user_id" uuid NOT NULL,
|
|
"name" varchar(255) NOT NULL,
|
|
"description" text,
|
|
"frequency_type" varchar(20) NOT NULL,
|
|
"target_count" integer DEFAULT 1 NOT NULL,
|
|
"interval_days" integer,
|
|
"active" boolean DEFAULT true NOT NULL,
|
|
"updated_at" timestamp DEFAULT now() NOT NULL,
|
|
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
"deleted_at" timestamp,
|
|
CONSTRAINT "freqency_type_check" CHECK ("habit_tracker"."habit"."frequency_type" IN ('daily', 'interval', 'multi_daily')),
|
|
CONSTRAINT "target_count_check" CHECK ("habit_tracker"."habit"."target_count" > 0),
|
|
CONSTRAINT "interval_days_check" CHECK (("habit_tracker"."habit"."frequency_type" = 'interval' AND "habit_tracker"."habit"."interval_days" IS NOT NULL AND "habit_tracker"."habit"."interval_days" > 0) OR ("habit_tracker"."habit"."frequency_type" != 'interval' AND "habit_tracker"."habit"."interval_days" IS NULL))
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE TABLE "habit_tracker"."habit_completion" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"habit_id" uuid NOT NULL,
|
|
"notes" text,
|
|
"completed_at" timestamp DEFAULT now() NOT NULL,
|
|
"updated_at" timestamp DEFAULT now() NOT NULL,
|
|
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
"deleted_at" timestamp
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE TABLE "intake_tracker"."daily_summary" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"intake_metric_id" uuid NOT NULL,
|
|
"date" date NOT NULL,
|
|
"total_value" numeric(10, 2) NOT NULL,
|
|
"entry_count" integer NOT NULL,
|
|
"first_entry_at" timestamp,
|
|
"last_entry_at" timestamp,
|
|
"updated_at" timestamp DEFAULT now() NOT NULL,
|
|
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
"deleted_at" timestamp,
|
|
CONSTRAINT "daily_summary_intake_metric_id_date_unique" UNIQUE("intake_metric_id","date"),
|
|
CONSTRAINT "positive_total_check" CHECK ("intake_tracker"."daily_summary"."total_value" > 0),
|
|
CONSTRAINT "positive_count_check" CHECK ("intake_tracker"."daily_summary"."entry_count" > 0)
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE TABLE "intake_tracker"."intake_metric" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"user_id" uuid NOT NULL,
|
|
"metric_type" varchar(50) NOT NULL,
|
|
"unit" varchar(20) NOT NULL,
|
|
"display_name" varchar(100) NOT NULL,
|
|
"target_value" numeric(10, 2),
|
|
"min_value" numeric(10, 2),
|
|
"max_value" numeric(10, 2),
|
|
"is_cumulative" boolean DEFAULT true NOT NULL,
|
|
"active" boolean DEFAULT true NOT NULL,
|
|
"updated_at" timestamp DEFAULT now() NOT NULL,
|
|
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
"deleted_at" timestamp,
|
|
CONSTRAINT "intake_metric_user_id_metric_type_unique" UNIQUE("user_id","metric_type"),
|
|
CONSTRAINT "positive_target_check" CHECK ("intake_tracker"."intake_metric"."target_value" IS NULL OR "intake_tracker"."intake_metric"."target_value" > 0),
|
|
CONSTRAINT "positive_min_check" CHECK ("intake_tracker"."intake_metric"."min_value" IS NULL OR "intake_tracker"."intake_metric"."min_value" >= 0),
|
|
CONSTRAINT "positive_max_check" CHECK ("intake_tracker"."intake_metric"."max_value" IS NULL OR "intake_tracker"."intake_metric"."max_value" >= 0),
|
|
CONSTRAINT "min_max_check" CHECK ("intake_tracker"."intake_metric"."min_value" IS NULL OR "intake_tracker"."intake_metric"."max_value" IS NULL OR "intake_tracker"."intake_metric"."min_value" <= "intake_tracker"."intake_metric"."max_value")
|
|
);
|
|
--> statement-breakpoint
|
|
CREATE TABLE "intake_tracker"."intake_record" (
|
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
"intake_metric_id" uuid NOT NULL,
|
|
"value" numeric(10, 2),
|
|
"recorded_at" timestamp DEFAULT now() NOT NULL,
|
|
"notes" text,
|
|
"updated_at" timestamp DEFAULT now() NOT NULL,
|
|
"created_at" timestamp DEFAULT now() NOT NULL,
|
|
"deleted_at" timestamp,
|
|
CONSTRAINT "positive_value_check" CHECK ("intake_tracker"."intake_record"."value" > 0),
|
|
CONSTRAINT "recorded_at_not_future_check" CHECK ("intake_tracker"."intake_record"."recorded_at" <= NOW())
|
|
);
|
|
--> statement-breakpoint
|
|
ALTER TABLE "habit_tracker"."habit" ADD CONSTRAINT "habit_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "shared"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
ALTER TABLE "habit_tracker"."habit_completion" ADD CONSTRAINT "habit_completion_habit_id_habit_id_fk" FOREIGN KEY ("habit_id") REFERENCES "habit_tracker"."habit"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
ALTER TABLE "intake_tracker"."daily_summary" ADD CONSTRAINT "daily_summary_intake_metric_id_intake_metric_id_fk" FOREIGN KEY ("intake_metric_id") REFERENCES "intake_tracker"."intake_metric"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
ALTER TABLE "intake_tracker"."intake_metric" ADD CONSTRAINT "intake_metric_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "shared"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
|
ALTER TABLE "intake_tracker"."intake_record" ADD CONSTRAINT "intake_record_intake_metric_id_intake_metric_id_fk" FOREIGN KEY ("intake_metric_id") REFERENCES "intake_tracker"."intake_metric"("id") ON DELETE cascade ON UPDATE no action; |