rails dbconsoleでDBサーバーに接続する

今更感があるけど、rails dbconsole を利用すると、Railsで設定しているDBに、接続できるんですね。

$ rails dbconsole
Enter password: 
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [hogedb_devel]> 

Railsのログをlogrotateする

Railsproduction.log などのログは、際限なく容量が増えるため、古いものから削除するなどの必要があります。これをログのローテーションと言います。

Linuxではlogrotateを使うことが一般的ですが、ローテーション後に、アプリケーションサーバーが新たにログを書き出せなくなる問題があります。これは、logrotateが新しいログファイルにした際に、アプリケーションサーバーが新しいログファイルの検知に失敗するために発生します。

この問題は、次のようにlogrotateのオプションに copytruncate を追加することで解決できます。このオプションは、新たにログファイルを作成するのではなく、ログをコピーして古いログをファイルの中身を空にするものです。

$ cat /etc/logrotate.d/my_rails_app
/var/www/my_rails_app/log/*.log {
  weekly
  missingok
  rotate 280
  compress
  delaycompress
  create 0664 ec2-user ec2-user 
  copytruncate
}

参考

copytruncate – Copy the log file and then empties it. This makes sure that the log file Rails is writing to always exists so you won’t get problems because the file does not actually change. If you don’t use this, you would need to restart your Rails application each time.

gorails.com

補足

ログのローテーション後に、pumaというアプリケーションサーバーに次のようなシグナルを送り、ログのリオープンを指示したが、うまく行きませんでした。

$ kill -HUP $puma_pid 

tech.basicinc.jp

MySQLの「0000-00-00 00:00:00」のデータを更新させたい。

MySQLのDATE/DATETIME型には、利用しないほうが良い「0000-00-00 00:00:00」という値がある。 詳細は他の方に譲るとして、今回はこのあたりをUPDATEしたいと思う。

mysql> UPDATE buildings set created_at = '2004-01-29 00:00:00' where created_at = '0000-00-00 00:00:00';
ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1

エラー出て更新できない。

mysql> select id from buildings where created_at = '0000-00-00 00:00:00';

created_atが '0000-00-00 00:00:00'になっているデータがヒットする。

mysql> UPDATE buildings set created_at = '2004-01-29 00:00:00' where id in (select id from buildings where created_at = '0000-00-00 00:00:00');
ERROR 1292 (22007): Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1

サブクエリでヒットさせようとしたけど失敗。

UPDATE buildings set created_at = '2004-01-29 00:00:00' where CAST(created_at AS CHAR(20)) = '0000-00-00 00:00:00';

StackOverFlowで検索したらこれならできるらしい。めんどくさ。

マージ済みのリモートブランチを一括削除

長年Gitを利用していると、消し忘れたブランチが増えてくるため、一括削除するためのコマンドを紹介する。

マージ済みのリモートブランチを一括削除する

$ git branch -r --merged マージ先ブランチ名 |  egrep -v "(^\*|除外するブランチ名)"  | sed -e 's/origin\///g' | xargs -i git push -d origin {} # 
$ git branch -r --merged origin/develop |  egrep -v "(^\*|master|staging|develop)"  | sed -e 's/origin\///g' | xargs -i git push -d origin {} # 具体例

リモートで消されたブランチをローカルでも削除する

$ git fetch --prune

UbuntuでMacのopenみたいなコマンドを利用する

Macで、open . とかでよくFinderを開いていた。xdg-open コマンドが該当するようだ。

.bash_profile に次のように記載する。

alias open='xdg-open'

URLを指定すればブラウザも開けるのかい。

open http://google.co.jp