fix: error when there is __init__.py in the migration folder (#272)
* fix: error when there is __init__.py in the migration folder * fix: check __init__.py in the migration folder * refactor * refactor & add test * refactor * Update changelog --------- Co-authored-by: Waket Zheng <waketzheng@gmail.com>
This commit is contained in:
parent
12d0a5dad1
commit
6270c4781e
@ -5,6 +5,7 @@
|
|||||||
### [0.8.1](Unreleased)
|
### [0.8.1](Unreleased)
|
||||||
|
|
||||||
#### Fixed
|
#### Fixed
|
||||||
|
- fix: error when there is __init__.py in the migration folder (#272)
|
||||||
- Setting null=false on m2m field causes migration to fail. (#334)
|
- Setting null=false on m2m field causes migration to fail. (#334)
|
||||||
- Fix NonExistentKey when running `aerich init` without `[tool]` section in config file. (#284)
|
- Fix NonExistentKey when running `aerich init` without `[tool]` section in config file. (#284)
|
||||||
- Fix configuration file reading error when containing Chinese characters. (#286)
|
- Fix configuration file reading error when containing Chinese characters. (#286)
|
||||||
|
@ -54,10 +54,18 @@ class Migrate:
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def get_all_version_files(cls) -> List[str]:
|
def get_all_version_files(cls) -> List[str]:
|
||||||
return sorted(
|
def get_file_version(file_name: str) -> str:
|
||||||
filter(lambda x: x.endswith("py"), os.listdir(cls.migrate_location)),
|
return file_name.split("_")[0]
|
||||||
key=lambda x: int(x.split("_")[0]),
|
|
||||||
)
|
def is_version_file(file_name: str) -> bool:
|
||||||
|
if not file_name.endswith("py"):
|
||||||
|
return False
|
||||||
|
if "_" not in file_name:
|
||||||
|
return False
|
||||||
|
return get_file_version(file_name).isdigit()
|
||||||
|
|
||||||
|
files = filter(is_version_file, os.listdir(cls.migrate_location))
|
||||||
|
return sorted(files, key=lambda x: int(get_file_version(x)))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _get_model(cls, model: str) -> Type[Model]:
|
def _get_model(cls, model: str) -> Type[Model]:
|
||||||
|
@ -1054,6 +1054,30 @@ def test_sort_all_version_files(mocker):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def test_sort_files_containing_non_migrations(mocker):
|
||||||
|
mocker.patch(
|
||||||
|
"os.listdir",
|
||||||
|
return_value=[
|
||||||
|
"1_datetime_update.py",
|
||||||
|
"11_datetime_update.py",
|
||||||
|
"10_datetime_update.py",
|
||||||
|
"2_datetime_update.py",
|
||||||
|
"not_a_migration.py",
|
||||||
|
"999.py",
|
||||||
|
"123foo_not_a_migration.py",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
Migrate.migrate_location = "."
|
||||||
|
|
||||||
|
assert Migrate.get_all_version_files() == [
|
||||||
|
"1_datetime_update.py",
|
||||||
|
"2_datetime_update.py",
|
||||||
|
"10_datetime_update.py",
|
||||||
|
"11_datetime_update.py",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
async def test_empty_migration(mocker, tmp_path: Path) -> None:
|
async def test_empty_migration(mocker, tmp_path: Path) -> None:
|
||||||
mocker.patch("os.listdir", return_value=[])
|
mocker.patch("os.listdir", return_value=[])
|
||||||
Migrate.app = "foo"
|
Migrate.app = "foo"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user