{"id":282,"date":"2016-07-07T11:28:27","date_gmt":"2016-07-07T02:28:27","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/07\/07\/rails_auto_gen_db_schema"},"modified":"2022-10-28T21:32:40","modified_gmt":"2022-10-28T12:32:40","slug":"rails_auto_gen_db_schema","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/07\/07\/rails_auto_gen_db_schema\/","title":{"rendered":"Rails\u3067DB\u5b9a\u7fa9\u66f8\u3092\u81ea\u52d5\u751f\u6210"},"content":{"rendered":"

\u3068\u3042\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u66f8\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u5909\u66f4\u304c\u3042\u308b\u5ea6\u306b\u624b\u52d5\u3067\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4fee\u6b63\u3059\u308b\u306e\u306f\u5927\u5909\u9762\u5012\u306a\u305f\u3081\u3001\u81ea\u52d5\u751f\u6210\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002
\nRails \u306e migration \u3092\u4f7f\u3063\u3066\u7ba1\u7406\u3057\u3066\u3044\u308b\u30c6\u30fc\u30d6\u30eb\u7fa4(MySQL)\u306e\u5b9a\u7fa9\u66f8\u3092 PDF \u3067\u51fa\u529b\u3059\u308b\u307e\u3067\u306e\u30d5\u30ed\u30fc\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002
\n\u3053\u306e\u8a18\u4e8b\u306e\u901a\u308a\u306b\u4f5c\u6210\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u69d8\u5f0f\u306e\u5b9a\u7fa9\u66f8\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u304a\u597d\u304d\u306a\u3088\u3046\u306b\u30c7\u30b6\u30a4\u30f3\u3092\u5909\u66f4\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u307e\u3059\u3002\u8a73\u7d30\u306f\u5f8c\u8ff0\u3002
\n
\n\"\"<\/p>\n

\u5168\u4f53\u306e\u6d41\u308c<\/h2>\n
    \n
  1. rake db:migrate<\/code> \u3067\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\/\u5909\u66f4\u3059\u308b<\/li>\n
  2. DB\u306e\u30b9\u30ad\u30fc\u30de\u69cb\u9020\u3092 XML \u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b<\/li>\n
  3. XML \u3092 HTML \u306b\u5909\u63db\u3059\u308b<\/li>\n
  4. HTML \u3092 PDF \u306b\u5909\u63db\u3059\u308b<\/li>\n<\/ol>\n

    \u306e\u5927\u304d\u304f4\u3064\u306e\u624b\u9806\u3067\u5b9a\u7fa9\u66f8\u3092\u51fa\u529b\u3057\u307e\u3059\u3002<\/p>\n

    1. rake db:migrate \u3067\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\/\u5909\u66f4\u3059\u308b<\/h3>\n

    \u3053\u306e\u90e8\u5206\u306f\u666e\u6bb5\u306e\u901a\u308a\u306b migration \u30d5\u30a1\u30a4\u30eb\u3092\u8a18\u8ff0\u3059\u308b\u3060\u3051\u306a\u306e\u3067\u3059\u304c\u3001\u4e0a\u90e8\u30b5\u30f3\u30d7\u30eb\u306e\u5b9a\u7fa9\u66f8\u306b\u3042\u308b\u3088\u3046\u306b\u30ab\u30e9\u30e0\u306e\u8ad6\u7406\u540d\u304c\u5fc5\u8981\u306a\u5834\u5408\u306b\u306f\u3001 migration_comments<\/a> \u3068\u3044\u3046 gem \u3092\u4f7f\u3044\u307e\u3059\u3002
    \n\u4ee5\u4e0b\u306e\u3088\u3046\u306b migration \u30d5\u30a1\u30a4\u30eb\u3092\u66f8\u304f\u3068\u8ad6\u7406\u540d\u3082\u8a18\u8ff0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n

    class CreateUser < ActiveRecord::Migration\n  def change\n    create_table :users do |t|\n      t.string :name,     null: false, default: 'No Name', comment: '\u30e6\u30fc\u30b6\u540d'\n      t.string :email,    null: false,                     comment: '\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9'\n      t.string :password, null: false,                     comment: '\u30d1\u30b9\u30ef\u30fc\u30c9'\n      t.integer :age,                                      comment: '\u5e74\u9f62'\n\n      t.timestamps null: false\n    end\n    add_index :users, :email, unique: true\n    set_table_comment :users, "\u30e6\u30fc\u30b6"\n    set_column_comment :users, :id, "\u30e6\u30fc\u30b6ID"\n  end\nend<\/code><\/pre>\n

    \u30ec\u30b3\u30fc\u30c9ID\u3068\u30c6\u30fc\u30d6\u30eb\u540d\u306f set_table_comment<\/code> \u3084 set_column_comment<\/code> \u3092\u4f7f\u3063\u3066\u5225\u9014\u5b9a\u7fa9\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n

    2. DB\u306e\u30b9\u30ad\u30fc\u30de\u69cb\u9020\u3092 XML \u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b<\/h3>\n

    MySQL \u306e\u5834\u5408\u306b\u306f mysqldump<\/code> \u30b3\u30de\u30f3\u30c9\u306e --no-data --xml<\/code> \u30aa\u30d7\u30b7\u30e7\u30f3\u3067\u30b9\u30ad\u30fc\u30de\u60c5\u5831\u3060\u3051\u3092 XML \u5f62\u5f0f\u3067\u51fa\u529b\u3067\u304d\u308b\u305f\u3081\u3001\u3053\u308c\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n

    3. XML \u3092 HTML \u306b\u5909\u63db\u3059\u308b<\/h3>\n

    xsltproc<\/code> \u3068\u3044\u3046\u30c4\u30fc\u30eb\u3092\u4f7f\u3046\u3068<\/p>\n

    $ xsltproc -o schema.html style.xsl schema.xml<\/code><\/pre>\n

    \u3067 XML \u3092 HTML \u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002XSL \u306f\u805e\u3044\u305f\u3053\u3068\u304c\u306a\u304b\u3063\u305f\u306e\u3067\u3059\u304c\u3001Extensible Stylesheet Language \u306e\u982d\u6587\u5b57\u3067\u3001XML \u306e\u5909\u63db\u3092\u884c\u3046\u305f\u3081\u306e\u30b9\u30bf\u30a4\u30eb\u30b7\u30fc\u30c8\u6280\u8853\u306e\u3088\u3046\u3067\u3059\u3002
    \n\u9577\u304f\u306a\u308b\u306e\u3067\u3053\u3053\u306b\u306f\u8a18\u8f09\u3057\u307e\u305b\u3093\u304c\u3001\u30b5\u30f3\u30d7\u30eb\u5b9a\u7fa9\u66f8\u3067\u4f7f\u7528\u3057\u305f XSL \u306f Gist
    showwin\/db_schema_style.xsl<\/a> \u306b\u4e0a\u3052\u305f\u306e\u3067\u5fc5\u8981\u306a\u65b9\u306f\u3053\u3061\u3089\u3082\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n

    4. HTML \u3092 PDF \u306b\u5909\u63db\u3059\u308b<\/h3>\n

    wkhtmltopdf<\/code> \u3068\u3044\u3046\u30c4\u30fc\u30eb\u3092\u4f7f\u3046\u3068<\/p>\n

    $ wkhtmltopdf schema.html schema.pdf<\/code><\/pre>\n

    \u3067 HTML \u304b\u3089 PDF \u306b\u5909\u63db\u3067\u304d\u308b\u306e\u3067\u3059\u304c\u3001Ubuntu \u306e\u5834\u5408\u3060\u3068 wkhtmltopdf: cannot connect to X server<\/code> \u3068\u30a8\u30e9\u30fc\u304c\u51fa\u3066\u3057\u307e\u3046\u306e\u3067<\/p>\n

    $ xvfb-run -a -s "-screen 0 1024x768x24" wkhtmltopdf schema.html schema.pdf<\/code><\/pre>\n

    \u3068\u3059\u308b\u3068\u826f\u3044\u3067\u3059\u3002\u3064\u3044\u3067\u306b fonts-ipafont<\/code> \u3092\u5165\u308c\u3066\u304a\u304f\u3068\u30d5\u30a9\u30f3\u30c8\u304c\u7dba\u9e97\u306b\u306a\u3063\u3066\u5e78\u305b\u306b\u306a\u308c\u307e\u3059\u3002<\/p>\n

    \u624b\u9806\u307e\u3068\u3081<\/h3>\n

    Rails + MySQL + Ubuntu \u306a\u74b0\u5883\u3067\u306e\u624b\u9806\u307e\u3068\u3081\u3067\u3059\u3002
    \n\u9055\u3046\u74b0\u5883\u306e\u304b\u305f\u306f\u9069\u5b9c\u5bfe\u5fdc\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n

    \u5fc5\u8981\u306a\u30c4\u30fc\u30eb\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n

    $ apt-get install xsltproc xvfb wkhtmltopdf fonts-ipafont<\/code><\/pre>\n

    Gemfile \u306b\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n

    gem 'migration_comments'<\/code><\/pre>\n

    migration \u30d5\u30a1\u30a4\u30eb\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n

    Rakefile \u306b rake db:migration<\/code> \u30b3\u30de\u30f3\u30c9\u306b\u30d5\u30c3\u30af\u3057\u30662,3,4\u306e\u51e6\u7406\u304c\u5b9f\u884c\u3055\u308c\u308b\u3088\u3046\u306b\u8a18\u8ff0\u3057\u307e\u3059\u3002\u4e2d\u9593\u751f\u6210\u7269\u306f home_path\/*<\/code> \u306b\u51fa\u529b\u3057\u3066\u3001\u6700\u7d42\u6210\u679c\u7269\u306eDB\u5b9a\u7fa9\u66f8\u306f doc\/schema.pdf<\/code> \u306b\u51fa\u529b\u3059\u308b\u4f8b\u3067\u3059\u3002style.xsl<\/code> \u3092\u4e8b\u524d\u306b\u304a\u3044\u3066\u304a\u304f\u3053\u3068\u3092\u5fd8\u308c\u306a\u3044\u3088\u3046\u306b\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n

    task :create_schema_doc do\n  home_path = 'path\/to\/somewhere'\n  # schema \u306e\u69cb\u9020\u3092 XML \u5f62\u5f0f\u3067\u51fa\u529b\n  `mysqldump -u $RAILS_DATABASE_USERNAME -p$RAILS_DATABASE_PASSWORD -h $RAILS_DATABASE_HOST --no-data --xml $RAILS_DATABASE_NAME > #{home_path}\/schema.xml`\n  # XML \u3092 HTML \u306b\u5909\u63db\n  `xsltproc -o #{home_path}\/schema.html #{home_path}\/style.xsl #{home_path}\/schema.xml`\n  # HTML \u3092 PDF \u306b\u5909\u63db\n  `xvfb-run -a -s "-screen 0 1024x768x24" wkhtmltopdf #{home_path}\/schema.html doc\/schema.pdf`\nend\n\nRake::Task['db:migrate'].enhance do\n  Rake::Task[:create_schema_doc].invoke\nend<\/code><\/pre>\n

    \u305d\u3057\u3066\u3001\u3001<\/p>\n

    $ rake db:migrate<\/code><\/pre>\n

    \u3067PDF\u306aDB\u5b9a\u7fa9\u66f8\u306e\u5b8c\u6210\u3067\u3059\uff01<\/p>\n

    \u307e\u3068\u3081<\/h2>\n

    Rails \u3067\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u66f8\u3044\u3066\u3044\u3066\u3001DB\u5b9a\u7fa9\u66f8\u304c\u5fc5\u8981\u306b\u306a\u308b\u30b1\u30fc\u30b9\u306f\u3042\u307e\u308a\u306a\u3044\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\u3001\u7279\u306b\u610f\u8b58\u3059\u308b\u3053\u3068\u306a\u304f rake db:migrate<\/code> \u306e\u5ea6\u306b\u81ea\u52d5\u3067DB\u5b9a\u7fa9\u66f8\u304c\u66f4\u65b0\u3055\u308c\u308b\u306e\u304c\u601d\u3044\u306e\u307b\u304b\u6c17\u6301\u3061\u3088\u304b\u3063\u305f\u306e\u3067\u8a18\u4e8b\u306b\u3057\u3066\u307f\u307e\u3057\u305f\u3002
    \nXML \u304b\u3089 HTML \u3078\u306e\u5909\u63db\u6642\u306e\u81ea\u7531\u5ea6\u304c\u9ad8\u304f\u3001\u597d\u307f\u306e\u30c7\u30b6\u30a4\u30f3\/\u30b9\u30bf\u30a4\u30eb\u3067\u51fa\u529b\u3067\u304d\u308b\u306e\u3082\u9b45\u529b\u7684\u3067\u3059\u306d\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

    \u3068\u3042\u308b\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u30c6\u30fc\u30d6\u30eb\u5b9a\u7fa9\u66f8\u3092\u4f5c\u6210\u3059\u308b\u5fc5\u8981\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u5909\u66f4\u304c\u3042\u308b\u5ea6\u306b\u624b\u52d5\u3067\u30c9\u30ad\u30e5\u30e1\u30f3\u30c8\u3092\u4fee\u6b63\u3059\u308b\u306e\u306f\u5927\u5909\u9762\u5012\u306a\u305f\u3081\u3001\u81ea\u52d5\u751f\u6210\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002 Rails \u306e migration \u3092\u4f7f\u3063\u3066\u7ba1\u7406\u3057\u3066\u3044\u308b\u30c6\u30fc\u30d6\u30eb\u7fa4(MySQL)\u306e\u5b9a\u7fa9\u66f8\u3092 PDF \u3067\u51fa\u529b\u3059\u308b\u307e\u3067\u306e\u30d5\u30ed\u30fc\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002 \u3053\u306e\u8a18\u4e8b\u306e\u901a\u308a\u306b\u4f5c\u6210\u3059\u308b\u3068\u4ee5\u4e0b\u306e\u69d8\u5f0f\u306e\u5b9a\u7fa9\u66f8\u306b\u306a\u308a\u307e\u3059\u304c\u3001\u304a\u597d\u304d\u306a\u3088\u3046\u306b\u30c7\u30b6\u30a4\u30f3\u3092\u5909\u66f4\u3059\u308b […]<\/p>\n","protected":false},"author":1,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[42],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/282"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=282"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/282\/revisions"}],"predecessor-version":[{"id":3363,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/282\/revisions\/3363"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}