Coder Social home page Coder Social logo

immigrant's Introduction

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:

gem 'immigrant'

If you're using a version of Rails prior to 4.2, you'll also need the Foreigner gem.

Usage

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:

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.

License

Copyright (c) 2012-2015 Jon Jensen, released under the MIT license

immigrant's People

Contributors

jenseng avatar seuros avatar eturino avatar reconbot avatar iamurali avatar zackxu1 avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.