# encoding:utf-8
class CreateProductAttrs < ActiveRecord::Migration
  def up
    #规格定义表
    create_table :product_attr_keys do |t|
      # 产品名称
      t.column :name, :string,:limit => 128
      # 是否发布
      t.column :status, :boolean, :default=>1
      t.timestamps
    end

    #规格明细表
    create_table :product_attrs do |t|
      # 规格ID
      t.column :attr_key_id, :integer, :limit => 8, :default => 0
      # 规格名称
      t.column :name, :string,:limit => 500
      # 排序
      t.column :recommend, :integer, :limit => 8, :default => 0
      # 状态
      t.column :status, :boolean, :default=>1
      t.timestamps
    end
    add_index :product_attrs, :attr_key_id


    #主商品规格配置表
    create_table :product_attr_configs do |t|
      # 主商品ID
      t.column :product_id, :integer, :limit => 8, :default => 0
      # 规格ID
      t.column :attr_key_id, :integer, :limit => 8, :default => 0
      # 类型
      t.column :size_type, :string, :limit => 64
      t.timestamps
    end

    add_index :product_attr_configs, :product_id
    add_index :product_attr_configs, :attr_key_id
    add_column :products, :size_id, :integer,:default => 0,:limit => 8
    add_column :products, :color_id, :integer,:default => 0,:limit => 8
    add_column :products, :relate_product_id, :integer
    add_column :products, :show_flag, :boolean,:limit => 8
    add_column :order_details, :size_name,:string,:limit => 128
    add_column :order_details, :color_name,:string,:limit => 128
    add_index :products, :size_id
    add_index :products, :color_id
  end

  def down
  	drop_table :product_attr_keys
    drop_table :product_attrs
    drop_table :product_attr_configs
    remove_column :products, :size_id
    remove_column :products, :color_id
    remove_column :products, :relate_product_id
    remove_column :products, :show_flag
    remove_column :order_details, :size_name
    remove_column :order_details, :color_name
  end
end
