foodsoft/db/migrate/006_create_articles.rb

37 lines
1.4 KiB
Ruby
Raw Normal View History

class CreateArticles < ActiveRecord::Migration[4.2]
SAMPLE_ARTICLE_NAMES = ['banana', 'kiwi', 'strawberry']
2009-01-06 11:49:19 +01:00
def self.up
create_table :articles do |t|
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
t.column :note, :string
t.column :availability, :boolean, :default => true, :null => false
t.column :current_price_id, :integer
end
add_index(:articles, :name, :unique => true)
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)
2009-01-06 11:49:19 +01:00
SAMPLE_ARTICLE_NAMES.each do |a|
puts 'Create Article ' + a
Article.create(:name => a,
:supplier => supplier,
:article_category => category,
:unit => '500g',
:note => 'delicious',
:availability => true)
end
2009-01-06 11:49:19 +01:00
raise "Failed!" unless Article.find(:all).length == SAMPLE_ARTICLE_NAMES.length
end
def self.down
drop_table :articles
end
end