- 更新日: 2014年2月4日
 - Rails
 
ActiveRecord 単体をインストールして sqlite3 データベースのマイグレーション
Rails の ActiveRecord のマイグレーション機能は便利なので、Rails 製でないアプリケーションでもマイグレーション機能を使いたい。ActiveRecord を単体でインストールし、Rake タスクを自作することで ActiveRecord のマイグレーション機能を利用できます。
— gem のバージョン —
activerecord-4.0.2
rake-10.1.1
sqlite3-1.3.8
ディレクトリ構成
ディレクトリ構成は以下の通りにします。マイグレートファイルの場所は Rails にならいました。また sqlite データベースは、database/db.sqlite に置きました。
app
| database
| | db.sqlite
| db/
| | migrate/
| | | 001_create_tests.rb
Rakefile
| 
					 1 2 3 4 5 6 7 8  | 
						$ mkdir app $ cd app $ mkdir database $ mkdir -p db/migrate $ touch db/migrate/001_create_tests.rb $ touch Rakefile  | 
					
必要な gem をインストール
今回扱ったデータベースは sqlite3 です。sqlite3, activerecord, rake の3つの gem をインストールします。
| 
					 1 2 3 4 5 6 7 8 9 10 11  | 
						$ cd app $ bundle init $ vi Gemfile source "https://rubygems.org" gem "sqlite3" gem "activerecord" gem "rake" $ bundle install --path vendor/bundle  | 
					
Rakefile を編集
Rakefile に Rakeタスクを追加します。
app/Rakefile
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  | 
						require 'active_record' require 'logger' namespace :db do   MIGRATIONS_DIR = 'db/migrate'   # connect the database   ActiveRecord::Base.establish_connection(     :adapter => 'sqlite3',     :database => 'database/db.sqlite'   )   # outpt logs   ActiveRecord::Base.logger = Logger.new(STDOUT)   desc "Migrate the database"   task :migrate do     ActiveRecord::Migrator.migrate(MIGRATIONS_DIR, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)   end   desc 'Roll back the database schema to the previous version'   task :rollback do     ActiveRecord::Migrator.rollback(MIGRATIONS_DIR, ENV['STEP'] ? ENV['STEP'].to_i : 1)   end end  | 
					
migration ファイルを編集
先ほど 001_create_tests.rb という名前で migrate 用のファイルを作成したので、クラス名を CreateTests とします。
db/migrate/001_create_tests.rb
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13  | 
						class CreateTests < ActiveRecord::Migration   def self.up     create_table :tests do |t|       t.string :name       t.timestamps     end   end   def self.down     drop_table :tests   end end  | 
					
migrate を実行
rake タスクが正しく登録されているか確認。
| 
					 1 2 3 4 5  | 
						$ bundle exec rake -vT rake db:migrate   # Migrate the database rake db:rollback  # Roll back the database schema to the previous version  | 
					
migrate を実行して tests テーブルを作成。
| 
					 1 2 3  | 
						$ bundle exec rake db:migrate  | 
					
tests テーブルが追加されているのを確認できる。
元に戻す(tests テーブルを削除)。
| 
					 1 2 3  | 
						$ bundle exec rake db:rollback  | 
					
以上です。
- – 参考リンク –
 - Exposing Gotchas: ActiveRecord migrations without Rails
 - Ruby – 非Rails AppでActiveRecord::Migrationを使う + Rakeでバージョン管理する – Qiita [キータ]
 - ActiveRecordでSQLiteを操作する – 働かないプログラマのメモ帳
 - ActiveRecordを単体で使うには | tsuchikazu blog
 
- Rails の関連記事
 - RailsでMySQLパーティショニングのマイグレーション
 - Rails ActiveRecordでdatetime型カラムのGROUP BY集計にタイムゾーンを考慮する
 - RailsプラグインGemの作成方法、RSpecテストまで含めたrails pluginの作り方
 - RailsでAMPに対応するgemをリリースしました
 - Railsでrequest.urlとrequest.original_urlの違い
 - Railsでwheneverによるcronバッチ処理
 - Google AnalyticsのRails Turbolinks対応
 - Railsアプリにソーシャル・シェアボタンを簡単設置
 - Rails監視ツール用にErrbitをHerokuで運用
 - Facebook APIバージョンのアップグレード手順(Rails OmniAuth)
 

画像付き英語辞書 Imagict | 英単語をイメージで暗記





Leave Your Message!