-
Notifications
You must be signed in to change notification settings - Fork 48
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
Adding adapters for MySQL and Postgres #37
Merged
Merged
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
This file contains 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,24 @@ | ||
module Polo | ||
module Adapters | ||
class MySQL | ||
def on_duplicate_key_update(inserts, records) | ||
insert_and_record = inserts.zip(records) | ||
insert_and_record.map do |insert, record| | ||
values_syntax = record.attributes.keys.map do |key| | ||
"#{key} = VALUES(#{key})" | ||
end | ||
|
||
on_dup_syntax = "ON DUPLICATE KEY UPDATE #{values_syntax.join(', ')}" | ||
|
||
"#{insert} #{on_dup_syntax}" | ||
end | ||
end | ||
|
||
def ignore_transform(inserts, records) | ||
inserts.map do |insert| | ||
insert.gsub("INSERT", "INSERT IGNORE") | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,32 @@ | ||
module Polo | ||
module Adapters | ||
class Postgres | ||
# TODO: Implement UPSERT. This command became available in 9.1. | ||
# | ||
# See: http://www.the-art-of-web.com/sql/upsert/ | ||
def on_duplicate_key_update(inserts, records) | ||
raise 'on_duplicate: :override is not currently supported in the PostgreSQL adapter' | ||
end | ||
|
||
# Internal: Transforms an INSERT with PostgreSQL-specific syntax. Ignores | ||
# records that alread exist in the table. To do this, it uses | ||
# a heuristic, i.e. checks if there is a record with the same id | ||
# in the table. | ||
# See: http://stackoverflow.com/a/6527838/32816 | ||
# | ||
# inserts - The Array of INSERT statements. | ||
# records - The Array of Arel objects. | ||
# | ||
# Returns the Array of transformed INSERT statements. | ||
def ignore_transform(inserts, records) | ||
insert_and_record = inserts.zip(records) | ||
insert_and_record.map do |insert, record| | ||
table_name = record.class.arel_table.name | ||
id = record[:id] | ||
insert = insert.gsub(/VALUES \((.+)\)$/m, 'SELECT \\1') | ||
insert << " WHERE NOT EXISTS (SELECT 1 FROM #{table_name} WHERE id=#{id});" | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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 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,41 @@ | ||
require 'spec_helper' | ||
|
||
describe Polo::Adapters::MySQL do | ||
|
||
let(:adapter) { Polo::Adapters::MySQL.new } | ||
|
||
let(:netto) do | ||
AR::Chef.where(name: 'Netto').first | ||
end | ||
|
||
before(:all) do | ||
TestData.create_netto | ||
end | ||
|
||
let(:translator) { Polo::SqlTranslator.new(netto, Polo::Configuration.new(adapter: :mysql)) } | ||
|
||
describe '#ignore_transform' do | ||
it 'appends the IGNORE command after INSERTs' do | ||
insert_netto = [%q{INSERT IGNORE INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', '[email protected]')}] | ||
|
||
records = translator.records | ||
inserts = translator.inserts | ||
translated_sql = adapter.ignore_transform(inserts, records) | ||
expect(translated_sql).to eq(insert_netto) | ||
end | ||
end | ||
|
||
|
||
describe '#on_duplicate_key_update' do | ||
it 'appends ON DUPLICATE KEY UPDATE with all values to the current INSERT statement' do | ||
insert_netto = [ | ||
%q{INSERT INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', '[email protected]') ON DUPLICATE KEY UPDATE id = VALUES(id), name = VALUES(name), email = VALUES(email)} | ||
] | ||
|
||
inserts = translator.inserts | ||
records = translator.records | ||
translated_sql = adapter.on_duplicate_key_update(inserts, records) | ||
expect(translated_sql).to eq(insert_netto) | ||
end | ||
end | ||
end |
This file contains 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,34 @@ | ||
require 'spec_helper' | ||
|
||
describe Polo::Adapters::Postgres do | ||
|
||
let(:adapter) { Polo::Adapters::Postgres.new } | ||
|
||
let(:netto) do | ||
AR::Chef.where(name: 'Netto').first | ||
end | ||
|
||
before(:all) do | ||
TestData.create_netto | ||
end | ||
|
||
let(:translator) { Polo::SqlTranslator.new(netto, Polo::Configuration.new(adapter: :postgres)) } | ||
|
||
describe '#on_duplicate_key_update' do | ||
it 'should raise an error' do | ||
expect { adapter.on_duplicate_key_update(double(), double()) }.to raise_error('on_duplicate: :override is not currently supported in the PostgreSQL adapter') | ||
end | ||
end | ||
|
||
describe '#ignore_transform' do | ||
it 'transforms INSERT by appending WHERE NOT EXISTS clause' do | ||
|
||
insert_netto = [%q{INSERT INTO "chefs" ("id", "name", "email") SELECT 1, 'Netto', '[email protected]' WHERE NOT EXISTS (SELECT 1 FROM chefs WHERE id=1);}] | ||
|
||
records = translator.records | ||
inserts = translator.inserts | ||
translated_sql = adapter.ignore_transform(inserts, records) | ||
expect(translated_sql).to eq(insert_netto) | ||
end | ||
end | ||
end |
This file contains 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 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 |
---|---|---|
|
@@ -21,25 +21,4 @@ | |
recipe_to_sql = Polo::SqlTranslator.new(recipe).to_sql.first | ||
expect(recipe_to_sql).to include(%q{'{"quality":"ok"}'}) # JSON, not YAML | ||
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. it would probably make sense to write some simple tests here just to guarantee that we're sending calls our strategy object. Nothing too fancy, but just to keep things consistent. |
||
end | ||
|
||
describe "options" do | ||
describe "on_duplicate: :ignore" do | ||
it 'uses INSERT IGNORE as opposed to regular inserts' do | ||
insert_netto = [%q{INSERT IGNORE INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', '[email protected]')}] | ||
netto_to_sql = Polo::SqlTranslator.new(netto, Polo::Configuration.new(on_duplicate: :ignore)).to_sql | ||
expect(netto_to_sql).to eq(insert_netto) | ||
end | ||
end | ||
|
||
describe "on_duplicate: :override" do | ||
it 'appends ON DUPLICATE KEY UPDATE to the statement' do | ||
insert_netto = [ | ||
%q{INSERT INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', '[email protected]') ON DUPLICATE KEY UPDATE id = VALUES(id), name = VALUES(name), email = VALUES(email)} | ||
] | ||
|
||
netto_to_sql = Polo::SqlTranslator.new(netto, Polo::Configuration.new(on_duplicate: :override)).to_sql | ||
expect(netto_to_sql).to eq(insert_netto) | ||
end | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this implementation, but find it a little hard to follow because of all the gsubing and the regexes.
Can you write some high level comments for this? (in the same format as our other comments) and also include some input and ouput samples?