Coder Social home page Coder Social logo

db_nazi's Introduction

DB Nazi

Encourages good DB practices in Active Record migrations.

What?

Active Record makes schema changes wonderfully easy, but due to some unfortunate defaults, it can be easy to forget to adhere to some basic database best practices, such as restricting columns to be non-nullable, or setting meaningful varchar limits. DB Nazi forces you to be explicit about these things so you can't simply forget.

It might take another 2 seconds to type null: true, but it may save you hours resolving integrity issues down the line!

Nullability

If DBNazi.require_nullability is set to true (the default), you must specify a :null option for all columns.

add_column :users, :awesome, :boolean              # raises DBNazi::NullabilityRequired
add_column :users, :awesome, :boolean, null: true  # ok

Varchar limits

If DBNazi.require_varchar_limits is set to true (the default), you must specify a :limit option for all :string columns.

add_column :users, :name, :string              # raises DBNazi::VarcharLimitRequired
add_column :users, :name, :string, limit: 100  # ok

Index uniqueness

If DBNazi.require_index_uniqueness is set the true (the default), you must specify a :unique option for all indexes.

add_index :users, :email                 # raises DBNazi::IndexUniquenessRequired
add_index :users, :email, unique: false  # ok

Usage

Since this tool is about enforcing developer discipline, I suggest including this only in the :development group in your Gemfile.

group :development do
  gem 'db_nazi'
end

If you have an established project, you probably don't want to lay the hard line on all your existing migrations. You can do this by specifying a minimum migration version in config/environments/development.rb:

DBNazi.from_version = 20120623000000

This means "only be a nazi from migration 20120623000000 onwards."

If you're using a migration written by a 3rd party, such as a generator you're using, I recommend editing it to conform to the rules above. After all, perhaps the 3rd party forgot a 'NOT NULL' or two.

If you're really sure you want to subvert DB Nazi for whatever reason, you may do so like this:

class BeAJerk < ActiveRecord::Migration
  no_nazi
  ...
end

Or just for a given block like this:

DBNazi.disable do
  ...
end

But no soup for you!

Contributing

  • Bug reports
  • Source
  • Patches: Fork on Github, send pull request.
    • Include tests where practical.
    • Leave the version alone, or bump it in a separate commit.

Copyright

Copyright (c) George Ogata. See LICENSE for details.

db_nazi's People

Contributors

bryanwoods avatar oggy 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.