Sha256: d571614d0e14476884e00eed2586223cc803a57fc08b87009e4610324ecd84e9

Contents?: true

Size: 1.63 KB

Versions: 30

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

require 'active_record/schema_dumper'

module Ridgepole
  module Ext
    module SchemaDumper
      def self.prepended(klass)
        klass.extend ClassMethods
      end

      module ClassMethods
        attr_reader :__with_default_fk_name

        def with_default_fk_name(value)
          @__with_default_fk_name = value
          yield
        ensure
          remove_instance_variable(:@__with_default_fk_name)
        end
      end

      def foreign_keys(table, stream)
        return super unless ActiveRecord::SchemaDumper.__with_default_fk_name

        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
            ]

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

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

            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

30 entries across 30 versions & 1 rubygems

Version Path
ridgepole-1.2.1 lib/ridgepole/ext/schema_dumper.rb
ridgepole-1.2.0 lib/ridgepole/ext/schema_dumper.rb
ridgepole-1.1.0 lib/ridgepole/ext/schema_dumper.rb
ridgepole-1.0.1 lib/ridgepole/ext/schema_dumper.rb
ridgepole-1.0.0 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.6 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.5 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.4 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.3 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.2 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.1 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.0 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.8.13 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.0.rc1 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.9.0.beta lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.8.12 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.8.11 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.8.10 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.8.9 lib/ridgepole/ext/schema_dumper.rb
ridgepole-0.8.8 lib/ridgepole/ext/schema_dumper.rb