CREATE TABLE `doc_snapshots` ( `page_id` text PRIMARY KEY NOT NULL, `yjs_state` blob NOT NULL, `snapshot_at` text DEFAULT (datetime('now')) NOT NULL, FOREIGN KEY (`page_id`) REFERENCES `pages`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE TABLE `invites` ( `id` text PRIMARY KEY NOT NULL, `email` text, `workspace_id` text NOT NULL, `invited_by` text NOT NULL, `role` text DEFAULT 'member' NOT NULL, `token` text NOT NULL, `accepted_at` text, `accepted_by` text, `revoked_at` text, `expires_at` text NOT NULL, `created_at` text DEFAULT (datetime('now')) NOT NULL, FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`invited_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`accepted_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE UNIQUE INDEX `invites_token_unique` ON `invites` (`token`);--> statement-breakpoint CREATE INDEX `idx_invites_token` ON `invites` (`token`);--> statement-breakpoint CREATE INDEX `idx_invites_email` ON `invites` (`email`);--> statement-breakpoint CREATE TABLE `memberships` ( `user_id` text NOT NULL, `workspace_id` text NOT NULL, `role` text NOT NULL, `joined_at` text DEFAULT (datetime('now')) NOT NULL, PRIMARY KEY(`user_id`, `workspace_id`), FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE TABLE `page_shares` ( `id` text PRIMARY KEY NOT NULL, `page_id` text NOT NULL, `grantee_type` text NOT NULL, `grantee_id` text, `permission` text NOT NULL, `link_token` text, `created_by` text NOT NULL, `created_at` text DEFAULT (datetime('now')) NOT NULL, FOREIGN KEY (`page_id`) REFERENCES `pages`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE UNIQUE INDEX `page_shares_link_token_unique` ON `page_shares` (`link_token`);--> statement-breakpoint CREATE INDEX `idx_page_shares_page` ON `page_shares` (`page_id`);--> statement-breakpoint CREATE INDEX `idx_page_shares_grantee` ON `page_shares` (`grantee_type`,`grantee_id`);--> statement-breakpoint CREATE TABLE `pages` ( `id` text PRIMARY KEY NOT NULL, `workspace_id` text NOT NULL, `parent_id` text, `title` text DEFAULT 'Untitled' NOT NULL, `icon` text, `cover_url` text, `position` real NOT NULL, `created_by` text NOT NULL, `created_at` text DEFAULT (datetime('now')) NOT NULL, `updated_at` text DEFAULT (datetime('now')) NOT NULL, `archived_at` text, FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`parent_id`) REFERENCES `pages`(`id`) ON UPDATE no action ON DELETE set null, FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE INDEX `idx_pages_parent` ON `pages` (`workspace_id`,`parent_id`,`position`);--> statement-breakpoint CREATE INDEX `idx_pages_workspace` ON `pages` (`workspace_id`,`archived_at`);--> statement-breakpoint CREATE TABLE `uploads` ( `id` text PRIMARY KEY NOT NULL, `workspace_id` text NOT NULL, `page_id` text, `uploaded_by` text NOT NULL, `filename` text NOT NULL, `content_type` text NOT NULL, `size_bytes` integer NOT NULL, `r2_key` text NOT NULL, `created_at` text DEFAULT (datetime('now')) NOT NULL, FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`page_id`) REFERENCES `pages`(`id`) ON UPDATE no action ON DELETE no action, FOREIGN KEY (`uploaded_by`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE TABLE `users` ( `id` text PRIMARY KEY NOT NULL, `email` text NOT NULL, `password_hash` text NOT NULL, `name` text NOT NULL, `avatar_url` text, `created_at` text DEFAULT (datetime('now')) NOT NULL, `updated_at` text DEFAULT (datetime('now')) NOT NULL ); --> statement-breakpoint CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint CREATE TABLE `workspaces` ( `id` text PRIMARY KEY NOT NULL, `name` text NOT NULL, `slug` text NOT NULL, `icon` text, `owner_id` text NOT NULL, `created_at` text DEFAULT (datetime('now')) NOT NULL, FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action ); --> statement-breakpoint CREATE UNIQUE INDEX `workspaces_slug_unique` ON `workspaces` (`slug`);