mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-11-24 12:53:08 +08:00
插件配置此前寄存在 systemconfig 的 plugin.<实例ID> 单键下,实例描述符则整份挤在
PluginInstances 这一个 JSON 键里,两者都不成立:
- plugin.<ID> 是裸字符串 key,而仓库规则本身禁止用裸字符串做 SystemConfig key,
只允许先定义 SystemConfigKey 枚举项;插件 ID 由用户安装决定,永远枚举不出常量。
条目数随安装量增长,混在系统设置表里会把主程序自己的设置项淹掉。
- 那个键存的其实是实例 ID,表里没有任何一列说得出它属于哪个插件,想列出某个插件
的全部实例配置只能靠字符串前缀去猜。
- 实例描述符整份读出再整份写回,改一个分身要重写全部分身。
改为 plugininstance 一实例一行:instance_id 与 source_plugin_id 构成身份,本体与
分身由两者是否相等派生而不设模式列——模式列会是这个等式的冗余副本,两者一旦失步
同一行就会在不同读取口被判成不同角色。展示信息与业务参数同存一行,属于同一个
生命周期,分表只会让建分身、删分身退化成两张表之间的协调问题。
读写路由落在 SystemConfigOper.get/set/delete,而不是在各个调用方各改一处:第三方
插件可能直接用 self.systemconfig.get("plugin.xxx") 读写自己的配置,只改
PluginConfigStore 与 _PluginBase.get_config/update_config 必然漏掉它们。路由只做
前缀识别,插入、更新与空本体行回收都委托 PluginInstanceOper,不在配置层重抄一遍。
PluginInstances 旧键迁移后不删,留作回滚依据,并以
SystemConfigKey.PluginInstancesImported 标志防止重复导入;判据不能是「表当前为空」,
否则用户把分身全部删光后,下次启动会把它们整批导回来。
迁移链:b2d4f6a8c1e3 -> 281965691a20(3.0.34 建表并搬描述符)
-> c4e1a7b9d2f6(3.0.35 加 config_data 并搬 plugin.* 配置)。
依赖基线与启动模块数随两个新模块重算,架构文档与数据库技能表目录同步登记新表。
141 lines
4.9 KiB
Python
141 lines
4.9 KiB
Python
"""3.0.35 插件配置迁入插件实例表。
|
||
|
||
Revision ID: c4e1a7b9d2f6
|
||
Revises: 281965691a20
|
||
Create Date: 2026-09-11
|
||
"""
|
||
|
||
import json
|
||
from datetime import datetime, timezone
|
||
|
||
import sqlalchemy as sa
|
||
from alembic import op
|
||
|
||
revision = "c4e1a7b9d2f6"
|
||
down_revision = "281965691a20"
|
||
branch_labels = None
|
||
depends_on = None
|
||
|
||
_TABLE = "plugininstance"
|
||
_LEGACY_PREFIX = "plugin."
|
||
|
||
|
||
def _table_names(connection) -> set:
|
||
"""读取当前数据库已有表名。"""
|
||
return set(sa.inspect(connection).get_table_names())
|
||
|
||
|
||
def _column_names(connection) -> set:
|
||
"""读取实例表已有列名;表不存在时为空集。"""
|
||
if _TABLE not in _table_names(connection):
|
||
return set()
|
||
return {column["name"] for column in sa.inspect(connection).get_columns(_TABLE)}
|
||
|
||
|
||
def _decode(value):
|
||
"""把 JSON 列还原为 Python 值,兼容驱动直接返回原始字符串的情形。"""
|
||
if isinstance(value, (str, bytes, bytearray)):
|
||
try:
|
||
return json.loads(value)
|
||
except (TypeError, ValueError):
|
||
return value
|
||
return value
|
||
|
||
|
||
def upgrade() -> None:
|
||
"""把每实例的业务参数收拢到它自己那一行上。
|
||
|
||
插件配置此前寄存在 systemconfig 的 plugin.<实例ID> 单键下:键由插件 ID 决定、
|
||
条目数随安装量增长,混在系统设置里会把主程序自己的设置项淹掉;而且那个键存的
|
||
其实是实例 ID,表里没有任何一列说得出它属于哪个插件,想列出某插件的全部实例
|
||
配置就只能靠字符串前缀去猜。
|
||
|
||
配置与实例身份同存一行,而不是另起一张同键的表:展示信息与业务参数都是这个
|
||
实例的设置,同一个生命周期,分表只会让「建分身、删分身」退化成两张表之间的
|
||
协调问题。
|
||
"""
|
||
connection = op.get_bind()
|
||
now = datetime.now(timezone.utc).isoformat()
|
||
columns = _column_names(connection)
|
||
if not columns:
|
||
return
|
||
|
||
if "config_data" not in columns:
|
||
op.add_column(_TABLE, sa.Column("config_data", sa.JSON(), nullable=True))
|
||
|
||
if "systemconfig" not in _table_names(connection):
|
||
return
|
||
|
||
existing = {
|
||
row[0]
|
||
for row in connection.execute(
|
||
sa.text(f"SELECT instance_id FROM {_TABLE}")
|
||
).fetchall()
|
||
}
|
||
legacy = connection.execute(
|
||
sa.text("SELECT key, value FROM systemconfig WHERE key LIKE :pattern"),
|
||
{"pattern": f"{_LEGACY_PREFIX}%"},
|
||
).fetchall()
|
||
migrated_keys = []
|
||
for key, value in legacy:
|
||
instance_id = key[len(_LEGACY_PREFIX):]
|
||
if not instance_id:
|
||
continue
|
||
payload = json.dumps(_decode(value))
|
||
if instance_id in existing:
|
||
connection.execute(
|
||
sa.text(
|
||
f"UPDATE {_TABLE} SET config_data = :config_data, updated_at = :now "
|
||
"WHERE instance_id = :instance_id"
|
||
),
|
||
{"config_data": payload, "instance_id": instance_id, "now": now},
|
||
)
|
||
else:
|
||
# 没有实例行的只可能是本体自身:分身的行在建分身时就已落盘
|
||
connection.execute(
|
||
sa.text(
|
||
f"INSERT INTO {_TABLE} "
|
||
"(instance_id, source_plugin_id, config_data, created_at, updated_at) "
|
||
"VALUES (:instance_id, :instance_id, :config_data, :now, :now)"
|
||
),
|
||
{"instance_id": instance_id, "config_data": payload, "now": now},
|
||
)
|
||
existing.add(instance_id)
|
||
migrated_keys.append(key)
|
||
|
||
# 同一份配置不能在两处各留一份,否则读取端按哪个都不对
|
||
for key in migrated_keys:
|
||
connection.execute(
|
||
sa.text("DELETE FROM systemconfig WHERE key = :key"),
|
||
{"key": key},
|
||
)
|
||
|
||
|
||
def downgrade() -> None:
|
||
"""把业务参数搬回 systemconfig 原键,并移除新增列,回退路径上不丢配置。"""
|
||
connection = op.get_bind()
|
||
columns = _column_names(connection)
|
||
if "config_data" not in columns:
|
||
return
|
||
|
||
if "systemconfig" in _table_names(connection):
|
||
rows = connection.execute(
|
||
sa.text(
|
||
f"SELECT instance_id, config_data FROM {_TABLE} WHERE config_data IS NOT NULL"
|
||
)
|
||
).fetchall()
|
||
for instance_id, config_data in rows:
|
||
key = f"{_LEGACY_PREFIX}{instance_id}"
|
||
exists = connection.execute(
|
||
sa.text("SELECT 1 FROM systemconfig WHERE key = :key"),
|
||
{"key": key},
|
||
).fetchone()
|
||
if not exists:
|
||
connection.execute(
|
||
sa.text("INSERT INTO systemconfig (key, value) VALUES (:key, :value)"),
|
||
{"key": key, "value": json.dumps(_decode(config_data))},
|
||
)
|
||
|
||
with op.batch_alter_table(_TABLE) as batch:
|
||
batch.drop_column("config_data")
|