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:
-
# db/migrate/001_create_posts.rb
-
class CreatePosts <Sequel::Migration
-
def up
-
create_table :posts do
-
primary_key :id
-
string :text
-
end
-
end
-
end
RUBY:
-
# 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の内容が実行されます
参考
[ruby] Sequelでのmigration用Rakefile - よしだメモ
SequelでMigration - みじんこ日記