PostgreSQLでtimestamp型のデフォルト値を現在時刻にするには
ドットインストールのPostgreSQL入門を勉強中に気づいた事をメモしておく。
「#05 フィールドに制約をつけてみよう」でtimestamp型のデフォルト値を現在時刻にするために以下のように指定していた。
create table posts ( id serial primary key, title varchar(255) not null, body text check(length(body) > 5), is_draft boolean default TRUE, created timestamp default 'now' ★★★ );
しかし、これだとテーブル作成時の時刻がデフォルト値になってしまう。
データをInsertした際の現在時刻をデフォルト値とする場合には以下のように指定する必要がある。
create table posts ( id serial primary key, title varchar(255) not null, body text check(length(body) > 5), is_draft boolean default TRUE, created timestamp default CURRENT_TIMESTAMP ★★★ );
【参考記事】
List of unit testing frameworks for Java
A programmer-oriented testing framework for Java
A Mocking framework that tastes really good
A JUnit extension to perform unit testing with database-driven programs
A Java logging API by means of a simple facade pattern (DbUnit uses SLF4J for logging purposes.).
A relational database management system written in Java
HUBOTのTips(警告"@http() is going to be deprecated in 3.0.0 use @robot.http()"への対処)
HUBOTに最初から含まれているサンプルのスクリプト(math.coffee、pugme.coffee、youtube.coffee等)を利用すると、以下のような警告が出るケースがある。
Hubot> hubot math me 1 + 2 Hubot> [Mon Jul 15 2013 00:22:55 GMT+0900 (JST)] WARNING @http() is going to be deprecated in 3.0.0 use @robot.http() 3 Hubot>
警告が出ている理由はResponseクラスのhttpメソッドを利用しているためである(ResponseクラスのhttpメソッドはHUBOTのバージョン3.0.0で廃止予定と言っている)。
実際に警告を出している場所は以下(response.coffee)。
class Response ... snip ... http: (url) -> @robot.logger.warning '@http() is going to be deprecated in 3.0.0 use @robot.http()' @robot.http(url) module.exports = Response
この警告を消すにはResponseクラスのhttpメソッドを利用する変わりに、Robotクラスのhttpメソッドを利用すれば良い。
具体的には以下のようにmath.coffeeを修正する。
module.exports = (robot) -> robot.respond /(calc|calculate|convert|math|maths)( me)? (.*)/i, (msg) -> # msg.httpの変わりにrobot.httpを使う # msg robot .http('https://www.google.com/ig/calculator') .query hl: 'en' q: msg.match[3] ... snip ...
【備考】
ver 2.6.0以降ではyoutube.coffeeは@robot.http()を利用するように修正されている。また、Responseクラスも警告メッセージを出さないよう修正されている。
How to use Hubot behind a proxy.
You may fail to run a bot which uses http connection with Hubot if you are behind a proxy such as Squid.
Hubot behind a proxy · Issue #287 · github/hubot · GitHub
For example, you'll have following type of error messages when you run "hubot youtube me [search key words]".
events.js:72 throw er; // Unhandled 'error' event ^ Error: connect ECONNREFUSED at errnoException (net.js:900:11) at Object.afterConnect [as oncomplete] (net.js:891:19)
In this case, you might be able to solve the issue to specify proxy address and proxy port in node-scoped-http-client which is one of the wrapper libraries of Node.js's HTTP client and used by Hubot.
How to modify the source code (hubot/node_modules/scoped-http-client/src/lib/index.js) is as follows.
port = this.options.port || ScopedClient.defaultPort[this.options.protocol] || 80; req = (this.options.protocol === 'https:' ? https : http).request({ // port: port, port: 3127, // you have to specify a proxy port. // host: this.options.hostname, host: 'localhost', // you have to specify a proxy server address or hostname. method: method, // path: this.fullPath(), path: 'http://' + this.options.hostname + this.fullPath(), headers: headers, agent: this.options.agent || false });
HUBOTのセットアップメモ
Linux環境(Cent OS 6.4)にHUBOTをセットアップした際のメモ(全てrootで実施)。
【事前準備】
HUBOTの前提となるnode.js、CoffeeScriptをインストールしておく(node.jsと一緒にnpmもインストールされる)。
また、Botで扱うデータを永続化するためにredisもインストールしておく(redisはサーバ起動時に自動起動するようにしておく)。
今回使用したバージョンは以下。
# node -v
v0.10.11
# npm -v
1.2.30
# coffee -v
CoffeeScript version 1.6.3
# redis-server -v
Redis server version 2.4.10 (00000000:0)
【Hubotのインストール】
npmでHUBOTをインストールする。具体的には以下のコマンドを実行。なお、以下の作業は全てrootユーザのホームディレクトリで実行した。
# npm install hubot
IRCと連携させるために、以下のコマンドを実行してIRC Adapterもインストール。
なお、本日時点でインストールされたバージョンはhubot 2.5.5とhubot-irc 0.1.15。
# npm install hubot-irc
【redisによるデータ永続化のための作業】
nodeでredisを利用するために、以下のコマンドを実行する。
# npm install redis
更にHUBOTからredisを使えるようにするため、3rd-partyのhubot scriptsをダウンロードする。
そして、hubot-scripts/src/scripts/redis-brain.coffeeを$HOME/node_modules/hubot/src/scripts配下にコピーする。
# npm install hubot-scripts
【Hubotの起動スクリプトを用意】
HUBOT起動用に以下のようなスクリプトを用意。これを$HOME/node_modules/hubot配下に置く。
#!/bin/sh # runhubot export HUBOT_IRC_NICK="hubot" export HUBOT_IRC_ROOMS="#Test" export HUBOT_IRC_SERVER="127.0.0.1" ./bin/hubot -a irc
あとはこのスクリプトを実行すればHUBOTを起動することが出来る。
HUBOT関連技術のメモ
HUBOT | Hubot is your friendly robot sidekick. Install him in your company to dramatically improve employee efficiency.とはGitHub社が開発したOSSのBOTフレームワーク(IRCのBOT等を簡単に作ることが出来る)。なお、HUBOTの詳細を知るには以下の記事が参考になる。
- Hubotとは
- GitHub製のコマンド実行ロボット·HUBOT MOONGIFT
- hubotで快適BOT生活
- github社製ボットフレームワーク、hubotをIRCボットとして導入した話(修正あり - id:anatooのブログ
HUBOTの関連技術の主な物は以下。
1. CoffeeScript
CoffeeScriptはプログラミング言語の一つで、JavaScriptをよりわかりやすく、簡潔に記述するための言語。
HUBOT自体がこのCoffeeScriptで書かれている。また、HUBOTを利用したBOTもCoffeeScriptで記述可能(JavaScriptでも書ける)。
なお、CoffeeScriptはドットインストールで勉強できる(http://dotinstall.com/lessons/basic_coffeescript)。
参考記事は以下。
2. Node.js
Node.jsはOSSでサーバーサイドのJavaScriptインタープリタ。Node.jsを使うことで、サーバサイドのアプリケーションの実装言語として、JavaScriptを使用出来る(Node.js自体はC++で実装されている)。HUBOTもこのNode.jsを利用している。
参考記事は以下。
- サーバサイドJavaScriptの本命「node.js」の基礎知識 (1/3):node.jsでサーバサイドJavaScript開発入門(1) - @IT
- 【ビセラの最安値情報】Amazon・楽天・メルカリ・公式サイトを値段比較
3. Redis
RedisはOSSでKVS型のインメモリデータベース(Cで実装されている)。HUBOTで作ったBOTのデータを永続化する場合(HUBOTを再起動してもデータを利用可能にする等)に必要。
なお、Redisもドットインストールで勉強できる(http://dotinstall.com/lessons/basic_redis)。
参考記事は以下。
4. CampFire
CampFireは企業向けのコラボレーションツール。HUBOTは元々このCampFire用のボット作成ツールとして作られたそう。
CampFireを開発しているのは、プロジェクト管理ツールBasecamp等も作っている37signals。
【その他参考記事】