-
-
Save saicologic/435afdfbe856d9213c10 to your computer and use it in GitHub Desktop.
データベース定義書を作るタスク(rails用)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rake' | |
Table = Struct.new(:name, :comment, :columns, :indexes) | |
Column = Struct.new(:name, :type, :not_null, :default, :primary_key, :comment) | |
Index = Struct.new(:name, :columns, :primary, :unique) | |
def get_schema_info(klass) | |
table = Table.new | |
table.name = klass.table_name | |
table.columns = [] | |
table.indexes = [] | |
table.comment = klass.connection.retrieve_table_comment(klass.table_name) if defined? MigrationComments | |
column_comments = (defined? MigrationComments)? klass.connection.retrieve_column_comments(klass.table_name) : {} | |
cols = klass.columns | |
cols.each do |col| | |
column = Column.new | |
column.name = col.name | |
column.type = col.sql_type.to_s | |
column.default = klass.column_defaults[column.name] unless col.default.nil? || column.type == "jsonb" | |
column.not_null = col.null | |
column.primary_key = (klass.primary_key && (klass.primary_key.is_a?(Array) ? klass.primary_key.collect{|c|c.to_sym}.include?(col.name.to_sym) : col.name.to_sym == klass.primary_key.to_sym)) | |
column.comment = column_comments[col.name.to_sym] | |
table.columns << column | |
end | |
indexes = klass.connection.indexes(klass.table_name) | |
indexes.sort_by{|index| index.name}.each do |idx| | |
index = Index.new | |
index.name = idx.name | |
index.columns = idx.columns.join(',') | |
index.unique = idx.unique | |
table.indexes << index | |
end | |
table | |
end | |
desc 'Create database definition' | |
task dbdoc: :environment do | |
klasses = Dir["app/models/**/*.rb"]. | |
reject{|f| f["concerns/"] }. | |
map{|f| f.gsub(/^app\/models\/(.+?)\.rb$/, '\1') }. | |
map{|m| ActiveSupport::Inflector.camelize(m) }. | |
map{|k| ActiveSupport::Inflector.constantize(k) } | |
datum = klasses.map{|k| | |
get_schema_info(k) if k.respond_to?(:table_name) | |
} | |
package = Axlsx::Package.new | |
styles = package.workbook.styles | |
title = styles.add_style(bg_color: 'c0c0c0', b: true, font_name: 'MS P Gothic') | |
header = styles.add_style(bg_color: 'c0c0c0', b: true, font_name: 'MS P Gothic', font_size: 'large') | |
cols = styles.add_style(font_name: 'MS P Gothic') | |
#sheet = package.workbook.add_worksheet(name: 'テーブル一覧') | |
datum.each do |table| | |
next if table.nil? | |
begin | |
sheet = package.workbook.add_worksheet(name: table.name) | |
rescue => e | |
puts e | |
next | |
end | |
sheet.add_row(['テーブル情報'], style: title) | |
sheet.add_row([]) | |
sheet.add_row(['テーブル名', '備考'], style: header) | |
sheet.add_row([table.name, table.comment], style: cols) | |
sheet.add_row([]) | |
sheet.add_row(['カラム情報'], style: title) | |
sheet.add_row([]) | |
sheet.add_row(['No', 'カラム名', 'データ型', 'Not Null', 'デフォルト値', '備考'], style: header) | |
table.columns.each_with_index do |col, idx| | |
sheet.add_row([idx + 1, col.name, col.type, col.not_null, col.default, col.comment], style: cols) | |
end | |
sheet.add_row([]) | |
sheet.add_row(['インデックス情報'], style: title) | |
sheet.add_row([]) | |
sheet.add_row(['No', 'インデックス名', 'カラム', 'ユニーク'], style: header) | |
table.indexes.each_with_index do |index, idx| | |
sheet.add_row([idx + 1, index.name, index.columns, index.unique], style: cols) | |
end | |
end | |
package.serialize('データベース定義書.xlsx') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment