Skip to content

Commit

Permalink
NXDRIVE-2882: fix_db should create dump.sql in same dir as db (#4472)
Browse files Browse the repository at this point in the history
* NXDRIVE-2882: fix_db should create dump.sql in same dir as db
  • Loading branch information
gitofanindya authored Jan 25, 2024
1 parent 51b8ffd commit 0d3eda4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changes/5.5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Release date: `2024-xx-xx`

## Core

- [NXDRIVE-2882](https://jira.nuxeo.com/browse/NXDRIVE-2882): fix_db should create dump.sql in same dir as db
- [NXDRIVE-2](https://jira.nuxeo.com/browse/NXDRIVE-2):

### Direct Edit
Expand Down
7 changes: 3 additions & 4 deletions nxdrive/dao/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def is_healthy(database: Path, /) -> bool:
con = sqlite3.connect(str(database))
try:
status = con.execute("PRAGMA integrity_check(1)").fetchone()
return bool(status[0] == "ok")
return status[0] == "ok"
finally:
# According to the documentation:
# Connection object used as context manager only commits or rollbacks
Expand Down Expand Up @@ -67,15 +67,14 @@ def fix_db(database: Path, /, *, dump_file: Path = Path("dump.sql")) -> None:
Re-generate the whole database content to fix eventual FS corruptions.
This will prevent `sqlite3.DatabaseError: database disk image is malformed`
issues. The whole operation is quick and help saving disk space.
>>> fix_db('ndrive_6bba111e18ba11e89cfd180373b6442e.db')
Will raise sqlite3.DatabaseError in case of unrecoverable file.
"""

if is_healthy(database):
return

# setting the path of dumpfile where the databast file exists.
dump_file = database.parent.joinpath(dump_file)
log.info(f"Re-generating the whole database content of {database!r}...")

# Dump
Expand Down
36 changes: 35 additions & 1 deletion tests/functional/test_db_backup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import os
from datetime import datetime
from logging import getLogger
from pathlib import Path
from sqlite3 import DatabaseError
from time import sleep

Expand All @@ -25,7 +27,6 @@ def test_create_backup(manager_factory, tmp, nuxeo_url, user_factory, monkeypatc
password=user.properties["password"],
start_engine=False,
)

# Check DB and backup exist
assert (home / "manager.db").exists()
assert len(list((home / "backups").glob("manager.db_*"))) == 1
Expand Down Expand Up @@ -86,3 +87,36 @@ def test_delete_old_backups(tmp):
assert int(remaining_backups[0].name.split("_")[-1]) > yesterday
# The newest should be more recent than the today timestamp
assert int(remaining_backups[-1].name.split("_")[-1]) > today


def test_fix_db(manager_factory, tmp, nuxeo_url, user_factory, monkeypatch):
home = tmp()
conf_folder = tmp() / "nuxeo-conf"
user = user_factory()

with manager_factory(home=home, with_engine=False) as manager:
manager.bind_server(
conf_folder,
nuxeo_url,
user.uid,
password=user.properties["password"],
start_engine=False,
)

available_databases = list((home).glob("*.db"))
assert len(available_databases) == 2
database_path = (
available_databases[1]
if "manager" not in str(available_databases[1])
else str(available_databases[0])
)
database = Path(os.path.basename(database_path))

def mocked_is_healthy(*args, **kwargs):
return False

monkeypatch.setattr("nxdrive.dao.utils.is_healthy", mocked_is_healthy)
nxdrive.dao.utils.fix_db(database)

assert (Path(database_path)).exists()
assert not (home / "dump.sql").exists()

0 comments on commit 0d3eda4

Please sign in to comment.