diff options
Diffstat (limited to 'activerecord/lib/active_record/schema_dumper.rb')
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index fae6427ea1..2362dae9fc 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -1,5 +1,4 @@ require 'stringio' -require 'active_support/core_ext/big_decimal' module ActiveRecord # = Active Record Schema Dumper @@ -44,7 +43,6 @@ module ActiveRecord def initialize(connection, options = {}) @connection = connection - @types = @connection.native_database_types @version = Migrator::current_version rescue nil @options = options end @@ -91,7 +89,7 @@ HEADER end def tables(stream) - sorted_tables = @connection.tables.sort + sorted_tables = @connection.data_sources.sort - @connection.views sorted_tables.each do |table_name| table(table_name, stream) unless ignored?(table_name) @@ -100,44 +98,56 @@ HEADER # dump foreign keys at the end to make sure all dependent tables exist. if @connection.supports_foreign_keys? sorted_tables.each do |tbl| - foreign_keys(tbl, stream) + foreign_keys(tbl, stream) unless ignored?(tbl) end end end def table(table, stream) - columns = @connection.columns(table) + columns = @connection.columns(table).map do |column| + column.instance_variable_set(:@table_name, table) + column + end begin tbl = StringIO.new # first dump primary key column - if @connection.respond_to?(:pk_and_sequence_for) - pk, _ = @connection.pk_and_sequence_for(table) - end - if !pk && @connection.respond_to?(:primary_key) + if @connection.respond_to?(:primary_keys) + pk = @connection.primary_keys(table) + pk = pk.first unless pk.size > 1 + else pk = @connection.primary_key(table) end tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}" - pkcol = columns.detect { |c| c.name == pk } - if pkcol - if pk != 'id' - tbl.print %Q(, primary_key: "#{pk}") - elsif pkcol.sql_type == 'uuid' - tbl.print ", id: :uuid" - tbl.print %Q(, default: "#{pkcol.default_function}") if pkcol.default_function + + case pk + when String + tbl.print ", primary_key: #{pk.inspect}" unless pk == 'id' + pkcol = columns.detect { |c| c.name == pk } + pkcolspec = @connection.column_spec_for_primary_key(pkcol) + if pkcolspec + pkcolspec.each do |key, value| + tbl.print ", #{key}: #{value}" + end end + when Array + tbl.print ", primary_key: #{pk.inspect}" else tbl.print ", id: false" end - tbl.print ", force: true" + tbl.print ", force: :cascade" + + table_options = @connection.table_options(table) + tbl.print ", options: #{table_options.inspect}" unless table_options.blank? + tbl.puts " do |t|" # then dump all non-primary key columns column_specs = columns.map do |column| raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" unless @connection.valid_type?(column.type) next if column.name == pk - @connection.column_spec(column, @types) + @connection.column_spec(column) end.compact # find all migration keys used in this table @@ -168,11 +178,11 @@ HEADER tbl.puts end + indexes(table, tbl) + tbl.puts " end" tbl.puts - indexes(table, tbl) - tbl.rewind stream.print tbl.read rescue => e @@ -188,29 +198,24 @@ HEADER if (indexes = @connection.indexes(table)).any? add_index_statements = indexes.map do |index| statement_parts = [ - ('add_index ' + remove_prefix_and_suffix(index.table).inspect), - index.columns.inspect, - ('name: ' + index.name.inspect), + "t.index #{index.columns.inspect}", + "name: #{index.name.inspect}", ] statement_parts << 'unique: true' if index.unique index_lengths = (index.lengths || []).compact - statement_parts << ('length: ' + Hash[index.columns.zip(index.lengths)].inspect) unless index_lengths.empty? - - index_orders = (index.orders || {}) - statement_parts << ('order: ' + index.orders.inspect) unless index_orders.empty? - - statement_parts << ('where: ' + index.where.inspect) if index.where - - statement_parts << ('using: ' + index.using.inspect) if index.using + statement_parts << "length: #{Hash[index.columns.zip(index.lengths)].inspect}" if index_lengths.any? - statement_parts << ('type: ' + index.type.inspect) if index.type + index_orders = index.orders || {} + statement_parts << "order: #{index.orders.inspect}" if index_orders.any? + statement_parts << "where: #{index.where.inspect}" if index.where + statement_parts << "using: #{index.using.inspect}" if index.using + statement_parts << "type: #{index.type.inspect}" if index.type - ' ' + statement_parts.join(', ') + " #{statement_parts.join(', ')}" end stream.puts add_index_statements.sort.join("\n") - stream.puts end end @@ -218,26 +223,26 @@ HEADER if (foreign_keys = @connection.foreign_keys(table)).any? add_foreign_key_statements = foreign_keys.map do |foreign_key| parts = [ - 'add_foreign_key ' + remove_prefix_and_suffix(foreign_key.from_table).inspect, - remove_prefix_and_suffix(foreign_key.to_table).inspect, - ] + "add_foreign_key #{remove_prefix_and_suffix(foreign_key.from_table).inspect}", + remove_prefix_and_suffix(foreign_key.to_table).inspect, + ] if foreign_key.column != @connection.foreign_key_column_for(foreign_key.to_table) - parts << ('column: ' + foreign_key.column.inspect) + parts << "column: #{foreign_key.column.inspect}" end if foreign_key.custom_primary_key? - parts << ('primary_key: ' + foreign_key.primary_key.inspect) + parts << "primary_key: #{foreign_key.primary_key.inspect}" end if foreign_key.name !~ /^fk_rails_[0-9a-f]{10}$/ - parts << ('name: ' + foreign_key.name.inspect) + parts << "name: #{foreign_key.name.inspect}" end - parts << ('on_update: ' + foreign_key.on_update.inspect) if foreign_key.on_update - parts << ('on_delete: ' + foreign_key.on_delete.inspect) if foreign_key.on_delete + parts << "on_update: #{foreign_key.on_update.inspect}" if foreign_key.on_update + parts << "on_delete: #{foreign_key.on_delete.inspect}" if foreign_key.on_delete - ' ' + parts.join(', ') + " #{parts.join(', ')}" end stream.puts add_foreign_key_statements.sort.join("\n") @@ -249,13 +254,8 @@ HEADER end def ignored?(table_name) - ['schema_migrations', ignore_tables].flatten.any? do |ignored| - case ignored - when String; remove_prefix_and_suffix(table_name) == ignored - when Regexp; remove_prefix_and_suffix(table_name) =~ ignored - else - raise StandardError, 'ActiveRecord::SchemaDumper.ignore_tables accepts an array of String and / or Regexp values.' - end + [ActiveRecord::Base.schema_migrations_table_name, ignore_tables].flatten.any? do |ignored| + ignored === remove_prefix_and_suffix(table_name) end end end |