Skip to content

Commit

Permalink
Dockblock and code formatting (#3)
Browse files Browse the repository at this point in the history
* Dockblock and code formatting

- Added docblock comments to all classes, methods, and traits
- Code formatted all PHP files.

Resolves issue #2: #2

* Converted tabs to spaces

* Applying PSR1/PSR2 standards
  • Loading branch information
mos3abof authored and schtr4jh committed Oct 13, 2017
1 parent c265741 commit 84b730f
Show file tree
Hide file tree
Showing 28 changed files with 921 additions and 176 deletions.
140 changes: 107 additions & 33 deletions src/Pckg/Migration/Command/ExecuteMigration.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace Pckg\Migration\Command;
<?php

namespace Pckg\Migration\Command;

use Exception;
use Pckg\Database\Entity;
Expand All @@ -10,32 +12,62 @@
use Pckg\Migration\Relation;
use Pckg\Migration\Table;

/**
* Class ExecuteMigration
*
* @package Pckg\Migration\Command
*/
class ExecuteMigration
{

/**
* @var Migration
*/
protected $migration;

/**
* @var array
*/
protected $sql = [];

/**
* @var array
*/
protected $sqls = [];

/**
* @var bool
*/
protected $fields = true;

/**
* @var bool
*/
protected $relations = true;

/**
* ExecuteMigration constructor.
*
* @param Migration $migration
*/
public function __construct(Migration $migration)
{
$this->migration = $migration;
}

/**
* @return $this
*/
public function onlyFields()
{
$this->fields = true;
$this->fields = true;
$this->relations = false;

return $this;
}

/**
*
*/
public function execute()
{
$entity = new Entity();
Expand All @@ -56,9 +88,12 @@ public function execute()
}
}

/**
*
*/
protected function applyMigration()
{
$sqls = implode(";\n\n", $this->sqls);
$sqls = implode(";\n\n", $this->sqls);
$installMigrator = context()->getOrDefault(InstallMigrator::class);

if (!$installMigrator) {
Expand All @@ -74,6 +109,9 @@ protected function applyMigration()
}
}

/**
* @throws Exception
*/
protected function executeInRepository()
{
$repositoryName = $this->migration->getRepository();
Expand All @@ -83,14 +121,15 @@ protected function executeInRepository()
$prepare = $repository->getConnection()->prepare($sql);
$execute = $prepare->execute();
if (!$execute) {
throw new Exception(
'Cannot execute query! ' . "\n" . $sql . "\n" . 'Error code ' . $prepare->errorCode() . "\n" .
$prepare->errorInfo()[2]
);
throw new Exception('Cannot execute query! ' . "\n" . $sql . "\n" . 'Error code ' . $prepare->errorCode() . "\n" . $prepare->errorInfo()[2]);
}
}
}

/**
* @param Cache $cache
* @param Table $table
*/
protected function updateTable(Cache $cache, Table $table)
{
foreach ($table->getFields() as $field) {
Expand All @@ -109,8 +148,7 @@ protected function updateTable(Cache $cache, Table $table)
}

if ($this->sql) {
$this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ' . "\n"
. ' ' . implode(",\n ", $this->sql);
$this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ' . "\n" . ' ' . implode(",\n ", $this->sql);
}

if ($this->relations) {
Expand Down Expand Up @@ -138,6 +176,11 @@ protected function updateTable(Cache $cache, Table $table)
}
}

/**
* @param Cache $cache
* @param Table $table
* @param Relation $relation
*/
public function updateRelation(Cache $cache, Table $table, Relation $relation)
{
$cached = $cache->getConstraint($relation->getName(), $table->getName());
Expand All @@ -148,26 +191,32 @@ public function updateRelation(Cache $cache, Table $table, Relation $relation)
return;
}

$current = $relation->getSqlByParams(
$cached['primary'],
$cached['references'],
$cached['on'],
Relation::RESTRICT,
Relation::CASCADE
);
$current = $relation->getSqlByParams($cached['primary'], $cached['references'], $cached['on'],
Relation::RESTRICT, Relation::CASCADE);

if ($current != $relation->getSql()) {
$this->output('APPLY RELATION MANUALLY: ' . "\n" . $relation->getSql());
}
}

