Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nshaw/4731003 to your computer and use it in GitHub Desktop.
Save nshaw/4731003 to your computer and use it in GitHub Desktop.
In 5.1.7, this index is unique (create unique index IX_3463D95B on JournalArticle (uuid_, groupId)) but in the client's database, it's showing as a non-unique index. So that explains why the duplicate entries are allowed in the database. This script will remove all old versions of JournalArticles and only retain the newest therefore satisfying t…
drop index IX_3463D95B on JournalArticle;
create temporary table temp_ja
select * from (
select groupId, articleId, uuid_, type_, max(version) as 'maxVersionId', count(id_) as 'countId' from JournalArticle ja
group by groupId, uuid_
) temp
where temp.countId > 1;
delete ja from JournalArticle ja
join temp_ja as tja on tja.groupId = ja.groupId and tja.uuid_ = ja.uuid_ and tja.articleId = ja.articleId
where ja.version < tja.maxVersionId;
drop table temp_ja;
create unique index IX_3463D95B on JournalArticle (uuid_, groupId);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment