Skip to content

Instantly share code, notes, and snippets.

@hiromi2424
Created December 18, 2011 21:06
Show Gist options
  • Save hiromi2424/1494463 to your computer and use it in GitHub Desktop.
Save hiromi2424/1494463 to your computer and use it in GitHub Desktop.
migrations with initial records
<?php
class M4ec9e1ac040840e8b4383c94d8a96c37 extends CakeMigration {
/**
* Records keyed by model name.
*
* @var array $records
*/
public $records = array(
'Category' => array(
array(
'id' => 1,
'name' => 'CakePHP',
),
array(
'id' => 2,
'name' => 'Awesome',
),
),
);
/**
* After migration callback
*
* @param string $direction, up or down direction of migration process
* @return boolean Should process continue
* @access public
*/
public function after($direction) {
if ($direction === 'up') {
foreach ($this->records as $model => $records) {
if (!$this->updateRecords($model, $records)) {
return false;
}
}
}
return true;
}
/**
* Update model records
*
* @param string $model model name to update
* @param string $records records to be stored
* @return boolean Should process continue
*/
public function updateRecords($model, $records, $scope = null) {
$Model = $this->generateModel($model);
foreach ($records as $record) {
$Model->create();
if (!$Model->save($record, false)) {
return false;
}
}
return true;
}
}
<?php
class M4ec9e1ac040840e8b4383c94d8a96c37 extends CakeMigration {
/**
* @var backup constraints
*/
public $constraints = array();
/**
* Before migration callback
*
* @param string $direction, up or down direction of migration process
* @return boolean Should process continue
* @access public
*/
public function before($direction) {
$this->backupConstraints('Post', 'Category');
if ($direction === 'down') {
// some process
}
return true;
}
/**
* After migration callback
*
* @param string $direction, up or down direction of migration process
* @return boolean Should process continue
* @access public
*/
public function after($direction) {
if ($direction === 'up') {
// some process
}
$this->restoreConstraints('Post');
return true;
}
/**
* Drops constraints and backup constraints
*
* @param string $baseModel base model for affect
* @param string $foreignModels model(s) having foreign constraint and ralated to base model
* @return void
*/
public function backupConstraints($baseModel, $foreignModels) {
$baseModel = $this->generateModel($baseModel);
foreach ((array)$foreignModels as $foreignModel) {
$foreignModel = $this->generateModel($foreignModel);
$foreignKey = Inflector::singularize($foreignModel->table)) . '_id';
$tableDefinition = $this->db->query("SHOW CREATE TABLE `{$baseModel->table}`");
if (preg_match("|CONSTRAINT `(.+?)` FOREIGN KEY \(`{$foreignKey}`\) REFERENCES `{$foreignModel->table}` \(`{$foreignModel->primaryKey}`\)|", $tableDefinition[0][0]['Create Table'], $matches)) {
$constraint = $matches[1];
$this->db->query("ALTER TABLE `{$baseModel->table}` DROP FOREIGN KEY `{$constraint}`");
if (!isset($this->constraints[$baseModel->name])) {
$this->constraints[$baseModel->name] = array();
}
$this->constraints[$baseModel->name][] = array($baseModel->table, $foreignModel->table, $foreignKey, $foreignModel->primaryKey, $constraint);
}
}
}
/**
* Restores constraints
*
* @param string $baseModel base model for affect
* @return void
*/
public function restoreConstraints($baseModel) {
if (!isset($this->constraints[$baseModel])) {
return false;
}
foreach ($this->constraints[$baseModel] as $constraints) {
list($baseTable, $foreignTable, $foreignKey, $foreignTablePrimaryKey, $constraint) = $constraints;
$this->db->query("ALTER TABLE `{$baseTable}` ADD CONSTRAINT `{$constraint}` FOREIGN KEY (`{$foreignKey}`) REFERENCES `{$foreignTable}` (`{$foreignTablePrimaryKey}`)");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment