feat(db): require migration versions to be consecutive

This commit is contained in:
psychedelicious 2023-12-12 09:43:09 +11:00
parent ef8284f009
commit 55acc16b2d
2 changed files with 7 additions and 5 deletions

View File

@ -40,8 +40,8 @@ class Migration(BaseModel):
@model_validator(mode="after")
def validate_to_version(self) -> "Migration":
if self.to_version <= self.from_version:
raise ValueError("to_version must be greater than from_version")
if self.to_version != self.from_version + 1:
raise ValueError("to_version must be one greater than from_version")
return self
def __hash__(self) -> int:

View File

@ -81,9 +81,11 @@ def create_migrate(i: int) -> MigrateCallback:
return migrate
def test_migration_to_version_gt_from_version(no_op_migrate_callback: MigrateCallback) -> None:
with pytest.raises(ValidationError, match="greater_than_equal"):
Migration(from_version=1, to_version=0, migrate=no_op_migrate_callback)
def test_migration_to_version_is_one_gt_from_version(no_op_migrate_callback: MigrateCallback) -> None:
with pytest.raises(ValidationError, match="to_version must be one greater than from_version"):
Migration(from_version=0, to_version=2, migrate=no_op_migrate_callback)
# not raising is sufficient
Migration(from_version=1, to_version=2, migrate=no_op_migrate_callback)
def test_migration_hash(no_op_migrate_callback: MigrateCallback) -> None: