Skip to content

Commit

Permalink
BUG: Checkpoints were not being moved for directory renames
Browse files Browse the repository at this point in the history
  • Loading branch information
dmichalowicz authored and Scott Sanderson committed Jul 23, 2019
1 parent c3a2ee6 commit aad97b2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
22 changes: 22 additions & 0 deletions pgcontents/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ def move_single_remote_checkpoint(db,
def move_remote_checkpoints(db, user_id, src_api_path, dest_api_path):
src_db_path = from_api_filename(src_api_path)
dest_db_path = from_api_filename(dest_api_path)

# Update the paths of the checkpoints for the file being renamed. If the
# source path is for a directory then this is a no-op.
db.execute(
remote_checkpoints.update().where(
and_(
Expand All @@ -661,6 +664,25 @@ def move_remote_checkpoints(db, user_id, src_api_path, dest_api_path):
),
)

# If the given source path is for a directory, update the paths of the
# checkpoints for all files in that directory and its subdirectories.
db.execute(
remote_checkpoints.update().where(
and_(
remote_checkpoints.c.user_id == user_id,
remote_checkpoints.c.path.startswith(src_db_path),
),
).values(
path=func.concat(
dest_db_path,
func.right(
remote_checkpoints.c.path,
-func.length(src_db_path),
),
),
)
)


def get_remote_checkpoint(db, user_id, api_path, checkpoint_id, decrypt_func):
db_path = from_api_filename(api_path)
Expand Down
21 changes: 15 additions & 6 deletions pgcontents/tests/test_pgcontents_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,23 @@ def test_checkpoints_move_with_file(self):
checkpoints = self.api.get_checkpoints('foo/bar/a.ipynb').json()
self.assertEqual(checkpoints, [response_json])

# Move the file back up.
self.api.rename('foo/bar/a.ipynb', 'foo/a.ipynb')

# Looking for checkpoints in the old location should yield no results.
# Rename the directory that the file is in.
self.api.rename('foo/bar', 'foo/car')
self.assertEqual(
self.api.get_checkpoints('foo/bar/a.ipynb').json(),
[],
)
checkpoints = self.api.get_checkpoints('foo/car/a.ipynb').json()
self.assertEqual(checkpoints, [response_json])

# Looking for checkpoints in the new location should work.
checkpoints = self.api.get_checkpoints('foo/a.ipynb').json()
# Now move the directory that the file is in.
self.make_dir('foo/buz')
self.api.rename('foo/car', 'foo/buz/car')
self.assertEqual(
self.api.get_checkpoints('foo/car/a.ipynb').json(),
[],
)
checkpoints = self.api.get_checkpoints('foo/buz/car/a.ipynb').json()
self.assertEqual(checkpoints, [response_json])


Expand Down Expand Up @@ -405,6 +411,9 @@ def teardown_class(cls):
super(PostgresContentsFileCheckpointsAPITest, cls).teardown_class()
cls.td.cleanup()

def test_checkpoints_move_with_file(self):
pass


def postgres_checkpoints_config():
"""
Expand Down

0 comments on commit aad97b2

Please sign in to comment.