https://github.com/jenseng/immigrant
Foreign key migration generator for Rails
https://github.com/jenseng/immigrant
Last synced: 7 months ago
JSON representation
Foreign key migration generator for Rails
- Host: GitHub
- URL: https://github.com/jenseng/immigrant
- Owner: jenseng
- License: mit
- Created: 2012-04-01T19:59:42.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2024-05-08T01:04:14.000Z (over 1 year ago)
- Last Synced: 2025-04-14T16:53:53.199Z (8 months ago)
- Language: Ruby
- Homepage:
- Size: 77.1 KB
- Stars: 485
- Watchers: 8
- Forks: 24
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-ruby-toolbox - immigrant - Adds a generator for creating a foreign key migration based on your current model associations (Active Record Plugins / Active Record Index Assistants)
README
# Immigrant
[
](http://travis-ci.org/jenseng/immigrant)
Immigrant gives Rails a foreign key migration generator so you can
effortlessly find and add missing keys. This is particularly helpful
when you decide to add keys to an established Rails app.
## Installation
Add the following to your Gemfile:
```ruby
gem 'immigrant'
```
If you're using a version of Rails prior to 4.2, you'll also need the
[Foreigner](https://github.com/matthuhiggins/foreigner) gem.
## Usage
```bash
rails generate immigration AddKeys
```
This will create a migration named AddKeys which will have `add_foreign_key`
statements for any missing foreign keys. Immigrant infers missing ones by
evaluating the associations in your models (e.g. `belongs_to`, `has_many`, etc.).
Only missing keys will be added; existing ones will never be altered or
removed.
### Rake Task
To help you remember to add keys in the future, there's a handy rake
task you can add to your CI setup. Just run `rake immigrant:check_keys`,
and if anything is missing it will tell you about it and exit with a
non-zero status.
### Skipping associations
`Immigrant.ignore_keys` allows you to specify a list of keys that should
be ignored (both in the migration generator and the rake task). This is
useful if you have associations spanning databases.
Just create an config/initializers/immigrant.rb file with something like
the following:
```ruby
Immigrant.ignore_keys = [
{ from_table: "users", column: "account_id" },
# etc
]
```
## Considerations
If the data in your tables is bad, then the migration will fail to run
(obviously). IOW, ensure you don't have orphaned records **before** you try to
add foreign keys.
## Known Issues
Immigrant currently only looks for foreign keys in `ActiveRecord::Base`'s
database. So if a model is using a different database connection and it has
foreign keys, Immigrant will incorrectly include them again in the generated
migration. `Immigrant.ignore_keys` can be used to work around this.
## [Changelog](CHANGELOG.md)
## License
Copyright (c) 2012-2015 Jon Jensen, released under the MIT license