Symbolic Link on Windows
What
You can use Symbolic links, Hard Links, and Junctions on Windows.
Insall(for 32bit) , How to use command
- download symlink-1.05-x86.cab
- uncompress
- move DIR of symlink-1.05-x86 to Your Path.(for example : C:\Documents and Settings\YourName\)
- add Path(for example : C:\Documents and Settings\YourName\symlink-1.05-x86)
- open command prompt.
- use ln command that seems like Unix ln Command, as follows.
> ln -s foo bar
I am on Windows XP Professional Version 2002 Service Pack 3.
Pipe Viewer : monitoring the progress
What
Pipe Viewer is a terminal-based tool for monitoring the progress of data through a pipeline.
Install(for i386)
wget http://pipeviewer.googlecode.com/files/pv-1.2.0-1.i386.rpm sudo rpm -i pv-1.2.0-1.i386.rpm
other version : http://en.sourceforge.jp/projects/freshmeat_pv/releases/
Check install
% pv --version pv 1.2.0 - Copyright(C) 2010 Andrew Wood <andrew.wood@ivarch.com> Web site: http://www.ivarch.com/programs/pv.shtml This program is free software, and is being distributed under the terms of the Artistic License 2.0. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
How to use pv
don't use pv.
% gzip access.log > access.log.gz
use pv.
% pv access.log | gzip > access.log.gz 520MB 0:00:08 [65.1MB/s] [================> ] 28% ETA 0:00:19
other
% man pv
Refarence
Customizing the MySQL Prompt
MySQL Prompt is default.
mysql>
I like this customizing that is [user name]@[host][selected database name], as follows.
root@localhost[hoge]>
Change the mysql> prompt using my.cnf or configration file for shell.
for example .zshrc, as follows.
export MYSQL_PS1='\U[\d]> '
for example my.cnf, as follows.
[mysql] prompt='\\U[\\d]> '
you can customize the MySQL Prompt if you want. http://dev.mysql.com/doc/refman/5.0/en/mysql-commands.html
Autoload Path and Nest
I have tow files hoge.rb and baz.rb.
app/models/hoge.rb
class Hoge class Fuga Baz.new end end
app/models/hoge/baz.rb
class Baz end
I have added the custom models to load path as follows.
config/environment.rb
config.autoload_paths += %W( #{RAILS_ROOT}/app/models/hoge )
Hoge::Fuga.new is LoadError: Expected in development mode as follows.
% script/console Loading development environment (Rails 2.3.11) >> Hoge::Fuga.new LoadError: Expected /home/admin/apps/ad_counter/hoge/app/models/hoge/baz.rb to define Hoge::Baz from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:452:in `load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:122:in `const_missing' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:463:in `load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:122:in `const_missing' from /home/admin/apps/ad_counter/hoge/app/models/hoge.rb:3 from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:406:in `load_without_new_constant_marking' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:406:in `load_file' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:547:in `new_constants_in' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:405:in `load_file' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:285:in `require_or_load' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:451:in `load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:106:in `const_missing' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:118:in `const_missing' from (irb):1
Next is production mode as follows.
% script/console production Loading production environment (Rails 2.3.11) /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:452:in `load_missing_constant': Expected /home/admin/apps/ad_counter/hoge/app/models/hoge/baz.rb to define Hoge::Baz (LoadError) from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:122:in `const_missing' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:463:in `load_missing_constant' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:122:in `const_missing' from /home/admin/apps/ad_counter/hoge/app/models/hoge.rb:3 from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require' from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:184:in `require' from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.11/lib/active_support/dependencies.rb:291:in `require_or_load' ... 18 levels... from /usr/local/lib/ruby/1.8/irb/init.rb:250:in `load_modules' from /usr/local/lib/ruby/1.8/irb/init.rb:21:in `setup' from /usr/local/lib/ruby/1.8/irb.rb:54:in `start' from /usr/local/bin/irb:13
raise is activesupport-2.3.11/lib/active_support/dependencies.rb L 452
452 raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless uninherited_const_defined?(from_mod, const_name)
It's OK when baz.rb file in app/models derectory.
% script/console Loading development environment (Rails 2.3.11) >> Hoge::Fuga.new => #<Hoge::Fuga:0xb7686ed0>
簡単にload averageを上げるワンライナー
あるサーバのロードアベレージを高い状態で維持したかったのですが、JMeterなどでテストを作るのも面倒だったため、簡単に出来ないかなと思いやってみました。
Railsのfind_by_sqlとSTIについて
STI 時に find_by_sql で返すインスタンスのクラスについて調べてみました。activerecord-1.13.2 と activerecord-2.3.11 で挙動を調べました。
activerecord-1.13.2/lib/active_record/base.rb
427 # Works like find(:all), but requires a complete SQL string. Examples: 428 # Post.find_by_sql "SELECT p.*, c.author FROM posts p, comments c WHERE p.id = c.post_id" 429 # Post.find_by_sql ["SELECT * FROM posts WHERE author = ? AND created > ?", author_id, start_date] 430 def find_by_sql(sql) 431 connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) } 432 end 885 # Finder methods must instantiate through this method to work with the single-table inheritance model 886 # that makes it possible to create objects of different types from the same table. 887 def instantiate(record)
activerecord-2.3.11/lib/active_record/base.rb
642 # Executes a custom SQL query against your database and returns all the results. The results will 643 # be returned as an array with columns requested encapsulated as attributes of the model you call 644 # this method from. If you call <tt>Product.find_by_sql</tt> then the results will be returned in 645 # a Product object with the attributes you specified in the SQL query. 646 # 647 # If you call a complicated SQL query which spans multiple tables the columns specified by the 648 # SELECT will be attributes of the model, whether or not they are columns of the corresponding 649 # table. 650 # 651 # The +sql+ parameter is a full SQL query as a string. It will be called as is, there will be 652 # no database agnostic conversions performed. This should be a last resort because using, for example, 653 # MySQL specific terms will lock you to using that particular database engine or require you to 654 # change your call if you switch engines. 655 # 656 # ==== Examples 657 # # A simple SQL query spanning multiple tables 658 # Post.find_by_sql "SELECT p.title, c.author FROM posts p, comments c WHERE p.id = c.post_id" 659 # > [#<Post:0x36bff9c @attributes={"title"=>"Ruby Meetup", "first_name"=>"Quentin"}>, ...] 660 # 661 # # You can use the same string replacement techniques as you can with ActiveRecord#find 662 # Post.find_by_sql ["SELECT title FROM posts WHERE author = ? AND created > ?", author_id, start_date] 663 # > [#<Post:0x36bff9c @attributes={"first_name"=>"The Cheap Man Buys Twice"}>, ...] 664 def find_by_sql(sql) 665 connection.select_all(sanitize_sql(sql), "#{name} Load").collect! { |record| instantiate(record) } 666 end 1651 # Finder methods must instantiate through this method to work with the 1652 # single-table inheritance model that makes it possible to create 1653 # objects of different types from the same table. 1654 def instantiate(record)
前提データ
usersテーブル
id | type | name | |||
1 | HogeUser | bob | |||
2 | FugaUser | mike |
fooテーブル
id | title | ||
1 | baz |
Railsにあるクラス
class User < ActiveRecord::Base end class HogeUser < User end class FugaUser < User end class Foo < ActiveRecord::Base end
activerecord-1.13.2/lib/active_record/base.rb の実行結果
typeを見て、HogeUser、FugaUserのインスタンスになります。
% script/console >> user = User.find_by_sql("select * from users") >> user.each {|u| p u.class } HogeUser FugaUser
type カラムを select から抜くと STI の効果はなくなります。
% script/console user = User.find_by_sql("select id, name from users") user.each {|u| p u.class } User User
Fooクラスでfind_by_sqlしても、typeカラムがあれば、HogeUser、FugaUserのインスタンスになります。
% script/console user = Foo.find_by_sql("select * from users") user.each {|u| p u.class } HogeUser FugaUser
typeをselectからなくして、Fooクラスでfind_by_sqlすれば、Fooクラスのインスタンスになります。
% script/console user = Foo.find_by_sql("select id, name from users") user.each {|u| p u.class } Foo Foo
もし、HogeUserクラス、FugaUserクラスが存在しない場合。ActiveRecord::SubclassNotFound が出ます。
% script/console >> User.find_by_sql("select * from users") User Load (0.000236) select * from users ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'HogeUser'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite User.inheritance_column to use another column for that information. from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:897:in `instantiate_without_callbacks' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/callbacks.rb:215:in `instantiate' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:431:in `find_by_sql' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:431:in `collect!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:431:in `find_by_sql' from (irb):1
activerecord-2.3.11/lib/active_record/base.rb の実行結果
typeを見て、HogeUser、FugaUserのインスタンスになります。
% script/console >> user = User.find_by_sql("select * from users") >> user.each {|u| p u.class } HogeUser(id: integer, type: string, name: string) FugaUser(id: integer, type: string, name: string)
type カラムを select から抜くと STI の効果はなくなります。
% script/console user = Foo.find_by_sql("select * from users") user.each {|u| p u.class } User(id: integer, type: string, name: string) User(id: integer, type: string, name: string)
Fooクラスを経由すると、Fooクラスとなります。ここは、activerecord-1.13.2とは違う挙動です。むしろ、find_by_sql がより直感的な挙動になったと思います。
% script/console user = Foo.find_by_sql("select * from users") user.each {|u| p u.class } Foo(id: integer, title: string) Foo(id: integer, title: string)
もし、HogeUserクラスとFugaUserクラスがActiveRecord::Baseを継承していた場合は hoge_users(fuga_users)テーブルがないとエラーが出ます。(activerecord-1.13.2ではこのエラーは出ません)
% script/console >> User.find_by_sql("select * from users") SQL (0.1ms) SET SQL_AUTO_IS_NULL=0 User Load (0.1ms) select * from users User Columns (0.2ms) SHOW FIELDS FROM `users` HogeUser Columns (0.0ms) Mysql::Error: Table 'find_by_sql.hoge_users' doesn't exist: SHOW FIELDS FROM `hoge_users` ActiveRecord::StatementInvalid: Mysql::Error: Table 'find_by_sql.hoge_users' doesn't exist: SHOW FIELDS FROM `hoge_users` from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `log' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/connection_adapters/mysql_adapter.rb:324:in `execute' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/connection_adapters/mysql_adapter.rb:471:in `columns' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:1305:in `columns' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:1318:in `column_names' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:2899:in `inspect' from /usr/local/lib/ruby/gems/1.8/gems/wirble-0.1.3/lib/wirble.rb:432:in `output_value' from /usr/local/lib/ruby/1.8/irb.rb:151:in `eval_input' from /usr/local/lib/ruby/1.8/irb.rb:263:in `signal_status' from /usr/local/lib/ruby/1.8/irb.rb:147:in `eval_input' from /usr/local/lib/ruby/1.8/irb.rb:146:in `eval_input' from /usr/local/lib/ruby/1.8/irb.rb:70:in `start' from /usr/local/lib/ruby/1.8/irb.rb:69:in `catch' from /usr/local/lib/ruby/1.8/irb.rb:69:in `start' from /usr/local/bin/irb:13
もし、HogeUserクラス、FugaUserクラスが存在しない場合。ActiveRecord::SubclassNotFound が出ます。
>> User.find_by_sql("select * from users") SQL (0.5ms) SET SQL_AUTO_IS_NULL=0 User Load (0.4ms) select * from users User Columns (0.3ms) SHOW FIELDS FROM `users` ActiveRecord::SubclassNotFound: The single-table inheritance mechanism failed to locate the subclass: 'HogeUser'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite User.inheritance_column to use another column for that information. from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:1671:in `instantiate' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:665:in `find_by_sql' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:665:in `collect!' from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.11/lib/active_record/base.rb:665:in `find_by_sql' from (irb):1
Rails2系でuse_transactional_fixturesを使うときの注意
テストは正しいと思うのに、何故か落ちる現象になりました。調べていたら、MySQLのMyISAMテーブルで use_transactional_fixtures が true にしていたことが原因であることが分かりました。
activerecord-2.3.10/lib/active_record/fixtures.rb
use_transactional_fixture についてARには以下のように書かれていました。トランザクションが使えないストレージエンジンではuse_transactional_fixturesをサポートしていないのでfalseにしましょうと書かれています。
211 # When *not* to use transactional fixtures: 212 # 213 # 1. You're testing whether a transaction works correctly. Nested transactions don't commit until all parent transactions commit, 214 # particularly, the fixtures transaction which is begun in setup and rolled back in teardown. Thus, you won't be able to verify 215 # the results of your transaction until Active Record supports nested transactions or savepoints (in progress). 216 # 2. Your database does not support transactions. Every Active Record database supports transactions except MySQL MyISAM. 217 # Use InnoDB, MaxDB, or NDB instead.
まとめ
RELEASE SAVEPOINTの問題の時もそうでしたが、Rails2はトランザクションが使えるDBを推奨して作られている感じがします。