diff --git a/odoo_openupgrade_wizard/tools_postgres.py b/odoo_openupgrade_wizard/tools_postgres.py index 53dddff..371eb88 100644 --- a/odoo_openupgrade_wizard/tools_postgres.py +++ b/odoo_openupgrade_wizard/tools_postgres.py @@ -40,15 +40,43 @@ def get_postgres_container(ctx): detach=True, ) # TODO, improve me. + # Postgres container doesn't seems available immediately. + # check in odoo container, i remember that there is + # some script to do the job time.sleep(3) return container -def execute_sql_file(ctx, request, sql_file): - # TODO. - # Note : work on path in a docker context. - # container = get_postgres_container(ctx) - pass +def execute_sql_file(ctx, database, sql_file): + container = get_postgres_container(ctx) + + # Recreate relative path to make posible to + # call psql in the container + if str(ctx.obj["env_folder_path"]) not in str(sql_file): + raise Exception( + "The SQL file %s is not in the" + " main folder %s available" + " in the postgres container." + % (sql_file, ctx.obj["env_folder_path"]) + ) + relative_path = Path( + str(sql_file).replace(str(ctx.obj["env_folder_path"]), ".") + ) + + container_path = Path("/env/") / relative_path + docker_command = ( + "psql" " --username=odoo" " --dbname={database}" " --file {file_path}" + ).format(database=database, file_path=container_path) + logger.info( + "Executing the script '%s' in postgres container" + " on database %s" % (relative_path, database) + ) + docker_result = container.exec_run(docker_command) + if docker_result.exit_code != 0: + raise Exception( + "The script '%s' failed on database %s. Exit Code : %d" + % (relative_path, database, docker_result.exit_code) + ) def execute_sql_request(ctx, request, database="postgres"): @@ -111,9 +139,9 @@ def ensure_database(ctx, database: str, state="present"): def execute_sql_files_pre_migration( ctx, database: str, migration_step: dict, sql_files: list = [] ): + ensure_database(ctx, database, state="present") 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) diff --git a/tests/cli_B_04_execute_script_python_test.py b/tests/cli_B_04_execute_script_python_test.py index 50d4775..a59fded 100644 --- a/tests/cli_B_04_execute_script_python_test.py +++ b/tests/cli_B_04_execute_script_python_test.py @@ -10,7 +10,7 @@ def test_cli_execute_script_python(): "./tests/extra_script_B/post-migration-custom_test.py" ).absolute() - db_name = "database_test_cli_execute_script" + db_name = "database_test_cli_execute_script_python" # Install Odoo on V13 with product installed cli_runner_invoke( diff --git a/tests/cli_B_05_execute_script_sql_test.py b/tests/cli_B_05_execute_script_sql_test.py index 794f53f..ea37075 100644 --- a/tests/cli_B_05_execute_script_sql_test.py +++ b/tests/cli_B_05_execute_script_sql_test.py @@ -1,2 +1,40 @@ +import shutil +from pathlib import Path + +from odoo_openupgrade_wizard.tools_postgres import ( + ensure_database, + execute_sql_request, +) + +from . import build_ctx_from_config_file, cli_runner_invoke + + def test_cli_execute_script_sql(): - return + output_folder_path = Path("./tests/output_B").absolute() + + extra_script_path = Path( + "./tests/extra_script_B/pre-migration-custom_test.sql" + ).absolute() + + destination_path = output_folder_path / "scripts/step_01__update__13.0" + shutil.copy(extra_script_path, destination_path) + ctx = build_ctx_from_config_file(output_folder_path) + db_name = "database_test_cli_execute_script_sql" + ensure_database(ctx, db_name, state="absent") + ensure_database(ctx, db_name, state="present") + + cli_runner_invoke( + [ + "--log-level=DEBUG", + "--env-folder=%s" % output_folder_path, + "execute-script-sql", + "--step=1", + "--database=%s" % db_name, + ] + ) + + # Ensure that the request has been done correctlys + request = "SELECT name from city order by id;" + result = execute_sql_request(ctx, request, database=db_name) + + assert result == [["Chicago"], ["Cavalaire Sur Mer"]]