Files
yunzerwebsiteallinone/sql/alter_password_store_to_multi_accounts.sql
T
2026-08-27 19:53:39 +08:00

79 lines
3.4 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.
-- ============================================================
-- 密码存储:单账号结构 -> 一个平台挂载多个账号(accounts JSON)
--
-- 使用说明:
-- 1. 先备份:
-- mysqldump -u用户 -p 库名 yz_platform_password_store yz_backend_password_store > pwd_backup.sql
-- 2. 按顺序执行下面的「第 1 步」「第 2 步」「第 3 步」。
-- 3. 如果某条 ALTER 报 "Duplicate column name 'accounts'",说明该列已存在,
-- 忽略该条错误继续往下执行即可。
-- 4. 「第 4 步」删除旧列不可逆,确认页面数据显示正常后再手动执行。
-- ============================================================
-- ---------- 第 1 步:新增 accounts 列 ----------
-- 报 Duplicate column name 表示已加过,忽略即可。
ALTER TABLE `yz_platform_password_store`
ADD COLUMN `accounts` longtext NULL
COMMENT '账号列表JSON: [{"username":"","password":"","registration_info":"","remark":""}]'
AFTER `url`;
ALTER TABLE `yz_backend_password_store`
ADD COLUMN `accounts` longtext NULL
COMMENT '账号列表JSON: [{"username":"","password":"","registration_info":"","remark":""}]'
AFTER `url`;
-- 旧表可能没有 delete_time 列,模型里有该字段,一并补上。
-- 报 Duplicate column name 同样忽略。
ALTER TABLE `yz_platform_password_store` ADD COLUMN `delete_time` datetime NULL;
ALTER TABLE `yz_backend_password_store` ADD COLUMN `delete_time` datetime NULL;
-- ---------- 第 2 步:把旧的单账号数据搬进 accounts ----------
-- 只处理 accounts 仍为空、且旧列确实有内容的行,包装成只含一条账号的 JSON 数组。
-- 如果报 Unknown column 'username',说明旧列已经删掉了,无需迁移,跳过这两条。
UPDATE `yz_platform_password_store`
SET `accounts` = JSON_ARRAY(JSON_OBJECT(
'username', IFNULL(`username`, ''),
'password', IFNULL(`password`, ''),
'registration_info', IFNULL(`registration_info`, ''),
'remark', ''
))
WHERE (`accounts` IS NULL OR `accounts` = '' OR `accounts` = '[]')
AND (IFNULL(`username`, '') <> ''
OR IFNULL(`password`, '') <> ''
OR IFNULL(`registration_info`, '') <> '');
UPDATE `yz_backend_password_store`
SET `accounts` = JSON_ARRAY(JSON_OBJECT(
'username', IFNULL(`username`, ''),
'password', IFNULL(`password`, ''),
'registration_info', IFNULL(`registration_info`, ''),
'remark', ''
))
WHERE (`accounts` IS NULL OR `accounts` = '' OR `accounts` = '[]')
AND (IFNULL(`username`, '') <> ''
OR IFNULL(`password`, '') <> ''
OR IFNULL(`registration_info`, '') <> '');
-- ---------- 第 3 步:accounts 为空的行统一补成空数组 ----------
UPDATE `yz_platform_password_store` SET `accounts` = '[]'
WHERE `accounts` IS NULL OR `accounts` = '';
UPDATE `yz_backend_password_store` SET `accounts` = '[]'
WHERE `accounts` IS NULL OR `accounts` = '';
-- ---------- 第 4 步(可选,不可逆):删除旧的单账号列 ----------
-- 先在页面上确认历史数据都能正常显示,再手动执行下面两条。
-- 旧表这三列是 NOT NULL DEFAULT '',不删也不影响新代码运行。
-- ALTER TABLE `yz_platform_password_store`
-- DROP COLUMN `username`, DROP COLUMN `password`, DROP COLUMN `registration_info`;
-- ALTER TABLE `yz_backend_password_store`
-- DROP COLUMN `username`, DROP COLUMN `password`, DROP COLUMN `registration_info`;