Skip to content

Instantly share code, notes, and snippets.

@zloyrusskiy
Created April 6, 2015 10:02
Show Gist options
  • Save zloyrusskiy/314c5b231d54dbe8ba13 to your computer and use it in GitHub Desktop.
Save zloyrusskiy/314c5b231d54dbe8ba13 to your computer and use it in GitHub Desktop.
Move location from one to another
SET @from_loc := 'ChIJ2aunYmllQTQRntXu6lNDlCU';
SET @to_loc := 'ChIJ-yRniZpWPEURE_YRZvj9CRQ';
START TRANSACTION;
# получение начальных данных
SELECT id, path, `level` INTO @from_id, @from_path, @from_level FROM locations WHERE place_id = @from_loc;
SELECT id, path, `level` INTO @to_id, @to_path, @to_level FROM locations WHERE place_id = @to_loc;
SET @new_path := CONCAT(@to_path, @to_id, ',');
# правим саму локацию
UPDATE locations
SET parent_id = @to_id, path = @new_path, `level` = @to_level + 1
WHERE id = @from_id;
SET @child_path_beginning := CONCAT(@from_path, @from_id, ',');
SET @new_child_path_beginning := CONCAT(@new_path, @from_id, ',');
# перебиндиваем детей
UPDATE locations
SET path = REPLACE(path, @child_path_beginning, @new_child_path_beginning), `level` = `level` - @from_level + @to_level + 1
WHERE path <> '' AND path LIKE CONCAT(@child_path_beginning, '%');
COMMIT;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment