Sha256: e34ee32b20d3f60c751fa0dcb8a9b68c1dbf86b1ea56e0afe40b8ac34dcea5ae

Contents?: true

Size: 1.84 KB

Versions: 106

Compression:

Stored size: 1.84 KB

Contents

module MigrationConstraintHelpers

   # Creates a foreign key from +table+.+field+ against referenced_table.referenced_field
   #
   # table: The tablename
   # field: A field of the table
   # referenced_table: The table which contains the field referenced
   # referenced_field: The field (which should be part of the primary key) of the referenced table
   # cascade: delete & update on cascade?
   def foreign_key(table, field, referenced_table, referenced_field = :id, cascade = true)
      execute "ALTER TABLE #{table} ADD CONSTRAINT #{constraint_name(table, field)}
               FOREIGN KEY #{constraint_name(table, field)} (#{field_list(field)})
               REFERENCES #{referenced_table}(#{field_list(referenced_field)})
               #{(cascade ? 'ON DELETE CASCADE ON UPDATE CASCADE' : '')}"
   end

   # Drops a foreign key from +table+.+field+ that has been created before with
   # foreign_key method
   #
   # table: The table name
   # field: A field (or array of fields) of the table
   def drop_foreign_key(table, field)
      execute "ALTER TABLE #{table} DROP FOREIGN KEY #{constraint_name(table, field)}"
   end

   # Creates a primary key for +table+, which right now HAS NOT primary key defined
   #
   # table: The table name
   # field: A field (or array of fields) of the table that will be part of the primary key
   def primary_key(table, field)
      execute "ALTER TABLE #{table} ADD PRIMARY KEY(#{field_list(field)})"
   end

   private

   # Creates a constraint name for table and field given as parameters
   #
   # table: The table name
   # field: A field of the table
   def constraint_name(table, field)
      "fk_#{table}_#{field_list_name(field)}"
   end

   def field_list(fields)
      fields.is_a?(Array) ? fields.join(',') : fields
   end

   def field_list_name(fields)
      fields.is_a?(Array) ? fields.join('_') : fields
   end
end

Version data entries

106 entries across 103 versions & 9 rubygems

Version Path
standalone_migrations-8.0.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-7.2.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-7.1.3 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-7.1.2 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-7.1.1 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations_sp-7.1.1 vendor/migration_helpers/lib/migration_helper.rb
cairn-7.1.1 vendor/migration_helpers/lib/migration_helper.rb
cairn-7.1.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations_new-7.1.2 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations_new-7.1.1 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations_new-7.1.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-7.1.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-6.1.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-6.0.0 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-5.2.7 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-5.2.6 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-5.2.5 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-5.2.4 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-5.2.3 vendor/migration_helpers/lib/migration_helper.rb
standalone_migrations-5.2.2 vendor/migration_helpers/lib/migration_helper.rb