Last active
October 12, 2015 19:18
-
-
Save rob-bar/4074425 to your computer and use it in GitHub Desktop.
#snippet #fuelphp #php #migration created & updated refactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fuel\Migrations; | |
class refactor_created_at_and_updated_at { | |
public function up() { | |
$tables = \DB::list_tables(); | |
foreach ($tables as $table) { | |
\Cli::write($table); | |
if(\DBUtil::field_exists($table, array('created_at', 'updated_at'))) { | |
$records = \DB::select()->from($table)->execute()->as_array(); | |
\Cli::write($table . ' has timestamps: REFACTORING...'); | |
foreach ($records as $record) { | |
$str = \Cli::color(implode(", ", $record), 'red'); | |
\Cli::write($str); | |
} | |
\DBUtil::drop_fields($table, array( | |
'created_at', 'updated_at' | |
)); | |
\DBUtil::add_fields($table, array( | |
'created_at' => array('type' => 'datetime'), | |
'updated_at' => array('type' => 'datetime'), | |
)); | |
\Cli::write('UPDATING...'); | |
foreach ($records as $record) { | |
$record['created_at'] = date("Y-m-d H:i:s", $record['created_at']); | |
$record['updated_at'] = date("Y-m-d H:i:s", $record['updated_at']); | |
$str = \Cli::color(implode(", ", $record), 'green'); | |
\Cli::write($str); | |
\DB::update($table) | |
->set(array('created_at' => $record['created_at'], 'updated_at' => $record['updated_at'])) | |
->where('id', '=', $record['id']) | |
->execute(); | |
} | |
} | |
} | |
$str = \Cli::color('REFACTORING SUCCESSFULL!', 'green'); | |
\Cli::write($str); | |
} | |
public function down() { | |
$tables = \DB::list_tables(); | |
foreach ($tables as $table) { | |
\Cli::write($table); | |
if(\DBUtil::field_exists($table, array('created_at', 'updated_at'))) { | |
\DBUtil::drop_fields($table, array( | |
'created_at','updated_at' | |
)); | |
\DBUtil::add_fields($table, array( | |
'created_at' => array('constraint' => 11, 'type' => 'int'), | |
'updated_at' => array('constraint' => 11, 'type' => 'int'), | |
)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment