mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
feat(db): improve docstrings in migrator
This commit is contained in:
parent
95198da645
commit
5f77ef7e99
@ -7,7 +7,16 @@ from pydantic import BaseModel, ConfigDict, Field, model_validator
|
|||||||
|
|
||||||
@runtime_checkable
|
@runtime_checkable
|
||||||
class MigrateCallback(Protocol):
|
class MigrateCallback(Protocol):
|
||||||
"""A callback that performs a migration."""
|
"""
|
||||||
|
A callback that performs a migration.
|
||||||
|
|
||||||
|
Migrate callbacks are provided an open cursor to the database. They should not commit their
|
||||||
|
transaction; this is handled by the migrator.
|
||||||
|
|
||||||
|
If the callback needs to access additional dependencies, will be provided to the callback at runtime.
|
||||||
|
|
||||||
|
See :class:`Migration` for an example.
|
||||||
|
"""
|
||||||
|
|
||||||
def __call__(self, cursor: sqlite3.Cursor, **kwargs: Any) -> None:
|
def __call__(self, cursor: sqlite3.Cursor, **kwargs: Any) -> None:
|
||||||
...
|
...
|
||||||
@ -26,7 +35,7 @@ class MigrationDependency:
|
|||||||
Represents a dependency for a migration.
|
Represents a dependency for a migration.
|
||||||
|
|
||||||
:param name: The name of the dependency
|
:param name: The name of the dependency
|
||||||
:param dependency_type: The type of the dependency (e.g. str, SomeClass, etc.)
|
:param dependency_type: The type of the dependency (e.g. `str`, `int`, `SomeClass`, etc.)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
@ -62,10 +71,10 @@ class Migration(BaseModel):
|
|||||||
|
|
||||||
Example Usage:
|
Example Usage:
|
||||||
```py
|
```py
|
||||||
# Define the migrate callback. The dependency may be accessed by name in the kwargs.
|
# Define the migrate callback. This migration adds a column to the sushi table.
|
||||||
def migrate_callback(cursor: sqlite3.Cursor, **kwargs) -> None:
|
def migrate_callback(cursor: sqlite3.Cursor, **kwargs) -> None:
|
||||||
# Execute SQL using the cursor, taking care to *not commit* a transaction
|
# Execute SQL using the cursor, taking care to *not commit* a transaction
|
||||||
cursor.execute('ALTER TABLE bananas ADD COLUMN with_sushi BOOLEAN DEFAULT FALSE;')
|
cursor.execute('ALTER TABLE sushi ADD COLUMN with_banana BOOLEAN DEFAULT TRUE;')
|
||||||
...
|
...
|
||||||
|
|
||||||
# Instantiate the migration
|
# Instantiate the migration
|
||||||
@ -114,6 +123,7 @@ class Migration(BaseModel):
|
|||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def validate_to_version(self) -> "Migration":
|
def validate_to_version(self) -> "Migration":
|
||||||
|
"""Validates that to_version is one greater than from_version."""
|
||||||
if self.to_version != self.from_version + 1:
|
if self.to_version != self.from_version + 1:
|
||||||
raise ValueError("to_version must be one greater than from_version")
|
raise ValueError("to_version must be one greater than from_version")
|
||||||
return self
|
return self
|
||||||
@ -143,7 +153,12 @@ class Migration(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class MigrationSet:
|
class MigrationSet:
|
||||||
"""A set of Migrations. Performs validation during migration registration and provides utility methods."""
|
"""
|
||||||
|
A set of Migrations. Performs validation during migration registration and provides utility methods.
|
||||||
|
|
||||||
|
Migrations should be registered with `register()`. Once all are registered, `validate_migration_chain()`
|
||||||
|
should be called to ensure that the migrations form a single chain of migrations from version 0 to the latest version.
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self._migrations: set[Migration] = set()
|
self._migrations: set[Migration] = set()
|
||||||
|
Loading…
Reference in New Issue
Block a user