There are three ways how we access models and associations with validity periods:
Usual case, when we browse the site on specific day. Queries in this case look like this:
[14] pry(main)> TimeMachine.at('2012-01-21') { Heading.first.chapter_dataset }
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `goods_nomenclatures` WHERE ((goods_nomenclatures.goods_nomenclature_item_id LIKE '__00000000') AND (`goods_nomenclatures`.`validity_start_date` <= '2012-01-21 00:00:00') AND ((`goods_nomenclatures`.`validity_end_date` >= '2012-01-21 00:00:00') OR (`goods_nomenclatures`.`validity_end_date` IS NULL)) AND (goods_nomenclatures.goods_nomenclature_item_id LIKE '0100000000')) ORDER BY `goods_nomenclature_item_id` ASC LIMIT 1">
Relevant dates when indexing for search. We traverse say all headings and what to fetch associated chapter that actually spans the validity period of the heading. E.g. there may be two Chapters '01' that were valid during different periods. When looking them up from Heading we need to fetch the relevant one. This is only used in indexing.
[1] pry(main)> TimeMachine.with_relevant_validity_periods { Heading.first.chapter_dataset }
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `goods_nomenclatures` WHERE ((goods_nomenclatures.goods_nomenclature_item_id LIKE '__00000000') AND (`goods_nomenclatures`.`validity_start_date` <= '1972-01-01 00:00:00') AND ((`goods_nomenclatures`.`validity_end_date` >= NULL) OR (`goods_nomenclatures`.`validity_end_date` IS NULL)) AND (goods_nomenclatures.goods_nomenclature_item_id LIKE '0100000000')) ORDER BY `goods_nomenclature_item_id` ASC LIMIT 1">
Normal console access, no time machine involved. We get:
[2] pry(main)> Heading.first.chapter_dataset
=> #<Sequel::Mysql2::Dataset: "SELECT * FROM `goods_nomenclatures` WHERE ((goods_nomenclatures.goods_nomenclature_item_id LIKE '__00000000') AND (goods_nomenclatures.goods_nomenclature_item_id LIKE '0100000000')) ORDER BY `goods_nomenclature_item_id` ASC LIMIT 1">
No conditions for time.
This change brings back the second flow for indexing as we lost it in the previous change. Added tests for Heading->Chapter, Commodity->Heading/Chapter associations that ensure second flow works too.
There are three ways how we access models and associations with validity periods:
Usual case, when we browse the site on specific day. Queries in this case look like this:
Relevant dates when indexing for search. We traverse say all headings and what to fetch associated chapter that actually spans the validity period of the heading. E.g. there may be two Chapters '01' that were valid during different periods. When looking them up from Heading we need to fetch the relevant one. This is only used in indexing.
Normal console access, no time machine involved. We get:
No conditions for time.
This change brings back the second flow for indexing as we lost it in the previous change. Added tests for Heading->Chapter, Commodity->Heading/Chapter associations that ensure second flow works too.