Update docs

This commit is contained in:
hhh
2024-03-17 17:26:03 +02:00
parent bb2b5e6a4f
commit fe1554045e
11 changed files with 152 additions and 98 deletions

View File

@@ -1,8 +1,8 @@
Schema Migrations
==================
Datalite provides a module, ``datalite.migrations`` that handles schema migrations. When a class
definition is modified, ``datalite.migrations.basic_migration`` can be called to automatically
Datalite provides a module, ``aiodatalite.migrations`` that handles schema migrations. When a class
definition is modified, ``aiodatalite.migrations.migrate`` can be called to
transfer records to a table fitting the new definitions.
Let us say we have made changes to the fields of a dataclass called ``Student`` and now,
@@ -13,7 +13,34 @@ name change. We can achieve this easily by:
.. code-block:: python
datalite.basic_migration(Student, {'studentt_id': 'student_id'})
await migrate(Student, {'studentt_id': 'student_id'})
This will make all the changes, if we had not provided the second argument,
the values would be lost.
the values would be lost.
Also, ``migrate`` provides automatic backup before migration, you can turn it off by passing ``do_backup=False`` into
function.
We also introduce safe migration defaults. This parameter should be passed a key-value dictionary, where the key is the
name of the new required field, and the value is what should be written to the old records in the database.
.. code-block:: python
from aiodatalite import datalite
from aiodatalite.typed import DataliteHinted
@datalite(db_path='db.db')
@dataclass
class Student(DataliteHinted):
new_obligatory_field: str # the database will break if not all records have this field
student_id: int = 1
student_name: str = "Kurt Gödel"
student_gpa: float = 3.9
So in this situation basically what you need to do is:
.. code-block:: python
await migrate(Student, safe_migration_defaults={
"new_obligatory_field": "some value for records that already exist in the database"
}
)