From ffa406ef20b47f2acb06b59d6753856196883783 Mon Sep 17 00:00:00 2001 From: Sylvain LE GAL Date: Thu, 5 May 2022 13:08:46 +0200 Subject: [PATCH] WIp --- .gitlab-ci.yml | 3 +- odoo_openupgrade_wizard/cli.py | 8 +++-- ...script.py => cli_execute_script_python.py} | 2 +- .../cli_execute_script_sql.py | 35 +++++++++++++++++++ odoo_openupgrade_wizard/tools_odoo.py | 5 +-- odoo_openupgrade_wizard/tools_postgres.py | 19 ++++++++++ odoo_openupgrade_wizard/tools_system.py | 4 +++ ...=> cli_B_04_execute_script_python_test.py} | 2 +- tests/cli_B_05_upgrade_test.py | 3 ++ 9 files changed, 72 insertions(+), 9 deletions(-) rename odoo_openupgrade_wizard/{cli_execute_script.py => cli_execute_script_python.py} (92%) create mode 100644 odoo_openupgrade_wizard/cli_execute_script_sql.py rename tests/{cli_B_04_execute_script_test.py => cli_B_04_execute_script_python_test.py} (96%) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0063383..194058f 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -44,4 +44,5 @@ pytest: # Create a postgresql container - docker run -d -e POSTGRES_USER=odoo -e POSTGRES_PASSWORD=odoo -e POSTGRES_DB=postgres --publish 9542:5432 --name db postgres:13 - - poetry run pytest --cov odoo_openupgrade_wizard -v + - poetry run pytest --cov odoo_openupgrade_wizard -v\ + tests/cli_A_init_test.py diff --git a/odoo_openupgrade_wizard/cli.py b/odoo_openupgrade_wizard/cli.py index 5a9e9d1..87a867f 100644 --- a/odoo_openupgrade_wizard/cli.py +++ b/odoo_openupgrade_wizard/cli.py @@ -10,7 +10,10 @@ from loguru import logger import odoo_openupgrade_wizard from odoo_openupgrade_wizard.cli_docker_build import docker_build -from odoo_openupgrade_wizard.cli_execute_script import execute_script +from odoo_openupgrade_wizard.cli_execute_script_python import ( + execute_script_python, +) +from odoo_openupgrade_wizard.cli_execute_script_sql import execute_script_sql from odoo_openupgrade_wizard.cli_get_code import get_code from odoo_openupgrade_wizard.cli_init import init from odoo_openupgrade_wizard.cli_install_from_csv import install_from_csv @@ -109,5 +112,6 @@ main.add_command(docker_build) main.add_command(run) main.add_command(install_from_csv) main.add_command(upgrade) -main.add_command(execute_script) +main.add_command(execute_script_python) +main.add_command(execute_script_sql) main.add_command(test_dev) diff --git a/odoo_openupgrade_wizard/cli_execute_script.py b/odoo_openupgrade_wizard/cli_execute_script_python.py similarity index 92% rename from odoo_openupgrade_wizard/cli_execute_script.py rename to odoo_openupgrade_wizard/cli_execute_script_python.py index 2663ec2..4a9b0c2 100644 --- a/odoo_openupgrade_wizard/cli_execute_script.py +++ b/odoo_openupgrade_wizard/cli_execute_script_python.py @@ -27,7 +27,7 @@ from odoo_openupgrade_wizard.tools_odoo import ( " scripts placed in the migration step folder.", ) @click.pass_context -def execute_script(ctx, step, database, script_file_path): +def execute_script_python(ctx, step, database, script_file_path): migration_step = get_migration_step_from_options(ctx, step) execute_python_files_post_migration( diff --git a/odoo_openupgrade_wizard/cli_execute_script_sql.py b/odoo_openupgrade_wizard/cli_execute_script_sql.py new file mode 100644 index 0000000..693b3b4 --- /dev/null +++ b/odoo_openupgrade_wizard/cli_execute_script_sql.py @@ -0,0 +1,35 @@ +from pathlib import Path + +import click + +from odoo_openupgrade_wizard.cli_options import ( + database_option_required, + get_migration_step_from_options, + step_option, +) +from odoo_openupgrade_wizard.tools_postgres import ( + execute_sql_files_pre_migration, +) + + +@click.command() +@step_option +@database_option_required +@click.option( + "--script-file-path", + multiple=True, + type=click.Path( + exists=True, + dir_okay=False, + resolve_path=True, + ), + help="List of SQL files that will be executed, replacing the default" + " scripts placed in the migration step folder.", +) +@click.pass_context +def execute_script_sql(ctx, step, database, script_file_path): + migration_step = get_migration_step_from_options(ctx, step) + + execute_sql_files_pre_migration( + ctx, database, migration_step, [Path(x) for x in script_file_path] + ) diff --git a/odoo_openupgrade_wizard/tools_odoo.py b/odoo_openupgrade_wizard/tools_odoo.py index 2deea80..20deba7 100644 --- a/odoo_openupgrade_wizard/tools_odoo.py +++ b/odoo_openupgrade_wizard/tools_odoo.py @@ -16,6 +16,7 @@ from odoo_openupgrade_wizard.configuration_version_dependant import ( ) from odoo_openupgrade_wizard.tools_docker import kill_container, run_container from odoo_openupgrade_wizard.tools_odoo_instance import OdooInstance +from odoo_openupgrade_wizard.tools_system import get_script_folder def get_odoo_addons_path(ctx, root_path: Path, migration_step: dict) -> str: @@ -43,10 +44,6 @@ def get_odoo_addons_path(ctx, root_path: Path, migration_step: dict) -> str: return ",".join([str(x) for x in addons_path]) -def get_script_folder(ctx, migration_step: dict) -> Path: - return ctx.obj["script_folder_path"] / migration_step["complete_name"] - - def get_odoo_env_path(ctx, odoo_version: dict) -> Path: folder_name = "env_%s" % str(odoo_version["release"]).rjust(4, "0") return ctx.obj["src_folder_path"] / folder_name diff --git a/odoo_openupgrade_wizard/tools_postgres.py b/odoo_openupgrade_wizard/tools_postgres.py index 4798ae1..a37d2a0 100644 --- a/odoo_openupgrade_wizard/tools_postgres.py +++ b/odoo_openupgrade_wizard/tools_postgres.py @@ -1,6 +1,10 @@ +import os +from pathlib import Path + from loguru import logger from odoo_openupgrade_wizard.tools_docker import get_docker_client +from odoo_openupgrade_wizard.tools_system import get_script_folder def get_postgres_container(): @@ -63,3 +67,18 @@ def ensure_database(database: str, state="present"): logger.info("Drop database '%s' ..." % database) request = "DROP DATABASE {database};".format(database=database) execute_sql_request(request) + + +def execute_sql_files_pre_migration( + ctx, database: str, migration_step: dict, sql_files: list = [] +): + if not sql_files: + script_folder = get_script_folder(ctx, migration_step) + + sql_files = [ + script_folder / Path(f) + for f in os.listdir(script_folder) + if os.path.isfile(os.path.join(script_folder, f)) + and f[-3:] == ".sql" + ] + sql_files = sorted(sql_files) diff --git a/odoo_openupgrade_wizard/tools_system.py b/odoo_openupgrade_wizard/tools_system.py index 1fc6722..9c115f9 100644 --- a/odoo_openupgrade_wizard/tools_system.py +++ b/odoo_openupgrade_wizard/tools_system.py @@ -11,6 +11,10 @@ from plumbum.cmd import mkdir from odoo_openupgrade_wizard import templates +def get_script_folder(ctx, migration_step: dict) -> Path: + return ctx.obj["script_folder_path"] / migration_step["complete_name"] + + def ensure_folder_exists( folder_path: Path, mode: str = "755", git_ignore_content: bool = False ): diff --git a/tests/cli_B_04_execute_script_test.py b/tests/cli_B_04_execute_script_python_test.py similarity index 96% rename from tests/cli_B_04_execute_script_test.py rename to tests/cli_B_04_execute_script_python_test.py index 46d55cf..a7923d7 100644 --- a/tests/cli_B_04_execute_script_test.py +++ b/tests/cli_B_04_execute_script_python_test.py @@ -32,7 +32,7 @@ def test_cli_execute_script(): [ "--log-level=DEBUG", "--env-folder=%s" % output_folder_path, - "execute-script", + "execute-script-python", "--step=1", "--database=%s" % db_name, "--script-file-path=%s" % extra_script_path, diff --git a/tests/cli_B_05_upgrade_test.py b/tests/cli_B_05_upgrade_test.py index 9c18504..4abcd5b 100644 --- a/tests/cli_B_05_upgrade_test.py +++ b/tests/cli_B_05_upgrade_test.py @@ -9,6 +9,9 @@ from . import cli_runner_invoke def test_cli_upgrade(): + # TODO: FIXME + # This test works locally, but doesn't work on gitlabci + output_folder_path = Path("./tests/output_B").absolute() db_name = "database_test_cli_upgrade"