Ruby on Rails for Mac OSX 10.3.x (Panther) + PostgreSQL7.4 インストールメモ
はじめに
Railsはとりあえずインストールすれば、開発ができるようになる一通りそろったフレームワークです。けれど、いろいろインストールしなければならない様子。まだ上手くインストールできていませんが開発というよりも、コード生成コマンドでかなりの雛型を作成してしまうフレームワーク(コマンド集?)ってイメージです。デフォルトのデータベースがMySQL使えよ!というニュアンスが、オフィシャルのインストールガイドなどを見ていると感じるので、単純にMySQLなり、Linuxにインストールするのが無難。こんな環境でインストール試しているのは私ぐらいかも。。。PostgreSQLのドライバ部分で試行錯誤しているのが現状ですが、とりあえずメモを残して置きます。
- Ruby 1.8.2 for osx Panther
- RubyGems
- Rails 0.13.1
- PostgreSQL 8.0.x
- Ruby extension Library (PostgreSQL interface )
環境
iBook G4 CPU:1.2GHz, Memory:768MB, Disk 80GBRuby 1.8.2のインストール
fink からではなく、Railsからリンクされたサイトから Ruby 1.8.2 Stable for Pantherをインストールしてみる。
http://homepage.mac.com/discord/Ruby/
これは、デフォルトでは/usr/local/binにインストールされるのでオリジナルの
Rubyは変更されない。
/usr/local/bin/ruby --version
これで、1.8.2が入っていれば問題ない。
Rubygemsをインストール
次にfinkを使ってrubygemsをインストール
fink install rubygems
XCode関連でgcc3.3にバージョンを上げているので、gcc3.1の依存関係でfinkからrubygemsを
うまくインストールできなかった。
http://rubyforge.org/frs/?group_id=126から、rubygems-0.8.11.tgzをダウンロードしてインストール。
$sudo ruby setup.rb
$which gem
/usr/local/bin/gem
問題なくインストールできたようだ。
railsのインストール
rails自体は、コード生成用のコマンドなので、gemでrails一式をインストールします。--include-dependenciesオプションは、依存するもの全てインストールする時に使うオプションです。
$sudo gem install rails --include-dependencies
railsコマンドの確認
which rails
/usr/local/bin/rails
postgresqlのインストール
$fink install postgresql
finkの場合バイナリ提供だと思っていたが、postgresqlはコンパイルされるので
しばし放置。コーヒーでも飲んでましょう。
fink でのpostfresql7.4, postgresql8.0のヘッダファイルとライブラリインストールだとlibpq.soを作ってくれないので次のRuby extension libraryのインストールに進めない。postgres.orgから7.4のソースコードを持ってきてコンパイル。
http://developer.apple.com/internet/opensource/postgres.html
上記がpostgresqlのインストールの参考になる。またMacOSXはデフォルトで
postgresユーザを持っているのでNetInfoやniutilでユーザを修正する。具体的には、/User/postgresディレクトリの作成と/sw/bin/bashをデフォルトのシェルにする事ぐらいかな。
Ruby exntension libraryのインストール
http://ruby.scripting.ca/postgres/からRuby extension libraryのインストール
これでRubyからPostgresqlが利用できるようになる。
-bash-3.00$ sudo ruby extconf.rb --with-pgsql-include-dir=/usr/local/pgsql/include/ --with-pgsql-lib-dir=/usr/local/pgsql/lib
checking for cygwin32_socket() in -lwsock32... no
checking for socket() in -lsocket... no
checking for gethostbyname() in -linet... no
checking for gethostbyname() in -lnsl... no
checking for sys/un.h... yes
checking for socket()... yes
checking for hsterror()... no
checking for gethostname()... yes
Using PostgreSQL include directory: /usr/local/pgsql/include/
Using PostgreSQL lib directory: /usr/local/pgsql/lib
checking for PQsetdbLogin() in -lpq... yes
checking for PQsetClientEncoding()... yes
checking for pg_encoding_to_char()... yes
checking for PQescapeString()... yes
creating Makefile
-bash-3.00$ make
gcc -fno-common -I/usr/local/pgsql/include/ -I. -I/usr/local/lib/ruby/1.8/powerpc-darwin7.8.0 -I/usr/local/lib/ruby/1.8/powerpc-darwin7.8.0 -I. -DHAVE_SYS_UN_H -DHAVE_SOCKET -DHAVE_GETHOSTNAME -DHAVE_PQSETCLIENTENCODING -DHAVE_PG_ENCODING_TO_CHAR -DHAVE_PQESCAPESTRING -c postgres.c
cc -dynamic -bundle -undefined suppress -flat_namespace -L/usr/local/pgsql/lib -L"/usr/local/lib" -o postgres.bundle postgres.o -lruby -lpq -ldl -lobjc
-bash-3.00$ sudo make install
install -c -p -m 0755 postgres.bundle /usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin7.8.0
Postgresql7.4 テスト
-su-3.00$ cat ~/.bash_profile
set -o vi
umask 022
export PGDATA=/usr/local/pgsql/data
export PATH=$PATH:/usr/local/pgsql/bin
initdb -D /usr/local/pgsql/data
pg_hba.conf
# TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
local all all trust
# IPv4-style local connections:
#host all all 127.0.0.1 255.255.255.255 trust
host all all 0.0.0.0 0.0.0.0 trust
# IPv6-style local connections:
host all all ::1 ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff trust
-su-3.00$ pg_ctl stopwaiting for postmaster to shut down......done
postmaster successfully shut down
-su-3.00$ /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile -o -i start
postmaster successfully started
createdb test
psql test
test=> create table foo (name varchar, foo_id serial);
NOTICE: CREATE TABLE will create implicit sequence 'foo_foo_id_seq' for
SERIAL column 'foo.foo_id' CREATE TABLE
test=> insert into foo (name) values ('Liz');
INSERT 16985 1
test=> insert into foo (name) values ('Jason');
INSERT 16986 1
select * from foo;
Rubyプログラムから動作確認
require "postgres"
conn = PGconn.connect(
"127.0.0.1", 5432, "", "", "test", "postgres", "postgres")
res = conn.exec("select * from hoge;")
p res
-bash-3.00$ ruby ConnectPostgreSQL.rb
#<PGresult:0x27ae0>
これでとりあえず、インストール終了。
uninitialized constant PGconn
NameError in Aaas#index
uninitialized constant PGconn
/app/controllers/aaas_controller.rb:8:in `list'
/app/controllers/aaas_controller.rb:3:in `index'
Show framework trace
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.1.1/lib/active_support/dependencies.rb:186:in
`const_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/postgresql_adapter.rb:37:in
`postgresql_connection'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/query_cache.rb:52:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/query_cache.rb:52:in
`connection='
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/connection_adapters/abstract_adapter.rb:108:in
`retrieve_connection'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:239:in
`connection'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:461:in
`count_by_sql'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.11.1/lib/active_record/base.rb:454:in
`count'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/pagination.rb:154:in
`count_collection_for_pagination'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/pagination.rb:171:in
`paginator_and_collection_for'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/pagination.rb:111:in
`paginate'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:756:in
`perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/filters.rb:295:in
`perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in
`measure'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/benchmarking.rb:41:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/rescue.rb:80:in
`perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in
`send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-1.9.1/lib/action_controller/base.rb:356:in
`process'
/usr/local/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/dispatcher.rb:32:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:105:in
`handle_dispatch'
/usr/local/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:71:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/local/lib/ruby/1.8/webrick/server.rb:155:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:144:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:144:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:94:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:89:in `each'
/usr/local/lib/ruby/1.8/webrick/server.rb:89:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:79:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:79:in `start'
/usr/local/lib/ruby/gems/1.8/gems/rails-0.13.1/lib/webrick_server.rb:57:in `dispatch'
/Users/okitasatoshi/develop/ruby/demo/script/server:49
This error occured while loading the following files:
p_gconn.rb
LD_LIBRARY_PATH環境変数に/usr/local/pgsql/libを入れているのと、自作のPostgresqlへのrubyプログラム接続は、Rubyプログラムから動作確認で確認しているのだが、どうやらpostgresqlのドライバローディングが上手く出来ていないのでpostgresql_adapter.rb:37で例外がraiseしている模様。スクリプト言語はその場でソースコードが見れるので凄く良い。
/usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin7.8.0をLD_LIBRARY_PATHに入れてみても、エラーが消えない。はまってる…
ライブラリのローディングを調べていたら-rオプションでライブラリを指定できるので試してみる。
-bash-3.00$ ruby -r /usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin7.8.0/postgres.bundle script/generate scaffold aaa
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
skip app/models/aaa.rb
skip test/unit/aaa_test.rb
skip test/fixtures/aaas.yml
exists app/controllers/
exists app/helpers/
exists app/views/aaas
exists test/functional/
overwrite app/controllers/aaas_controller.rb? [Ynaq] a
forcing scaffold
force app/controllers/aaas_controller.rb
force test/functional/aaas_controller_test.rb
force app/helpers/aaas_helper.rb
force app/views/layouts/aaas.rhtml
force public/stylesheets/scaffold.css
force app/views/aaas/list.rhtml
force app/views/aaas/show.rhtml
force app/views/aaas/new.rhtml
force app/views/aaas/edit.rhtml
error Before updating scaffolding from new DB schema, try creating a table for your model (Aaa)
Aaaテーブルを作ってみろってerrorを出力しているので試しに作ってみるがうまくいかないので、
Ruby on Rails?…????Web?A?v???P?[?V?????????¨?J?≠
http://www-6.ibm.com/jp/developerworks/linux/050708/j_l-rubyrails.html
を参考にしてみると、Railsはcontactsテーブルをまずは作らなければいけないようだ。
postgresql7.4でとりあえず制約はPrimaryKeyのみ付加してテーブル作成してみる。
test=> create table contacts ( id smallint primary key, name varchar(30), created_on timestamp, updatedF_on timestamp);
rubyコマンドに-rオプションをつけてscaffoldを実行してみる
-bash-3.00$ ruby -r /usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin7.8.0/postgres.bundle script/generate scaffold contact
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
skip app/models/contact.rb
skip test/unit/contact_test.rb
skip test/fixtures/contacts.yml
exists app/controllers/
exists app/helpers/
exists app/views/contacts
exists test/functional/
overwrite app/controllers/contacts_controller.rb? [Ynaq] a
forcing scaffold
force app/controllers/contacts_controller.rb
force test/functional/contacts_controller_test.rb
force app/helpers/contacts_helper.rb
force app/views/layouts/contacts.rhtml
force public/stylesheets/scaffold.css
force app/views/contacts/list.rhtml
force app/views/contacts/show.rhtml
force app/views/contacts/new.rhtml
force app/views/contacts/edit.rhtml
force app/views/contacts/_form.rhtml
問題なくコード生成がおこなえた。
次にWebrickで動作確認
-bash-3.00$ ruby -r /usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin7.8.0/postgres.bundle script/server
=> Rails application started on http://0.0.0.0:3000
=> Ctrl-C to shutdown server; call with --help for options
[2005-10-16 18:51:56] INFO WEBrick 1.3.1
[2005-10-16 18:51:56] INFO ruby 1.8.2 (2004-12-25) [powerpc-darwin7.8.0]
[2005-10-16 18:51:56] INFO WEBrick::HTTPServer#start: pid=12933 port=3000
http://localhost:3000/contactsにブラウザでアクセス。正常に動作できた。
ちょっと注意するのは、テーブル名はcontacts, コード生成はcontact, ブラウザアクセスはcontacts.
ActionRecordの仕組みなどを理解すれば自ずとわかるはずなので取り合えずこのまま進めてみる。
Aaaテーブルも同じようなテーブル構造であれば動作するのか?
test=> create table aaas ( id smallint primary key, name varchar(30), created_on timestamp, updatedF_on timestamp);
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "aaas_pkey" for table "aaas"
CREATE TABLE
test=> drop table aaa;
DROP TABLE
-bash-3.00$ ruby -r /usr/local/lib/ruby/site_ruby/1.8/powerpc-darwin7.8.0/postgres.bundle script/generate scaffold aaa
dependency model
exists app/models/
exists test/unit/
exists test/fixtures/
skip app/models/aaa.rb
skip test/unit/aaa_test.rb
skip test/fixtures/aaas.yml
exists app/controllers/
exists app/helpers/
exists app/views/aaas
exists test/functional/
overwrite app/controllers/aaas_controller.rb? [Ynaq] a
forcing scaffold
force app/controllers/aaas_controller.rb
force test/functional/aaas_controller_test.rb
force app/helpers/aaas_helper.rb
force app/views/layouts/aaas.rhtml
force public/stylesheets/scaffold.css
force app/views/aaas/list.rhtml
force app/views/aaas/show.rhtml
force app/views/aaas/new.rhtml
force app/views/aaas/edit.rhtml
create app/views/aaas/_form.rhtml
問題なくコード生成ができた。後はブラウザで確認してみる。
http://localhost:3000/aaas
問題なくブラウザではじめの画面は表示できた。
そのままでは、idカラムにシーケンスオブジェクトが紐づかないため以下のようなDDLに変更したら、CRUDがうまくいった。
test=> create table aaas(id serial, name varchar(30), created_on timestamp, updated_on timestamp);
まとめ
Railsをデフォルトで利用する場合は、ある程度テーブル構造が決められていることが判明。既存の企業アプリケーションは新規で作成するよりもその他のデータモデルと結合してシテスムを拡大していく場合があるので、テーブル構造が変更できない場合にRailsは使えるのだろうか?と疑問も出てきた。
