2015年6月29日月曜日

gulp.jsでreactjsのuglifyでエラー出たときの対応

gulp.jsでreact.jsで書いたものを圧縮しようとしたときにエラーが出たときの対応です。

まず以下のようなgulpfile.jsを書きました。
var gulp = require('gulp');
var browserify = require('browserify');
var source = require("vinyl-source-stream");
var reactify = require('reactify');
var watchify = require('watchify');
var sass = require('gulp-sass');
var buffer = require('vinyl-buffer');
var plumber = require('gulp-plumber');
var uglify = require('gulp-uglify');
 
gulp.task('browserify', function(){
  var b = browserify({
    entries: ['./src/test.jsx'],
    transform: [reactify]
  });
  return b.bundle()
    .pipe(source('test.js'))
    .pipe(uglify())
    .pipe(gulp.dest('./dist'));
});

それをbrowserifyしてみたところ、エラーになりました。
$ gulp browserify
[10:26:15] Using gulpfile ~/Documents/github/test/gulpfile.js
[10:26:15] Starting 'browserify'...
 
events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: /Users/xxxx/Documents/github/test/test.js: Streaming not supported

調べたところ、gulp-streamifyを使うとよいようです。
・インストール
$ npm install --save-dev gulp-streamify
 
・requireを追加
var streamify = require('gulp-streamify');

・uglify()をstreamify(uglify())に変更
gulp.task('browserify', function(){
  var b = browserify({
    entries: ['./src/test.jsx'],
    transform: [reactify]
  });
  return b.bundle()
    .pipe(source('test.js'))
    .pipe(streamify(uglify()))
    .pipe(gulp.dest('./dist'));
});

修正して実行したらエラー出ませんでした。
$ gulp browserify
[10:29:52] Using gulpfile ~/Documents/github/test/gulpfile.js
[10:29:52] Starting 'browserify'...
[10:29:56] Finished 'browserify' after 3.85 s

参考URL
http://yebisupress.dac.co.jp/2015/02/26/gulp-js%E3%81%A7%E5%BA%83%E5%91%8A%E3%82%BF%E3%82%B0%E3%81%AE%E9%96%8B%E7%99%BA%E7%92%B0%E5%A2%83%E3%82%92%E6%95%B4%E3%81%88%E3%82%8B/
https://github.com/hughsk/vinyl-source-stream/issues/1

2015年6月22日月曜日

watchifyでreactjsで書いたものがエラーになる

reactjsで開発することになったので、watchifyで楽しようとしたらエラー出たときの対応です。

