Ensure that the article_id is set for every article_price

This addresses the TODO from dc92de771e.
This commit is contained in:
Patrick Gansterer 2020-08-17 14:00:26 +02:00
parent 26aecbbe9e
commit 3a6276aece
3 changed files with 22 additions and 8 deletions

View file

@ -166,13 +166,7 @@ class OrderArticle < ApplicationRecord
else else
# Creates a new article_price if neccessary # Creates a new article_price if neccessary
# Set created_at timestamp to order ends, to make sure the current article price isn't changed # Set created_at timestamp to order ends, to make sure the current article price isn't changed
create_article_price!(price_attributes.merge(created_at: order.ends)) and save create_article_price!(price_attributes.merge(article_id: article_id, created_at: order.ends)) and save
# TODO: The price_attributes do not include an article_id so that
# the entry in the database will not "know" which article is
# referenced. Let us check the effect of that and change it or
# comment on the meaning.
# Possibly this is the real reason why the global price is not
# affected instead of the `created_at: order.ends` injection.
end end
# Updates ordergroup values # Updates ordergroup values

View file

@ -0,0 +1,20 @@
class EnsureArticleForArticlePrice < ActiveRecord::Migration
class ArticlePrice < ActiveRecord::Base; end
def change
reversible do |dir|
dir.up do
execute <<-SQL
UPDATE article_prices SET article_id = (
SELECT article_id FROM order_articles
WHERE article_price_id = article_prices.id
)
WHERE article_id IS NULL
SQL
ArticlePrice.where(article_id: nil).destroy_all
end
end
change_column_null :article_prices, :article_id, false
end
end

View file

@ -21,7 +21,7 @@ ActiveRecord::Schema.define(version: 20181205010000) do
add_index "article_categories", ["name"], name: "index_article_categories_on_name", unique: true, using: :btree add_index "article_categories", ["name"], name: "index_article_categories_on_name", unique: true, using: :btree
create_table "article_prices", force: :cascade do |t| create_table "article_prices", force: :cascade do |t|
t.integer "article_id", limit: 4 t.integer "article_id", limit: 4, null: false
t.decimal "price", precision: 8, scale: 2, default: 0, null: false t.decimal "price", precision: 8, scale: 2, default: 0, null: false
t.decimal "tax", precision: 8, scale: 2, default: 0, null: false t.decimal "tax", precision: 8, scale: 2, default: 0, null: false
t.decimal "deposit", precision: 8, scale: 2, default: 0, null: false t.decimal "deposit", precision: 8, scale: 2, default: 0, null: false