async def init(ctx: Context, tortoise_orm, location, src_folder): config_file = ctx.obj["config_file"] if os.path.isabs(src_folder): src_folder = os.path.relpath(os.getcwd(), src_folder) # Add ./ so it's clear that this is relative path if not src_folder.startswith("./"): src_folder = "./" + src_folder # check that we can find the configuration, if not we can fail before the config file gets created add_src_path(src_folder) get_tortoise_config(ctx, tortoise_orm) if Path(config_file).exists(): with open(config_file, "r") as f: content = f.read() doc = tomlkit.parse(content) else: doc = tomlkit.parse("[tool.aerich]") table = tomlkit.table() table["tortoise_orm"] = tortoise_orm table["location"] = location table["src_folder"] = src_folder doc["tool"]["aerich"] = table with open(config_file, "w") as f: f.write(tomlkit.dumps(doc)) Path(location).mkdir(parents=True, exist_ok=True) click.secho(f"Success create migrate location {location}", fg=Color.green) click.secho(f"Success write config to {config_file}", fg=Color.green)
async def cli(ctx: Context, config, app, name): ctx.ensure_object(dict) ctx.obj["config_file"] = config ctx.obj["name"] = name invoked_subcommand = ctx.invoked_subcommand if invoked_subcommand != "init": if not os.path.exists(config): raise UsageError("You must exec init first", ctx=ctx) parser.read(config) location = parser[name]["location"] tortoise_orm = parser[name]["tortoise_orm"] tortoise_config = get_tortoise_config(ctx, tortoise_orm) app = app or list(tortoise_config.get("apps").keys())[0] if "aerich.models" not in tortoise_config.get("apps").get(app).get( "models"): raise UsageError( "Check your tortoise config and add aerich.models to it.", ctx=ctx) ctx.obj["config"] = tortoise_config ctx.obj["location"] = location ctx.obj["app"] = app Migrate.app = app if invoked_subcommand != "init-db": await Migrate.init_with_old_models(tortoise_config, app, location)
async def cli(ctx: Context, config, app): ctx.ensure_object(dict) ctx.obj["config_file"] = config invoked_subcommand = ctx.invoked_subcommand if invoked_subcommand != "init": if not Path(config).exists(): raise UsageError("You must exec init first", ctx=ctx) with open(config, "r") as f: content = f.read() doc = tomlkit.parse(content) try: tool = doc["tool"]["aerich"] location = tool["location"] tortoise_orm = tool["tortoise_orm"] src_folder = tool.get("src_folder", CONFIG_DEFAULT_VALUES["src_folder"]) except NonExistentKey: raise UsageError( "You need run aerich init again when upgrade to 0.6.0+") add_src_path(src_folder) tortoise_config = get_tortoise_config(ctx, tortoise_orm) app = app or list(tortoise_config.get("apps").keys())[0] command = Command(tortoise_config=tortoise_config, app=app, location=location) ctx.obj["command"] = command if invoked_subcommand != "init-db": if not Path(location, app).exists(): raise UsageError("You must exec init-db first", ctx=ctx) await command.init()
async def cli(ctx: Context, config, app, name): ctx.ensure_object(dict) ctx.obj["config_file"] = config ctx.obj["name"] = name ctx.obj["app"] = app invoked_subcommand = ctx.invoked_subcommand if invoked_subcommand != "init": if not os.path.exists(config): raise UsageError("You must exec init first", ctx=ctx) parser.read(config) location = parser[name]["location"] tortoise_orm = parser[name]["tortoise_orm"] tortoise_config = get_tortoise_config(ctx, tortoise_orm) ctx.obj["config"] = tortoise_config ctx.obj["location"] = location if invoked_subcommand != "init-db": try: await Migrate.init_with_old_models(tortoise_config, app, location) except ConfigurationError: raise UsageError(ctx=ctx, message="You must exec init-db first")
async def cli(ctx: Context, config, app, name): ctx.ensure_object(dict) ctx.obj["config_file"] = config ctx.obj["name"] = name invoked_subcommand = ctx.invoked_subcommand if invoked_subcommand != "init": if not Path(config).exists(): raise UsageError("You must exec init first", ctx=ctx) parser.read(config) location = parser[name]["location"] tortoise_orm = parser[name]["tortoise_orm"] tortoise_config = get_tortoise_config(ctx, tortoise_orm) app = app or list(tortoise_config.get("apps").keys())[0] ctx.obj["config"] = tortoise_config ctx.obj["location"] = location ctx.obj["app"] = app Migrate.app = app if invoked_subcommand != "init-db": if not Path(location, app).exists(): raise UsageError("You must exec init-db first", ctx=ctx) await Migrate.init(tortoise_config, app, location)