Forked from tlikai/export_mysql_table_schema_to_markdown_table.php
Last active
December 16, 2019 05:11
-
-
Save bantya/dd680523dfe3cf8019f41ea2075ed5f8 to your computer and use it in GitHub Desktop.
export mysql table schema to markdown table
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=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 "| Field name | Type | Is Empty | Default | Index | Remarks |\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