Merge pull request #22703 from ankush/restore

fix: backward compatible get_site_path
This commit is contained in:
Ankush Menat 2023-10-11 19:00:54 +05:30 committed by GitHub
commit e47492b4db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 6 deletions

View file

@ -1449,9 +1449,9 @@ def get_site_path(*joins):
"""Return path of current site.
:param *joins: Join additional path elements using `os.path.join`."""
from os.path import join, normpath
from os.path import join
return normpath(join(local.site_path, *joins))
return join(local.site_path, *joins)
def get_pymodule_path(modulename, *joins):

View file

@ -38,7 +38,7 @@ from frappe.utils.jinja_globals import bundled_asset
from frappe.utils.scheduler import enable_scheduler, is_scheduler_inactive
_result: Result | None = None
TEST_SITE = "commands-site-O4PN2QKA.test" # added random string tag to avoid collisions
TEST_SITE = "commands-site-O4PN2QK.test" # added random string tag to avoid collisions
CLI_CONTEXT = frappe._dict(sites=[TEST_SITE])
@ -252,14 +252,40 @@ class TestCommands(BaseTestCommands):
if value:
self.execute(f"bench set-config {key} {value} -g")
with self.switch_site(TEST_SITE):
public_file = frappe.new_doc(
"File", file_name=f"test_{frappe.generate_hash()}", content=frappe.generate_hash()
).insert()
private_file = frappe.new_doc(
"File", file_name=f"test_{frappe.generate_hash()}", content=frappe.generate_hash()
).insert()
# test 1: bench restore from full backup
self.execute("bench --site {test_site} backup --ignore-backup-conf", site_data)
self.execute("bench --site {test_site} backup --ignore-backup-conf --with-files", site_data)
self.execute(
"bench --site {test_site} execute frappe.utils.backups.fetch_latest_backups",
site_data,
)
site_data.update({"database": json.loads(self.stdout)["database"]})
self.execute("bench --site {test_site} restore {database}", site_data)
# Destroy some data and files to verify that they are indeed being restored.
with self.switch_site(TEST_SITE):
public_file.delete_file_data_content()
private_file.delete_file_data_content()
frappe.db.sql_ddl("DROP TABLE IF EXISTS `tabToDo`")
self.assertFalse(public_file.exists_on_disk())
self.assertFalse(private_file.exists_on_disk())
backup_data = json.loads(self.stdout)
site_data.update(backup_data)
self.execute(
"bench --site {test_site} restore {database} --with-public-files {public} --with-private-files {private} ",
site_data,
)
self.assertEqual(self.returncode, 0)
with self.switch_site(TEST_SITE):
self.assertTrue(frappe.db.table_exists("ToDo", cached=False))
self.assertTrue(public_file.exists_on_disk())
self.assertTrue(private_file.exists_on_disk())
# test 2: restore from partial backup
self.execute("bench --site {test_site} backup --exclude 'ToDo'", site_data)

View file

@ -142,6 +142,18 @@ class FrappeTestCase(unittest.TestCase):
yield
frappe.set_user(old_user)
@contextmanager
def switch_site(self, site: str):
"""Switch connection to different site.
Note: Drops current site connection completely."""
old_site = frappe.local.site
frappe.init(site, force=True)
frappe.connect()
yield
frappe.init(old_site, force=True)
frappe.connect()
class MockedRequestTestCase(FrappeTestCase):
def setUp(self):