分享 Description,一个通过分析 ActiveRecord::Migration 文件提取数据库文档的工具

kayakjiang · 2013年08月31日 · 最后由 kamechen 回复于 2013年08月31日 · 2977 次阅读

花了半个多月的时间写了个小工具 Description, Description 能对你的 rails 项目的 ActiveRecord::Migration 文件进行分析,然后得到和项目有关的数据库文档

安装

git clone https://github.com/baya/description.git cd description bundle install

使用

在 description 目录下执行 chmod +x bin/describle bin/describle /path/to/your/rails/app 然后使用浏览器访问 http://localhost:9393

主要功能

1.提取 migration 文件中的 comment,比如你创建一个表 contests,

    class CreateContests < ActiveRecord::Migration
    def change
      create_table :contests do |t|
        t.integer    :itype, default: 0, null: false  # 比赛形式, 0 photo, 1 video, 2 word, 3 audio, 4 组合
        t.integer    :mode, default: 0, null: false   # 是否可加入比赛, 0 pk, 1 调查, 2 多选
        t.integer    :format, default: 0, null: false # 比赛方式, 1 pk, 0 binary(传统比赛)
        t.date       :start_day                       # 比赛开始日期
        t.integer    :length                          # 比赛时长以天为单位
        t.integer    :vote_right                      # 投票权, 0 public, 1 friends only, 2 specific
        t.integer    :contestant_right                # 参赛权, 0 public, 1 friedns only, 2 specific
        t.string     :logo                            # 比赛logo,图片
        t.timestamps
      end
    end
end


然后在后面的开发中有增加字段或者重命名字段,

class AddStatusToContests < ActiveRecord::Migration
   def change
      add_column :contests, :pub_status, :integer, default: 0 # 发布状态, 0 草稿, 1 发布
   end
end

description 会将你的代码解析成一个 markdown 格式的 table,

2.快速检索表和字段 有些项目的表很多,表很大,在 description 中,你可以通过表名和字段名快速找出你想要查看的表和字段

3.提供表的 markdown 文件的下载

结合 mou 等工具你可以很容易制作出十几页的项目文档,从此你就不需要为项目 交接时的文档发愁了

后记

这个小工具完全是为自己的工作需要而开发的,觉得有用 (至少对我自己来说),就分享出来了。 我主要用 activerecord,所以没有提供其他 orm 框架的支持了。

@hooopo 有 star 这个项目,但是一直没有用。我接手过的一些老项目 (没有任何的独立的数据库文档,也没有使用类似 schema_comments 的 gem),就是把 comment 写在 migration 文件里,所以为了能从这些老项目中提取出一些文档,我就写了这样一个小工具

不错,有用👍

需要 登录 后方可回复, 如果你还没有账号请 注册新账号