-
Notifications
You must be signed in to change notification settings - Fork 49
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
Use ActiveRecord methods to generate INSERT SQLs #26
Merged
nettofarah
merged 2 commits into
IFTTT:master
from
dmnelson:use-ar-methods-to-generate-sql
Dec 7, 2015
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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 |
---|---|---|
|
@@ -8,13 +8,13 @@ | |
|
||
it 'generates an insert query for the base object' do | ||
exp = Polo.explore(AR::Chef, 1) | ||
insert = "INSERT INTO `chefs` (`id`, `name`, `email`) VALUES (1, 'Netto', '[email protected]')" | ||
insert = %q{INSERT INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', '[email protected]')} | ||
expect(exp).to include(insert) | ||
end | ||
|
||
it 'generates an insert query for the objects with non-standard primary keys' do | ||
exp = Polo.explore(AR::Person, 1) | ||
insert = "INSERT INTO `people` (`ssn`, `name`) VALUES (1, 'John Doe')" | ||
insert = %q{INSERT INTO "people" ("ssn", "name") VALUES (1, 'John Doe')} | ||
expect(exp).to include(insert) | ||
end | ||
|
||
|
@@ -25,8 +25,8 @@ | |
serialized_nil = "'null'" | ||
end | ||
|
||
turkey_insert = "INSERT INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`, `metadata`) VALUES (1, 'Turkey Sandwich', NULL, 1, #{serialized_nil})" | ||
cheese_burger_insert = "INSERT INTO `recipes` (`id`, `title`, `num_steps`, `chef_id`, `metadata`) VALUES (2, 'Cheese Burger', NULL, 1, #{serialized_nil})" | ||
turkey_insert = %Q{INSERT INTO "recipes" ("id", "title", "num_steps", "chef_id", "metadata") VALUES (1, 'Turkey Sandwich', NULL, 1, #{serialized_nil})} | ||
cheese_burger_insert = %Q{INSERT INTO "recipes" ("id", "title", "num_steps", "chef_id", "metadata") VALUES (2, 'Cheese Burger', NULL, 1, #{serialized_nil})} | ||
|
||
inserts = Polo.explore(AR::Chef, 1, [:recipes]) | ||
|
||
|
@@ -35,10 +35,10 @@ | |
end | ||
|
||
it 'generates queries for nested dependencies' do | ||
patty = "INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (3, 'Patty', '1')" | ||
turkey = "INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (1, 'Turkey', 'a lot')" | ||
one_cheese = "INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (2, 'Cheese', '1 slice')" | ||
two_cheeses = "INSERT INTO `ingredients` (`id`, `name`, `quantity`) VALUES (4, 'Cheese', '2 slices')" | ||
patty = %q{INSERT INTO "ingredients" ("id", "name", "quantity") VALUES (3, 'Patty', '1')} | ||
turkey = %q{INSERT INTO "ingredients" ("id", "name", "quantity") VALUES (1, 'Turkey', 'a lot')} | ||
one_cheese = %q{INSERT INTO "ingredients" ("id", "name", "quantity") VALUES (2, 'Cheese', '1 slice')} | ||
two_cheeses = %q{INSERT INTO "ingredients" ("id", "name", "quantity") VALUES (4, 'Cheese', '2 slices')} | ||
|
||
inserts = Polo.explore(AR::Chef, 1, :recipes => :ingredients) | ||
|
||
|
@@ -50,10 +50,10 @@ | |
|
||
it 'generates inserts for many to many relationships' do | ||
many_to_many_inserts = [ | ||
"INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (1, 1, 1)", | ||
"INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (2, 1, 2)", | ||
"INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (3, 2, 3)", | ||
"INSERT INTO `recipes_ingredients` (`id`, `recipe_id`, `ingredient_id`) VALUES (4, 2, 4)", | ||
%q{INSERT INTO "recipes_ingredients" ("id", "recipe_id", "ingredient_id") VALUES (1, 1, 1)}, | ||
%q{INSERT INTO "recipes_ingredients" ("id", "recipe_id", "ingredient_id") VALUES (2, 1, 2)}, | ||
%q{INSERT INTO "recipes_ingredients" ("id", "recipe_id", "ingredient_id") VALUES (3, 2, 3)}, | ||
%q{INSERT INTO "recipes_ingredients" ("id", "recipe_id", "ingredient_id") VALUES (4, 2, 4)}, | ||
] | ||
|
||
inserts = Polo.explore(AR::Chef, 1, :recipes => :ingredients) | ||
|
@@ -72,7 +72,7 @@ | |
end | ||
|
||
exp = Polo.explore(AR::Chef, 1) | ||
insert = /INSERT INTO `chefs` \(`id`, `name`, `email`\) VALUES \(1, 'Netto', (.+)\)/ | ||
insert = /INSERT INTO "chefs" \("id", "name", "email"\) VALUES \(1, 'Netto', (.+)\)/ | ||
scrambled_email = insert.match(exp.first)[1] | ||
|
||
expect(scrambled_email).to_not eq('[email protected]') | ||
|
@@ -86,7 +86,7 @@ | |
|
||
inserts = Polo.explore(AR::Chef, 1) | ||
|
||
expect(inserts).to eq [ %q{INSERT INTO `chefs` (`id`, `name`, `email`) VALUES (1, 'Netto', 'changeme')} ] | ||
expect(inserts).to eq [ %q{INSERT INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', 'changeme')} ] | ||
end | ||
end | ||
|
||
|
@@ -97,7 +97,7 @@ | |
end | ||
|
||
exp = Polo.explore(AR::Chef, 1) | ||
insert = /INSERT IGNORE INTO `chefs` \(`id`, `name`, `email`\) VALUES \(1, 'Netto', (.+)\)/ | ||
insert = /INSERT IGNORE INTO "chefs" \("id", "name", "email"\) VALUES \(1, 'Netto', (.+)\)/ | ||
expect(insert).to match(exp.first) | ||
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 |
---|---|---|
|
@@ -11,7 +11,7 @@ | |
end | ||
|
||
it 'translates records to inserts' do | ||
insert_netto = [%q{INSERT INTO `chefs` (`id`, `name`, `email`) VALUES (1, 'Netto', '[email protected]')}] | ||
insert_netto = [%q{INSERT INTO "chefs" ("id", "name", "email") VALUES (1, 'Netto', '[email protected]')}] | ||
netto_to_sql = Polo::SqlTranslator.new(netto).to_sql | ||
expect(netto_to_sql).to eq(insert_netto) | ||
end | ||
|
@@ -25,7 +25,7 @@ | |
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]')}] | ||
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 | ||
|
@@ -34,7 +34,7 @@ | |
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)} | ||
%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 | ||
|
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 wish this block could be avoided and have that done automatically, but I couldn't figure out how.
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 think it looks alright. It's ok since this code is extracted into its own method.
It might be worth writing some docs for this
method
/module
now that things are getting a little more complicated.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.
@nettofarah Done! Let me know if it looks alright.
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.
This is fantastic.