Files
go_blog/scripts/add_files_table.sql
T
dsh 09bf35d0dd feat: 新增全站统一上传文件表 files,迁移 attachments 数据(type=attachments)
- models/file.go:File 模型,字段与 Attachment 对齐并新增 Type 归属类型字段
- models/db.go:AutoMigrate 注册 File;启动时幂等迁移 attachments → files
  (按主键 id 对齐跳过已迁移行,含软删除行一并复制)
- scripts/add_files_table.sql:MariaDB 幂等迁移脚本(建表 + INSERT...SELECT)
- 线上库 blog_go:files 表已建立,15 条 attachments 记录已迁入,type='attachments'
2026-08-28 19:16:42 +08:00

40 lines
2.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Migration: 新增全站统一上传文件表 files,并把 attachments 数据迁入(type='attachments'
-- Date: 2026-08-28
-- Description: files 表用于管理全站所有上传文件(附件/头像/Logo 等),
-- Type 字段区分归属类型;attachments 历史数据逐行复制,type 填 'attachments'。
-- 幂等:表用 IF NOT EXISTS,数据按主键 id 对齐跳过已迁移行,可重复执行。
-- 1) 建表(与 GORM AutoMigrate 输出一致)
CREATE TABLE IF NOT EXISTS `files` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`type` varchar(32) DEFAULT 'attachments',
`article_id` bigint(20) unsigned DEFAULT NULL,
`session_token` varchar(64) DEFAULT NULL,
`uploader_id` bigint(20) unsigned DEFAULT NULL,
`filename` varchar(255) DEFAULT NULL,
`stored_name` varchar(64) DEFAULT NULL,
`ext` varchar(32) DEFAULT NULL,
`mime` varchar(128) DEFAULT NULL,
`size` bigint(20) DEFAULT 0,
`category` varchar(32) DEFAULT NULL,
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
`deleted_at` datetime(3) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `idx_files_type` (`type`),
KEY `idx_files_article_id` (`article_id`),
KEY `idx_files_session_token` (`session_token`),
KEY `idx_files_uploader_id` (`uploader_id`),
KEY `idx_files_stored_name` (`stored_name`),
KEY `idx_files_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- 2) 数据迁移:attachments 全部行(含软删除)复制进 filestype='attachments'
INSERT INTO `files`
(`id`,`type`,`article_id`,`session_token`,`uploader_id`,`filename`,`stored_name`,`ext`,`mime`,`size`,`category`,`created_at`,`updated_at`,`deleted_at`)
SELECT
a.`id`, 'attachments', a.`article_id`, a.`session_token`, a.`uploader_id`, a.`filename`,
a.`stored_name`, a.`ext`, a.`mime`, a.`size`, a.`category`, a.`created_at`, a.`updated_at`, a.`deleted_at`
FROM `attachments` a
WHERE NOT EXISTS (SELECT 1 FROM `files` f WHERE f.`id` = a.`id`);