-
Notifications
You must be signed in to change notification settings - Fork 67
Add bulk upsert to lucky #789
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
robcole
wants to merge
1
commit into
luckyframework:main
Choose a base branch
from
robcole:add-bulk-upsert-to-lucky
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
db/migrations/20220113043033_add_unique_constraint_to_users.cr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| class AddUniqueConstraintToUsers::V20220113043033 < Avram::Migrator::Migration::V1 | ||
| def migrate | ||
| create_index :users, [:name, :nickname], unique: true | ||
| end | ||
|
|
||
| def rollback | ||
| drop_index :users, [:name, :nickname] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| class Avram::BulkUpsert(T) | ||
| @column_types : Hash(String, String) | ||
| @permitted_fields : Array(Symbol) | ||
|
|
||
| def initialize(@records : Array(T), | ||
robcole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| @conflicts : Array(Symbol), | ||
| permitted_fields : Array(Symbol)) | ||
| set_timestamps | ||
| @sample_record = @records.first.as(T) | ||
| @permitted_fields = permitted_fields_for(permitted_fields) | ||
|
|
||
| @column_types = T.database_table_info.columns.map do |col_info| | ||
| { | ||
| col_info.column_name, | ||
| col_info.data_type, | ||
| } | ||
| end.to_h | ||
| end | ||
|
|
||
| def statement | ||
| <<-SQL | ||
| INSERT INTO #{table}(#{fields}) | ||
| (SELECT * FROM unnest(#{value_placeholders})) | ||
| ON CONFLICT (#{conflicts}) DO UPDATE SET #{updates} | ||
| RETURNING #{returning} | ||
| SQL | ||
| end | ||
|
|
||
| def args | ||
| @records.map do |record| | ||
| permitted_attributes(record).map(&.value) | ||
| end.transpose | ||
robcole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
|
|
||
| private def permitted_fields_for(fields : Array(Symbol)) | ||
| fields.push(:created_at) if @sample_record.responds_to?(:created_at) | ||
| fields.push(:updated_at) if @sample_record.responds_to?(:updated_at) | ||
| fields.uniq! | ||
| end | ||
|
|
||
| private def permitted_attributes(record) | ||
| record | ||
| .attributes | ||
| .select { |attr| @permitted_fields.includes?(attr.name) } | ||
| end | ||
|
|
||
| private def permitted_attributes | ||
| permitted_attributes(@sample_record) | ||
| end | ||
|
|
||
| private def conflicts | ||
| @conflicts.join(", ") | ||
| end | ||
|
|
||
| private def set_timestamps | ||
| @records.each do |record| | ||
| record.created_at.value ||= Time.utc if record.responds_to?(:created_at) | ||
| record.updated_at.value ||= Time.utc if record.responds_to?(:updated_at) | ||
| end | ||
| end | ||
|
|
||
| private def table | ||
| @sample_record.table_name | ||
| end | ||
|
|
||
| private def updates | ||
| (permitted_attribute_column_names - [:created_at]).compact_map do |column| | ||
| "#{column}=EXCLUDED.#{column}" | ||
| end.join(", ") | ||
| end | ||
|
|
||
| private def returning | ||
| T.column_names.join(", ") | ||
| end | ||
|
|
||
| private def permitted_attribute_column_names | ||
| permitted_attributes.map(&.name) | ||
| end | ||
|
|
||
| private def fields | ||
| permitted_attribute_column_names.map(&.to_s).join(", ") | ||
| end | ||
|
|
||
| private def value_placeholders | ||
| permitted_attributes.map_with_index(1) do |column, index| | ||
| "$#{index}::#{@column_types[column.name.to_s]}[]" | ||
| end.join(", ") | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -90,6 +90,29 @@ module Avram::Upsert | |
| end | ||
| end | ||
|
|
||
| macro upsert_unique_on(*attribute_names) | ||
robcole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def self.upsert(upserts : Array(X)) forall X | ||
| \{% | ||
| if X > NamedTuple | ||
| raise("All array elements for #{@type}.upsert must be NamedTuples. You provided: #{X}") | ||
| elsif X.union? | ||
| keys = X.union_types.map(&.keys).join(", ") | ||
| raise("All tuples for #{@type}.upsert must have the same keys. Given: " + keys) | ||
| end | ||
| %} | ||
|
|
||
| upsert = Avram::BulkUpsert(self).new( | ||
| records: upserts.map { |upsert_args| new(**upsert_args) }, | ||
| conflicts: {{ attribute_names }}.to_a, | ||
| permitted_fields: upserts.first.keys.to_a | ||
| ) | ||
|
|
||
| new.database.query upsert.statement, args: upsert.args do |rs| | ||
| T.from_rs(rs) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # :nodoc: | ||
| macro included | ||
| {% for method in ["upsert", "upsert!"] %} | ||
|
|
@@ -100,5 +123,9 @@ module Avram::Upsert | |
| \{% raise "Please use the 'upsert_lookup_columns' macro in #{@type} before using '{{ method.id }}'" %} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| end | ||
| {% end %} | ||
|
|
||
| def self.upsert(_upserts : Array) | ||
| \{% raise "Please use the 'upsert_unique_on' macro in #{@type} before using '.upsert'" %} | ||
robcole marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Uh oh!
There was an error while loading. Please reload this page.