Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bantya/dd680523dfe3cf8019f41ea2075ed5f8 to your computer and use it in GitHub Desktop.
Save bantya/dd680523dfe3cf8019f41ea2075ed5f8 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 "| 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