diff --git a/docs/db-schema.sql b/docs/db-schema.sql new file mode 100644 index 0000000..da1fad3 --- /dev/null +++ b/docs/db-schema.sql @@ -0,0 +1,242 @@ +-- MediaCrawler 监控层 —— 生产库表结构快照 +-- +-- 从 192.168.20.220 上的 mediacrawler_prod 里 SHOW CREATE TABLE 导出来的, +-- 2026-10-11。**这是一份快照**,不是迁移脚本 —— 真正的建表/改列由 +-- api/monitor/db.py::init_db() 在启动时按 ORM 元数据自动完成(create_all 建缺失的 +-- 表,_ensure_columns 补缺失的列)。 +-- +-- 要看最新的,重新导一遍就行: +-- docker compose exec -T mediacrawler python -c \ +-- 'import asyncio;from sqlalchemy import text;from api.monitor.db import get_session; +-- ...' # 见 docs/db-schema.sql 的生成方式,或问 Claude +-- + + +CREATE TABLE `monitor_comment` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `note_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `comment_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `content` text COLLATE utf8mb4_unicode_ci NOT NULL, + `nickname` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, + `creator_hash` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `create_time` bigint(20) DEFAULT NULL, + `like_count` int(11) DEFAULT NULL, + `sub_comment_count` int(11) NOT NULL, + `parent_comment_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `first_seen_run_id` int(11) DEFAULT NULL, + `first_seen_at` bigint(20) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_monitor_comment` (`task_id`,`note_id`,`comment_id`), + KEY `ix_monitor_comment_task_id` (`task_id`), + KEY `ix_monitor_comment_note_id` (`note_id`) +) ENGINE=InnoDB AUTO_INCREMENT=287 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_creator_alias` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `platform` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `creator_hash` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `alias` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `updated_at` bigint(20) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_creator_alias` (`platform`,`creator_hash`), + KEY `ix_monitor_creator_alias_platform` (`platform`), + KEY `ix_monitor_creator_alias_creator_hash` (`creator_hash`) +) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_creator_stat` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `run_id` int(11) NOT NULL, + `creator_hash` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `nickname` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `fans` bigint(20) DEFAULT NULL, + `total_favorited` bigint(20) DEFAULT NULL, + `works_count` bigint(20) DEFAULT NULL, + `following` bigint(20) DEFAULT NULL, + `captured_at` bigint(20) NOT NULL, + `creator_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + PRIMARY KEY (`id`), + UNIQUE KEY `uq_creator_stat` (`task_id`,`creator_hash`,`run_id`), + KEY `ix_monitor_creator_stat_task_id` (`task_id`), + KEY `ix_monitor_creator_stat_captured_at` (`captured_at`), + KEY `ix_monitor_creator_stat_creator_hash` (`creator_hash`), + KEY `ix_monitor_creator_stat_run_id` (`run_id`), + CONSTRAINT `monitor_creator_stat_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `monitor_task` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=90 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_event` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `run_id` int(11) DEFAULT NULL, + `type` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, + `severity` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `target_kind` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `target_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `title` text COLLATE utf8mb4_unicode_ci NOT NULL, + `payload_json` text COLLATE utf8mb4_unicode_ci NOT NULL, + `created_at` bigint(20) NOT NULL, + `is_read` tinyint(1) NOT NULL, + PRIMARY KEY (`id`), + KEY `ix_monitor_event_type` (`type`), + KEY `ix_monitor_event_task_id` (`task_id`), + KEY `ix_monitor_event_run_id` (`run_id`), + KEY `ix_monitor_event_created_at` (`created_at`) +) ENGINE=InnoDB AUTO_INCREMENT=380 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_note` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `note_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `title` text COLLATE utf8mb4_unicode_ci NOT NULL, + `note_url` text COLLATE utf8mb4_unicode_ci NOT NULL, + `cover` text COLLATE utf8mb4_unicode_ci NOT NULL, + `creator_hash` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `source_kind` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `published_at` bigint(20) DEFAULT NULL, + `first_seen_run_id` int(11) DEFAULT NULL, + `first_seen_at` bigint(20) NOT NULL, + `last_seen_run_id` int(11) DEFAULT NULL, + `last_seen_at` bigint(20) NOT NULL, + `creator_name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + PRIMARY KEY (`id`), + UNIQUE KEY `uq_monitor_note` (`task_id`,`note_id`), + KEY `ix_monitor_note_task_id` (`task_id`), + KEY `ix_monitor_note_note_id` (`note_id`), + CONSTRAINT `monitor_note_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `monitor_task` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=192 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_note_alias` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `platform` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `note_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `alias` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `updated_at` bigint(20) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_note_alias` (`platform`,`note_id`), + KEY `ix_monitor_note_alias_note_id` (`note_id`), + KEY `ix_monitor_note_alias_platform` (`platform`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_note_metric` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `note_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `run_id` int(11) NOT NULL, + `captured_at` bigint(20) NOT NULL, + `liked_count` int(11) DEFAULT NULL, + `comment_count` int(11) DEFAULT NULL, + `collected_count` int(11) DEFAULT NULL, + `share_count` int(11) DEFAULT NULL, + `raw_liked_count` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `raw_comment_count` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `raw_collected_count` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `raw_share_count` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_note_metric` (`task_id`,`note_id`,`run_id`), + KEY `ix_monitor_note_metric_task_id` (`task_id`), + KEY `ix_monitor_note_metric_note_id` (`note_id`), + KEY `ix_monitor_note_metric_run_id` (`run_id`) +) ENGINE=InnoDB AUTO_INCREMENT=404 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_note_tag` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `platform` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `note_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `tag_id` int(11) NOT NULL, + `created_at` bigint(20) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_note_tag` (`platform`,`note_id`,`tag_id`), + KEY `ix_monitor_note_tag_note_id` (`note_id`), + KEY `ix_monitor_note_tag_tag_id` (`tag_id`), + KEY `ix_monitor_note_tag_platform` (`platform`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_run` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `trigger` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `status` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `phase` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `save_data_path` text COLLATE utf8mb4_unicode_ci NOT NULL, + `queued_at` bigint(20) NOT NULL, + `not_before` bigint(20) NOT NULL, + `started_at` bigint(20) DEFAULT NULL, + `finished_at` bigint(20) DEFAULT NULL, + `exit_code` bigint(20) DEFAULT NULL, + `notes_fetched` int(11) NOT NULL, + `comments_fetched` int(11) NOT NULL, + `new_notes` int(11) NOT NULL, + `new_comments` int(11) NOT NULL, + `is_baseline` tinyint(1) NOT NULL, + `max_comments_count` int(11) NOT NULL, + `error_message` text COLLATE utf8mb4_unicode_ci, + PRIMARY KEY (`id`), + KEY `ix_monitor_run_status` (`status`), + KEY `ix_monitor_run_task_id` (`task_id`), + CONSTRAINT `monitor_run_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `monitor_task` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=73 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_setting` ( + `key` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `value` text COLLATE utf8mb4_unicode_ci NOT NULL, + `updated_at` bigint(20) NOT NULL, + PRIMARY KEY (`key`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_tag` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, + `color` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `sort_order` int(11) NOT NULL, + `created_at` bigint(20) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_tag_name` (`name`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_target` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `task_id` int(11) NOT NULL, + `kind` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `external_id` varchar(128) COLLATE utf8mb4_unicode_ci NOT NULL, + `xsec_token` varchar(512) COLLATE utf8mb4_unicode_ci NOT NULL, + `xsec_source` varchar(64) COLLATE utf8mb4_unicode_ci NOT NULL, + `raw_value` text COLLATE utf8mb4_unicode_ci NOT NULL, + `label` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, + `enabled` tinyint(1) NOT NULL, + `created_at` bigint(20) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `uq_monitor_target` (`task_id`,`kind`,`external_id`), + KEY `ix_monitor_target_task_id` (`task_id`), + CONSTRAINT `monitor_target_ibfk_1` FOREIGN KEY (`task_id`) REFERENCES `monitor_task` (`id`) ON DELETE CASCADE +) ENGINE=InnoDB AUTO_INCREMENT=84 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +CREATE TABLE `monitor_task` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL, + `platform` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, + `mode` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL, + `enabled` tinyint(1) NOT NULL, + `interval_minutes` int(11) NOT NULL, + `max_notes_count` int(11) NOT NULL, + `enable_comments` tinyint(1) NOT NULL, + `max_comments_count` int(11) NOT NULL, + `run_timeout_seconds` int(11) NOT NULL, + `notify_enabled` tinyint(1) NOT NULL, + `next_run_at` bigint(20) DEFAULT NULL, + `last_run_at` bigint(20) DEFAULT NULL, + `last_status` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL, + `last_error` text COLLATE utf8mb4_unicode_ci, + `last_notified_at` bigint(20) DEFAULT NULL, + `created_at` bigint(20) NOT NULL, + `updated_at` bigint(20) NOT NULL, + `schedule_mode` varchar(16) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'interval', + `schedule_hours` varchar(96) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `schedule_days` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '', + `schedule_minute` int(11) NOT NULL DEFAULT '0', + `notify_failures` tinyint(1) NOT NULL DEFAULT '1', + PRIMARY KEY (`id`), + KEY `ix_monitor_task_next_run_at` (`next_run_at`) +) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +--- stderr ---