lib/parallel_tests/tasks.rb in parallel_tests-3.7.3 vs lib/parallel_tests/tasks.rb in parallel_tests-3.8.0

- old
+ new

@@ -90,10 +90,36 @@ options = args.shift pass_through = args.shift [num_processes, pattern.to_s, options.to_s, pass_through.to_s] end + + def schema_format_based_on_rails_version + if rails_7_or_greater? + ActiveRecord.schema_format + else + ActiveRecord::Base.schema_format + end + end + + def schema_type_based_on_rails_version + if rails_61_or_greater? || schema_format_based_on_rails_version == :ruby + "schema" + else + "structure" + end + end + + private + + def rails_7_or_greater? + Gem::Version.new(Rails.version) >= Gem::Version.new('7.0') + end + + def rails_61_or_greater? + Gem::Version.new(Rails.version) >= Gem::Version.new('6.1.0') + end end end end namespace :parallel do @@ -119,18 +145,14 @@ end desc "Update test databases by dumping and loading --> parallel:prepare[num_cpus]" task(:prepare, [:count]) do |_, args| ParallelTests::Tasks.check_for_pending_migrations - if defined?(ActiveRecord::Base) && [:ruby, :sql].include?(ActiveRecord::Base.schema_format) + + if defined?(ActiveRecord) && [:ruby, :sql].include?(ParallelTests::Tasks.schema_format_based_on_rails_version) # fast: dump once, load in parallel - type = - if Gem::Version.new(Rails.version) >= Gem::Version.new('6.1.0') - "schema" - else - ActiveRecord::Base.schema_format == :ruby ? "schema" : "structure" - end + type = ParallelTests::Tasks.schema_type_based_on_rails_version Rake::Task["db:#{type}:dump"].invoke # remove database connection to prevent "database is being accessed by other users" ActiveRecord::Base.remove_connection if ActiveRecord::Base.configurations.any? @@ -161,11 +183,12 @@ end # just load the schema (good for integration server <-> no development db) desc "Load dumped schema for test databases via db:schema:load --> parallel:load_schema[num_cpus]" task :load_schema, :count do |_, args| - command = "#{ParallelTests::Tasks.rake_bin} #{ParallelTests::Tasks.purge_before_load} " \ + command = + "#{ParallelTests::Tasks.rake_bin} #{ParallelTests::Tasks.purge_before_load} " \ "db:schema:load RAILS_ENV=#{ParallelTests::Tasks.rails_env} DISABLE_DATABASE_ENVIRONMENT_CHECK=1" ParallelTests::Tasks.run_in_parallel(ParallelTests::Tasks.suppress_schema_load_output(command), args) end # load the structure from the structure.sql file @@ -209,10 +232,11 @@ type = 'features' if test_framework == 'spinach' # Using the relative path to find the binary allow to run a specific version of it executable = File.join(File.dirname(__FILE__), '..', '..', 'bin', 'parallel_test') - command = "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} #{type} " \ + command = + "#{ParallelTests.with_ruby_binary(Shellwords.escape(executable))} #{type} " \ "--type #{test_framework} " \ "-n #{count} " \ "--pattern '#{pattern}' " \ "--test-options '#{options}' " \ "#{pass_through}"