fuel / oil

Fuel PHP Framework - Fuel v1.x Oil command-line package
http://fuelphp.com/docs/packages/oil/intro.html
106 stars 67 forks source link

Feature Request: New Magic Migration "set_unique" #63

Closed knudpotente closed 12 years ago

knudpotente commented 12 years ago

FuelPHP already has excellent tools that help us implement data validation and integrity checks in at the Model level - as it should be! Nevertheless, many programmers have to specify data integrity constraints also at database level. In my case, for instance, this is a requirement at my workplace that I cannot get around.

It would be wonderful if a new 'magic migration' could be added to oil's already fantastic set, that would enable me to simply call:

oil g migration set_unique_email_in_users

The format for the call, obviously, is: set_unique_{fieldname}_in_{table name}. It seems to me that using the word 'set' is clearer than using, let's say, the word 'add', which makes it more ambiguous (makes it seem as if we're adding a field, rather than modifying an existing one).

This call would create a migration file with the following template:

<?php

namespace Fuel\Migrations;

class Set_unique_{fieldname}_in_{tablename} {

 public function up()
 {  
  //Adding UNIQUE constraint to '{fieldname}' column
  \DB::query("ALTER TABLE `{tablename}` ADD CONSTRAINT `uk_{fieldname}` UNIQUE (`{fieldname}`)")->execute();
 }

 public function down()
 {
  //Removing UNIQUE constraint from '{fieldname}' column
  \DB::query("ALTER TABLE `{tablename}` DROP INDEX `uk_{fieldname}`")->execute();
 }
}

Thank you for your consideration, and for all your great work with oil.

philsturgeon commented 12 years ago

Looks great, would you turn this into a pull request?

WanWizard commented 12 years ago

Guess not...