Skip to content

Instantly share code, notes, and snippets.

@vjt
Last active August 29, 2015 13:57
Show Gist options
  • Save vjt/9861225 to your computer and use it in GitHub Desktop.
Save vjt/9861225 to your computer and use it in GitHub Desktop.

Revisions

  1. vjt revised this gist Mar 29, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion chronomodel-fix-db-issue-23.rb
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Download this file in your app root:
    #
    # curl -O https://gist.github.com/vjt/9861225/raw/dd2466cd948afbdb6013d0a788745f4be076ce7a/chronomodel-fix-db-issue-23.rb , and then run
    # curl -O https://gist.github.com/vjt/9861225/raw/dd2466cd948afbdb6013d0a788745f4be076ce7a/chronomodel-fix-db-issue-23.rb
    #
    # Run it:
    #
  2. vjt revised this gist Mar 29, 2014. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion chronomodel-fix-db-issue-23.rb
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,11 @@
    #
    # https://github.com/ifad/chronomodel/issues/23
    #
    # Download this file in your app root, and then run
    # Download this file in your app root:
    #
    # curl -O https://gist.github.com/vjt/9861225/raw/dd2466cd948afbdb6013d0a788745f4be076ce7a/chronomodel-fix-db-issue-23.rb , and then run
    #
    # Run it:
    #
    # rails run ./chronomodel-fix-db-issue-23.rb
    #
  3. vjt created this gist Mar 29, 2014.
    28 changes: 28 additions & 0 deletions chronomodel-fix-db-issue-23.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    # Fixes wrong database structure created by ChronoModel's issue #23
    #
    # https://github.com/ifad/chronomodel/issues/23
    #
    # Download this file in your app root, and then run
    #
    # 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