2020-08-07 01:14:14 +02:00
|
|
|
class RemoveTableArticlePrices < ActiveRecord::Migration[4.2]
|
2009-01-06 11:49:19 +01:00
|
|
|
def self.up
|
2023-05-12 13:01:12 +02:00
|
|
|
puts 'create columns in articles ...'
|
|
|
|
add_column 'articles', 'clear_price', :decimal, precision: 8, scale: 2, default: 0.0, null: false
|
|
|
|
add_column 'articles', 'gross_price', :decimal, precision: 8, scale: 2, default: 0.0, null: false
|
|
|
|
add_column 'articles', 'tax', :float
|
|
|
|
add_column 'articles', 'refund', :decimal, precision: 8, scale: 2, default: 0.0, null: false
|
|
|
|
add_column 'articles', 'unit_quantity', :integer, default: 1, null: false
|
|
|
|
add_column 'articles', 'order_number', :string
|
|
|
|
add_column 'articles', 'created_at', :datetime
|
|
|
|
add_column 'articles', 'updated_at', :datetime
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# stop auto-updating the timestamps to make the data-copy safe!
|
|
|
|
Article.record_timestamps = false
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
puts 'now copy values of article_prices into new articles-columns...'
|
2009-01-06 11:49:19 +01:00
|
|
|
Article.find(:all).each do |article|
|
|
|
|
price = article.current_price
|
2022-10-13 18:25:52 +02:00
|
|
|
article.update!(clear_price: price.clear_price,
|
|
|
|
gross_price: price.gross_price,
|
|
|
|
tax: price.tax,
|
|
|
|
refund: price.refund,
|
|
|
|
unit_quantity: price.unit_quantity,
|
|
|
|
order_number: price.order_number,
|
|
|
|
updated_at: price.updated_on,
|
|
|
|
created_at: price.updated_on)
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
puts 'delete article_prices, current_price attribute'
|
2009-01-06 11:49:19 +01:00
|
|
|
drop_table :article_prices
|
|
|
|
remove_column :articles, :current_price_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.down
|
|
|
|
add_column :articles, :current_price_id, :integer
|
2023-05-12 13:01:12 +02:00
|
|
|
create_table 'article_prices', force: true do |t|
|
|
|
|
t.integer 'article_id', default: 0, null: false
|
|
|
|
t.decimal 'clear_price', precision: 8, scale: 2, default: 0.0, null: false
|
|
|
|
t.decimal 'gross_price', precision: 8, scale: 2, default: 0.0, null: false
|
|
|
|
t.float 'tax', default: 0.0, null: false
|
|
|
|
t.decimal 'refund', precision: 8, scale: 2, default: 0.0, null: false
|
|
|
|
t.datetime 'updated_on'
|
|
|
|
t.integer 'unit_quantity', default: 1, null: false
|
|
|
|
t.string 'order_number'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# copy data from article now into old ArticlePrice-object
|
|
|
|
Article.find(:all).each do |article|
|
2023-05-12 13:01:12 +02:00
|
|
|
price = ArticlePrice.create(clear_price: article.clear_price,
|
|
|
|
gross_price: article.gross_price,
|
|
|
|
tax: article.tax,
|
|
|
|
refund: article.refund,
|
|
|
|
unit_quantity: article.unit_quantity,
|
|
|
|
order_number: article.order_number.presence,
|
|
|
|
updated_on: article.updated_at)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
article.update_attribute(:current_price, price)
|
|
|
|
price.update_attribute(:article, article)
|
|
|
|
end
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# remove new columns
|
2023-05-12 13:01:12 +02:00
|
|
|
remove_column 'articles', 'clear_price'
|
|
|
|
remove_column 'articles', 'gross_price'
|
|
|
|
remove_column 'articles', 'tax'
|
|
|
|
remove_column 'articles', 'refund'
|
|
|
|
remove_column 'articles', 'unit_quantity'
|
|
|
|
remove_column 'articles', 'order_number'
|
|
|
|
remove_column 'articles', 'created_at'
|
|
|
|
remove_column 'articles', 'updated_at'
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
end
|