Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generate schemas optionally #3

Open
daeun-ws opened this issue Jun 14, 2023 · 0 comments
Open

generate schemas optionally #3

daeun-ws opened this issue Jun 14, 2023 · 0 comments

Comments

@daeun-ws
Copy link

Hello, thank you for your work on this. I've been using your project very helpfully.
I have one feature request : generate schemas optionally.

I'm using a db user that does not have permission to execute DDL for tortoise-orm and executing shell command configured with the user occurs OperationalError.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/dapi-venv/bin/tortoise-cli", line 8, in <module>
    sys.exit(main())
  File "/dapi-venv/lib/python3.9/site-packages/tortoise_cli/cli.py", line 76, in main
    cli()
  File "/dapi-venv/lib/python3.9/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/dapi-venv/lib/python3.9/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/dapi-venv/lib/python3.9/site-packages/click/core.py", line 1654, in invoke
    super().invoke(ctx)
  File "/dapi-venv/lib/python3.9/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/dapi-venv/lib/python3.9/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/dapi-venv/lib/python3.9/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/dapi-venv/lib/python3.9/site-packages/tortoise_cli/cli.py", line 21, in wrapper
    loop.run_until_complete(f(*args, **kwargs))
  File "/usr/local/lib/python3.9/asyncio/base_events.py", line 642, in run_until_complete
    return future.result()
  File "/dapi-venv/lib/python3.9/site-packages/tortoise_cli/cli.py", line 55, in cli
    await Tortoise.generate_schemas(safe=True)
  File "/dapi-venv/lib/python3.9/site-packages/tortoise/__init__.py", line 662, in generate_schemas
    await generate_schema_for_client(connection, safe)
  File "/dapi-venv/lib/python3.9/site-packages/tortoise/utils.py", line 31, in generate_schema_for_client
    await generator.generate_from_string(schema)
  File "/dapi-venv/lib/python3.9/site-packages/tortoise/backends/base/schema_generator.py", line 448, in generate_from_string
    await self.client.execute_script(creation_string)
  File "/dapi-venv/lib/python3.9/site-packages/tortoise/backends/mysql/client.py", line 52, in translate_exceptions_
    raise OperationalError(exc)
tortoise.exceptions.OperationalError: (1142, "CREATE command denied to user 'user_rw'@'10.240.35.55' for table 'city'")

So I think the command will be more useful if it allows to generate schema optionally like below.

@click.group(context_settings={"help_option_names": ["-h", "--help"]})
@click.version_option(__version__, "-V", "--version")
@click.option(
    "-c",
    "--config",
    help="TortoiseORM config dictionary path, like settings.TORTOISE_ORM",
)
@click.option(
    "--generate-schema",
    show_default=True,
    default=True,
    help="Generate database schema.",
)
@click.pass_context
@coro
async def cli(ctx: click.Context, config: Optional[str], generate_schema: bool):
    ...
    if generate_schema:
        print("generate schema automatically..")
        tortoise_config = utils.get_tortoise_config(ctx, config)
        await Tortoise.init(config=tortoise_config)
        await Tortoise.generate_schemas(safe=True)
  • with generate-schema param
$ tortoise-cli --generate-schema false -c settings.TORTOISE_ORM shell
  • without generate-schema param (generate schemas by default)
$ tortoise-cli -c settings.TORTOISE_ORM shell 

I'll be appreciated if you consider my request and give me an opportunity to create PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant