async def init_db(ctx: Context, safe): config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] dirname = os.path.join(location, app) if not os.path.isdir(dirname): os.mkdir(dirname) click.secho(f"Success create app migrate location {dirname}", fg=Color.green) else: return click.secho(f"Inited {app} already", fg=Color.yellow) Migrate.write_old_models(config, app, location) await Tortoise.init(config=config) connection = get_app_connection(config, app) await generate_schema_for_client(connection, safe) schema = get_schema_sql(connection, safe) version = await Migrate.generate_version() await Aerich.create(version=version, app=app) with open(os.path.join(dirname, version), "w") as f: content = { "upgrade": schema, } json.dump(content, f, ensure_ascii=False, indent=2) return click.secho(f'Success generate schema for app "{app}"', fg=Color.green)
async def init_db( ctx: Context, safe: bool = typer.Option( True, help="When set to true, creates the table only when it does not already exist.", ) ): """Generate schema and generate app migrate location.""" config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] dirname = os.path.join(location, app) if not os.path.isdir(dirname): os.mkdir(dirname) typer.secho(f"Success create app migrate location {dirname}", fg=typer.colors.GREEN) else: return typer.secho(f"Inited {app} already", fg=typer.colors.YELLOW) Migrate.write_old_models(config, app, location) await Tortoise.init(config=config) connection = get_app_connection(config, app) await generate_schema_for_client(connection, safe) schema = get_schema_sql(connection, safe) version = await Migrate.generate_version() await Aerich.create(version=version, app=app) with open(os.path.join(dirname, version), "w", encoding="utf-8") as f: content = { "upgrade": [schema], } json.dump(content, f, ensure_ascii=False, indent=2) return typer.secho(f'Success generate schema for app "{app}"', fg=typer.colors.GREEN)
async def migrate(ctx: Context, name: str = typer.Option("update", help="Migrate name.")): """Generate migrate changes file.""" app, config, location = await connect_tortoise(ctx) ret = await Migrate.migrate(name) if not ret: return typer.secho("No changes detected", fg=typer.colors.YELLOW) Migrate.write_old_models(config, app, location) typer.secho(f"Success migrate {ret}", fg=typer.colors.GREEN)
async def migrate(ctx: Context, name): config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] ret = await Migrate.migrate(name) if not ret: return click.secho("No changes detected", fg=Color.yellow) Migrate.write_old_models(config, app, location) click.secho(f"Success migrate {ret}", fg=Color.green)
async def init_db(ctx: Context, safe): config = ctx.obj["config"] location = ctx.obj["location"] app = ctx.obj["app"] dirname = os.path.join(location, app) if not os.path.isdir(dirname): os.mkdir(dirname) click.secho(f"Success create app migrate location {dirname}", fg=Color.green) else: return click.secho(f'Already inited app "{app}"', fg=Color.yellow) Migrate.write_old_models(config, app, location) await Tortoise.init(config=config) connection = get_app_connection(config, app) await generate_schema_for_client(connection, safe) return click.secho(f'Success generate schema for app "{app}"', fg=Color.green)