Commit baa00178 by Bob Potter

Add options for show indexes in annotations

parent 4aee03d3
...@@ -12,6 +12,7 @@ OptionParser.new do |opts| ...@@ -12,6 +12,7 @@ OptionParser.new do |opts|
opts.on('-r', '--routes') { task = :annotate_routes } opts.on('-r', '--routes') { task = :annotate_routes }
opts.on('-v', '--version') { puts "Annotate v#{Annotate::VERSION}"; exit } opts.on('-v', '--version') { puts "Annotate v#{Annotate::VERSION}"; exit }
opts.on('-m', '--show-migration') { ENV['include_version'] = "yes" } opts.on('-m', '--show-migration') { ENV['include_version'] = "yes" }
opts.on('-i', '--show-indexes') { ENV['show_indexes'] = "yes" }
end.parse! end.parse!
begin begin
......
...@@ -32,7 +32,7 @@ module AnnotateModels ...@@ -32,7 +32,7 @@ module AnnotateModels
# to create a comment block containing a line for # to create a comment block containing a line for
# each column. The line contains the column name, # each column. The line contains the column name,
# the type (and length), and any optional attributes # the type (and length), and any optional attributes
def get_schema_info(klass, header) def get_schema_info(klass, header, options = {})
info = "# #{header}\n#\n" info = "# #{header}\n#\n"
info << "# Table name: #{klass.table_name}\n#\n" info << "# Table name: #{klass.table_name}\n#\n"
...@@ -59,9 +59,26 @@ module AnnotateModels ...@@ -59,9 +59,26 @@ module AnnotateModels
info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n" info << sprintf("# %-#{max_size}.#{max_size}s:%-15.15s %s", col.name, col_type, attrs.join(", ")).rstrip + "\n"
end end
if options[:show_indexes]
info << get_index_info(klass)
end
info << "#\n\n" info << "#\n\n"
end end
def get_index_info(klass)
index_info = "#\n# Indexes\n#\n"
indexes = klass.connection.indexes(klass.table_name)
return "" if indexes.empty?
max_size = indexes.collect{|index| index.name.size}.max + 1
indexes.each do |index|
index_info << sprintf("# %-#{max_size}.#{max_size}s %s %s", index.name, "(#{index.columns.join(",")})", index.unique ? "UNIQUE" : "").rstrip + "\n"
end
return index_info
end
# Add a schema block to a file. If the file already contains # Add a schema block to a file. If the file already contains
# a schema info block (a comment starting with "== Schema Information"), check if it # a schema info block (a comment starting with "== Schema Information"), check if it
# matches the block that is already there. If so, leave it be. If not, remove the old # matches the block that is already there. If so, leave it be. If not, remove the old
...@@ -116,7 +133,7 @@ module AnnotateModels ...@@ -116,7 +133,7 @@ module AnnotateModels
# files were modified. # files were modified.
def annotate(klass, file, header,options={}) def annotate(klass, file, header,options={})
info = get_schema_info(klass, header) info = get_schema_info(klass, header, options)
annotated = false annotated = false
model_name = klass.name.underscore model_name = klass.name.underscore
model_file_name = File.join(MODEL_DIR, file) model_file_name = File.join(MODEL_DIR, file)
......
...@@ -12,4 +12,4 @@ desc "Remove schema information from model and fixture files" ...@@ -12,4 +12,4 @@ desc "Remove schema information from model and fixture files"
task :remove_annotation => :environment do task :remove_annotation => :environment do
require 'annotate_models' require 'annotate_models'
AnnotateModels.remove_annotations AnnotateModels.remove_annotations
end end
\ No newline at end of file
...@@ -4,6 +4,7 @@ task :annotate_models => :environment do ...@@ -4,6 +4,7 @@ task :annotate_models => :environment do
options={} options={}
options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before options[:position_in_class] = ENV['position_in_class'] || ENV['position'] || :before
options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before options[:position_in_fixture] = ENV['position_in_fixture'] || ENV['position'] || :before
options[:show_indexes] = ENV['show_indexes']
AnnotateModels.do_annotations(options) AnnotateModels.do_annotations(options)
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment