[ADD] psql command
This commit is contained in:
parent
3b34f52ddd
commit
284417bb00
|
|
@ -24,6 +24,7 @@ from odoo_openupgrade_wizard.cli.cli_generate_module_analysis import (
|
||||||
from odoo_openupgrade_wizard.cli.cli_get_code import get_code
|
from odoo_openupgrade_wizard.cli.cli_get_code import get_code
|
||||||
from odoo_openupgrade_wizard.cli.cli_init import init
|
from odoo_openupgrade_wizard.cli.cli_init import init
|
||||||
from odoo_openupgrade_wizard.cli.cli_install_from_csv import install_from_csv
|
from odoo_openupgrade_wizard.cli.cli_install_from_csv import install_from_csv
|
||||||
|
from odoo_openupgrade_wizard.cli.cli_psql import psql
|
||||||
from odoo_openupgrade_wizard.cli.cli_pull_submodule import pull_submodule
|
from odoo_openupgrade_wizard.cli.cli_pull_submodule import pull_submodule
|
||||||
from odoo_openupgrade_wizard.cli.cli_run import run
|
from odoo_openupgrade_wizard.cli.cli_run import run
|
||||||
from odoo_openupgrade_wizard.cli.cli_upgrade import upgrade
|
from odoo_openupgrade_wizard.cli.cli_upgrade import upgrade
|
||||||
|
|
@ -113,6 +114,7 @@ main.add_command(generate_module_analysis)
|
||||||
main.add_command(get_code)
|
main.add_command(get_code)
|
||||||
main.add_command(init)
|
main.add_command(init)
|
||||||
main.add_command(install_from_csv)
|
main.add_command(install_from_csv)
|
||||||
|
main.add_command(psql)
|
||||||
main.add_command(pull_submodule)
|
main.add_command(pull_submodule)
|
||||||
main.add_command(run)
|
main.add_command(run)
|
||||||
main.add_command(upgrade)
|
main.add_command(upgrade)
|
||||||
|
|
|
||||||
21
odoo_openupgrade_wizard/cli/cli_psql.py
Normal file
21
odoo_openupgrade_wizard/cli/cli_psql.py
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
import click
|
||||||
|
|
||||||
|
from odoo_openupgrade_wizard.cli.cli_options import database_option
|
||||||
|
from odoo_openupgrade_wizard.tools.tools_postgres import execute_psql_command
|
||||||
|
|
||||||
|
|
||||||
|
@click.command(context_settings={"ignore_unknown_options": True})
|
||||||
|
@database_option
|
||||||
|
@click.option("-c", "--command", "request")
|
||||||
|
@click.option("--pager/--no-pager", default=True)
|
||||||
|
@click.argument("psql_args", nargs=-1, type=click.UNPROCESSED)
|
||||||
|
@click.pass_context
|
||||||
|
def psql(ctx, request, database, pager, psql_args):
|
||||||
|
"""Run psql in the postgres container. Fill any parameters of psql
|
||||||
|
as PSQLARGS.
|
||||||
|
"""
|
||||||
|
result = execute_psql_command(ctx, request, database, psql_args)
|
||||||
|
if pager:
|
||||||
|
click.echo_via_pager(result)
|
||||||
|
else:
|
||||||
|
click.echo(result)
|
||||||
|
|
@ -104,21 +104,9 @@ def execute_sql_file(ctx, database, sql_file):
|
||||||
|
|
||||||
|
|
||||||
def execute_sql_request(ctx, request, database="postgres"):
|
def execute_sql_request(ctx, request, database="postgres"):
|
||||||
container = get_postgres_container(ctx)
|
psql_args = ("--tuples-only",)
|
||||||
command = (
|
output = execute_psql_command(ctx, request, database, psql_args)
|
||||||
"psql"
|
lines = output.split("\n")
|
||||||
" --username=odoo"
|
|
||||||
" --dbname={database}"
|
|
||||||
" --tuples-only"
|
|
||||||
' --command "{request}"'
|
|
||||||
).format(database=database, request=request)
|
|
||||||
logger.debug(
|
|
||||||
"Executing the following command in postgres container"
|
|
||||||
" on database %s \n %s" % (database, request)
|
|
||||||
)
|
|
||||||
docker_result = exec_container(container, command)
|
|
||||||
|
|
||||||
lines = docker_result.output.decode("utf-8").split("\n")
|
|
||||||
result = []
|
result = []
|
||||||
for line in lines:
|
for line in lines:
|
||||||
if not line:
|
if not line:
|
||||||
|
|
@ -127,6 +115,28 @@ def execute_sql_request(ctx, request, database="postgres"):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
def execute_psql_command(
|
||||||
|
ctx, request: str, database: str = "postgres", psql_args=None
|
||||||
|
):
|
||||||
|
"""Execute psql request in postgres container with psql_args on database"""
|
||||||
|
if psql_args and not isinstance(psql_args, str):
|
||||||
|
psql_args = " ".join(psql_args)
|
||||||
|
container = get_postgres_container(ctx)
|
||||||
|
command = (
|
||||||
|
"psql"
|
||||||
|
" --username=odoo"
|
||||||
|
" --dbname={database}"
|
||||||
|
' --command "{request}"'
|
||||||
|
" {psql_args}"
|
||||||
|
).format(database=database, request=request, psql_args=psql_args)
|
||||||
|
logger.debug(
|
||||||
|
"Executing the following command in postgres container\n"
|
||||||
|
"%s" % (command)
|
||||||
|
)
|
||||||
|
docker_result = exec_container(container, command)
|
||||||
|
return docker_result.output.decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
def ensure_database(ctx, database: str, state="present"):
|
def ensure_database(ctx, database: str, state="present"):
|
||||||
"""
|
"""
|
||||||
- Connect to postgres container.
|
- Connect to postgres container.
|
||||||
|
|
|
||||||
73
tests/cli_30_psql_test.py
Normal file
73
tests/cli_30_psql_test.py
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
from pytest import raises
|
||||||
|
|
||||||
|
from odoo_openupgrade_wizard.tools.tools_postgres import (
|
||||||
|
ensure_database,
|
||||||
|
execute_psql_command,
|
||||||
|
)
|
||||||
|
|
||||||
|
from . import (
|
||||||
|
build_ctx_from_config_file,
|
||||||
|
cli_runner_invoke,
|
||||||
|
move_to_test_folder,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def test_cli_psql():
|
||||||
|
move_to_test_folder()
|
||||||
|
ctx = build_ctx_from_config_file()
|
||||||
|
|
||||||
|
db_name = "database_test_cli___psql"
|
||||||
|
ensure_database(ctx, db_name, state="absent")
|
||||||
|
|
||||||
|
# initialize database
|
||||||
|
cli_runner_invoke(
|
||||||
|
[
|
||||||
|
"--log-level=DEBUG",
|
||||||
|
"run",
|
||||||
|
"--step=1",
|
||||||
|
"--database=%s" % db_name,
|
||||||
|
"--init-modules=base",
|
||||||
|
"--stop-after-init",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Test requests from lib
|
||||||
|
request = (
|
||||||
|
"SELECT name"
|
||||||
|
" FROM ir_module_module"
|
||||||
|
" WHERE state ='installed'"
|
||||||
|
" AND name='base';"
|
||||||
|
)
|
||||||
|
output = execute_psql_command(
|
||||||
|
ctx,
|
||||||
|
request,
|
||||||
|
database=db_name,
|
||||||
|
psql_args=("--tuples-only",),
|
||||||
|
)
|
||||||
|
assert output.strip() == "base"
|
||||||
|
|
||||||
|
# test via cli ok
|
||||||
|
cli_runner_invoke(
|
||||||
|
[
|
||||||
|
"--log-level=DEBUG",
|
||||||
|
"psql",
|
||||||
|
"--database=%s" % db_name,
|
||||||
|
'--command "%s"' % request,
|
||||||
|
"--no-pager",
|
||||||
|
"--tuples-only",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
# test that cli fails with wrong parameters
|
||||||
|
with raises(Exception):
|
||||||
|
cli_runner_invoke(
|
||||||
|
[
|
||||||
|
"--log-level=DEBUG",
|
||||||
|
"psql",
|
||||||
|
"--database=%s" % db_name,
|
||||||
|
'--command "%s"' % request,
|
||||||
|
"--no-pager",
|
||||||
|
"--tuples-only",
|
||||||
|
"---unkwon-argument",
|
||||||
|
],
|
||||||
|
)
|
||||||
Loading…
Reference in New Issue
Block a user