-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add _As Of System Time_ support for models. See https://www.cockroachlabs.com/docs/stable/as-of-system-time Fixes #281
- Loading branch information
Showing
8 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Arel | ||
module Nodes | ||
module JoinSourceExt | ||
def initialize(...) | ||
super | ||
@aost = nil | ||
end | ||
|
||
def hash | ||
[*super, aost].hash | ||
end | ||
|
||
def eql?(other) | ||
super && aost == other.aost | ||
end | ||
alias_method :==, :eql? | ||
end | ||
JoinSource.attr_accessor :aost | ||
JoinSource.prepend JoinSourceExt | ||
end | ||
module SelectManagerExt | ||
def aost(time) | ||
@ctx.source.aost = time | ||
nil | ||
end | ||
end | ||
SelectManager.prepend SelectManagerExt | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
# This file may be remove after next | ||
# rails release. | ||
# Whenever https://github.com/rails/rails/commit/8bd463c | ||
# is part of a rails version. | ||
|
||
require "cases/arel/helper" | ||
|
||
module Arel | ||
module Nodes | ||
class TestNodes < Arel::Test | ||
def test_every_arel_nodes_have_hash_eql_eqeq_from_same_class | ||
# #descendants code from activesupport | ||
node_descendants = [] | ||
ObjectSpace.each_object(Arel::Nodes::Node.singleton_class) do |k| | ||
next if k.respond_to?(:singleton_class?) && k.singleton_class? | ||
node_descendants.unshift k unless k == self | ||
end | ||
node_descendants.delete(Arel::Nodes::Node) | ||
node_descendants.delete(Arel::Nodes::NodeExpression) | ||
|
||
default_hash_owner = Object.instance_method(:hash).owner | ||
|
||
bad_node_descendants = node_descendants.reject do |subnode| | ||
eqeq_owner = subnode.instance_method(:==).owner | ||
eql_owner = subnode.instance_method(:eql?).owner | ||
hash_owner = subnode.instance_method(:hash).owner | ||
|
||
hash_owner != default_hash_owner && | ||
eqeq_owner == eql_owner && | ||
eqeq_owner == hash_owner | ||
end | ||
|
||
problem_msg = "Some subclasses of Arel::Nodes::Node do not have a" \ | ||
" #== or #eql? or #hash defined from the same class as the others" | ||
assert_empty bad_node_descendants, problem_msg | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper_cockroachdb" | ||
require "models/post" | ||
require "models/comment" | ||
|
||
module CockroachDB | ||
class AostTest < ActiveRecord::TestCase | ||
def test_simple_aost | ||
time = 2.days.ago | ||
re_time = Regexp.quote(time.iso8601) | ||
assert_match(/from "posts" as of system time '#{re_time}'/i, Post.aost(time).to_sql) | ||
assert_match(/from "posts" as of system time '#{re_time}'/i, Post.where(name: "foo").aost(time).to_sql) | ||
end | ||
|
||
def test_reset_aost | ||
time = 1.second.from_now | ||
assert_match(/from "posts"\z/i, Post.aost(time).aost(nil).to_sql) | ||
end | ||
|
||
def test_aost_with_join | ||
time = Time.now | ||
assert_match( | ||
/FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" AS OF SYSTEM TIME '#{Regexp.quote time.iso8601}'/, | ||
Post.joins(:comments).aost(time).to_sql | ||
) | ||
end | ||
|
||
def test_aost_with_subquery | ||
time = 4.seconds.ago | ||
assert_match(/from \(.*?\) subquery as of system time '#{Regexp.quote time.iso8601}'/i, Post.from(Post.where(name: "foo")).aost(time).to_sql) | ||
end | ||
|
||
def test_only_time_input | ||
time = 1.second.ago | ||
expected = "SELECT \"posts\".* FROM \"posts\" AS OF SYSTEM TIME '#{time.iso8601}'" | ||
assert_equal expected, Post.aost(time).to_sql | ||
assert_raises(ArgumentError) { Post.aost("no time") } | ||
assert_raises(ArgumentError) { Post.aost(true) } | ||
end | ||
end | ||
|
||
class AostNoTransactionTest < ActiveRecord::TestCase | ||
# AOST per query is not compatible with transactions. | ||
self.use_transactional_tests = false | ||
|
||
def test_aost_with_multiple_queries | ||
time = 1.second.ago | ||
queries = capture_sql { | ||
Post.aost(time).limit(2).find_each(batch_size: 1).to_a | ||
} | ||
queries.each do | ||
assert_match /FROM \"posts\" AS OF SYSTEM TIME '#{Regexp.quote time.iso8601}'/, _1 | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exclude :test_every_arel_nodes_have_hash_eql_eqeq_from_same_class, "Overwitten, see https://github.com/rails/rails/issues/49274" |