Created
October 3, 2014 17:58
-
-
Save anonymous/9d54cc1fb6cb74372353 to your computer and use it in GitHub Desktop.
Delete revisions from the Drupal 7 node_revisions table such that only the newest 3 revisions remain for each node. It joins on the node table to make sure that the current, active vid assigned in the node table doesn't get deleted.
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
DELETE FROM node_revisions WHERE vid IN ( | |
SELECT subquery.vid FROM ( | |
SELECT @row_num := IF(@prev_value=nr.nid,@row_num+1,1) AS RowNumber | |
,nr.nid | |
,nr.vid | |
,nr.timestamp | |
,@prev_value := nr.nid | |
FROM node_revisions nr, | |
(SELECT @row_num := 1) x, | |
(SELECT @prev_value := '') y | |
ORDER BY nr.nid, nr.timestamp DESC | |
) subquery LEFT JOIN node n ON n.vid = subquery.vid | |
WHERE RowNumber > 3 AND n.nid IS NULL | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment