Skip to content

Commit dfc434f

Browse files
authored
fix: include scheduled tasks in backups (#10644)
1 parent 8825a22 commit dfc434f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

‎astrbot/core/backup/constants.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
CommandConfig,
1212
CommandConflict,
1313
ConversationV2,
14+
CronJob,
1415
Persona,
1516
PersonaFolder,
1617
PlatformMessageHistory,
@@ -43,6 +44,7 @@
4344
MAIN_DB_MODELS: dict[str, type[SQLModel]] = {
4445
"platform_stats": PlatformStat,
4546
"conversations": ConversationV2,
47+
"cron_jobs": CronJob,
4648
"personas": Persona,
4749
"persona_folders": PersonaFolder,
4850
"preferences": Preference,

‎tests/test_backup.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from astrbot.core.db.po import (
2929
ConversationV2,
3030
)
31+
from astrbot.core.db.sqlite import SQLiteDatabase
3132
from astrbot.core.utils.upload import UploadTooLargeError
3233
from astrbot.core.utils.version_comparator import VersionComparator
3334
from astrbot.dashboard.services.backup_service import (
@@ -1048,6 +1049,36 @@ def test_kb_metadata_models_contain_expected_tables(self):
10481049
class TestBackupIntegration:
10491050
"""备份集成测试"""
10501051

1052+
@pytest.mark.asyncio
1053+
async def test_cron_jobs_roundtrip(self, tmp_path):
1054+
"""Preserve a scheduled task through backup export and import."""
1055+
source = SQLiteDatabase(str(tmp_path / "source.db"))
1056+
target = SQLiteDatabase(str(tmp_path / "target.db"))
1057+
try:
1058+
job = await source.create_cron_job(
1059+
name="Daily reminder",
1060+
job_type="active_agent",
1061+
cron_expression="0 9 * * *",
1062+
timezone="Asia/Taipei",
1063+
payload={"message": "Reminder"},
1064+
)
1065+
exporter = AstrBotExporter(
1066+
main_db=source, config_path=str(tmp_path / "missing-config.json")
1067+
)
1068+
with patch(
1069+
"astrbot.core.backup.exporter.get_backup_directories", return_value={}
1070+
):
1071+
zip_path = await exporter.export_all(output_dir=str(tmp_path / "backups"))
1072+
1073+
result = await AstrBotImporter(main_db=target).import_all(zip_path)
1074+
assert result.success, result.errors
1075+
restored = await target.list_cron_jobs()
1076+
assert len(restored) == 1
1077+
assert restored[0].model_dump() == job.model_dump()
1078+
finally:
1079+
await source.engine.dispose()
1080+
await target.engine.dispose()
1081+
10511082
@pytest.mark.asyncio
10521083
async def test_export_import_roundtrip(self, tmp_path):
10531084
"""测试导出-导入往返"""

0 commit comments

Comments
 (0)