Skip to content

Instantly share code, notes, and snippets.

@jhawthorn
Forked from radar/gist:00e321fb4be0c20666aa
Last active August 29, 2015 14:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhawthorn/37723788edfe2cea07fc to your computer and use it in GitHub Desktop.
Save jhawthorn/37723788edfe2cea07fc to your computer and use it in GitHub Desktop.

We need an improvement to our architecture to allow us to test portions of spree more easily. However I believe we will always need working complete factories form, at least for integration tests.

Regardless of what we're to do in the future. I thought it may be useful to document why all these queries are currently run, and what options we have to reduce their number.

[1] pry(#<RSpec::Core::ExampleGroup::Nested_1::Nested_2>)> create(:line_item)

Out default order factory comes with a user, but we could probably use an anonymous order instead.

   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.5ms)  INSERT INTO "spree_users" ("authentication_token", "created_at", "email", "login", "updated_at") VALUES (?, ?, ?, ?, ?)  [["authentication_token", "xxxx14028747076021xxxxxxxxxxxxx"], ["created_at", "2014-06-15 23:25:07.770910"], ["email", "marcus@beattyschaden.info"], ["login", "marcus@beattyschaden.info"], ["updated_at", "2014-06-15 23:25:07.770910"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1

Spree currently uses tables for countries and states. There was a pull request to use carmen instead of these custom tables

  Spree::Country Load (0.2ms)  SELECT  "spree_countries".* FROM "spree_countries"  WHERE "spree_countries"."numcode" = 840 LIMIT 1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (1.0ms)  INSERT INTO "spree_countries" ("iso", "iso3", "iso_name", "name", "numcode", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["iso", "US"], ["iso3", "USA"], ["iso_name", "UNITED STATES"], ["name", "United States of America"], ["numcode", 840], ["updated_at", "2014-06-15 23:25:07.843809"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.2ms)  INSERT INTO "spree_states" ("abbr", "country_id", "name", "updated_at") VALUES (?, ?, ?, ?)  [["abbr", "AL"], ["country_id", 1], ["name", "Alabama"], ["updated_at", "2014-06-15 23:25:07.850510"]]
   (0.0ms)  RELEASE SAVEPOINT active_record_1

Billing address. I don't think this is actually required by an order in the cart state, but some tests probably expect it to be there.

   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.3ms)  INSERT INTO "spree_addresses" ("address1", "address2", "alternative_phone", "city", "company", "country_id", "created_at", "firstname", "lastname", "phone", "state_id", "updated_at", "zipcode") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["address1", "10 Lovely Street"], ["address2", "Northwest"], ["alternative_phone", "123-456-7899"], ["city", "Herndon"], ["company", "Company"], ["country_id", 1], ["created_at", "2014-06-15 23:25:07.859706"], ["firstname", "John"], ["lastname", "Doe"], ["phone", "123-456-7890"], ["state_id", 1], ["updated_at", "2014-06-15 23:25:07.859706"], ["zipcode", "35005"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1

The Spree::Order Load here comes from generate_order_number, checking that the order number is unused. We should change the index on spree_orders.number to have a uniqueness constraint. After doing so, we should consider removing this check (although IMO we should add the constraint regardless).

   (0.1ms)  SAVEPOINT active_record_1
  Spree::Order Load (0.3ms)  SELECT  "spree_orders".* FROM "spree_orders"  WHERE "spree_orders"."number" = 'R460154224'  ORDER BY "spree_orders"."id" ASC LIMIT 1
   (0.2ms)  SELECT COUNT(*) FROM "spree_orders"  WHERE (spree_orders.number LIKE 'R460154224%')

Create the order

  SQL (0.4ms)  INSERT INTO "spree_orders" ("bill_address_id", "created_at", "currency", "email", "number", "state", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["bill_address_id", 1], ["created_at", "2014-06-15 23:25:07.913363"], ["currency", "USD"], ["email", "marcus@beattyschaden.info"], ["number", "R460154224"], ["state", "cart"], ["updated_at", "2014-06-15 23:25:07.913363"], ["user_id", 1]]

iirc @huoxito had a commit moving spree_tokenized_permissions onto the orders table (as spree_orders.token).

  Spree::TokenizedPermission Load (0.2ms)  SELECT  "spree_tokenized_permissions".* FROM "spree_tokenized_permissions"  WHERE "spree_tokenized_permissions"."permissable_id" = ? AND "spree_tokenized_permissions"."permissable_type" = ? LIMIT 1  [["permissable_id", 1], ["permissable_type", "Spree::Order"]]
  SQL (0.2ms)  INSERT INTO "spree_tokenized_permissions" ("created_at", "permissable_id", "permissable_type", "token", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", "2014-06-15 23:25:07.923980"], ["permissable_id", 1], ["permissable_type", "Spree::Order"], ["token", "33f0ab8a1fe27ec4"], ["updated_at", "2014-06-15 23:25:07.923980"]]

The factory checks for an existing shippingcategory (required on a product), and creates one if none exists.

   (0.1ms)  RELEASE SAVEPOINT active_record_1
  Spree::ShippingCategory Load (0.1ms)  SELECT  "spree_shipping_categories".* FROM "spree_shipping_categories"   ORDER BY "spree_shipping_categories"."id" ASC LIMIT 1
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.1ms)  INSERT INTO "spree_shipping_categories" ("created_at", "name", "updated_at") VALUES (?, ?, ?)  [["created_at", "2014-06-15 23:25:07.977084"], ["name", "ShippingCategory #1"], ["updated_at", "2014-06-15 23:25:07.977084"]]
   (0.0ms)  RELEASE SAVEPOINT active_record_1

Same for tax categories

  Spree::TaxCategory Load (0.1ms)  SELECT  "spree_tax_categories".* FROM "spree_tax_categories"  WHERE "spree_tax_categories"."deleted_at" IS NULL  ORDER BY "spree_tax_categories"."id" ASC LIMIT 1
   (0.1ms)  SAVEPOINT active_record_1
  Spree::TaxCategory Exists (0.1ms)  SELECT  1 AS one FROM "spree_tax_categories"  WHERE ("spree_tax_categories"."name" = 'TaxCategory - 810539' AND "spree_tax_categories"."deleted_at" IS NULL) LIMIT 1
  SQL (0.3ms)  INSERT INTO "spree_tax_categories" ("created_at", "description", "name", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2014-06-15 23:25:07.989318"], ["description", "Accusamus doloremque et rem voluptates impedit."], ["name", "TaxCategory - 810539"], ["updated_at", "2014-06-15 23:25:07.989318"]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SELECT COUNT(*) FROM "spree_stock_locations"
  Spree::Country Load (0.1ms)  SELECT  "spree_countries".* FROM "spree_countries"   ORDER BY "spree_countries"."id" ASC LIMIT 1
  Spree::State Load (0.2ms)  SELECT  "spree_states".* FROM "spree_states"  WHERE "spree_states"."country_id" = ?  ORDER BY name ASC LIMIT 1  [["country_id", 1]]

Creates the (required) stock location for a product

   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.4ms)  INSERT INTO "spree_stock_locations" ("address1", "backorderable_default", "city", "country_id", "created_at", "name", "phone", "state_id", "updated_at", "zipcode") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  [["address1", "1600 Pennsylvania Ave NW"], ["backorderable_default", "t"], ["city", "Washington"], ["country_id", 1], ["created_at", "2014-06-15 23:25:08.022983"], ["name", "NY Warehouse"], ["phone", "(202) 456-1111"], ["state_id", 1], ["updated_at", "2014-06-15 23:25:08.022983"], ["zipcode", "20500"]]
  Spree::Variant Load (0.2ms)  SELECT  "spree_variants".* FROM "spree_variants"  WHERE "spree_variants"."deleted_at" IS NULL  ORDER BY "spree_variants"."id" ASC LIMIT 1000
   (0.1ms)  RELEASE SAVEPOINT active_record_1

Checks for uniqueness on a product slug. And then creates a product.

   (0.0ms)  SAVEPOINT active_record_1
  Spree::Product Exists (0.2ms)  SELECT  1 AS one FROM "spree_products"  WHERE ("spree_products"."id" IS NOT NULL) AND "spree_products"."slug" = 'product-1-3674' LIMIT 1
  SQL (0.4ms)  INSERT INTO "spree_products" ("available_on", "created_at", "description", "name", "shipping_category_id", "slug", "tax_category_id", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["available_on", "2013-06-15 23:25:07.972301"], ["created_at", "2014-06-15 23:25:08.114657"], ["description", "Voluptas molestias harum sequi ut nostrum deserunt occaecati error. Fuga repellat voluptatem repudiandae dignissimos quam. Non et id aspernatur alias architecto sit consequatur. Iure sint pariatur ea totam eum voluptatum omnis consectetur.\nAut quia dolorem ea sit numquam labore ea. Ipsa ut eaque quod laboriosam. Doloremque qui vel aut cum consequuntur quas. Quasi iste facilis assumenda ut."], ["name", "Product #1 - 3674"], ["shipping_category_id", 1], ["slug", "product-1-3674"], ["tax_category_id", 1], ["updated_at", "2014-06-15 23:25:08.114657"]]

The master variant of a product

  SQL (0.4ms)  INSERT INTO "spree_variants" ("cost_currency", "cost_price", "product_id", "sku", "updated_at") VALUES (?, ?, ?, ?, ?)  [["cost_currency", "USD"], ["cost_price", 17.0], ["product_id", 1], ["sku", "ABC"], ["updated_at", "2014-06-15 23:25:08.118595"]]

Creates the price for the master variant

  Spree::Variant Load (0.2ms)  SELECT  "spree_variants".* FROM "spree_variants"  WHERE "spree_variants"."id" = ? LIMIT 1  [["id", 1]]
  SQL (0.2ms)  INSERT INTO "spree_prices" ("amount", "currency", "variant_id") VALUES (?, ?, ?)  [["amount", 19.99], ["currency", "USD"], ["variant_id", 1]]
  SQL (0.1ms)  UPDATE "spree_variants" SET "updated_at" = '2014-06-15 23:25:08.129096' WHERE "spree_variants"."id" = 1
  Spree::Product Load (0.1ms)  SELECT  "spree_products".* FROM "spree_products"  WHERE "spree_products"."id" = ? LIMIT 1  [["id", 1]]
  SQL (0.1ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.131220' WHERE "spree_products"."id" = 1
  Spree::Taxon Load (0.2ms)  SELECT "spree_taxons".* FROM "spree_taxons" INNER JOIN "spree_products_taxons" ON "spree_taxons"."id" = "spree_products_taxons"."taxon_id" WHERE "spree_products_taxons"."product_id" = ?  [["product_id", 1]]
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.146864' WHERE 1=0
  SQL (0.2ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.148613' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
  Spree::StockLocation Load (0.1ms)  SELECT "spree_stock_locations".* FROM "spree_stock_locations"
  Spree::StockLocation Load (0.2ms)  SELECT  "spree_stock_locations".* FROM "spree_stock_locations"  WHERE "spree_stock_locations"."id" = ? LIMIT 1  [["id", 1]]
  Spree::StockItem Exists (0.1ms)  SELECT  1 AS one FROM "spree_stock_items"  WHERE ("spree_stock_items"."variant_id" = 1 AND "spree_stock_items"."stock_location_id" = 1 AND "spree_stock_items"."deleted_at" IS NULL) LIMIT 1
  SQL (0.2ms)  INSERT INTO "spree_stock_items" ("backorderable", "created_at", "stock_location_id", "updated_at", "variant_id") VALUES (?, ?, ?, ?, ?)  [["backorderable", "t"], ["created_at", "2014-06-15 23:25:08.179942"], ["stock_location_id", 1], ["updated_at", "2014-06-15 23:25:08.179942"], ["variant_id", 1]]

This is all touch:true and after_touch callbacks. A good portion of them have WHERE 1=0, which means they could be skipped. Even better IMNSHO is to stop touching records unless they really are dependant.

  SQL (0.1ms)  UPDATE "spree_variants" SET "updated_at" = '2014-06-15 23:25:08.181558' WHERE "spree_variants"."id" = 1
  SQL (0.1ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.182419' WHERE "spree_products"."id" = 1
  Spree::Taxon Load (0.1ms)  SELECT "spree_taxons".* FROM "spree_taxons" INNER JOIN "spree_products_taxons" ON "spree_taxons"."id" = "spree_products_taxons"."taxon_id" WHERE "spree_products_taxons"."product_id" = ?  [["product_id", 1]]
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.184081' WHERE 1=0
  SQL (0.1ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.184789' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
   (0.2ms)  SELECT MAX("spree_variants"."position") AS max_id FROM "spree_variants"  WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = ? AND "spree_variants"."is_master" = 'f'  [["product_id", 1]]
  SQL (0.2ms)  UPDATE "spree_variants" SET "position" = 1 WHERE "spree_variants"."id" = 1
   (0.2ms)  SELECT SUM("spree_stock_items"."count_on_hand") AS sum_id FROM "spree_stock_items" INNER JOIN "spree_stock_locations" ON "spree_stock_locations"."id" = "spree_stock_items"."stock_location_id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_stock_items"."variant_id" = 1 AND "spree_stock_locations"."active" = 't'
  SQL (0.2ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.199672' WHERE "spree_products"."id" = 1
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.200667' WHERE 1=0
  SQL (0.2ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.201465' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
  SQL (0.3ms)  UPDATE "spree_variants" SET "is_master" = ?, "updated_at" = ? WHERE "spree_variants"."id" = 1  [["is_master", "t"], ["updated_at", "2014-06-15 23:25:08.203017"]]
  SQL (0.2ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.205210' WHERE "spree_products"."id" = 1
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.206148' WHERE 1=0
  SQL (0.1ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.206934' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
  SQL (0.1ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.207772' WHERE "spree_products"."id" = 1
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.208523' WHERE 1=0
  SQL (0.2ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.209632' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
   (0.1ms)  RELEASE SAVEPOINT active_record_1

The following is to create another variant of the same product. This creates an OptionType and OptionValue as well as creating another Variant, Price, and StockItem.

I believe the line_item factory should by default use the master variant created above instead. I'm working on this, but currently have a few tests failing.

   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.2ms)  INSERT INTO "spree_option_types" ("created_at", "name", "presentation", "updated_at") VALUES (?, ?, ?, ?)  [["created_at", "2014-06-15 23:25:08.226751"], ["name", "foo-size"], ["presentation", "Size"], ["updated_at", "2014-06-15 23:25:08.226751"]]
   (0.0ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  Spree::OptionValue Load (0.2ms)  SELECT  "spree_option_values".* FROM "spree_option_values"  WHERE (spree_option_values.position IS NOT NULL) AND ("spree_option_values"."option_type_id" = 1)  ORDER BY spree_option_values.position DESC LIMIT 1
  SQL (0.2ms)  INSERT INTO "spree_option_values" ("created_at", "name", "option_type_id", "position", "presentation", "updated_at") VALUES (?, ?, ?, ?, ?, ?)  [["created_at", "2014-06-15 23:25:08.249286"], ["name", "Size"], ["option_type_id", 1], ["position", 1], ["presentation", "S"], ["updated_at", "2014-06-15 23:25:08.249286"]]
  SQL (0.1ms)  UPDATE "spree_option_types" SET "updated_at" = '2014-06-15 23:25:08.251789' WHERE "spree_option_types"."id" = 1
  Spree::Product Load (0.2ms)  SELECT  "spree_products".* FROM "spree_products" INNER JOIN "spree_product_option_types" ON "spree_products"."id" = "spree_product_option_types"."product_id" WHERE "spree_products"."deleted_at" IS NULL AND "spree_product_option_types"."option_type_id" = ?  ORDER BY "spree_products"."id" ASC LIMIT 1000  [["option_type_id", 1]]
   (0.1ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SELECT COUNT(*) FROM "spree_stock_locations"
   (0.1ms)  SAVEPOINT active_record_1
  SQL (0.2ms)  INSERT INTO "spree_variants" ("cost_currency", "cost_price", "depth", "height", "product_id", "sku", "updated_at", "weight", "width") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["cost_currency", "USD"], ["cost_price", 17.0], ["depth", 184.28], ["height", 130.22], ["product_id", 1], ["sku", "9cd38a8464b9b69935ec8d8240453c97"], ["updated_at", "2014-06-15 23:25:08.275652"], ["weight", 63.38], ["width", 3.37]]
  SQL (0.2ms)  INSERT INTO "spree_option_values_variants" ("option_value_id", "variant_id") VALUES (?, ?)  [["option_value_id", 1], ["variant_id", 2]]
  Spree::Variant Load (0.1ms)  SELECT  "spree_variants".* FROM "spree_variants"  WHERE "spree_variants"."id" = ? LIMIT 1  [["id", 2]]
  SQL (0.1ms)  INSERT INTO "spree_prices" ("amount", "currency", "variant_id") VALUES (?, ?, ?)  [["amount", 19.99], ["currency", "USD"], ["variant_id", 2]]
  SQL (0.1ms)  UPDATE "spree_variants" SET "updated_at" = '2014-06-15 23:25:08.279982' WHERE "spree_variants"."id" = 2
  Spree::Product Load (0.0ms)  SELECT  "spree_products".* FROM "spree_products"  WHERE "spree_products"."id" = ? LIMIT 1  [["id", 1]]
  SQL (0.1ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.281493' WHERE "spree_products"."id" = 1
  Spree::Taxon Load (0.0ms)  SELECT "spree_taxons".* FROM "spree_taxons" INNER JOIN "spree_products_taxons" ON "spree_taxons"."id" = "spree_products_taxons"."taxon_id" WHERE "spree_products_taxons"."product_id" = ?  [["product_id", 1]]
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.283090' WHERE 1=0
  SQL (0.2ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.283813' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
  Spree::StockLocation Load (0.1ms)  SELECT "spree_stock_locations".* FROM "spree_stock_locations"
  Spree::StockLocation Load (0.1ms)  SELECT  "spree_stock_locations".* FROM "spree_stock_locations"  WHERE "spree_stock_locations"."id" = ? LIMIT 1  [["id", 1]]
  Spree::StockItem Exists (0.1ms)  SELECT  1 AS one FROM "spree_stock_items"  WHERE ("spree_stock_items"."variant_id" = 2 AND "spree_stock_items"."stock_location_id" = 1 AND "spree_stock_items"."deleted_at" IS NULL) LIMIT 1
  SQL (0.2ms)  INSERT INTO "spree_stock_items" ("backorderable", "created_at", "stock_location_id", "updated_at", "variant_id") VALUES (?, ?, ?, ?, ?)  [["backorderable", "t"], ["created_at", "2014-06-15 23:25:08.288257"], ["stock_location_id", 1], ["updated_at", "2014-06-15 23:25:08.288257"], ["variant_id", 2]]
  SQL (0.1ms)  UPDATE "spree_variants" SET "updated_at" = '2014-06-15 23:25:08.289576' WHERE "spree_variants"."id" = 2
  SQL (0.1ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.290429' WHERE "spree_products"."id" = 1
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.291129' WHERE 1=0
  SQL (0.1ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.291842' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
   (0.1ms)  SELECT MAX("spree_variants"."position") AS max_id FROM "spree_variants"  WHERE "spree_variants"."deleted_at" IS NULL AND "spree_variants"."product_id" = ? AND "spree_variants"."is_master" = 'f'  [["product_id", 1]]
  SQL (0.2ms)  UPDATE "spree_variants" SET "position" = 1 WHERE "spree_variants"."id" = 2
  SQL (0.2ms)  UPDATE "spree_products" SET "updated_at" = '2014-06-15 23:25:08.294737' WHERE "spree_products"."id" = 1
  SQL (0.1ms)  UPDATE "spree_taxons" SET "updated_at" = '2014-06-15 23:25:08.295704' WHERE 1=0
  SQL (0.2ms)  UPDATE "spree_taxonomies" SET "updated_at" = '2014-06-15 23:25:08.296856' WHERE "spree_taxonomies"."id" IN (SELECT "spree_taxonomies"."id" FROM "spree_taxonomies"  WHERE 1=0  ORDER BY spree_taxonomies.position)
   (0.0ms)  RELEASE SAVEPOINT active_record_1
   (0.1ms)  SAVEPOINT active_record_1
  Spree::TaxCategory Load (0.2ms)  SELECT  "spree_tax_categories".* FROM "spree_tax_categories"  WHERE "spree_tax_categories"."deleted_at" IS NULL AND "spree_tax_categories"."id" = ? LIMIT 1  [["id", 1]]

Ensuring we have a enough stock to add this variant to the order. Obviously not necessary in this factory, but this is currently a tight coupling in spree.

   (0.2ms)  SELECT SUM("spree_stock_items"."count_on_hand") AS sum_id FROM "spree_stock_items" INNER JOIN "spree_stock_locations" ON "spree_stock_locations"."id" = "spree_stock_items"."stock_location_id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_stock_items"."variant_id" = 2 AND "spree_stock_locations"."active" = 't'
  Spree::StockItem Load (0.2ms)  SELECT "spree_stock_items".* FROM "spree_stock_items" INNER JOIN "spree_stock_locations" ON "spree_stock_locations"."id" = "spree_stock_items"."stock_location_id" WHERE "spree_stock_items"."deleted_at" IS NULL AND "spree_stock_items"."variant_id" = 2 AND "spree_stock_locations"."active" = 't'

Finally creating our line item.

  SQL (0.3ms)  INSERT INTO "spree_line_items" ("cost_price", "created_at", "currency", "order_id", "price", "quantity", "tax_category_id", "updated_at", "variant_id") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)  [["cost_price", 17.0], ["created_at", "2014-06-15 23:25:08.313417"], ["currency", "USD"], ["order_id", 1], ["price", 10.0], ["quantity", 1], ["tax_category_id", 1], ["updated_at", "2014-06-15 23:25:08.313417"], ["variant_id", 2]]

The following is from after_save callbacks on the line_item

  • touch:true on order
  • update_tax_charge
  • update_adjustments (the bulk of this)
  Spree::Zone Load (0.1ms)  SELECT  "spree_zones".* FROM "spree_zones"  WHERE "spree_zones"."default_tax" = 't'  ORDER BY "spree_zones"."id" ASC LIMIT 1
  SQL (0.1ms)  UPDATE "spree_orders" SET "updated_at" = '2014-06-15 23:25:08.317759' WHERE "spree_orders"."id" = 1
  Spree::Zone Load (0.1ms)  SELECT  "spree_zones".* FROM "spree_zones"  WHERE "spree_zones"."default_tax" = 't'  ORDER BY "spree_zones"."id" ASC LIMIT 1
  Spree::LineItem Load (0.2ms)  SELECT  "spree_line_items".* FROM "spree_line_items"  WHERE "spree_line_items"."id" = ? LIMIT 1  [["id", 1]]
  Spree::Adjustment Load (0.1ms)  SELECT "spree_adjustments".* FROM "spree_adjustments"  WHERE "spree_adjustments"."adjustable_id" = ? AND "spree_adjustments"."adjustable_type" = ? AND "spree_adjustments"."source_type" = 'Spree::PromotionAction'  [["adjustable_id", 1], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.2ms)  SELECT  "spree_adjustments".* FROM "spree_adjustments"  WHERE "spree_adjustments"."adjustable_id" = ? AND "spree_adjustments"."adjustable_type" = ? AND "spree_adjustments"."source_type" = 'Spree::PromotionAction' AND "spree_adjustments"."eligible" = 't'  ORDER BY amount ASC, created_at DESC LIMIT 1  [["adjustable_id", 1], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.1ms)  SELECT "spree_adjustments".* FROM "spree_adjustments"  WHERE "spree_adjustments"."adjustable_id" = ? AND "spree_adjustments"."adjustable_type" = ? AND "spree_adjustments"."source_type" = 'Spree::TaxRate' AND "spree_adjustments"."included" = 't'  [["adjustable_id", 1], ["adjustable_type", "Spree::LineItem"]]
  Spree::Adjustment Load (0.1ms)  SELECT "spree_adjustments".* FROM "spree_adjustments"  WHERE "spree_adjustments"."adjustable_id" = ? AND "spree_adjustments"."adjustable_type" = ? AND "spree_adjustments"."source_type" = 'Spree::TaxRate' AND "spree_adjustments"."included" = 'f'  [["adjustable_id", 1], ["adjustable_type", "Spree::LineItem"]]
  SQL (0.1ms)  UPDATE "spree_line_items" SET "promo_total" = 0.0, "included_tax_total" = 0, "additional_tax_total" = 0, "adjustment_total" = 0.0, "updated_at" = '2014-06-15 23:25:08.331023' WHERE "spree_line_items"."id" = 1
   (0.0ms)  RELEASE SAVEPOINT active_record_1

Fortunately, we're only creating an order in the cart state here. This gets worse when looking at a completed order which requires a shipping address, shipment, shpping rates, inventory units, and a payment.

=> #<Spree::LineItem id: 1, variant_id: 2, order_id: 1, quantity: 1, price: #<BigDecimal: 10.0>, created_at: "2014-06-15 23:25:08", updated_at: "2014-06-15 23:25:08", currency: "USD", cost_price: #<BigDecimal: 17.0>, tax_category_id: 1, adjustment_total: #<BigDecimal: 0.0>, additional_tax_total: #<BigDecimal: 0.0>, promo_total: #<BigDecimal: 0.0>, included_tax_total: #<BigDecimal: 0.0>, pre_tax_amount: nil>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment