File: each_with_ar_sqlite.rb

package info (click to toggle)
ruby-parallel 1.22.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 464 kB
  • sloc: ruby: 1,930; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,133 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require './spec/cases/helper'
require "active_record"
require "sqlite3"
require "tempfile"
$stdout.sync = true
in_worker_type = "in_#{ENV.fetch('WORKER_TYPE')}".to_sym

Tempfile.create("db") do |temp|
  ActiveRecord::Schema.verbose = false
  ActiveRecord::Base.establish_connection(
    adapter: "sqlite3",
    database: temp.path
  )

  class User < ActiveRecord::Base # rubocop:disable Lint/ConstantDefinitionInBlock
  end

  class Callback # rubocop:disable Lint/ConstantDefinitionInBlock
    def self.call(_)
      $stdout.sync = true
      puts "Parallel: #{User.all.map(&:name).join}"
    end
  end

  # create tables
  unless User.table_exists?
    ActiveRecord::Schema.define(version: 1) do
      create_table :users do |t|
        t.string :name
      end
    end
  end

  User.delete_all

  3.times { User.create!(name: "X") }

  puts "Parent: #{User.first.name}"

  if in_worker_type == :in_ractors
    Parallel.each([1], in_worker_type => 1, ractor: [Callback, :call])
  else
    Parallel.each([1], in_worker_type => 1) { |x| Callback.call x }
  end

  puts "Parent: #{User.first.name}"
end