557a55496676404bb03fb89b12a63855eda43366
5 # This class is used to dump the database schema for some connection to some
6 # output format (i.e., ActiveRecord::Schema).
7 class SchemaDumper
#:nodoc:
8 private_class_method
:new
12 # A list of tables which should not be dumped to the schema.
13 # Acceptable values are strings as well as regexp.
14 # This setting is only used if ActiveRecord::Base.schema_format == :ruby
15 cattr_accessor
:ignore_tables
18 def self.dump(connection
=ActiveRecord
::Base.connection
, stream
=STDOUT)
19 new(connection
).dump(stream
)
32 def initialize(connection
)
33 @connection = connection
34 @types = @connection.native_database_types
35 @version = Migrator
::current_version rescue nil
39 define_params
= @version ? ":version => #{@version}" : ""
42 # This file is auto-generated from the current state of the database. Instead of editing this file,
43 # please use the migrations feature of Active Record to incrementally modify your database, and
44 # then regenerate this schema definition.
46 # Note that this schema.rb definition is the authoritative source for your database schema. If you need
47 # to create the application database on another system, you should be using db:schema:load, not running
48 # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations
49 # you'll amass, the slower it'll run and the greater likelihood for issues).
51 # It's strongly recommended to check this file into your version control system.
53 ActiveRecord::Schema.define(#{define_params}) do
63 @connection.tables
.sort
.each
do |tbl
|
64 next if ['schema_migrations', ignore_tables
].flatten
.any
? do |ignored
|
66 when String
; tbl
== ignored
67 when Regexp
; tbl
=~ ignored
69 raise StandardError
, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.'
76 def table(table
, stream
)
77 columns
= @connection.columns(table
)
81 if @connection.respond_to
?(:pk_and_sequence_for)
82 pk
, pk_seq
= @connection.pk_and_sequence_for(table
)
86 tbl
.print
" create_table #{table.inspect}"
87 if columns
.detect
{ |c
| c
.name
== pk
}
89 tbl
.print
%Q(, :primary_key => "#{pk}")
92 tbl.print ", :id => false"
94 tbl.print ", :force => true"
97 column_specs = columns.map do |column|
98 raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil?
99 next if column.name == pk
101 spec[:name] = column.name.inspect
102 spec[:type] = column.type.to_s
103 spec[:limit] = column.limit.inspect if column.limit != @types[column.type][:limit] && column.type != :decimal
104 spec[:precision] = column.precision.inspect if !column.precision.nil?
105 spec[:scale] = column.scale.inspect if !column.scale.nil?
106 spec[:null] = 'false' if !column.null
107 spec[:default] = default_string(column.default) if column.has_default?
108 (spec.keys - [:name, :type]).each{ |k| spec[k].insert(0, "#{k.inspect} => ")}
112 # find all migration keys used in this table
113 keys = [:name, :limit, :precision, :scale, :default, :null] & column_specs.map(&:keys).flatten
115 # figure out the lengths for each column based on above keys
116 lengths = keys.map{ |key| column_specs.map{ |spec| spec[key] ? spec[key].length + 2 : 0 }.max }
118 # the string we're going to sprintf our values against, with standardized column widths
119 format_string = lengths.map{ |len| "%-#{len}s" }
121 # find the max length for the 'type' column, which is special
122 type_length = column_specs.map{ |column| column[:type].length }.max
124 # add column type definition to our format string
125 format_string.unshift " t.%-#{type_length}s "
129 column_specs.each do |colspec|
130 values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len }
131 values.unshift colspec[:type]
132 tbl.print((format_string % values).gsub(/,\s*$/, ''))
142 stream.print tbl.read
144 stream.puts "# Could not dump table #{table.inspect} because of following #{e.class}"
145 stream.puts "# #{e.message}"
152 def default_string(value)
156 when Date, DateTime, Time
157 "'" + value.to_s(:db) + "'"
163 def indexes(table, stream)
164 if (indexes = @connection.indexes(table)).any?
165 add_index_statements = indexes.map do |index|
166 statment_parts = [ ('add_index ' + index.table.inspect) ]
167 statment_parts << index.columns.inspect
168 statment_parts << (':name => ' + index.name.inspect)
169 statment_parts << ':unique => true' if index.unique
171 ' ' + statment_parts.join(', ')
174 stream.puts add_index_statements.sort.join("\n")