CREATE TABLE `media_assets` (
	`id` integer PRIMARY KEY NOT NULL,
	`user_id` integer NOT NULL,
	`storage_key` text NOT NULL,
	`mime` text NOT NULL,
	`size_bytes` integer NOT NULL,
	`sha256` text,
	`variants_json` text,
	`status` text DEFAULT 'ready' NOT NULL,
	`first_referenced_at` integer,
	`dereferenced_at` integer,
	`created_at` integer DEFAULT (cast((julianday('now') - 2440587.5)*86400000 as integer)) NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `media_assets_storage_key_unique` ON `media_assets` (`storage_key`);--> statement-breakpoint
CREATE INDEX `media_assets_user_id_idx` ON `media_assets` (`user_id`);--> statement-breakpoint
CREATE TABLE `post_media_refs` (
	`post_id` integer NOT NULL,
	`asset_id` integer NOT NULL,
	PRIMARY KEY(`post_id`, `asset_id`),
	FOREIGN KEY (`post_id`) REFERENCES `posts`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`asset_id`) REFERENCES `media_assets`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `post_media_refs_asset_id_idx` ON `post_media_refs` (`asset_id`);