feat(db): add versioned habits and calendar domain contracts

This commit is contained in:
syntaxbullet
2026-09-04 09:00:21 +02:00
parent d3cbf2f7dc
commit 7fefe0c7d8
10 changed files with 1170 additions and 1 deletions

View File

@@ -0,0 +1,75 @@
CREATE TABLE `combined_charts` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`name` text NOT NULL,
`habit_ids` text NOT NULL,
`settings` text NOT NULL,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `combined_charts_user_idx` ON `combined_charts` (`user_id`);--> statement-breakpoint
CREATE TABLE `habit_calendar_settings` (
`habit_id` text PRIMARY KEY NOT NULL,
`settings` text NOT NULL,
FOREIGN KEY (`habit_id`) REFERENCES `habits`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `habit_days` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`habit_id` text NOT NULL,
`revision_id` integer NOT NULL,
`date` text NOT NULL,
`timezone` text NOT NULL,
`ends_at` integer NOT NULL,
`count` integer DEFAULT 0 NOT NULL,
`done` integer DEFAULT false NOT NULL,
FOREIGN KEY (`habit_id`) REFERENCES `habits`(`id`) ON UPDATE no action ON DELETE no action,
FOREIGN KEY (`revision_id`) REFERENCES `habit_revisions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `habit_days_lookup_idx` ON `habit_days` (`habit_id`,`date`,`revision_id`);--> statement-breakpoint
CREATE TABLE `habit_revisions` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`habit_id` text NOT NULL,
`effective_date` text NOT NULL,
`config` text NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`habit_id`) REFERENCES `habits`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `habit_revisions_date_idx` ON `habit_revisions` (`habit_id`,`effective_date`,`id`);--> statement-breakpoint
CREATE TABLE `habits` (
`id` text PRIMARY KEY NOT NULL,
`user_id` text NOT NULL,
`created_date` text NOT NULL,
`materialized_through` text,
`created_at` integer NOT NULL,
FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `habits_user_idx` ON `habits` (`user_id`);--> statement-breakpoint
CREATE TABLE `progress_events` (
`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
`day_id` integer NOT NULL,
`occurrence_id` text,
`before` text NOT NULL,
`after` text NOT NULL,
`created_at` integer NOT NULL,
FOREIGN KEY (`day_id`) REFERENCES `habit_days`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `progress_events_day_idx` ON `progress_events` (`day_id`);--> statement-breakpoint
CREATE TABLE `task_occurrences` (
`id` text PRIMARY KEY NOT NULL,
`day_id` integer NOT NULL,
`task_id` text NOT NULL,
`name` text NOT NULL,
`done` integer DEFAULT false NOT NULL,
`expired_at` integer,
`updated_at` integer NOT NULL,
FOREIGN KEY (`day_id`) REFERENCES `habit_days`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `task_occurrences_day_idx` ON `task_occurrences` (`day_id`);