Merge branch 'fix_run_multi_post_py_files_by_step' into 'main'

[FIX] allow to run multiple post-*py by step

See merge request odoo-openupgrade-wizard/odoo-openupgrade-wizard!76
This commit is contained in:
LE GAL SYLVAIN 2024-08-22 09:19:40 +00:00
commit 45df9053ff
5 changed files with 55 additions and 12 deletions

View File

@ -327,7 +327,7 @@ def execute_click_odoo_python_files(
]
python_files = sorted(python_files)
command = generate_odoo_command(
base_command = generate_odoo_command(
ctx,
migration_step,
execution_context,
@ -336,16 +336,13 @@ def execute_click_odoo_python_files(
)
for python_file in python_files:
command = ("/bin/bash -c 'cat /env/{python_file} | {command}'").format(
command=command,
python_file=str(python_file),
)
command = f"/bin/bash -c 'cat /env/{python_file} | {base_command}'"
try:
logger.info(
f"Step {migration_step['complete_name']}."
f" Executing script {python_file} ..."
)
return run_container_odoo(
run_container_odoo(
ctx,
migration_step,
command,

View File

@ -19,11 +19,11 @@ def test_cli_execute_script_python():
ctx = build_ctx_from_config_file()
extra_script_path = Path(
"../extra_script/post-migration-custom_test.py"
"../extra_script/01-post-migration-custom_test.py"
).absolute()
cp(
extra_script_path,
Path("post-migration-custom_test.py"),
Path("01-post-migration-custom_test.py"),
)
db_name = "database_test_cli___execute_script_python"
@ -52,7 +52,7 @@ def test_cli_execute_script_python():
"execute-script-python",
"--step=1",
f"--database={db_name}",
"--script-file-path=post-migration-custom_test.py",
"--script-file-path=01-post-migration-custom_test.py",
],
)
partner_quantity_after = int(

View File

@ -1,3 +1,6 @@
from pathlib import Path
from shutil import copy
from odoo_openupgrade_wizard.tools.tools_postgres import (
ensure_database,
execute_sql_request,
@ -14,6 +17,17 @@ def test_cli_upgrade():
move_to_test_folder()
ctx = build_ctx_from_config_file()
for n in ["01", "02"]:
copy(
Path(
f"../extra_script/{n}-post-migration-custom_test.py"
).absolute(),
Path(
"scripts/step_01__regular__14.0/"
f"{n}-post-migration-custom_test.py"
),
)
# Initialize database
db_name = "database_test_cli___upgrade"
ensure_database(ctx, db_name, state="absent")
@ -58,3 +72,23 @@ def test_cli_upgrade():
latest_version = execute_sql_request(ctx, request, database=db_name)
assert latest_version[0][0].startswith("15.")
# ensure the first post-migration-custom scripts have been executed
request = (
"SELECT name"
" FROM res_partner"
" WHERE name like 'Post Script 1 - Partner #%';"
)
result = execute_sql_request(ctx, request, database=db_name)
assert len(result) == 10
# ensure the second post-migration-custom scripts have been executed
request = (
"SELECT name"
" FROM res_partner"
" WHERE name = 'Post Script 2 - Partner #1';"
)
result = execute_sql_request(ctx, request, database=db_name)
assert len(result) == 1

View File

@ -1,15 +1,15 @@
import logging
_logger = logging.getLogger(__name__)
_logger.info("post-migration-custom_test.py : Begin of script ...")
_logger.info("01-post-migration-custom_test.py : Begin of script ...")
env = env # noqa: F821
for i in range(0, 10):
partner_name = "Partner #%d" % (i)
partner_name = "Post Script 1 - Partner #%d" % (i)
_logger.info("Create Partner %s" % partner_name)
env["res.partner"].create({"name": partner_name})
_logger.info("post-migration-custom_test.py : End of script.")
_logger.info("01-post-migration-custom_test.py : End of script.")
env.cr.commit()

View File

@ -0,0 +1,12 @@
import logging
_logger = logging.getLogger(__name__)
_logger.info("02-post-migration-custom_test.py : Begin of script ...")
env = env # noqa: F821
env["res.partner"].create({"name": "Post Script 2 - Partner #1"})
_logger.info("02-post-migration-custom_test.py : End of script.")
env.cr.commit()