Home > All > sequelでmigration

sequelでmigration

October 21st, 2009 Leave a comment Go to comments

普通にschema pluginを使ってモデルを定義していましたが、
リファレンスを見るとschemaはテスト向けの変更の予定が無いDB向けで、
普通はmigrationを使ってね、と書いてありました

This plugin is mostly suited to test code. If there is any chance that your application’s schema could change, you should be using the migration extension instead.

Sequel: The Database Toolkit for Ruby Sequel::Plugins::Schema

# db/migrate/001_create_posts.rb
class CreatePosts < Sequel::Migration
  def up
    create_table :posts do
      primary_key :id
      string :text
    end
  end
end
# rakefile.rb
require 'rubygems'
require 'rake'
require 'sequel'
require 'sequel/extensions/migration'

namespace :db do
 desc 'migrate database'
  task :migrate do
    DB = Sequel.connect('sqlite//db/development.sqlite3')
    Sequel::Migrator.apply(DB, './db/migrate')
  end
end

これでコンソールから
rake db:migrate
とすると、001_create_posts.rbの内容が実行されます

参考
Sequelでのmigration用Rakefile – よしだメモ
SequelでMigration – みじんこ日記

関連記事:

Tags:
  1. No comments yet.
  1. No trackbacks yet.