ラベル Rake の投稿を表示しています。 すべての投稿を表示
ラベル Rake の投稿を表示しています。 すべての投稿を表示

2013年1月10日木曜日

【Rake】ymlでテストデータを作成・読出し


テストデータフォルダ:test/fixtures


■DBからymlへデータをsave
rake db:fixtures:extract [FIXTURES=ファイル名]

 ファイル名に拡張子(.yml)はつけない
   例)
  rake db:fixtures:extract FIXTURES=employees

(参考)
  http://techracho.bpsinc.jp/baba/2012_05_14/5456


■ymlからDBへデータをload
rake db:fixtures:load [FIXTURES=ファイル名]


2012年11月21日水曜日

【Rake】初期データを投入する


【初期データの設定】
  db/seed.rb で、モデルの初期データ設定を記述(create)
 csvからの読み込みも可能

 (参考)
  http://www.rubylife.jp/rails/model/index10.html



【初期データの投入】

    rake db:seed


【DB作成&初期データ投入】


 ・DB作成&初期データ投入(migration実行ではなく、schema.rbから復帰)
  (Create the database, load the schema, and initialize with the seed data)


      rake db:setup

 ・DB再作成&初期データ投入(migration実行ではなく、schema.rbから復帰)
  (Drops and recreates the database from db/schema.rb
       for the current environment and loads the seeds)

      rake db:reset

  ・schema.rbでなく、migration から再作成したいときは

      rake db:migrate:reset

(参考)
rakeのタスク一覧
http://pentan.info/ruby/ror/rake_list.html#rake_db

2012年7月20日金曜日

既存テーブルにカラム追加する(migrate)



(1)指定カラムを追加するmigrateファイルを作成する( def change )

  rails g migration AddColnameToTablename colname:string


(2)作成したファイルの内容変更
 ・カラム属性を追加(:null, :default など)

(3) マイグレーション実行 
  rake db:migrate



 カラムの属性を変更するときは以下で仮作成

 rails g migration ChangeColNameToTablename
 

2012年3月14日水曜日

rake コマンドめも

・環境に設定されているroute を表示
rake routes

・Databaseの削除
rake db:drop

・Databaseの作成
rake db:create

・テーブルの作成
rake db:migrate

rake をbudle install した後「command not found 」になる現象

新しいバージョンのRake(0.8.7 → 0.9.2)をGemfileに追加して、bundle install でインストール
→ Rake 実行すると、「-bash: rake: command not found」
→bundle では、rake のPATHがとおらない様子

【対処】
gemコマンドで直接インストールするとOK

sudo gem install rake -v '0.9.2'

2012年3月7日水曜日

rake dbコマンドでテスト用DB複製

[rake dbコマンド]
rake db:test:clone                          # 現在の環境の DB スキーマから、テスト用 DB を複製

rake db:test:clone_structure         # Development のstructureから、テスト用 DB を複製
rake db:test:prepare                 # テスト用 DB を準備して、スキーマを読み込む
rake db:test:purge                   # テスト用 DB を空にする
rake db:test:clone RAILS_ENV=production     # production環境の DB スキーマから、テスト用 DB を複製