Skip to content

Model Migration

Danio's provide a simple way to support model migration.

Get Migration SQL

danio.manage.make_migration:

async def make_migration(
    db: Database, models: typing.Sequence[typing.Type[Model]], dir: str
) -> str

This function will compare the table schema between passed models and current database, then write 2 migration sql file(_up.sql, _down.sql) to dir directory.

Support migrate action for now:

  • add new table
  • add/drop field
  • modify field type
  • add/drop table index

The example migration sql file(change field data type from TEXT to VARCHAR(255)):

2022_02_28_08_43_00_up.sql:

USE `test`;
ALTER TABLE `log` MODIFY `data` VARCHAR(255);

2022_02_28_08_43_00_down.sql:

USE `test`;
ALTER TABLE `log` MODIFY `data` TEXT;

Get Code Models

danio.manage.get_models:

def get_models(paths: typing.List[str]) -> typing.List[typing.Type[Model]]

This function will find and return all model except abstracted.