Skip to content

Instantly share code, notes, and snippets.

@jambun
Created April 8, 2020 00:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jambun/157ddb4479664f481e9780a72fe3c8e2 to your computer and use it in GitHub Desktop.
Save jambun/157ddb4479664f481e9780a72fe3c8e2 to your computer and use it in GitHub Desktop.
this is very slow - can we make it faster?
Sequel.migration do
up do
alter_table(:history) do
add_column(:revision, Integer, :null => false, :default => 1)
end
revision = 1
last_version = false
self[:history].select(:model, :record_id, :lock_version).order_by(:model, :record_id, :lock_version).each do |version|
if last_version && (last_version[:model] != version[:model] || last_version[:record_id] != version[:record_id])
revision = 1
end
self[:history].filter(version).update(:revision => revision)
revision += 1
last_version = version
end
alter_table(:history) do
add_index([:record_id, :model, :revision], :unique => true, :name => "uniq_history_record_revision")
end
end
down do
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment