Skip to content

Commit 0c5a18e

Browse files
committed
BUG: Checkpoints were not being moved for directory renames
1 parent d5568fd commit 0c5a18e

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

pgcontents/query.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,9 @@ def move_single_remote_checkpoint(db,
650650
def move_remote_checkpoints(db, user_id, src_api_path, dest_api_path):
651651
src_db_path = from_api_filename(src_api_path)
652652
dest_db_path = from_api_filename(dest_api_path)
653+
654+
# Update the paths of the checkpoints for the file being renamed. If the
655+
# source path is for a directory then this is a no-op.
653656
db.execute(
654657
remote_checkpoints.update().where(
655658
and_(
@@ -661,6 +664,25 @@ def move_remote_checkpoints(db, user_id, src_api_path, dest_api_path):
661664
),
662665
)
663666

667+
# If the given source path is for a directory, update the paths of the
668+
# checkpoints for all files in that directory and its subdirectories.
669+
db.execute(
670+
remote_checkpoints.update().where(
671+
and_(
672+
remote_checkpoints.c.user_id == user_id,
673+
remote_checkpoints.c.path.startswith(src_db_path),
674+
),
675+
).values(
676+
path=func.concat(
677+
dest_db_path,
678+
func.right(
679+
remote_checkpoints.c.path,
680+
-func.length(src_db_path),
681+
),
682+
),
683+
)
684+
)
685+
664686

665687
def get_remote_checkpoint(db, user_id, api_path, checkpoint_id, decrypt_func):
666688
db_path = from_api_filename(api_path)

pgcontents/tests/test_pgcontents_api.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,17 +215,23 @@ def test_checkpoints_move_with_file(self):
215215
checkpoints = self.api.get_checkpoints('foo/bar/a.ipynb').json()
216216
self.assertEqual(checkpoints, [response_json])
217217

218-
# Move the file back up.
219-
self.api.rename('foo/bar/a.ipynb', 'foo/a.ipynb')
220-
221-
# Looking for checkpoints in the old location should yield no results.
218+
# Rename the directory that the file is in.
219+
self.api.rename('foo/bar', 'foo/car')
222220
self.assertEqual(
223221
self.api.get_checkpoints('foo/bar/a.ipynb').json(),
224222
[],
225223
)
224+
checkpoints = self.api.get_checkpoints('foo/car/a.ipynb').json()
225+
self.assertEqual(checkpoints, [response_json])
226226

227-
# Looking for checkpoints in the new location should work.
228-
checkpoints = self.api.get_checkpoints('foo/a.ipynb').json()
227+
# Now move the directory that the file is in.
228+
self.make_dir('foo/buz')
229+
self.api.rename('foo/car', 'foo/buz/car')
230+
self.assertEqual(
231+
self.api.get_checkpoints('foo/car/a.ipynb').json(),
232+
[],
233+
)
234+
checkpoints = self.api.get_checkpoints('foo/buz/car/a.ipynb').json()
229235
self.assertEqual(checkpoints, [response_json])
230236

231237

@@ -405,6 +411,9 @@ def teardown_class(cls):
405411
super(PostgresContentsFileCheckpointsAPITest, cls).teardown_class()
406412
cls.td.cleanup()
407413

414+
def test_checkpoints_move_with_file(self):
415+
pass
416+
408417

409418
def postgres_checkpoints_config():
410419
"""

0 commit comments

Comments
 (0)