LoginSignup
19
13

More than 5 years have passed since last update.

MySQLでDUMPしたテーブル定義をConfluence Wiki記法やMarkdown記法にするワンライナー

Posted at

MySQL で DUMP したテーブル定義を Confluence に簡単に貼り付けたい。そんなことあるでしょう。
MySQL で DUMP したテーブル定義を Qiita に簡単に貼り付けたい。そんなことあるでしょう。

納品するドキュメントで、DB のテーブル定義書をチマチマ書くのがかったるい。こういう風に書いてといちいち指示するのもかったるい。全く生産的ではないので、一発で終わらせたい。そんな人向け。

目標

+--------------------+------------------+-----------------+------+-----+---------+-----------------------------+---------------------------------+--------------------------------------------------------+
| Field              | Type             | Collation       | Null | Key | Default | Extra                       | Privileges                      | Comment                                                |
+--------------------+------------------+-----------------+------+-----+---------+-----------------------------+---------------------------------+--------------------------------------------------------+
| id                 | int(10) unsigned | NULL            | NO   | PRI | NULL    | auto_increment              | select,insert,update,references | ID                                                     |
| name               | varchar(256)     | utf8_general_ci | YES  |     | NULL    |                             | select,insert,update,references | 名称                                                   |
+--------------------+------------------+-----------------+------+-----+---------+-----------------------------+---------------------------------+--------------------------------------------------------+

これを
table.png

こう。

結論のワンライナー

Confluence 用(Confluence Wiki)

$ mysql -h (host) -u (user) -p (schema) -e 'show full columns from (table)' | sed 's/^/|/g;s/$/|/g;s/\t/ | /g;1 s/|/||/g'

Qiita 用(Markdown)

$ mysql -h (host) -u (user) -p (schema) -e 'show full columns from (table)' | sed -E "s/^/\| /g;s/$/ \|/g;s/\t/ \| /g;1{h; p; s/\w+/:-:/g}"

方針

MySQL の結果を標準出力に出して、sed でゴニョゴニョしてコピペ。

表の書式を確認

header1 header2
cel1 cel2

こういう表を作りたい。

Confluence

Confluence では、表の書式は、以下らしい。他にもあるけど、今回はいらないので、これで。

|| header1 || header2 ||
| cel1 | cel2 |

なお、空欄はスペース入れないとずれるので注意。
Insert Markup > Confluence Wiki
でこれを書くと、表として入る。

Insert Markup > Markdown で Markdown 記法の表が入らない理由は、謎。

Markdown

Qiita では、Markdown 記法なので、こんな感じ

| header1 | header2 |
|:-:|:-:|
| cel1 | cel2 |

+ で始まる行を削除して、1行目の | を || に変えれば良いんじゃない?もしかして楽勝??

やってみる

今回の実行環境

AWS 上の AmazonLinux サーバーから AWS の RDS に立てた MySQL にアクセスしています。

item version
OS Amazon Linux 4.9.77-31.58.amzn1.x86_64
MySQL 5.6.35-log
MySQL client Ver 14.14 Distrib 5.5.59

MySQL のテーブル定義を取得

mysql> show full column from (table);

MySQL のテーブル定義を標準出力に出す

$ mysql -h (host) -u (user) -p (schema) -e 'show full columns from (table)' 

パスワードを求められるので、入れると、テーブル定義が出る。

こんなにいらないという場合は、実行する SQL 文を

mysql> select column_name, column_type, is_nullable, column_key, column_default, extra, column_comment from information_schema.columns where table_schema='(schema)' and table_name='(table)';

こんな感じでお好きに。以降の手順は変わりません。名前とかは好きに整形してください。

sed で置換

パイプでつないで、sed で置換していく。が、やってみたら、 +----+ とか | が消えてて、豪快に空振り。
仕方がないので、コツコツやっていく。sed は、あんまり得意じゃないので、誰かもっとエレガントに書いてください。

| で挟む

行頭・行末・間のタブを | に置換すれば良い。

sed 's/^/|/g;s/$/|/g;s/\t/ | /g"

タブを置換するときに、スペースで挟んだのは、値が何も入っていないときに、Confluence でうまく表示できないから。

|Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment|
|id | int(10) unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | ID|
|name | varchar(256) | utf8_general_ci | YES |  | NULL |  | select,insert,update,references | 名称|

これをベースに、Confluence 用と Markdown 用に加工する。

ヘッダー行の | を2重にする

sed 's/^/|/g;s/$/|/g;s/\t/ | /g;1 s/|/||/g'
||Field || Type || Collation || Null || Key || Default || Extra || Privileges || Comment||
|id | int(10) unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | ID|
|name | varchar(256) | utf8_general_ci | YES |  | NULL |  | select,insert,update,references | 名称|

あれ、できました。簡単でしたね。

Confluence 用のワンライナー

$ mysql -h (host) -u (user) -p (schema) -e 'show full columns from (table)' | sed 's/^/|/g;s/$/|/g;s/\t/ | /g;1 s/|/||/g'

insert_markup.png
markup.png

つぎ、Markdown

1行目を複製して、| 以外の連続する文字列を :-: に置換する

正規表現を有効にして、

sed -E "1{h; p; s/\w+/:-:/g}"

こうすれば、できますね。

| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
| :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: | :-: |
| id | int(10) unsigned | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | ID |
| name | varchar(256) | utf8_general_ci | YES |  | NULL |  | select,insert,update,references | 名称 |

どーん。

Field Type Collation Null Key Default Extra Privileges Comment
id int(10) unsigned NULL NO PRI NULL auto_increment select,insert,update,references ID
name varchar(256) utf8_general_ci YES NULL select,insert,update,references 名称

Markdown 用のワンライナー

$ mysql -h (host) -u (user) -p (schema) -e 'show full columns from (table)' | sed -E "s/^/\| /g;s/$/ \|/g;s/\t/ \| /g;1{h; p; s/\w+/:-:/g}"

Confluence 用のものから、正規表現を有効にしたので、| をエスケープしたのと、挿入したらペーストしたものは完全になくなってしまう Confluence と違って、Markdown でも美しくありたいので、行頭・行末の | の前後にスペース付けました。

(おまけ)クリップボードにコピーまでワンライナーでやりたい。

Linux は、xsel をインストールして上記のワンライナーに続けてください。

.... | xsel --clipboard --input

Mac は、pbcopy を使ってください。

.... | pbcopy

Windows? なんかあるんじゃないですか?きっと。

今回の環境では、RDS はサーバー上からしかアクセス出来ないように制限していて、余計なものをインストールしたくなかったので、コピーは手動にしました。

19
13
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
13