Skip to content
Snippets Groups Projects
Verified Commit b8159c6d authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Allow migration script cli to work for edge case named scripts

There exists some files in the scheduler which contains - (e.g. 30-bis.sql). Having
those in the dataset just make the use of the cli not working [1]

This commit just allows the part listing the files to have it within the scripts to
execute.

[1] Refs. swh/infra/sysadm-environment#4933 (comment 141771)
parent 984da898
No related branches found
No related tags found
No related merge requests found
Pipeline #3075 passed
# Copyright (C) 2015-2022 The Software Heritage developers
# Copyright (C) 2015-2023 The Software Heritage developers
# See the AUTHORS file at the top-level directory of this distribution
# License: GNU General Public License version 3, or any later version
# See top-level LICENSE file for more information
......@@ -148,6 +148,12 @@ def swh_db_versions(
return None
def migration_version_from_script_filename(fname: str) -> int:
"""Determine the version number from the script filename."""
filename = path.splitext(path.basename(fname))[0]
return int(filename.split("-")[0])
def swh_db_upgrade(
conninfo: str, modname: str, to_version: Optional[int] = None
) -> int:
......@@ -179,14 +185,14 @@ def swh_db_upgrade(
sqlfiles = [
fname
for fname in get_sql_for_package(modname, upgrade=True)
if db_version < int(fname.stem) <= to_version
if db_version < migration_version_from_script_filename(fname.stem) <= to_version
]
if not sqlfiles:
return db_version
for sqlfile in sqlfiles:
new_version = int(path.splitext(path.basename(sqlfile))[0])
new_version = migration_version_from_script_filename(str(sqlfile))
logger.info("Executing migration script '%s'", sqlfile)
if db_version is not None and (new_version - db_version) > 1:
logger.error(
......
......@@ -13,6 +13,7 @@ from swh.core.db import BaseDb
from swh.core.db.db_utils import (
get_database_info,
get_sql_for_package,
migration_version_from_script_filename,
now,
swh_db_module,
swh_db_upgrade,
......@@ -206,3 +207,11 @@ def test_db_utils_flavor(cli_runner, postgresql, mock_import_swhmodule, module,
dbmodule, _dbversion, dbflavor = get_database_info(conninfo)
assert dbmodule == module
assert dbflavor == (flavor or "default")
@pytest.mark.parametrize(
"filename, expected_version",
[("30", 30), ("30-bis", 30), ("/some/path/to/99-bis.sql", 99)],
)
def test_migration_version_from_script_filename(filename, expected_version):
assert migration_version_from_script_filename(filename) == expected_version
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment