{"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
rake db:migrate<\/code> \u3067\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\/\u5909\u66f4\u3059\u308b<\/li>\n- DB\u306e\u30b9\u30ad\u30fc\u30de\u69cb\u9020\u3092 XML \u5f62\u5f0f\u3067\u51fa\u529b\u3059\u308b<\/li>\n
- XML \u3092 HTML \u306b\u5909\u63db\u3059\u308b<\/li>\n
- 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>\nclass 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>\n2. 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>\n3. 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