Skip to content

Instantly share code, notes, and snippets.

@tlikai
Last active October 1, 2020 01:07
Show Gist options
  • Save tlikai/0588b127ea432b7d48b1cafd76752723 to your computer and use it in GitHub Desktop.
Save tlikai/0588b127ea432b7d48b1cafd76752723 to your computer and use it in GitHub Desktop.
export mysql table schema to markdown table
#!/usr/bin/env php
<?php
if (count($argv) == 1) {
throw new InvalidArgumentException('Missing tables');
}
$tables = array_slice($argv, 1);
$db = new PDO('mysql:dbname=uniqueway_development;host=127.0.0.1;charset=utf8mb4', 'root', '');
foreach ($tables as $table) {
$query = $db->query("SHOW FULL COLUMNS FROM $table");
echo "# $table\n";
echo "| 字段名 | 类型 | 是否为空 | 默认值 | 索引 | 备注 |\n";
echo "| --- | --- | --- | --- | --- | --- |\n";
while (($row = $query->fetch(PDO::FETCH_NUM))) {
list($field, $type, , $nullable, $key, $default, $extra, , $comment) = $row;
$comment = str_replace(["\n", '|'], ['<br />', '-'], $comment);
$default = empty($extra) ? $default : $extra;
$nullable = strtolower($nullable);
echo "| $field | $type | $nullable | $default | $key | $comment |\n";
}
echo "\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment