From 315fe22c05907c5d3f86910db0f62f3e2924d0a9 Mon Sep 17 00:00:00 2001 From: donv Date: Sun, 28 Apr 2024 23:42:05 +0200 Subject: [PATCH] Upgrade to ActiveRecord 7.1 + Ruby 3.3 --- .github/workflows/test.yml | 12 +++--- .rubocop.yml | 2 +- .rubocop_todo.yml | 46 ++++++++++------------ .travis.yml | 29 -------------- Gemfile | 2 + activerecord-time.gemspec | 9 +---- gemfiles/ar61.gemfile | 3 -- gemfiles/ar70.gemfile | 3 -- gemfiles/common.gemfile | 26 +------------ gemfiles/gems_ar70.rb | 12 ++++++ gemfiles/gems_ar71.rb | 18 +++++++++ lib/activerecord-time/version.rb | 2 +- lib/time_of_day.rb | 12 +++++- lib/time_of_day/core_ext.rb | 1 + matrix_test.rb | 66 ++++++++++++++++---------------- test/test_helper.rb | 2 +- test/time_of_day_test.rb | 3 +- 17 files changed, 110 insertions(+), 138 deletions(-) delete mode 100644 .travis.yml delete mode 100644 gemfiles/ar61.gemfile delete mode 100644 gemfiles/ar70.gemfile create mode 100644 gemfiles/gems_ar70.rb create mode 100644 gemfiles/gems_ar71.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d6c7fda..acbec40 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,12 +18,12 @@ jobs: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 with: - ruby-version: '3.0' + ruby-version: '3.3' bundler-cache: true # runs 'bundle install' and caches installed gems automatically - name: Rubocop run: bundle exec rubocop --auto-correct-all Test: - runs-on: ubuntu-20.04 + runs-on: ubuntu-latest services: postgres: image: postgres @@ -37,15 +37,15 @@ jobs: fail-fast: false matrix: # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0' - ruby: [ 3.1, '3.0', 2.7, 2.6, jruby, truffleruby ] - gemfile: [ ar70, ar61 ] + ruby: [ 3.3, 3.2, 3.1, jruby, truffleruby ] + gemfile: [ ar71, ar70 ] adapter: [ postgresql, sqlite3 ] exclude: - ruby: jruby - adapter: sqlite3 + - ruby: truffleruby env: ADAPTER: ${{ matrix.adapter }} - BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile + BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/gems_${{ matrix.gemfile }}.rb steps: - uses: actions/checkout@v2 - uses: ruby/setup-ruby@v1 diff --git a/.rubocop.yml b/.rubocop.yml index 08dbab3..fcb9910 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -58,7 +58,7 @@ Rails: Security/Eval: Exclude: - - 'gemfiles/*.gemfile' + - 'gemfiles/gems_*.rb' Style/Documentation: Enabled: false diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 77640f8..a22f361 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -1,6 +1,6 @@ # This configuration was generated by # `rubocop --auto-gen-config` -# on 2021-11-25 11:12:32 UTC using RuboCop version 1.23.0. +# on 2024-04-28 21:09:41 UTC using RuboCop version 1.63.4. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. # Note that changes in the inspected code, or installation of new @@ -17,38 +17,33 @@ Lint/DuplicateBranch: Exclude: - 'lib/time_of_day.rb' -# Offense count: 3 -# Configuration parameters: IgnoredMethods, CountRepeatedAttributes. +# Offense count: 4 +# Configuration parameters: AllowedMethods, AllowedPatterns, CountRepeatedAttributes. Metrics/AbcSize: - Max: 31 - -# Offense count: 1 -# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. -# IgnoredMethods: refine -Metrics/BlockLength: - Max: 28 + Max: 32 # Offense count: 2 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 188 + Max: 189 -# Offense count: 1 -# Configuration parameters: IgnoredMethods. +# Offense count: 2 +# Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/CyclomaticComplexity: Max: 10 # Offense count: 3 -# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods. +# Configuration parameters: CountComments, CountAsOne, AllowedMethods, AllowedPatterns. Metrics/MethodLength: Max: 13 -# Offense count: 1 -# Configuration parameters: IgnoredMethods. +# Offense count: 2 +# Configuration parameters: AllowedMethods, AllowedPatterns. Metrics/PerceivedComplexity: Max: 11 # Offense count: 2 +# This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle, AllowToTime. # SupportedStyles: strict, flexible Rails/Date: @@ -56,32 +51,31 @@ Rails/Date: - 'lib/time_of_day.rb' # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports unsafe autocorrection (--autocorrect-all). Rails/WhereEquals: Exclude: - 'test/time_of_day_test.rb' # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports safe autocorrection (--autocorrect). Rails/WhereNot: Exclude: - 'test/time_of_day_test.rb' -# Offense count: 2 -# Cop supports --auto-correct. -Security/YAMLLoad: +# Offense count: 1 +Security/CompoundHash: Exclude: - - 'test/test_helper.rb' - - 'test/time_of_day_test.rb' + - 'lib/time_of_day.rb' # Offense count: 1 +# This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: AllowModifiersOnSymbols. # SupportedStyles: inline, group Style/AccessModifierDeclarations: EnforcedStyle: inline # Offense count: 3 -# Cop supports --auto-correct. +# This cop supports safe autocorrection (--autocorrect). # Configuration parameters: EnforcedStyle. # SupportedStyles: separated, grouped Style/AccessorGrouping: @@ -89,7 +83,7 @@ Style/AccessorGrouping: - 'lib/time_of_day.rb' # Offense count: 1 -# Cop supports --auto-correct. +# This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: EnforcedStyle. # SupportedStyles: nested, compact Style/ClassAndModuleChildren: @@ -104,7 +98,7 @@ Style/OptionalBooleanParameter: - 'lib/time_of_day.rb' # Offense count: 5 -# Cop supports --auto-correct. +# This cop supports unsafe autocorrection (--autocorrect-all). # Configuration parameters: Mode. Style/StringConcatenation: Exclude: diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 79701d8..0000000 --- a/.travis.yml +++ /dev/null @@ -1,29 +0,0 @@ -language: ruby -sudo: false -cache: bundler -services: - - postgresql -jdk: - - openjdk11 -rvm: - - ruby-3.0 - - ruby-2.7 - - ruby-2.6 - - ruby-2.5 - - jruby -gemfile: - - gemfiles/ar61.gemfile - - gemfiles/ar60.gemfile - - gemfiles/ar52.gemfile - - gemfiles/ar51.gemfile -env: - global: - - JRUBY_OPTS="--dev --debug" - matrix: - - ADAPTER="postgresql" - - ADAPTER="sqlite3" -matrix: - allow_failures: - - rvm: jruby - env: ADAPTER="sqlite3" - - rvm: ruby-2.7 diff --git a/Gemfile b/Gemfile index bfcc670..861f3a8 100644 --- a/Gemfile +++ b/Gemfile @@ -5,4 +5,6 @@ source 'https://rubygems.org' # Specify your gem's dependencies in activerecord-time.gemspec gemspec +eval File.read("#{__dir__}/gemfiles/common.gemfile") # rubocop:disable Security/Eval + gem 'activerecord', ENV['AR_VERSION'] if ENV['AR_VERSION'] diff --git a/activerecord-time.gemspec b/activerecord-time.gemspec index 62dfe20..d7ae3f3 100644 --- a/activerecord-time.gemspec +++ b/activerecord-time.gemspec @@ -16,7 +16,7 @@ Gem::Specification.new do |gem| gem.summary = 'A handler for storing TimeOfDay objects in ActiveRecord objects as sql time values.' gem.homepage = 'https://github.com/donv/activerecord-time' gem.license = 'MIT' - gem.required_ruby_version = '>= 2.6' + gem.required_ruby_version = '>= 3.1' gem.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) } @@ -25,13 +25,6 @@ Gem::Specification.new do |gem| gem.add_runtime_dependency 'activerecord', '~>7.0' - if defined? JRUBY_VERSION - gem.add_development_dependency 'activerecord-jdbcpostgresql-adapter' - gem.add_development_dependency 'activerecord-jdbcsqlite3-adapter' - else - gem.add_development_dependency 'pg' - gem.add_development_dependency 'sqlite3' - end gem.add_development_dependency 'rake' gem.add_development_dependency 'rubocop' gem.add_development_dependency 'rubocop-performance' diff --git a/gemfiles/ar61.gemfile b/gemfiles/ar61.gemfile deleted file mode 100644 index 37a47ca..0000000 --- a/gemfiles/ar61.gemfile +++ /dev/null @@ -1,3 +0,0 @@ -eval File.read("#{File.dirname __FILE__}/common.gemfile") - -gem 'activerecord', '~>6.1.0' diff --git a/gemfiles/ar70.gemfile b/gemfiles/ar70.gemfile deleted file mode 100644 index accc0ad..0000000 --- a/gemfiles/ar70.gemfile +++ /dev/null @@ -1,3 +0,0 @@ -eval File.read("#{File.dirname __FILE__}/common.gemfile") - -gem 'activerecord', '~>7.0.0' diff --git a/gemfiles/common.gemfile b/gemfiles/common.gemfile index d045b90..6df25af 100644 --- a/gemfiles/common.gemfile +++ b/gemfiles/common.gemfile @@ -1,32 +1,8 @@ source 'https://rubygems.org' -unless defined?(SKIP_JRUBY) - platform :jruby do - if defined?(RAILS_5) - git_source(:github) do |repo_name| - repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/') - "https://github.com/#{repo_name}.git" - end - gem 'activerecord-jdbc-adapter', github: 'jruby/activerecord-jdbc-adapter' - gem 'activerecord-jdbcderby-adapter', github: 'jruby/activerecord-jdbc-adapter' - gem 'activerecord-jdbcpostgresql-adapter', github: 'jruby/activerecord-jdbc-adapter' - gem 'activerecord-jdbcsqlite3-adapter', github: 'jruby/activerecord-jdbc-adapter' - gem 'jdbc-postgres', github: 'jruby/activerecord-jdbc-adapter' - gem 'jdbc-sqlite3', github: 'jruby/activerecord-jdbc-adapter' - else - # rubocop: disable Bundler/DuplicatedGem - gem 'activerecord-jdbc-adapter' - gem 'activerecord-jdbcderby-adapter' - gem 'activerecord-jdbcpostgresql-adapter' - gem 'activerecord-jdbcsqlite3-adapter' - # rubocop: enable Bundler/DuplicatedGem - end - end -end - platform :ruby do gem 'pg' - gem 'sqlite3' + gem 'sqlite3', '<2' end gem 'minitest-reporters' diff --git a/gemfiles/gems_ar70.rb b/gemfiles/gems_ar70.rb new file mode 100644 index 0000000..3adcc0e --- /dev/null +++ b/gemfiles/gems_ar70.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +eval File.read("#{File.dirname __FILE__}/common.gemfile") + +platform :jruby do + # gem 'activerecord-jdbc-adapter' + # gem 'activerecord-jdbcderby-adapter' + gem 'activerecord-jdbcpostgresql-adapter' + gem 'activerecord-jdbcsqlite3-adapter' +end + +gem 'activerecord', '~>7.0.0' diff --git a/gemfiles/gems_ar71.rb b/gemfiles/gems_ar71.rb new file mode 100644 index 0000000..3db5721 --- /dev/null +++ b/gemfiles/gems_ar71.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +eval File.read("#{File.dirname __FILE__}/common.gemfile") + +platform :jruby do + git_source(:github) do |repo_name| + repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/') + "https://github.com/#{repo_name}.git" + end + gem 'activerecord-jdbc-adapter', github: 'jruby/activerecord-jdbc-adapter' + # gem 'activerecord-jdbcderby-adapter', github: 'jruby/activerecord-jdbc-adapter' + gem 'activerecord-jdbcpostgresql-adapter', github: 'jruby/activerecord-jdbc-adapter' + gem 'activerecord-jdbcsqlite3-adapter', github: 'jruby/activerecord-jdbc-adapter' + gem 'jdbc-postgres', github: 'jruby/activerecord-jdbc-adapter' + gem 'jdbc-sqlite3', github: 'jruby/activerecord-jdbc-adapter' +end + +gem 'activerecord', '~>7.1.0' diff --git a/lib/activerecord-time/version.rb b/lib/activerecord-time/version.rb index ae369e1..fff9394 100644 --- a/lib/activerecord-time/version.rb +++ b/lib/activerecord-time/version.rb @@ -2,6 +2,6 @@ module Activerecord module Time - VERSION = '0.21.0' + VERSION = '0.22.0' end end diff --git a/lib/time_of_day.rb b/lib/time_of_day.rb index 69512ad..cf6d3d8 100644 --- a/lib/time_of_day.rb +++ b/lib/time_of_day.rb @@ -74,6 +74,14 @@ def on(date) Time.local(date.year, date.month, date.day, hour, minute, second) # rubocop: disable Rails/TimeZone end + def change(*) + self + end + + def year + 2000 + end + def +(other) raise "Illegal argument: #{other.inspect}" unless other.is_a? Numeric @@ -89,7 +97,7 @@ def -(other) t2 = Time.local(0, 1, 1, other.hour, other.minute, other.second) # rubocop: disable Rails/TimeZone (t1 - t2).seconds when Numeric - self.+(-other) + self + -other else raise "Illegal argument: #{other.inspect}" end @@ -133,7 +141,7 @@ def to_s(with_seconds = true) rescue "#{@hour.inspect}:#{@minute.inspect}:#{@second.inspect}" end - alias_method :to_fs, :to_s + alias to_fs to_s def inspect "#<#{self.class} hour=#{@hour}, minute=#{@minute}, second=#{@second}>" diff --git a/lib/time_of_day/core_ext.rb b/lib/time_of_day/core_ext.rb index 6497483..d1f28f1 100644 --- a/lib/time_of_day/core_ext.rb +++ b/lib/time_of_day/core_ext.rb @@ -8,6 +8,7 @@ def time_of_day end end +require 'date' class DateTime def time_of_day TimeOfDay.new(hour, min, sec) diff --git a/matrix_test.rb b/matrix_test.rb index c2f2677..af0492e 100755 --- a/matrix_test.rb +++ b/matrix_test.rb @@ -1,16 +1,16 @@ #!/usr/bin/env ruby -w # frozen_string_literal: true -system('rubocop --auto-correct-all') || exit(1) +system('rubocop --autocorrect-all') || exit(1) update_gemfiles = ARGV.delete('--update') require 'yaml' -travis = YAML.safe_load(File.read('.travis.yml')) +actions = YAML.safe_load_file('.github/workflows/test.yml') def run_script(ruby, env, gemfile) - env.scan(/\b(?[A-Z_]+)="(?.+?)"/) do |key, value| - ENV[key] = value + env.each do |key, value| + ENV[key.to_s] = value end puts '*' * 80 puts "Testing #{ruby} #{gemfile} #{env}" @@ -24,10 +24,9 @@ def use_gemfile(ruby, gemfile, update_gemfiles) puts '$' * 80 puts "Testing #{gemfile}" puts - ENV['BUNDLE_GEMFILE'] = gemfile - system "chruby-exec #{ruby} -- bundle -v" + ENV['BUNDLE_GEMFILE'] = "gemfiles/gems_#{gemfile}.rb" if update_gemfiles - system "chruby-exec #{ruby} -- bundle update" + system "chruby-exec #{ruby} -- bundle update && chruby-exec #{ruby} -- bundle update --bundler" else system "chruby-exec #{ruby} -- bundle check || chruby-exec #{ruby} -- bundle install" end || exit(1) @@ -36,39 +35,42 @@ def use_gemfile(ruby, gemfile, update_gemfiles) puts '$' * 80 end -bad_variants = (travis.dig('matrix', 'exclude').to_a + travis.dig('matrix', 'allow_failures').to_a) - -travis['env']['global'].each do |env| - env.scan(/\b(?[A-Z_]+)="(?.+?)"/) do |key, value| - ENV[key] = value +def bad_variant?(bad_variants, ruby, gemfile = nil, adapter = nil) + bad_variants.find do |f| + !(ruby.nil? ^ f['ruby'].nil?) && f['ruby'] == ruby && + !(gemfile.nil? ^ f['gemfile'].nil?) && f['gemfile'] == gemfile && + !(adapter.nil? ^ f['adapter'].nil?) && f['adapter'] == adapter end end -travis['rvm'].each do |ruby| - next if /head/.match?(ruby) # ruby-install does not support HEAD installation +actions['env'].each do |key, value| + ENV[key] = value +end + +matrix = actions['jobs']['Test']['strategy']['matrix'] +bad_variants = matrix['exclude'] + +matrix['ruby'].each do |ruby| + next if bad_variant?(bad_variants, ruby) puts '#' * 80 puts "Testing #{ruby}" puts - system "ruby-install --no-reinstall #{ruby}" || exit(1) - bundler_version = '1.17.2' - gem_cmd = "chruby-exec #{ruby} -- gem" - system "#{gem_cmd} uninstall --force --all --version '!=#{bundler_version}' bundler" - bundler_gem_check_cmd = "#{gem_cmd} query -i -n '^bundler$' -v '#{bundler_version}' >/dev/null" - bundler_install_cmd = "#{gem_cmd} install bundler -v '#{bundler_version}'" - system "#{bundler_gem_check_cmd} || #{bundler_install_cmd}" || exit(1) - travis['gemfile'].each do |gemfile| - use_gemfile(ruby, gemfile, update_gemfiles) do - travis['env']['matrix'].each do |env| - bad_variant = bad_variants.any? do |f| - (f['rvm'].nil? || f['rvm'] == ruby) && - (f['gemfile'].nil? || f['gemfile'] == gemfile) && (f['env'].nil? || f['env'] == env) - end - if bad_variant - puts 'Skipping known failure.' - next + Bundler.with_unbundled_env do + system("ruby-install --no-reinstall #{ruby}") || exit(1) + matrix['gemfile'].each do |gemfile| + next if bad_variant?(bad_variants, ruby, gemfile) + + use_gemfile(ruby, gemfile, update_gemfiles) do + matrix['adapter'].each do |adapter| + next if bad_variant?(bad_variants, ruby, gemfile, adapter) + + puts '-' * 80 + puts "Testing #{adapter}" + puts + + run_script(ruby, { ADAPTER: adapter }, gemfile) end - run_script(ruby, env, gemfile) end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 3b65327..bce5f09 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -19,7 +19,7 @@ end FileUtils.rm_rf File.expand_path(':memory:', File.dirname(__FILE__)) -config = YAML.load(File.read(File.dirname(__FILE__) + '/database.yml')) +config = YAML.load_file(File.dirname(__FILE__) + '/database.yml') ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + '/test.log') adapter = ENV['ADAPTER'] || 'sqlite3' if adapter != 'sqlite3' diff --git a/test/time_of_day_test.rb b/test/time_of_day_test.rb index 1be3a58..728d80f 100644 --- a/test/time_of_day_test.rb +++ b/test/time_of_day_test.rb @@ -99,8 +99,9 @@ def test_column_type end def test_activerecord - Event.create! name: 'Bored meeting', start_at: @twelve_o_clock + t0 = Event.create! name: 'Bored meeting', start_at: @twelve_o_clock t = Event.where(start_at: '12:00:00').first + assert_equal t0, t assert_equal TimeOfDay, t.start_at.class assert_equal t, Event.where(start_at: @twelve_o_clock).first assert_equal t, Event.where('start_at = ?', @twelve_o_clock).first