Ensure that the article_id is set for every article_price
This addresses the TODO from dc92de771e.
This commit is contained in:
parent
26aecbbe9e
commit
3a6276aece
3 changed files with 22 additions and 8 deletions
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
||||
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 "tax", precision: 8, scale: 2, default: 0, null: false
|
||||
t.decimal "deposit", precision: 8, scale: 2, default: 0, null: false
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue