Sha256: 8557fe3fdcd9a8399c7479d7238ba06384685508235ebfffcf28ad480eb8bfa5

Contents?: true

Size: 1009 Bytes

Versions: 26

Compression:

Stored size: 1009 Bytes

Contents

class ConstraintMigration < ActiveRecord::Migration
  
  # Creates a foreign key constraint
  def self.create_constraint(table, foreign_table, local_key = nil, foreign_key = nil)
    local_key ||= "#{foreign_table.singularize}_id"
    foreign_key ||= "id"
    if(ENV['noconstraints'] != 'yes')
      puts "Creating foreign key constraint on #{table}. Try 'noconstraints=yes' if this fails."
      execute "alter table #{table} add constraint #{table}_to_#{foreign_table} foreign key (#{local_key}) references #{foreign_table}(#{foreign_key})"
    else
      puts "Ignoring foreign key constraint"
    end
  end
  
  # Removes a foreign key constraint
  def self.remove_constraint(table, foreign_table)
    if(ENV['noconstraints'] != 'yes')
      puts "Removing foreign key constraint on #{table}. Try 'noconstraints=yes' if this fails."
      execute "alter table #{table} drop foreign key #{table}_to_#{foreign_table}"
    else
      puts "Not removing foreign key constraint for #{table}"
    end
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
talia_core-0.4.7 generators/talia_base/templates/migrations/constraint_migration.rb
talia_core-0.4.6 generators/talia_base/templates/migrations/constraint_migration.rb
talia_core-0.4.5 generators/talia_base/templates/migrations/constraint_migration.rb
talia_core-0.4.4 generators/talia_base/templates/migrations/constraint_migration.rb
talia_core-0.4.3 generators/talia_base/templates/migrations/constraint_migration.rb
talia_core-0.4.2 generators/talia_base/templates/migrations/constraint_migration.rb