watchifyしたときのエラーはこんな感じ
$ watchify src/test.jsx -o dist/test.js -v
Error: Parsing file /Users/xxxx/Documents/github/module/src/test.jsx: Unexpected token (196:8)
196行目は以下の<div>のところ
    render: function(){
      return(
        <div className="item">
調べたところ、-tでモジュールを指定する必要があるようです。
以下の形だとエラー出ませんでした。
$ watchify -t reactify src/test.jsx -o dist/test.js -v
640293 bytes written to dist/test.js (2.10 seconds)

参考URL
http://qiita.com/masato/items/35b0900e3a7282b33bf8

2015年6月15日月曜日

herokuでrake db:migrate:resetするとエラーになる

herokuでdbのmigrateを再実行したかったので、
db:migrate:resetを実行したらエラーになったときの対応です。

実行してみたときのエラーはこんな感じ
$ heroku run rake db:migrate:reset
Running `rake db:migrate:reset` attached to terminal... up, run.7795
FATAL:  permission denied for database "postgres"
DETAIL:  User does not have CONNECT privilege.
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:567:in `retrieve_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:87:in `connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `connection'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:30:in `drop'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:114:in `drop'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:128:in `block in drop_current'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:274:in `each'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:127:in `drop_current'
/app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:28:in `block (2 levels) in '
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:240:in `call'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:235:in `each'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:235:in `execute'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:199:in `each'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:165:in `invoke'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `each'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:100:in `top_level'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:78:in `block in run'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/app/bin/rake:8:in `
' Couldn't drop db_table FATAL: permission denied for database "postgres" DETAIL: User does not have CONNECT privilege. /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `initialize' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `new' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `new' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/postgresql_adapter.rb:44:in `postgresql_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:567:in `retrieve_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:113:in `retrieve_connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_handling.rb:87:in `connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:8:in `connection' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/postgresql_database_tasks.rb:17:in `create' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:93:in `create' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:107:in `block in create_current' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:275:in `block in each_current_configuration' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:274:in `each' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:274:in `each_current_configuration' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:106:in `create_current' /app/vendor/bundle/ruby/2.2.0/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:17:in `block (2 levels) in ' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:240:in `call' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:240:in `block in execute' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:235:in `each' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:235:in `execute' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:179:in `block in invoke_with_call_chain' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:201:in `block in invoke_prerequisites' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:199:in `each' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:199:in `invoke_prerequisites' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:178:in `block in invoke_with_call_chain' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:172:in `invoke_with_call_chain' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/task.rb:165:in `invoke' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:150:in `invoke_task' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `block (2 levels) in top_level' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `each' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:106:in `block in top_level' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:115:in `run_with_threads' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:100:in `top_level' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:78:in `block in run' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling' /app/vendor/ruby-2.2.2/lib/ruby/2.2.0/rake/application.rb:75:in `run' /app/bin/rake:8:in `
' Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "pool"=>5, "database"=>"db_table", "username"=>"user", "password"=>"password", "port"=>5432, "host"=>"hostname"}
調べてみたところ、DBを書き換えるようなコマンドはpermissionエラーになるようになっているようです。
rakeタスクでやらずにpostgresqlを直接操作して削除し、
それからrake db:migrateでやるとよいようです。
$ heroku pg:reset DATABASE
 
 !    WARNING: Destructive Action
 !    This command will affect the app: heroku-host
 !    To proceed, type "heroku-host" or re-run this command with --confirm heroku-host
 
> heroku-host
Resetting DATABASE_URL... done

$ heroku run rake db:migrate


参考URL
http://blog.aripei.com/2013/10/rails-operation-in-heoku.html

2015年6月8日月曜日

ActiveRecordにattr_accessorで定義したものがto_jsonで含まれない

何も考えずにActiveRecordでto_jsonやっていたら、
attr_accessorで定義したものがJSONに含まれてなかったので、
そのときに調べたことを書いていきます。

クラスはこんな感じ
# == Schema Information
#
# Table name: items
#
#  id         :integer          not null, primary key
#  name       :string
#  img        :string
#  price      :integer
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
 
class Item < ActiveRecord::Base
  attr_accessor :view_img_url
end

item = Item.new
item.name = 'なまえ'
item.img = 'test.jpg'
item.view_img_url = 'http://aaa.bbb/ccc/ddd/test.jpg'
item.price = 100
 
p item.to_json
JSONにview_img_urlが含まれていませんでした。
view_img_urlを含むためにはto_jsonの引数にmethods: :view_img_urlを与える必要があります。
p item.to_json(methods: :view_img_url)
これでJSONにview_img_urlが含まれるようになりました。

2015年6月1日月曜日

postgresqlでadd_columnのafterが効かない

herokuにrailsアプリを載せるために開発していたのですが、
カラムを追加したいとなり、どうせならきれいな位置に配置したいなと思い、
afterを使ってカラムの位置を指定してみたところ、
指定した位置に追加されなかったので調べたときの件です。

こういうテーブルをもともと用意していました。
class CreateItems < ActiveRecord::Migration
  def change
    create_table :items do |t|
      t.string :name
      t.string :img
      t.integer :price
 
      t.timestamps null: false
    end
  end
end

カラムを追加するために以下のコードを追加しました。
class AddColumnToItem < ActiveRecord::Migration
  def change
    add_column :items, :stock, :integer, after: :price
  end
end

追加後にpostgresqlのテーブル定義を確認したところ、
stockがpriceの後ろではなく、テーブルの一番最後のカラムに追加されてしまいました。
$ psql test
psql (9.4.1)
Type "help" for help.
 
test=# \d items
                                     Table "public.items"
   Column   |            Type             |                     Modifiers                      
------------+-----------------------------+----------------------------------------------------
 id         | integer                     | not null default nextval('items_id_seq'::regclass)
 name       | character varying           | 
 img        | character varying           | 
 price      | integer                     | 
 created_at | timestamp without time zone | not null
 updated_at | timestamp without time zone | not null
 stock      | integer                     | 
Indexes:
    "items_pkey" PRIMARY KEY, btree (id)


調べてみたところ、
postgresqlではそもそも列の位置を変えることができないようです。
列の位置を変えたい場合はテーブル作り直すか、
最終的なレイアウトになるまでデータを移す作業をしていくしかないようです。
これに関してはMySQLの方が柔軟でよいですね。
結局、まだ開発中のものだったので、初めからstockをpriceの後に入れたものでテーブルを作り直しました。


http://qiita.com/masamitsu-konya/items/c4f75fc02347de9f8b0f
http://stackoverflow.com/questions/27214251/postgresql-add-column-after-option-usage-in-rails-migration
http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_column#637-Options
http://dba.stackexchange.com/questions/3276/how-can-i-specify-the-position-for-a-new-column-in-postgresql
https://wiki.postgresql.org/wiki/Alter_column_position/ja