Merge pull request #97 from TrDex/pathlib-for-path-resolving
Use `pathlib` for path resolving
This commit is contained in:
commit
f00715d4c4
@ -3,6 +3,7 @@ import os
|
||||
import sys
|
||||
from configparser import ConfigParser
|
||||
from functools import wraps
|
||||
from pathlib import Path
|
||||
|
||||
import click
|
||||
from click import Context, UsageError
|
||||
@ -66,7 +67,7 @@ async def cli(ctx: Context, config, app, name):
|
||||
|
||||
invoked_subcommand = ctx.invoked_subcommand
|
||||
if invoked_subcommand != "init":
|
||||
if not os.path.exists(config):
|
||||
if not Path(config).exists():
|
||||
raise UsageError("You must exec init first", ctx=ctx)
|
||||
parser.read(config)
|
||||
|
||||
@ -109,7 +110,7 @@ async def upgrade(ctx: Context):
|
||||
exists = False
|
||||
if not exists:
|
||||
async with in_transaction(get_app_connection_name(config, app)) as conn:
|
||||
file_path = os.path.join(Migrate.migrate_location, version_file)
|
||||
file_path = Path(Migrate.migrate_location, version_file)
|
||||
content = get_version_content_from_file(file_path)
|
||||
upgrade_query_list = content.get("upgrade")
|
||||
for upgrade_query in upgrade_query_list:
|
||||
@ -163,7 +164,7 @@ async def downgrade(ctx: Context, version: int, delete: bool):
|
||||
for version in versions:
|
||||
file = version.version
|
||||
async with in_transaction(get_app_connection_name(config, app)) as conn:
|
||||
file_path = os.path.join(Migrate.migrate_location, file)
|
||||
file_path = Path(Migrate.migrate_location, file)
|
||||
content = get_version_content_from_file(file_path)
|
||||
downgrade_query_list = content.get("downgrade")
|
||||
if not downgrade_query_list:
|
||||
@ -224,7 +225,7 @@ async def init(
|
||||
):
|
||||
config_file = ctx.obj["config_file"]
|
||||
name = ctx.obj["name"]
|
||||
if os.path.exists(config_file):
|
||||
if Path(config_file).exists():
|
||||
return click.secho("You have inited", fg=Color.yellow)
|
||||
|
||||
parser.add_section(name)
|
||||
@ -234,7 +235,7 @@ async def init(
|
||||
with open(config_file, "w", encoding="utf-8") as f:
|
||||
parser.write(f)
|
||||
|
||||
if not os.path.isdir(location):
|
||||
if not Path(location).is_dir():
|
||||
os.mkdir(location)
|
||||
|
||||
click.secho(f"Success create migrate location {location}", fg=Color.green)
|
||||
@ -256,8 +257,8 @@ async def init_db(ctx: Context, safe):
|
||||
location = ctx.obj["location"]
|
||||
app = ctx.obj["app"]
|
||||
|
||||
dirname = os.path.join(location, app)
|
||||
if not os.path.isdir(dirname):
|
||||
dirname = Path(location, app)
|
||||
if not dirname.is_dir():
|
||||
os.mkdir(dirname)
|
||||
click.secho(f"Success create app migrate location {dirname}", fg=Color.green)
|
||||
else:
|
||||
@ -280,7 +281,7 @@ async def init_db(ctx: Context, safe):
|
||||
content = {
|
||||
"upgrade": [schema],
|
||||
}
|
||||
write_version_file(os.path.join(dirname, version), content)
|
||||
write_version_file(Path(dirname, version), content)
|
||||
click.secho(f'Success generate schema for app "{app}"', fg=Color.green)
|
||||
|
||||
|
||||
|
@ -4,6 +4,7 @@ import re
|
||||
from datetime import datetime
|
||||
from importlib import import_module
|
||||
from io import StringIO
|
||||
from pathlib import Path
|
||||
from typing import Dict, List, Optional, Tuple, Type, Union
|
||||
|
||||
import click
|
||||
@ -48,7 +49,7 @@ class Migrate:
|
||||
|
||||
@classmethod
|
||||
def get_old_model_file(cls, app: str, location: str):
|
||||
return os.path.join(location, app, cls.old_models + ".py")
|
||||
return Path(location, app, cls.old_models + ".py")
|
||||
|
||||
@classmethod
|
||||
def get_all_version_files(cls) -> List[str]:
|
||||
@ -83,7 +84,7 @@ class Migrate:
|
||||
await Tortoise.init(config=config)
|
||||
last_version = await cls.get_last_version()
|
||||
cls.app = app
|
||||
cls.migrate_location = os.path.join(location, app)
|
||||
cls.migrate_location = Path(location, app)
|
||||
if last_version:
|
||||
content = last_version.content
|
||||
with open(cls.get_old_model_file(app, location), "w", encoding="utf-8") as f:
|
||||
@ -134,12 +135,12 @@ class Migrate:
|
||||
# delete if same version exists
|
||||
for version_file in cls.get_all_version_files():
|
||||
if version_file.startswith(version.split("_")[0]):
|
||||
os.unlink(os.path.join(cls.migrate_location, version_file))
|
||||
os.unlink(Path(cls.migrate_location, version_file))
|
||||
content = {
|
||||
"upgrade": cls.upgrade_operators,
|
||||
"downgrade": cls.downgrade_operators,
|
||||
}
|
||||
write_version_file(os.path.join(cls.migrate_location, version), content)
|
||||
write_version_file(Path(cls.migrate_location, version), content)
|
||||
return version
|
||||
|
||||
@classmethod
|
||||
@ -192,8 +193,7 @@ class Migrate:
|
||||
:param location:
|
||||
:return:
|
||||
"""
|
||||
path = os.path.join(location, app, cls.old_models)
|
||||
path = path.replace(os.sep, ".").lstrip(".")
|
||||
path = Path(location, app, cls.old_models).as_posix().replace("/", ".")
|
||||
config["apps"][cls.diff_app] = {
|
||||
"models": [path],
|
||||
"default_connection": config.get("apps").get(app).get("default_connection", "default"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user