Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Set up Ruby
- name: Set up Ruby and Bundler
uses: ruby/[email protected]
with:
ruby-version: 3.3.4
bundler-cache: true

- name: Install dependencies
run: bundle install

- name: Install pre-commit
run: |
sudo apt-get update
sudo apt-get install --yes --no-install-recommends python3 python3-pip
pip install pre-commit
pre-commit install --install-hooks

- name: Run linters
run: pre-commit run --all-files
- name: Setup pre-commit and run linters
uses: pre-commit/[email protected]

- name: Run tests
run: bin/rspec
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
batch_update (0.0.1)
batch_update (0.0.2)
activerecord (~> 7.0)
activesupport (~> 7.0)

Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,12 @@ Specify a different batch size (100 by default):
Cat.batch_update(cats, columns: :all, batch_size: 1000)
```

Generate the relevant SQL statements:
```ruby
Cat.batch_update_statements([{id: 1, name: 'Lilly'}, {id: 2, name: 'John'}]).each do |query|
Cat.connection.execute(query)
end
```

## License
MIT
2 changes: 1 addition & 1 deletion batch_update.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Gem::Specification.new do |s|
s.name = 'batch_update'
s.version = '0.0.1'
s.version = '0.0.2'
s.summary = 'Update multiple records with different values in a small number of queries'
s.description = 'A simple hello world gem'
s.authors = ['Quentin de Metz']
Expand Down
4 changes: 2 additions & 2 deletions lib/batch_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ def batch_update(entries, columns:, batch_size: 100, validate: true)
updated_count
end

private

def batch_update_statements(entries, update_on: :id, batch_size: 100)
update_on = Array.wrap(update_on).map(&:to_s)

Expand All @@ -62,6 +60,8 @@ def batch_update_statements(entries, update_on: :id, batch_size: 100)
end
end

private

def cte_table
@cte_table ||= Arel::Table.new('batch_updates')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/batch_update_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe BatchUpdate do
describe '#batch_update_statements' do
subject(:sql_queries) { Cat.__send__(:batch_update_statements, cats, **kwargs) }
subject(:sql_queries) { Cat.batch_update_statements(cats, **kwargs) }

let(:cats) { [] }
let(:kwargs) { {} }
Expand Down