Created
November 28, 2018 03:15
-
-
Save wowiwj/bd976212b052bf1da39e3c87465bd83c to your computer and use it in GitHub Desktop.
导出数据库的结构为markdown文件脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
if (count($argv) == 1) { | |
throw new InvalidArgumentException('Missing tables'); | |
} | |
$tables = array_slice($argv, 1); | |
$db = new PDO('mysql:dbname=requisition;host=127.0.0.1;charset=utf8mb4', 'root', '123456'); | |
foreach ($tables as $table) { | |
$query = $db->query("SHOW FULL COLUMNS FROM $table"); | |
echo "# $table\n\n"; | |
echo "## TABLE MARKDOWN\n\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"; | |
} | |
echo "### TABLE DDL\n\n"; | |
$query = $db->query("SHOW CREATE TABLE $table"); | |
$table = $query->fetch(PDO::FETCH_ASSOC); | |
echo "\n\n```mysql\n".$table['Create Table']."\n```\n\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment