Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(migrations): migration 目录不存在时拒绝而不是报 0 个迁移

longmeidaoa624586af300
diff --git a/src/peach/migrations.py b/src/peach/migrations.py
index c1aa32268..e69d0beac 100644
--- a/src/peach/migrations.py
+++ b/src/peach/migrations.py
@@ -27,6 +27,13 @@ class Migration:
def discover(directory: Path) -> list[Migration]:
+ # `Path.glob` 对不存在的目录返回空序列,于是「一个 migration 都没有」和「根本没找到
+ # migration 目录」会长得一模一样:`upgrade` 报 0 个迁移、`status` 报 0 pending,
+ # 而账本是个 0 字节的空文件。空账本被当成迁移到位的账本是数据完整性问题,所以这里
+ # 直接拒绝。目录缺失的真实原因通常是 `pip install .`(非 editable)——wheel 只装
+ # `src/` 下的包,仓库根的 `migrations/` 不在里面。
+ if not directory.is_dir():
+ raise FileNotFoundError(f"migration 目录不存在:{directory}")
migrations: list[Migration] = []
for path in sorted(directory.glob("[0-9][0-9][0-9][0-9]_*.sql")):
version, name = path.stem.split("_", 1)
diff --git a/tests/test_migrations.py b/tests/test_migrations.py
index 25c0b0d70..f1cb937e4 100644
--- a/tests/test_migrations.py
+++ b/tests/test_migrations.py
@@ -81,6 +81,26 @@ class MigrationTests(unittest.TestCase):
with self.assertRaises(RuntimeError):
plan(self.db, MIGRATIONS)
+ def test_missing_migration_directory_is_rejected_instead_of_reported_empty(self):
+ """migration 目录不存在时拒绝,不要把空账本当成迁移到位的账本。
+
+ `pip install .`(非 editable)只装 `src/` 下的包,仓库根的 `migrations/` 不进
+ wheel。目录缺失若被读成「0 个迁移」,`peach init` 会打印成功并留下 0 字节账本,
+ `peach migrate status` 还跟着报 0 pending。
+ """
+ absent = self.root / "no-such-migrations"
+ with self.assertRaises(FileNotFoundError) as caught:
+ plan(self.db, absent)
+ self.assertIn(str(absent), str(caught.exception))
+ with self.assertRaises(FileNotFoundError):
+ upgrade(self.db, absent)
+ self.assertFalse(self.db.exists())
+
+ def test_empty_migration_directory_reports_nothing_to_apply(self):
+ empty = self.root / "empty-migrations"
+ empty.mkdir()
+ self.assertEqual(upgrade(self.db, empty), [])
+
def test_entity_migration_backfills_flattened_relations(self):
base_migrations = self.root / "base-migrations"
base_migrations.mkdir()