LoginSignup
1
1

More than 1 year has passed since last update.

【python】テーブル定義を Backlog 記法の Markdown 形式で出力するスクリプトを作った

Posted at

MySQL のテーブル定義を backlog の wiki にまとめる作業が発生したのですが、テーブル数が数百もあり1つずつ手作業でまとめていくのは大変なので、自動的に backlog 記法形式でテーブル定義を作成する python スクリプトを作成しました。

準備

ツールは GitHub にあがっていますので、 clone します。
https://github.com/kiyo27/export-table-definition

また、dockerを使用しているので、開発マシンに docker がインストールされている必要があります。

テーブル定義をエクスポート

docker runコマンドでdocker コンテナを起動します。コンテナ起動時にエクスポート対象のデータベース情報を環境変数で渡します。

  • DATABASE: データベース名
  • USER: ユーザー名
  • PASSWORD: パスワード
  • PORT: ポート番号
docker run -e DATABASE=blog -e USER=root -e PASSWORD=root -e PORT=3306 -v ${PWD}/tsv:/tmp -v ${PWD}/script:/script mysql:5.7 sh /script/script.sh

コンテナを起動すると、引数に渡したスクリプトが実行されてtsvディレクトリにテーブル定義とインデックス情報が tsv 形式で出力されます。

.
├── README.md
├── definition.md
├── script
│   ├── list.txt
│   └── script.sh
├── sql-md.py
└── tsv
    ├── articles_desc.tsv
    ├── articles_index.tsv
    ├── articles_tags_desc.tsv
    ├── articles_tags_index.tsv
    ├── tags_desc.tsv
    ├── tags_index.tsv
    ├── users_desc.tsv
    └── users_index.tsv

Backlog 記法の Markdown ファイル作成

tsv ディレクトリにファイルが出力されたら、python スクリプトを実行して Backlog 記法の Markdown ファイルを作成します。

docker run -v ${PWD}:/app python:alpine3.10 python3 /app/sql-md.py

ルートディレクトリに definition.md というファイルが作成されて、Backlog 記法の Markdown で書かれたテーブル定義が出力されています。

** articles
''テーブル定義''
|Field|Type|Null|Key|Default|Extra|h
|id|int(11)|NO|PRI|NULL|auto_increment|
|user_id|int(11)|NO|MUL|NULL||
|title|varchar(255)|NO||NULL||
|slug|varchar(191)|NO|UNI|NULL||
|body|text|YES||NULL||
|published|tinyint(1)|YES||0||
|created|datetime|YES||NULL||
|modified|datetime|YES||NULL||
''インデックス''
|Table|Non_unique|Key_name|Seq_in_index|Column_name|Collation|Cardinality|Sub_part|Packed|Null|Index_type|Comment|Index_comment|h
|articles|0|PRIMARY|1|id|A|0|NULL|NULL||BTREE|||
|articles|0|slug|1|slug|A|0|NULL|NULL||BTREE|||
|articles|1|user_key|1|user_id|A|0|NULL|NULL||BTREE|||

** articles_tags
''テーブル定義''
|Field|Type|Null|Key|Default|Extra|h
|article_id|int(11)|NO|PRI|NULL||
|tag_id|int(11)|NO|PRI|NULL||
''インデックス''
|Table|Non_unique|Key_name|Seq_in_index|Column_name|Collation|Cardinality|Sub_part|Packed|Null|Index_type|Comment|Index_comment|h
|articles_tags|0|PRIMARY|1|article_id|A|0|NULL|NULL||BTREE|||
|articles_tags|0|PRIMARY|2|tag_id|A|0|NULL|NULL||BTREE|||
|articles_tags|1|tag_key|1|tag_id|A|0|NULL|NULL||BTREE|||

** tags
''テーブル定義''
|Field|Type|Null|Key|Default|Extra|h
|id|int(11)|NO|PRI|NULL|auto_increment|
|title|varchar(191)|YES|UNI|NULL||
|created|datetime|YES||NULL||
|modified|datetime|YES||NULL||
''インデックス''
|Table|Non_unique|Key_name|Seq_in_index|Column_name|Collation|Cardinality|Sub_part|Packed|Null|Index_type|Comment|Index_comment|h
|tags|0|PRIMARY|1|id|A|0|NULL|NULL||BTREE|||
|tags|0|title|1|title|A|0|NULL|NULL|YES|BTREE|||

** users
''テーブル定義''
|Field|Type|Null|Key|Default|Extra|h
|id|int(11)|NO|PRI|NULL|auto_increment|
|email|varchar(255)|NO||NULL||
|password|varchar(255)|NO||NULL||
|created|datetime|YES||NULL||
|modified|datetime|YES||NULL||
''インデックス''
|Table|Non_unique|Key_name|Seq_in_index|Column_name|Collation|Cardinality|Sub_part|Packed|Null|Index_type|Comment|Index_comment|h
|users|0|PRIMARY|1|id|A|0|NULL|NULL||BTREE|||

あとは、このファイルの中身をコピーして wiki に貼り付けるだけです。

backlog.png

1
1
0

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
1
1