Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bartimaeus/733811 to your computer and use it in GitHub Desktop.
Save bartimaeus/733811 to your computer and use it in GitHub Desktop.
Example of a CakePHP Migration that changes the database schema
<?php
/**
* @author Eric Shelley <[email protected]>
*/
class M20101208130030 extends CakeMigration
{
/**
* Migration description
*
* @var string
* @access public
*/
public $description = 'Rename inventories added_on and added_by fields to created standard';
/**
* Before migration callback
*
* @param string $direction, up or down direction of migration process
* @return boolean Should process continue
* @access public
*/
public function before($direction)
{
switch ($direction) {
case 'up':
echo "Going up...\n";
$Inventory = $this->generateModel('Inventory');
$Inventory->query(<<<PGSQL
ALTER TABLE inventories RENAME COLUMN added_by TO created_by;
ALTER TABLE inventories RENAME COLUMN added_on TO created;
PGSQL
);
echo "\nCompleted successfully\n\n";
break;
case 'down':
// No down b/c I'm correcting data
break;
}
return true;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment