YII2命令行實現數據庫表結構文檔自動生成

由於要寫關於數據庫表說明的文檔,須要有表結構,若是一個個表去建表格,而後在複製每一個字段進去,那就太沒效率了。其實程序很簡單,用程序獲取數據庫Schema,再寫入到markdown文件(文件格式md)裏就能夠了。由於項目是基於YII2框架,因此用YII2的命令工具來實現。大概的效果圖以下:
圖片描述php

<?php

namespace console\controllers;


use Yii;
use yii\console\Controller;

/**
 * 數據庫表結構文檔自動生成工具
 * Class TableSchemaController
 * @package console\controllers
 * @author wuzhc <wuzhc2016@163.com>
 * @since 2018-01-18
 */
class TableSchemaController extends Controller
{
    /**
     * 數據庫表生成
     */
    public function actionCreate()
    {
        global $argv;

        if (!$argv[2] || strcasecmp($argv[2], 'help') === 0) {
            echo "Usage: ./yii table-schema/create [all|tablename] [filename]\n";
            exit;
        }

        $db = Yii::$app->db;
        $allTables = $db->getSchema()->getTableNames();

        if ('all' === $argv[2]) {
            $tables = array_diff($allTables, $this->filterTables());
        } else {
            if (!in_array($argv[2], $allTables)) {
                echo sprintf("%s isn't exist \n", $argv[2]);
                exit;
            }
            $tables = (array)$argv[2];
        }

        // 當前數據庫沒有表
        if (count(array_filter($tables)) == 0) {
            echo "Database has not table \n";
        }

        $root = dirname(dirname(dirname(__FILE__)));
        $filename = $argv[3] ? $argv[3] : '/docs/note/數據庫設計及字典說明.md';
        $filePath = $root . $filename;

        $fp = fopen($filePath, 'a+');
        if (!$fp) {
            echo "Open file failed \n";
        }

        foreach ($tables as $table) {
            $schema = $db->getTableSchema($table, true);
            if (!$schema->columns) {
                continue;
            }

            fwrite($fp, "#### $schema->name 表 \n");

            // 表頭
            $header = "| 字段名 | 類型 | 說明 | \n";
            $header .= "|:--------:|:---------:|:-------:| \n";
            fwrite($fp, $header);

            // 字段
            $row = '';

            foreach ($schema->columns as $col => $obj) {
                $comment = $obj->isPrimaryKey ? '主鍵' : $obj->comment;
                $row .= "| $obj->name | $obj->dbType | $comment | \n";
            }

            fwrite($fp, $row);
            fwrite($fp, "\r\n\r\n");

            echo "$schema->name successfully \n";
        }

        fclose($fp);
    }

    /**
     * 須要過濾的表(不但願生成文檔的表)
     * @return array
     */
    protected function filterTables()
    {
        $filterTables = [
            'tbmigration',
            'tbAuthAssignment',
            'tbAuthItemChild',
            'tbAuthRule',
            'tbItemTable'
        ];

        return $filterTables;
    }

    /**
     * 全部表
     * @return \string[]
     */
    protected function allTables()
    {
        return Yii::$app->db->getSchema()->getTableNames();
    }

    /**
     * 清空
     */
    public function actionClear()
    {
        global $argv;

        if (!$argv[2] || strcasecmp($argv[2], 'help') === 0) {
            echo "Usage: ./yii table-schema/clear [filename]\n";
            exit;
        }

        $root = dirname(dirname(dirname(__FILE__)));
        $filePath = $argv[2] ? $argv[2] : '/docs/note/數據庫設計及字典說明.md';
        $filePath = $root . $filePath;

        if (!is_file($filePath)) {
            echo "$filePath isn't exists \n";
            exit;
        }

        $fp = fopen($filePath, 'w');
        if (!$fp) {
            echo "Open file failed \n";
        }

        fwrite($fp, '');
        fclose($fp);

        echo "Clear successfully \n";
    }
}

執行命令:數據庫

./yii table-schema/create [all|tablename] [filename] 
# 例如生成全部表到test.md文件 
./yii table-schema/create all test.md
# 生成user表到test.md文件
./yii table-schema/create user test.md
# 清空文檔
./yii table-schema/clear [filename]

程序很簡單,可是很實用~~bash

相關文章
相關標籤/搜索