method
table
table(table, stream)
private
Hide source
# File activerecord/lib/active_record/schema_dumper.rb, line 117 def table(table, stream) columns = @connection.columns(table) begin self.table_name = table tbl = StringIO.new # first dump primary key column pk = @connection.primary_key(table) tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}" case pk when String tbl.print ", primary_key: #{pk.inspect}" unless pk == "id" pkcol = columns.detect { |c| c.name == pk } pkcolspec = column_spec_for_primary_key(pkcol) unless pkcolspec.empty? if pkcolspec != pkcolspec.slice(:id, :default) pkcolspec = { id: { type: pkcolspec.delete(:id), **pkcolspec }.compact } end tbl.print ", #{format_colspec(pkcolspec)}" end when Array tbl.print ", primary_key: #{pk.inspect}" else tbl.print ", id: false" end table_options = @connection.table_options(table) if table_options.present? tbl.print ", #{format_options(table_options)}" end tbl.puts ", force: :cascade do |t|" # then dump all non-primary key columns columns.each do |column| raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type) next if column.name == pk type, colspec = column_spec(column) if type.is_a?(Symbol) tbl.print " t.#{type} #{column.name.inspect}" else tbl.print " t.column #{column.name.inspect}, #{type.inspect}" end tbl.print ", #{format_colspec(colspec)}" if colspec.present? tbl.puts end indexes_in_create(table, tbl) check_constraints_in_create(table, tbl) if @connection.supports_check_constraints? tbl.puts " end" tbl.puts 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 ensure self.table_name = nil end end