/**
* @param Table $table
* @param Relation $relation
*/
public function installRelation(Table $table, Relation $relation)
{
$this->sqls[] = 'SET foreign_key_checks = 0';
$this->sqls[] = 'ALTER TABLE `' . $table->getName() . '` ADD ' . $relation->getSql();
$this->sqls[] = 'SET foreign_key_checks = 1';
}

/**
* @param Cache $cache
* @param Table $table
* @param Field $field
*
* @return string
*/
protected function updateField(Cache $cache, Table $table, Field $field)
{
//$this->output('Updating field ' . $table->getName() . '.' . $field->getName());
Expand All @@ -179,6 +228,11 @@ protected function updateField(Cache $cache, Table $table, Field $field)
}
}

/**
* @param Cache $cache
* @param Table $table
* @param Constraint $key
*/
protected function updateConstraint(Cache $cache, Table $table, Constraint $key)
{
/*
Expand All @@ -187,6 +241,10 @@ protected function updateConstraint(Cache $cache, Table $table, Constraint $key)
*/
}

/**
* @param Cache $cache
* @param Table $table
*/
protected function installTable(Cache $cache, Table $table)
{
//$this->output('Installing table ' . $table->getName());
Expand All @@ -210,56 +268,72 @@ protected function installTable(Cache $cache, Table $table)
}

if ($this->sql) {
$this->sqls[] = 'CREATE TABLE IF NOT EXISTS `' . $table->getName() . '` (' . "\n"
. implode(",\n", $this->sql) . "\n" .
') ENGINE=InnoDB DEFAULT CHARSET=utf8';
$this->sqls[] = 'CREATE TABLE IF NOT EXISTS `' . $table->getName() . '` (' . "\n" . implode(",\n",
$this->sql) . "\n" . ') ENGINE=InnoDB DEFAULT CHARSET=utf8';
}
}

/**
* @param Field $field
*
* @return string
*/
protected function installField(Field $field)
{
return '`' . $field->getName() . '` ' . $field->getSql();
}

/**
* @param Constraint $key
*/
protected function installNewConstraint(Constraint $key)
{
$this->sql[] = $key->getSql();
}

/**
* @param Constraint $key
*/
protected function installConstraint(Constraint $key)
{
$this->sql[] = 'ADD ' . $key->getSql();
}

/**
* @param string $msg
*/
protected function output($msg = '')
{
echo '<question>' . $msg . "</question>\n";
}

/**
* @param Cache $cache
* @param Table $table
* @param Field $field
*
* @return string
*/
protected function buildOldFieldSql(Cache $cache, Table $table, Field $field)
{
$cachedField = $cache->getField($field->getName(), $table->getName());

return strtoupper($cachedField['type'])
. ($cachedField['limit'] ? '(' . $cachedField['limit'] . ')' : '')
. ($cachedField['null'] ? ' NULL' : ' NOT NULL')
. ($cachedField['default']
? ' DEFAULT ' . ($cachedField['default'] == 'CURRENT_TIMESTAMP'
? $cachedField['default']
: ("'" . $cachedField['default'] . "'"))
: ($cachedField['null'] ? ' DEFAULT NULL' : ''))
. ($cachedField['extra'] ? ' ' . strtoupper($cachedField['extra']) : '');
return strtoupper($cachedField['type']) . ($cachedField['limit'] ? '(' . $cachedField['limit'] . ')' : '') . ($cachedField['null'] ? ' NULL' : ' NOT NULL') . ($cachedField['default'] ? ' DEFAULT ' . ($cachedField['default'] == 'CURRENT_TIMESTAMP' ? $cachedField['default'] : ("'" . $cachedField['default'] . "'")) : ($cachedField['null'] ? ' DEFAULT NULL' : '')) . ($cachedField['extra'] ? ' ' . strtoupper($cachedField['extra']) : '');
}

/**
* @param Cache $cache
* @param Table $table
* @param Constraint $key
*
* @return string
*/
protected function buildOldKeySql(Cache $cache, Table $table, Constraint $key)
{
$cachedConstraint = $cache->getConstraint($key->getName(), $table->getName());

$type = $cachedConstraint['type'] == 'PRIMARY KEY'
? 'PRIMARY'
: ($cachedConstraint['type'] . ' KEY');
$type = $cachedConstraint['type'] == 'PRIMARY KEY' ? 'PRIMARY' : ($cachedConstraint['type'] . ' KEY');

return $type . ' ' . $key->getName();
}

}
Loading

0 comments on commit 84b730f

Please sign in to comment.