Sha256: f21e0d417723157313917bfb6c959c919115c2f213a21fef1a076a958c93103a

Contents?: true

Size: 1.24 KB

Versions: 45

Compression:

Stored size: 1.24 KB

Contents

require 'active_record/schema_dumper'

module Ridgepole
  module Ext
    module SchemaDumper
      def foreign_keys(table, stream)
        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,
            ]

            if foreign_key.column != @connection.foreign_key_column_for(foreign_key.to_table)
              parts << "column: #{foreign_key.column.inspect}"
            end

            if foreign_key.custom_primary_key?
              parts << "primary_key: #{foreign_key.primary_key.inspect}"
            end

            parts << "name: #{foreign_key.name.inspect}"

            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(', ')}"
          end

          stream.puts add_foreign_key_statements.sort.join("\n")
        end
      end
    end
  end
end

module ActiveRecord
  class SchemaDumper
    prepend Ridgepole::Ext::SchemaDumper
  end
end

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
ridgepole-0.6.6 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta18 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta17 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta16 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta15 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta14 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta13 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta12 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta11 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta10 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta9 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta8 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta7 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta6 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta5 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta4 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta3 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta2 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.beta lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.7.0.alpha3 lib/ridgepole/ext/schema_dumper.rb