migration 문법

IT(Old)/RubyOnRails 2008. 1. 21. 15:08
1. 널값 허용X

create_table "groups" do |t|
  t.column "name", :string, Lnull => false
end

2. default값 지정
create_table "users" do |t|
  t.column "type", :string, Ldefault => "일반"
end

3. 문자열 컬럼 길이 지정
create_table "groups" do |t|
  t.column "name", :string, Llimit => 10
end

4. 숫자컬럼 길이지정
create_table "people" do |t|
  t.column "name", :string
  t.column "height", :decimal, Lprecision => 5, Lscale => 2
end

5. boolean
boolean은 TINYINT로 지정이 되어 true(1), false(2) 로 저장이 된다.

6. 테이블명 변경
rename_table "groups", categories"

7. 인덱스 추가
  add_index "groups", "name", :unique => true
  remove_index "groups", "name"

8. 기타 SQL
마이그레이션에서 지원하지 않는 비표준 기능은 execute를 사용해라

execute "ALTER TABLE people ADD CONSTRAINT fk_person_group"
 

migration 원하는 버전으로 되돌리기

IT(Old)/RubyOnRails-Tip 2008. 1. 21. 14:38
rake db:migrate VERSION=1


여기서 VERSION은 반드시 대문자로 표시해야된다.


가장 최근으로 migration을 원한다면
rake db:migrate

시간/날짜 관련 표현식

IT(Old)/RubyOnRails-Tip 2008. 1. 21. 12:14

t = Time.now

t.strftime("%Y/%m/%d %H:%M:%S")
=>2008/01/21 12:13:30


t.strftime("%y/%m/%d %I:%M:%S %p");
=>08/01/21 12:13:30 PM

초를 더할때

t2 = t + 30

5일을 더할때
t3 = t + 60*60*24*5

크기로 비교 가능

t2 < t3


today = Data.today
christmas = Date.new(2007,12,25)

날짜는 +연산을 하면 일자가 더해진다.