diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index b070c34..60c5b51 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -142,18 +142,6 @@ Style/ParallelAssignment: - 'lib/chrono_model/adapter/ddl.rb' - 'lib/chrono_model/time_machine.rb' -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: PreferredDelimiters. -Style/PercentLiteralDelimiters: - Exclude: - - 'lib/chrono_model/adapter.rb' - - 'lib/chrono_model/adapter/ddl.rb' - - 'lib/chrono_model/adapter/indexes.rb' - - 'lib/chrono_model/time_machine.rb' - - 'lib/chrono_model/time_machine/history_model.rb' - - 'lib/chrono_model/time_machine/time_query.rb' - - 'lib/chrono_model/time_machine/timeline.rb' - # This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: RequireEnglish, EnforcedStyle. # SupportedStyles: use_perl_names, use_english_names, use_builtin_english_names @@ -178,10 +166,3 @@ Style/StringLiterals: - 'lib/chrono_model/adapter/upgrade.rb' - 'lib/chrono_model/time_machine/time_query.rb' - 'lib/chrono_model/version.rb' - -# This cop supports safe autocorrection (--autocorrect). -# Configuration parameters: EnforcedStyle, MinSize. -# SupportedStyles: percent, brackets -Style/SymbolArray: - Exclude: - - 'lib/chrono_model/adapter.rb' diff --git a/benchmarks/benchmark.rb b/benchmarks/benchmark.rb index b9f3edf..2de4a0b 100755 --- a/benchmarks/benchmark.rb +++ b/benchmarks/benchmark.rb @@ -14,7 +14,8 @@ ITERATIONS = 5_000 -Foo.create! name: 'Ciao' +Foo.create! name: 'My Foo' +Bar.create! name: 'My Bar' first_foo = Foo.first @@ -58,4 +59,16 @@ x.report('#as_of') do ITERATIONS.times { first_foo.as_of(Time.now) } end + + x.report('TimeGate .merge') do + ITERATIONS.times { Bar.merge(Bar.all) } + end + + x.report('TimeGate .first') do + ITERATIONS.times { Bar.first } + end + + x.report('TimeGate .last') do + ITERATIONS.times { Bar.last } + end end diff --git a/benchmarks/benchmark_sample.rb b/benchmarks/benchmark_sample.rb index 30fa962..c90566f 100755 --- a/benchmarks/benchmark_sample.rb +++ b/benchmarks/benchmark_sample.rb @@ -17,12 +17,21 @@ t.string :name t.timestamps end + + create_table :bars, force: true do |t| + t.string :name + t.timestamps + end end class Foo < ActiveRecord::Base include ChronoModel::TimeMachine end +class Bar < ActiveRecord::Base + include ChronoModel::TimeGate +end + def run_benchmark_sample(iterations: 100) # Create iterations.times { |i| Foo.create! name: "Foo #{i}" } diff --git a/lib/chrono_model/adapter.rb b/lib/chrono_model/adapter.rb index 499dd6d..7f2bf5c 100644 --- a/lib/chrono_model/adapter.rb +++ b/lib/chrono_model/adapter.rb @@ -63,7 +63,7 @@ def chrono_setup! # def primary_key(table_name); end - [:primary_key, :indexes, :default_sequence_name].each do |method| + %i[primary_key indexes default_sequence_name].each do |method| define_method(method) do |*args| table_name = args.first return super(*args) unless is_chrono?(table_name) @@ -161,11 +161,11 @@ def chrono_metadata_for(view_name) def chrono_metadata_set(view_name, metadata) comment = MultiJson.dump(metadata) - execute %[ COMMENT ON VIEW #{view_name} IS #{quote(comment)} ] + execute %( COMMENT ON VIEW #{view_name} IS #{quote(comment)} ) end def valid_table_definition_options - super + [:temporal, :journal, :no_journal, :full_journal] + super + %i[temporal journal no_journal full_journal] end private diff --git a/lib/chrono_model/adapter/ddl.rb b/lib/chrono_model/adapter/ddl.rb index 19661f0..1809b72 100644 --- a/lib/chrono_model/adapter/ddl.rb +++ b/lib/chrono_model/adapter/ddl.rb @@ -214,7 +214,7 @@ def chrono_create_DELETE_trigger(table, pk, current, history) end def chrono_drop_trigger_functions_for(table_name) - %w(insert update delete).each do |func| + %w[insert update delete].each do |func| execute "DROP FUNCTION IF EXISTS chronomodel_#{table_name}_#{func}()" end end diff --git a/lib/chrono_model/adapter/indexes.rb b/lib/chrono_model/adapter/indexes.rb index b031248..dd2ec65 100644 --- a/lib/chrono_model/adapter/indexes.rb +++ b/lib/chrono_model/adapter/indexes.rb @@ -90,10 +90,10 @@ def chrono_create_history_indexes_for(table, p_pkey) # def chrono_rename_history_indexes(name, new_name) on_history_schema do - standard_index_names = %w( + standard_index_names = %w[ inherit_pkey instance_history pkey recorded_at timeline_consistency - ) + ] old_names = temporal_index_names(name, :validity) + standard_index_names.map { |i| "#{name}_#{i}" } diff --git a/lib/chrono_model/time_machine.rb b/lib/chrono_model/time_machine.rb index c2bc229..bb5aece 100644 --- a/lib/chrono_model/time_machine.rb +++ b/lib/chrono_model/time_machine.rb @@ -108,7 +108,7 @@ def as_of(time) def attribute_names_for_history_changes @attribute_names_for_history_changes ||= attribute_names - - %w(id hid validity recorded_at) + %w[id hid validity recorded_at] end def has_timeline(options) diff --git a/lib/chrono_model/time_machine/history_model.rb b/lib/chrono_model/time_machine/history_model.rb index 4d45479..433a7f1 100644 --- a/lib/chrono_model/time_machine/history_model.rb +++ b/lib/chrono_model/time_machine/history_model.rb @@ -90,7 +90,7 @@ def at(time) # Returns the history sorted by recorded_at # def sorted - all.order(Arel.sql(%[ #{quoted_table_name}."recorded_at" ASC, #{quoted_table_name}."hid" ASC ])) + all.order(Arel.sql(%( #{quoted_table_name}."recorded_at" ASC, #{quoted_table_name}."hid" ASC ))) end # Fetches the given +object+ history, sorted by history record time diff --git a/lib/chrono_model/time_machine/time_query.rb b/lib/chrono_model/time_machine/time_query.rb index 002556f..d38d8c3 100644 --- a/lib/chrono_model/time_machine/time_query.rb +++ b/lib/chrono_model/time_machine/time_query.rb @@ -93,7 +93,7 @@ def build_time_query(time, range, op = '&&') if time.is_a?(Array) Arel.sql %[ #{range.type}(#{time.first}, #{time.last}) #{op} #{table_name}.#{range.name} ] else - Arel.sql %[ #{time} <@ #{table_name}.#{range.name} ] + Arel.sql %( #{time} <@ #{table_name}.#{range.name} ) end end end diff --git a/lib/chrono_model/time_machine/timeline.rb b/lib/chrono_model/time_machine/timeline.rb index ccf4e83..baa65a9 100644 --- a/lib/chrono_model/time_machine/timeline.rb +++ b/lib/chrono_model/time_machine/timeline.rb @@ -56,7 +56,7 @@ def timeline(record = nil, options = {}) relation = relation.order("ts #{relation_order}") - relation = relation.from(%["public".#{quoted_table_name}]) unless chrono? + relation = relation.from("public.#{quoted_table_name}") unless chrono? relation = relation.where(id: rid) if rid sql = "SELECT ts FROM ( #{relation.to_sql} ) AS foo WHERE ts IS NOT NULL".dup @@ -110,7 +110,7 @@ def quoted_history_fields @quoted_history_fields ||= begin validity = "#{connection.quote_table_name(table_name)}.#{connection.quote_column_name('validity')}" - %w(lower upper).map! { |func| "#{func}(#{validity})" } + %w[lower upper].map! { |func| "#{func}(#{validity})" } end end end