Skip to content

Instantly share code, notes, and snippets.

@FooBarWidget
Created September 23, 2008 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FooBarWidget/12331 to your computer and use it in GitHub Desktop.
Save FooBarWidget/12331 to your computer and use it in GitHub Desktop.
ActiveRecord::SchemaDumper.class_eval do
def table(table, stream)
columns = @connection.columns(table)
begin
tbl = StringIO.new
if @connection.respond_to?(:pk_and_sequence_for)
pk, pk_seq = @connection.pk_and_sequence_for(table)
end
pk ||= 'id'
tbl.print " create_table #{table.inspect}"
#### Changed: ####
# Check whether the table has a primary key.
primary_key_column = columns.detect { |c| c.name == pk }
if primary_key_column
if pk != 'id'
# There's a primary key but not called 'id', so note this.
tbl.print %Q(, :primary_key => "#{pk}")
elsif primary_key_column.type != :integer
# There's a primary key but it's not an integer, so define a
# custom primary key.
tbl.print ", :id => false"
#p primary_key_column
#p primary_key_column.type.to_s
#exit!
end
else
tbl.print ", :id => false"
end
tbl.print ", :force => true"
tbl.puts " do |t|"
column_specs = columns.map do |column|
raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
#### Changed: ####
if column.name == pk && column.type == :integer
# Normally we do not print the primary key. However, if the primary
# key exists but is of a different type, then we will want to print it.
next
end
spec = {}
#### Changed: ####
if column == primary_key_column
column_name_string = column.name.inspect
column_type_string = "#{column.sql_type} NOT NULL PRIMARY KEY".inspect
spec[:name] = "#{column_name_string}, #{column_type_string}"
spec[:type] = "column"
else
spec[:name] = column.name.inspect
spec[:type] = column.type.to_s
spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && column.type != :decimal
spec[:precision] = column.precision.inspect if !column.precision.nil?
spec[:scale] = column.scale.inspect if !column.scale.nil?
spec[:null] = 'false' if !column.null
spec[:default] = default_string(column.default) if !column.default.nil?
end
#### Changed: (syntax only) ####
(spec.keys - [:name, :type]).each do |k|
spec[k].insert(0, "#{k.inspect} => ")
end
spec
end.compact
# find all migration keys used in this table
keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten
#### Changed: ####
# figure out the lengths for each column based on above keys
lengths = keys.map do |key|
column_specs.map do |spec|
# If we have a non-integer primary key then don't take its
# dump length into consideration, since it can be excessively long.
if spec[key] && !(primary_key_column && spec[:type] == "column")
spec[key].length + 2
else
0
end
end.max
end
# the string we're going to sprintf our values against, with standardized column widths
format_string = lengths.map{ |len| "%-#{len}s" }
# find the max length for the 'type' column, which is special
type_length = column_specs.map{ |column| column[:type].length }.max
# add column type definition to our format string
format_string.unshift " t.%-#{type_length}s "
format_string *= ''
column_specs.each do |colspec|
#### Changed (only syntax): ####
values = keys.zip(lengths).map do |key, len|
if colspec.key?(key)
colspec[key] + ", "
else
" " * len
end
end
values.unshift colspec[:type]
tbl.print((format_string % values).gsub(/,\s*$/, ''))
tbl.puts
end
tbl.puts " end"
tbl.puts
indexes(table, tbl)
tbl.rewind
stream.print tbl.read
rescue => e
stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
stream.puts "# #{e.message}"
stream.puts
end
stream
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment