Skip to content

Instantly share code, notes, and snippets.

@linxlad
Created July 3, 2015 10:13
Show Gist options
  • Save linxlad/e301d04e3f71513d7669 to your computer and use it in GitHub Desktop.
Save linxlad/e301d04e3f71513d7669 to your computer and use it in GitHub Desktop.
Status corrections
<?php
namespace Application\Migrations;
use Doctrine\DBAL\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema;
/**
* Class Version20150625094625
* @package Application\Migrations
*/
class Version20150625094625 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
// Remove record inserted by migration
$this->connection->executeQuery('DELETE FROM property_status_change_log WHERE user_id = 2;');
// Add the new column "status_type" to hold the type of the status
$this->connection->executeQuery('ALTER TABLE property_status_change_log ADD COLUMN `status_type` VARCHAR(255) AFTER `status`;');
// Get all property statuses and status_dates and insert them into the property_status_change_log table
$results = $this->connection->fetchAll('SELECT id, status, status_date, go_live_date, is_active, deactivated_date FROM `properties` WHERE `created` >= "2014-03-12 09:00" ORDER BY status_date ASC;');
$this->abortIf(!$results, 'No results found');
$count = count($results);
$date = new \DateTime();
$this->write("\r\n Inserting records.");
foreach ($results as $key => $result) {
$statusDate = $result['status_date'];
$goLiveDate = $result['go_live_date'];
$deactivatedDate = $result['deactivated_date'];
if (!is_null($statusDate) && $statusDate != '1970-01-01') {
$this->connection->executeQuery('INSERT INTO `property_status_change_log` (property_id, user_id, status, status_type, status_date, created) VALUES ('. $result['id'] .', 2, \''. $result['status'] .'\', "marketing", \''. $statusDate. '\', \''. $date->format('Y-m-d H:i:s') .'\');');
}
if (!is_null($goLiveDate) && $goLiveDate != '1970-01-01') {
$this->connection->executeQuery('INSERT INTO `property_status_change_log` (property_id, user_id, status, status_type, status_date, created) VALUES ('. $result['id'] .', 2, "active", "activity", \''. $goLiveDate. '\', \''. $date->format('Y-m-d H:i:s') .'\');');
}
if (!is_null($deactivatedDate) && $deactivatedDate != '1970-01-01') {
$this->connection->executeQuery('INSERT INTO `property_status_change_log` (property_id, user_id, status, status_type, status_date, created) VALUES ('. $result['id'] .', 2, "inactive", "activity", \''. $deactivatedDate. '\', \''. $date->format('Y-m-d H:i:s') .'\');');
}
if ($key == ($count - 1)) {
$this->write("\r\n <info>✔</info> All record inserted.");
} else {
printf("\r" . str_repeat("=", round(($key / $count) * 100)) . "> %d%% (%s/%s)", round(($key / $count) * 100), $key, $count);
}
}
$this->write("\r\n" . count($results) . " property statuses inserted.");
$this->write("\r\n\n Amending records.");
$psclResults = $this->connection->fetchAll('SELECT id FROM property_status_change_log WHERE status_type IS NULL;');
if ($psclResults) {
$psclCount = count($psclResults);
foreach ($psclResults as $key => $result) {
$key++;
$this->connection->executeQuery('UPDATE property_status_change_log SET status_type = "activity" WHERE id = ' . $result['id'] . ';');
if ($key == $psclCount) {
$this->write("\r\n <info>✔</info> All record amended.");
} else {
printf("\r" . str_repeat("=", round(($key / $psclCount) * 100)) . "> %d%% (%s/%s)", round(($key / $psclCount) * 100), $key, $psclCount);
}
}
}
$this->write("\r\n");
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{ // Add the new column "status_type" to hold the type of the status
$this->addSql('ALTER TABLE property_status_change_log DROP COLUMN `status_type`;');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment