2020-08-07 01:14:14 +02:00
|
|
|
class CreateArticles < ActiveRecord::Migration[4.2]
|
2023-05-12 13:01:12 +02:00
|
|
|
SAMPLE_ARTICLE_NAMES = %w[banana kiwi strawberry]
|
2009-01-06 11:49:19 +01:00
|
|
|
|
|
|
|
def self.up
|
|
|
|
create_table :articles do |t|
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :name, :string, null: false
|
|
|
|
t.column :supplier_id, :integer, null: false
|
|
|
|
t.column :article_category_id, :integer, null: false
|
|
|
|
t.column :unit, :string, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
t.column :note, :string
|
2023-05-12 13:01:12 +02:00
|
|
|
t.column :availability, :boolean, default: true, null: false
|
2009-01-06 11:49:19 +01:00
|
|
|
t.column :current_price_id, :integer
|
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
add_index(:articles, :name, unique: true)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
# Create 30 sample articles...
|
|
|
|
puts "Create 3 articles of the supplier '#{CreateSuppliers::SUPPLIER_SAMPLE}'..."
|
|
|
|
raise "Supplier '#{CreateSuppliers::SUPPLIER_SAMPLE}' not found!" unless supplier = Supplier.find_by_name(CreateSuppliers::SUPPLIER_SAMPLE)
|
|
|
|
raise "Category '#{CreateArticleMeta::CATEGORY_SAMPLE}' not found!" unless category = ArticleCategory.find_by_name(CreateArticleMeta::CATEGORY_SAMPLE)
|
2021-03-01 15:27:26 +01:00
|
|
|
|
2009-01-06 11:49:19 +01:00
|
|
|
SAMPLE_ARTICLE_NAMES.each do |a|
|
2021-03-01 15:27:26 +01:00
|
|
|
puts 'Create Article ' + a
|
2023-05-12 13:01:12 +02:00
|
|
|
Article.create(name: a,
|
|
|
|
supplier: supplier,
|
|
|
|
article_category: category,
|
|
|
|
unit: '500g',
|
|
|
|
note: 'delicious',
|
|
|
|
availability: true)
|
2021-03-01 15:27:26 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
raise 'Failed!' unless Article.find(:all).length == SAMPLE_ARTICLE_NAMES.length
|
2009-01-06 11:49:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.down
|
|
|
|
drop_table :articles
|
|
|
|
end
|
|
|
|
end
|