Sha256: 183f7445a82e0d0579988d9bdcf144873445693aaae735e7c66449a918c25e56

Contents?: true

Size: 1.81 KB

Versions: 28

Compression:

Stored size: 1.81 KB

Contents

require 'ardb'

module Ardb; end
module Ardb::MigrationHelpers
  module_function

  def foreign_key(from_table, from_column, to_table, options={})
    fk = ForeignKey.new(from_table, from_column, to_table, options)
    execute(fk.add_sql)
  end

  def drop_foreign_key(*args)
    from_table, from_column = args[0..1]
    options = args.last.kind_of?(Hash) ? args.last : {}
    fk = ForeignKey.new(from_table, from_column, nil, options)
    execute(fk.drop_sql)
  end

  def remove_column_with_fk(table, column)
    drop_foreign_key(table, column)
    remove_column(table, column)
  end

  class ForeignKey
    attr_reader :from_table, :from_column, :to_table, :to_column, :name, :adapter

    def initialize(from_table, from_column, to_table, options=nil)
      options ||= {}
      @from_table  = from_table.to_s
      @from_column = from_column.to_s
      @to_table    = to_table.to_s
      @to_column   = (options[:to_column] || 'id').to_s
      @name        = (options[:name] || "fk_#{@from_table}_#{@from_column}").to_s
      @adapter     = Ardb::Adapter.send(Ardb.config.db.adapter)
    end

    def add_sql
      apply_data(@adapter.foreign_key_add_sql)
    end

    def drop_sql
      apply_data(@adapter.foreign_key_drop_sql)
    end

    private

    def apply_data(template_sql)
      template_sql.
        gsub(':from_table',  @from_table).
        gsub(':from_column', @from_column).
        gsub(':to_table',    @to_table).
        gsub(':to_column',   @to_column).
        gsub(':name',        @name)
    end
  end

  # This file will setup the AR migration command recorder for being able to change our
  # stuff, require it in an initializer

  module RecorderMixin

    def foreign_key(*args)
      record(:foreign_key, args)
    end

    protected

    def invert_foreign_key(args)
      [ :drop_foreign_key, args ]
    end

  end

end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
ardb-0.7.0 lib/ardb/migration_helpers.rb
ardb-0.6.0 lib/ardb/migration_helpers.rb
ardb-0.5.0 lib/ardb/migration_helpers.rb
ardb-0.4.1 lib/ardb/migration_helpers.rb
ardb-0.4.0 lib/ardb/migration_helpers.rb
ardb-0.3.0 lib/ardb/migration_helpers.rb
ardb-0.2.0 lib/ardb/migration_helpers.rb
ardb-0.1.0 lib/ardb/migration_helpers.rb