Skip to content

Commit

Permalink
Added data migration support
Browse files Browse the repository at this point in the history
  • Loading branch information
dracony committed Jun 4, 2013
1 parent 7a1344d commit 0e2470e
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
41 changes: 41 additions & 0 deletions assets/migrations/2_adding_pixies_table.php
Expand Up @@ -9,6 +9,47 @@

//Renaming a column
'name'=>'fairy_name'
),

//Adding data to the table
'_data' => array(

//Rules for updating data to this revision
//Executes after table upgrade
'up' => array(

//Row insertions
'insert' => array(
array('fairy_name' => 'Tinkerbell'),
array('fairy_name' => 'Trixie')
),

//Row updates
'update' => array(
array(
//Data to update
'data' => array('fairy_name' => 'Sylph'),

//Conditions for the update
//Same syntax as in where() Query method
'conds' => array('fairy_name', 'Trixie')
)
)
),

//Rules for downgrading data to this revision
//Executes before table downgrade
'down' => array(

//Row deletions
'delete' => array(

//Same syntax as in where() Query method
array('fairy_name', 'Tinkerbell'),
array('fairy_name', 'Sylph')
)

)
)
),

Expand Down
51 changes: 49 additions & 2 deletions classes/PHPixie/Migrate/Migrator.php
Expand Up @@ -141,7 +141,17 @@ protected function apply($version_key, $direction) {
$rules = array_reverse($rules);
$this->pixie->debug->log($rules);
foreach($rules as $table => $columns) {


$data_updates = null;

if (isset($columns['_data'])) {
$data_updates = $this->pixie->arr($columns['_data'], $direction);
unset($columns['_data']);
}

if ($data_updates && $direction=='down')
$this->update_table_data($table, $data_updates);

if ($columns=='drop'&&$direction=='up') {
$this->drop_table($table);
continue;
Expand Down Expand Up @@ -181,6 +191,7 @@ protected function apply($version_key, $direction) {
continue;
}


foreach($columns as $name => $def) {

if ($direction == 'up' && $def == 'drop')
Expand Down Expand Up @@ -218,8 +229,12 @@ protected function apply($version_key, $direction) {
if(isset($target_schema[$target_table][$target_name]))
$columns[$name] = array_merge($columns[$name],$target_schema[$target_table][$target_name]);
}
$this->pixie->debug->log(array($target_table, $columns));

$this->pixie-> debug->log(array($target_table, $columns));
$this->alter_columns($target_table, $columns);
if ($data_updates && $direction=='up')
$this->update_table_data($table, $data_updates);


}

Expand Down Expand Up @@ -247,6 +262,9 @@ public function get_version_schema($target) {

if(!isset($schema[$table]))
$schema[$table] = array();

if (isset($columns['_data']))
unset($columns['_data']);

if (isset($columns['rename'])) {
echo($columns['rename']);
Expand Down Expand Up @@ -277,6 +295,35 @@ public function get_version_schema($target) {
return $schema;
}

/**
* Process table data updates
*
* @param string $table Name of the table to update
* @param array $data Update rules. An associative array
* Containing optional 'insert', 'update' and 'delete' rules.
* @return void
*/
protected function update_table_data($table, $data) {
foreach ($this->pixie->arr($data, 'insert', array()) as $insert)
$this->_db->query('insert')
->table($table)
->data($insert)
->execute();

foreach ($this->pixie->arr($data, 'update', array()) as $update)
$this->_db->query('update')
->table($table)
->data($update['data'])
->where($update['conds'])
->execute();

foreach ($this->pixie->arr($data, 'delete', array()) as $delete)
$this->_db->query('delete')
->table($table)
->where($delete)
->execute();
}

/**
* Abstract function for dropping a table
*
Expand Down

0 comments on commit 0e2470e

Please sign in to comment.