Coder Social home page Coder Social logo

bradediger / mig_constraints Goto Github PK

View Code? Open in Web Editor NEW

This project forked from joelhoffman/mig_constraints

1.0 2.0 0.0 96 KB

Rails plugin that allows creation and management of database constraints such as uniqueness and referential integrity (foreign keys) using ActiveRecord Migrations. (Originally written by Alistair A. Israel)

License: MIT License

Ruby 100.00%

mig_constraints's Introduction

# Migrations Constraints

MigrationsConstraints adds the capability to add/drop table constraints or 
specify column constraints during table creation time to 
ActiveRecord::Migrations.

It currently supports the following:

  create_table :users do |t|
    t.column :login, :string, :limit => 20, :null => false, :unique => true
    t.column :type, :string, :limit => 80, :null => false
  end
    
  create_table :projects do |t|
    t.column :owner_id, :integer, :null => false, :references => :users
    t.column :name, :string, :null => false
  end
  
  create_table :line_items do |t|
    t.column :quantity, :integer, :null => false, :check => '> 0'
  end
  
  add_constraint :projects, :unique => :name
  drop_constraint :projects, :unique => :name
  
  add_constraint :project_members, :foreign_key => :project_id, :references => :projects
  add_constraint :project_members, :foreign_key => :user_id, :references => :users
  
  drop_constraint :project_members, :foreign_key => :user_id, :references => :users
  drop_constraint :project_members, :foreign_key => :project_id, :references => :projects
  
  add_constraint :line_items, :name => 'line_items_max_quantity_check', :check => 'quantity < 10000'
  drop_constraint :line_items, :name => 'line_items_max_quantity_check'

## Constraint Naming

Constraints by default are named according to the default naming convention used
by PostgreSQL 8.2.x. That is for a given table +table+, column +column+:

* A unique constraint is named +table_column_uq+.
* A foreign key constraint is named +table_column_fkey+.
* A check constraint is named +table_column_check+.

Otherwise, a constraint's name can be specified explicitly via the +:name+
parameter:

  add_constraint :books, :foreign_key => :author_id, :references => :authors, :name => 'books_authors_fkey'

## Release Notes

MigrationsConstraints is currently being developed and tested using a PostgreSQL
8.2 database back-end.

If the database you're using doesn't follow the same (ANSI SQL 92) data
definition language for specifying database constraints, then please support
this project by contributing to the code. :)

## History

[Alistair A. Israel](http://alistairisrael.wordpress.com/) created this plugin in 2007 ([see the RubyForge project](http://rubyforge.org/projects/mig-constraints/)).

mig_constraints's People

Contributors

bradediger avatar jlw avatar

Stargazers

 avatar

Watchers

 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.