Last active
August 29, 2015 13:57
-
-
Save vjt/9861225 to your computer and use it in GitHub Desktop.
Database fix for wrongly generated ChronoModel constraints (issue #23).
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
# Fixes wrong database structure created by ChronoModel's issue #23 | |
# | |
# https://github.com/ifad/chronomodel/issues/23 | |
# | |
# Download this file in your app root: | |
# | |
# curl -O https://gist.githubusercontent.com/vjt/9861225/raw/dd2466cd948afbdb6013d0a788745f4be076ce7a/chronomodel-fix-db-issue-23.rb | |
# | |
# Run it: | |
# | |
# rails run ./chronomodel-fix-db-issue-23.rb | |
# | |
# Sorry for the hassle :-). | |
# | |
# - vjt | |
# | |
conn = ActiveRecord::Base.connection | |
Dir['./app/models/**/*.rb'].sort.each {|f| require f} | |
chrono_tables = ChronoModel::TimeMachine.chrono_models.keys | |
conn.transaction do | |
chrono_tables.each do |t| | |
print "Fixing #{t}..." | |
pk = conn.primary_key(t) | |
conn.execute "alter table history.#{t} drop constraint #{t}_timeline_consistency" | |
conn.execute "alter table history.#{t} add constraint #{t}_timeline_consistency exclude using gist(#{pk} with =, validity with &&)" | |
puts "done" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ref: ifad/chronomodel#23
Download link: https://gist.githubusercontent.com/vjt/9861225/raw/dd2466cd948afbdb6013d0a788745f4be076ce7a/chronomodel-fix-db-issue-23.rb