Home > All > sequelでmigration

sequelでmigration

10月 21st, 2009

普通に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

RUBY:
  1. # db/migrate/001_create_posts.rb
  2. class CreatePosts <Sequel::Migration
  3.   def up
  4.     create_table :posts do
  5.       primary_key :id
  6.       string :text
  7.     end
  8.   end
  9. end

RUBY:
  1. # rakefile.rb
  2. require 'rubygems'
  3. require 'rake'
  4. require 'sequel'
  5. require 'sequel/extensions/migration'
  6.  
  7. namespace :db do
  8.  desc 'migrate database'
  9.   task :migrate do
  10.     DB = Sequel.connect('sqlite//db/development.sqlite3')
  11.     Sequel::Migrator.apply(DB, './db/migrate')
  12.   end
  13. end

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

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

関連エントリ

  1. No comments yet.
  1. No trackbacks yet.