forked from gramps-project/gramps-web-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement check & repair endpoint (fixes gramps-project#479)
- Loading branch information
1 parent
6a4015c
commit 9f8e858
Showing
6 changed files
with
200 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
"""Check and repair a Gramps database.""" | ||
|
||
from gramps.gen.db import DbTxn, DbWriteBase | ||
from gramps.gen.dbstate import DbState | ||
from gramps.plugins.tool.check import CheckIntegrity | ||
|
||
|
||
def check_database(db_handle: DbWriteBase): | ||
with DbTxn("Check Integrity", db_handle, batch=True) as trans: | ||
db_handle.disable_signals() | ||
dbstate = DbState() | ||
dbstate.change_database(db_handle) | ||
checker = CheckIntegrity(dbstate, None, trans) | ||
|
||
# start with empty objects, broken links can be corrected below | ||
# then. This is done before fixing encoding and missing photos, | ||
# since otherwise we will be trying to fix empty records which are | ||
# then going to be deleted. | ||
checker.cleanup_empty_objects() | ||
checker.fix_encoding() | ||
checker.fix_alt_place_names() | ||
checker.fix_ctrlchars_in_notes() | ||
# checker.cleanup_missing_photos(cli=1) # should not be done on Web API | ||
checker.cleanup_deleted_name_formats() | ||
|
||
prev_total = -1 | ||
total = 0 | ||
|
||
while prev_total != total: | ||
prev_total = total | ||
|
||
checker.check_for_broken_family_links() | ||
checker.check_parent_relationships() | ||
checker.cleanup_empty_families(1) | ||
checker.cleanup_duplicate_spouses() | ||
|
||
total = checker.family_errors() | ||
|
||
checker.fix_duplicated_grampsid() | ||
checker.check_events() | ||
checker.check_person_references() | ||
checker.check_family_references() | ||
checker.check_place_references() | ||
checker.check_source_references() | ||
checker.check_citation_references() | ||
checker.check_media_references() | ||
checker.check_repo_references() | ||
checker.check_note_references() | ||
checker.check_tag_references() | ||
# checker.check_checksum() # should not be done on Web API | ||
checker.check_media_sourceref() | ||
# checker.check_note_links() # requires Gramps 5.2 | ||
checker.check_backlinks() | ||
|
||
# rebuilding reference maps needs to be done outside of a transaction | ||
# to avoid nesting transactions. | ||
if checker.bad_backlinks: | ||
checker.progress.set_pass("Rebuilding reference maps...", 6) | ||
db_handle.reindex_reference_map(checker.callback) | ||
|
||
db_handle.enable_signals() | ||
db_handle.request_rebuild() | ||
|
||
errs = checker.build_report() | ||
text = checker.text.getvalue() | ||
return {"num_errors": errs, "message": text} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# | ||
# Gramps Web API - A RESTful API for the Gramps genealogy program | ||
# | ||
# Copyright (C) 2024 David Straub | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation; either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
# | ||
|
||
"""Tests for the database repair endpoint.""" | ||
|
||
import os | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from gramps.cli.clidbman import CLIDbManager | ||
from gramps.gen.dbstate import DbState | ||
|
||
from gramps_webapi.app import create_app | ||
from gramps_webapi.auth import add_user, user_db | ||
from gramps_webapi.auth.const import ROLE_GUEST, ROLE_OWNER | ||
from gramps_webapi.const import ENV_CONFIG_FILE, TEST_AUTH_CONFIG | ||
|
||
|
||
class TestRepair(unittest.TestCase): | ||
"""Test database repair.""" | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.name = "Test Web API Repair" | ||
cls.dbman = CLIDbManager(DbState()) | ||
dirpath, _ = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite") | ||
tree = os.path.basename(dirpath) | ||
with patch.dict("os.environ", {ENV_CONFIG_FILE: TEST_AUTH_CONFIG}): | ||
cls.app = create_app() | ||
cls.app.config["TESTING"] = True | ||
cls.client = cls.app.test_client() | ||
with cls.app.app_context(): | ||
user_db.create_all() | ||
add_user(name="user", password="123", role=ROLE_GUEST, tree=tree) | ||
add_user(name="owner", password="123", role=ROLE_OWNER, tree=tree) | ||
rv = cls.client.post( | ||
"/api/token/", json={"username": "owner", "password": "123"} | ||
) | ||
access_token = rv.json["access_token"] | ||
cls.headers = {"Authorization": f"Bearer {access_token}"} | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
cls.dbman.remove_database(cls.name) | ||
|
||
def test_repair_empty_database(self): | ||
"""Test Repairing the empty database.""" | ||
rv = self.client.post("/api/trees/-/repair", headers=self.headers) | ||
assert rv.status_code == 201 | ||
assert rv.json["num_errors"] == 0 | ||
assert rv.json["message"] == "" | ||
|
||
def test_repair_empty_person(self): | ||
"""Test Repairing an empty person.""" | ||
rv = self.client.post("/api/people/", json={}, headers=self.headers) | ||
assert rv.status_code == 201 | ||
rv = self.client.get("/api/people/", headers=self.headers) | ||
assert rv.status_code == 200 | ||
assert len(rv.json) == 1 | ||
rv = self.client.post("/api/trees/-/repair", headers=self.headers) | ||
assert rv.status_code == 201 | ||
assert rv.json["num_errors"] == 1 | ||
rv = self.client.get("/api/people/", headers=self.headers) | ||
assert rv.status_code == 200 | ||
assert len(rv.json) == 0 | ||
|
||
def test_repair_empty_event(self): | ||
"""Test Repairing an empty event.""" | ||
rv = self.client.post("/api/events/", json={}, headers=self.headers) | ||
assert rv.status_code == 201 | ||
rv = self.client.get("/api/events/", headers=self.headers) | ||
assert rv.status_code == 200 | ||
assert len(rv.json) == 1 | ||
rv = self.client.post("/api/trees/-/repair", headers=self.headers) | ||
assert rv.status_code == 201 | ||
assert rv.json["num_errors"] == 1 | ||
rv = self.client.get("/api/events/", headers=self.headers) | ||
assert rv.status_code == 200 | ||
assert len(rv.json) == 0 |