chronomodel icon indicating copy to clipboard operation
chronomodel copied to clipboard

Missing relation indexes on historical tables

Open tagliala opened this issue 7 months ago • 2 comments

When using t.references or t.belongs_to in a migration, Chronomodel does not create the reference index in the historical table

Reproducible test case

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  gem 'chrono_model'
  # Test against latest Chronomodel:
  # gem 'chrono_model', github: 'ifad/chronomodel'

  gem 'pg'
  gem 'debug'
  gem 'rails'
end

require 'chrono_model'
require 'minitest/autorun'
require 'logger'
require 'debug'

# Needs a database called `chronomodel_test`

ActiveRecord::Base.establish_connection(adapter: 'chronomodel', database: 'chronomodel_test')
ActiveRecord::Base.logger = Logger.new($stdout)

ActiveRecord::Schema.define do
  enable_extension :btree_gist

  create_table :activities, temporal: true, force: true do |t|
    t.string :name
    t.timestamps
  end

  create_table :activity_roadmaps, temporal: true, force: true do |t|
    t.belongs_to :activity
    t.string :name

    t.timestamps
  end
end

class Activity < ActiveRecord::Base
  include ChronoModel::TimeMachine

  has_one :activity_roadmap, dependent: :destroy
end

class ActivityRoadmap < ActiveRecord::Base
  include ChronoModel::TimeMachine

  belongs_to :activity
end

SQL = <<~SQL.squish
  SELECT 1 AS one
    FROM pg_indexes
    WHERE tablename = 'activity_roadmaps'
    AND schemaname = '%<schemaname>s'
    AND indexname = 'index_activity_roadmaps_on_activity_id';
SQL

class BugTest < Minitest::Test
  def test_temporal_index
    refute_empty ActiveRecord::Base.connection.select_values(format(SQL, schemaname: 'temporal'))
  end

  def test_history_index
    refute_empty ActiveRecord::Base.connection.select_values(format(SQL, schemaname: 'history'))
  end
end

tagliala avatar Jul 24 '24 11:07 tagliala