added option to generate empty migration file
This commit is contained in:
parent
01264f3f27
commit
b1ff2418f5
@ -123,8 +123,8 @@ class Command:
|
||||
inspect = cls(connection, tables)
|
||||
return await inspect.inspect()
|
||||
|
||||
async def migrate(self, name: str = "update"):
|
||||
return await Migrate.migrate(name)
|
||||
async def migrate(self, name: str = "update", empty: bool = False) -> str:
|
||||
return await Migrate.migrate(name, empty)
|
||||
|
||||
async def init_db(self, safe: bool):
|
||||
location = self.location
|
||||
|
@ -130,12 +130,16 @@ class Migrate:
|
||||
return version
|
||||
|
||||
@classmethod
|
||||
async def migrate(cls, name) -> str:
|
||||
async def migrate(cls, name: str, empty: bool) -> str:
|
||||
"""
|
||||
diff old models and new models to generate diff content
|
||||
:param name:
|
||||
:param name: str name for migration
|
||||
:param empty: bool if True generates empty migration
|
||||
:return:
|
||||
"""
|
||||
if empty:
|
||||
return await cls._generate_diff_py(name)
|
||||
|
||||
new_version_content = get_models_describe(cls.app)
|
||||
cls.diff_models(cls._last_version_content, new_version_content)
|
||||
cls.diff_models(new_version_content, cls._last_version_content, False)
|
||||
|
@ -1,3 +1,6 @@
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from pytest_mock import MockerFixture
|
||||
|
||||
@ -5,7 +8,7 @@ from aerich.ddl.mysql import MysqlDDL
|
||||
from aerich.ddl.postgres import PostgresDDL
|
||||
from aerich.ddl.sqlite import SqliteDDL
|
||||
from aerich.exceptions import NotSupportError
|
||||
from aerich.migrate import Migrate
|
||||
from aerich.migrate import MIGRATE_TEMPLATE, Migrate
|
||||
from aerich.utils import get_models_describe
|
||||
|
||||
old_models_describe = {
|
||||
@ -966,3 +969,15 @@ def test_sort_all_version_files(mocker):
|
||||
"10_datetime_update.py",
|
||||
"11_datetime_update.py",
|
||||
]
|
||||
|
||||
async def test_empty_migration(mocker) -> None:
|
||||
mocker.patch("os.listdir", return_value=[])
|
||||
Migrate.app = "foo"
|
||||
expected_content = MIGRATE_TEMPLATE.format(upgrade_sql=";", downgrade_sql=";")
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
Migrate.migrate_location = temp_dir
|
||||
|
||||
migration_file = await Migrate.migrate("update", True)
|
||||
|
||||
with open(Path(temp_dir, migration_file), "r") as f:
|
||||
assert f.read() == expected_content
|
||||
|
Loading…
x
Reference in New Issue
Block a user