Skip to content

Commit

Permalink
🐛 🎉 Fix count issue on empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
weezqyd committed Jan 24, 2018
1 parent c00e5d8 commit 87ff8ee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 30 deletions.
27 changes: 8 additions & 19 deletions src/DbExporter/DbExporter.php
Expand Up @@ -3,7 +3,6 @@
namespace Elimuswift\DbExporter;

use DB;
use Storage;

abstract class DbExporter
{
Expand Down Expand Up @@ -48,26 +47,22 @@ abstract class DbExporter
'key_column_usage.referenced_column_name as References',
'REFERENTIAL_CONSTRAINTS.UPDATE_RULE as onUpdate',
'REFERENTIAL_CONSTRAINTS.DELETE_RULE as onDelete',
];
];

protected function getTables()
{
$pdo = DB::connection()->getPdo();

return $pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema="' . $this->database . '"');
return $pdo->query('SELECT table_name FROM information_schema.tables WHERE table_schema="'.$this->database.'"');
}

//end getTables()

public function getTableIndexes($table)
{
$pdo = DB::connection()->getPdo();

return $pdo->query('SHOW INDEX FROM ' . $this->database . '.' . $table . ' WHERE Key_name != "PRIMARY"');
return $pdo->query('SHOW INDEX FROM '.$this->database.'.'.$table.' WHERE Key_name != "PRIMARY"');
}

//end getTableIndexes()

/**
* Get all the columns for a given table.
*
Expand All @@ -83,8 +78,6 @@ protected function getTableDescribes($table)
->get($this->selects);
}

//end getTableDescribes()

/**
* Get all the foreign key constraints for a given table.
*
Expand All @@ -102,8 +95,6 @@ protected function getTableConstraints($table)
->get($this->constraints);
}

//end getTableConstraints()

/**
* Grab all the table data.
*
Expand All @@ -113,10 +104,10 @@ protected function getTableConstraints($table)
*/
protected function getTableData($table)
{
return DB::table($this->database . '.' . $table)->get();
return DB::table($this->database.'.'.$table)->get();
}

//end getTableData()
//end getTableData()

/**
* Try to create directories if they dont exist.
Expand All @@ -130,17 +121,15 @@ protected function makePath($path)
$directories = explode($del, $path);
foreach ($directories as $directory) {
if (!empty($directory)) {
$dir .= $del . $directory;
$dir .= $del.$directory;
}

if (!is_dir($dir)) {
Storage::makeDirectory($dir);
@mkdir($dir, 0775, true);
}
}
}

//end makePath()

/**
* Write the file.
*
Expand All @@ -163,4 +152,4 @@ abstract public function convert($database = null);
* @return mixed
*/
abstract protected function compile();
}//end class
}
23 changes: 12 additions & 11 deletions src/DbExporter/DbMigrations.php
Expand Up @@ -132,20 +132,20 @@ public function convert($database = null)
$para = strpos($values->Type, '(');
$type = $para > -1 ? substr($values->Type, 0, $para) : $values->Type;
$numbers = '';
$nullable = $values->null == 'NO' ? '' : '->nullable()';
$default = empty($values->Default) ? '' : "->default(\"{$values->Default}\")";
$default = $values->Default == 'CURRENT_TIMESTAMP' ? '->useCurrent()' : $default;
$unsigned = strpos($values->Type, 'unsigned') === false ? '' : '->unsigned()';
$nullable = 'NO' == $values->null ? '' : '->nullable()';
$default = empty($values->Default) || 'NULL' == $values->Default ? '' : "->default(\"{$values->Default}\")";
$default = 'CURRENT_TIMESTAMP' == $values->Default ? '->useCurrent()' : $default;
$unsigned = false === strpos($values->Type, 'unsigned') ? '' : '->unsigned()';
$pri = '';

if (in_array($type, ['var', 'varchar', 'double', 'enum', 'decimal', 'float'])) {
$para = strpos($values->Type, '(');
$opt = substr($values->Type, ($para + 1), -1);
$numbers = $type == 'enum' ? ', array('.$opt.')' : ', '.$opt;
$numbers = 'enum' == $type ? ', array('.$opt.')' : ', '.$opt;
}

$method = $this->columnType($type);
if ($values->Key == 'PRI') {
if ('PRI' == $values->Key) {
$tmp = $this->columnType($values->Data_Type, 'primaryKeys');
$method = empty($tmp) ? $method : $tmp;
$pri .= empty($tmp) ? ' $'."table->primary('".$values->Field."');\n" : '';
Expand All @@ -155,10 +155,10 @@ public function convert($database = null)
$up .= $pri;
}//end foreach

$tableIndexes = $this->getTableIndexes($value['table_name']);
$tableIndexes = (array) $this->getTableIndexes($value['table_name']);
if (!is_null($tableIndexes) && count($tableIndexes)) {
foreach ($tableIndexes as $index) {
if (Str::endsWith($index['Key_name'], '_index')) {
if (Str::endsWith(@$index['Key_name'], '_index')) {
$up .= ' $'."table->index('".$index['Column_name']."');\n";
}
}
Expand All @@ -170,13 +170,14 @@ public function convert($database = null)
* @var array
*/
$tableConstraints = $this->getTableConstraints($value['table_name']);
if (!is_null($tableConstraints) && count($tableConstraints)) {
if (!is_null($tableConstraints) && $tableConstraints->count()) {
$Constraint = $ConstraintDown = "
Schema::table('{$value['table_name']}', function(Blueprint $"."table) {\n";
$tables = [];
foreach ($tableConstraints as $foreign) {
if (!in_array($foreign->Field, $tables)) {
$ConstraintDown .= ' $'."table->dropForeign('".$foreign->Field."');\n";
$field = "{$foreign->Table}_{$foreign->Field}_foreign";
$ConstraintDown .= ' $'."table->dropForeign('".$field."');\n";
$Constraint .= ' $'."table->foreign('".$foreign->Field."')->references('".$foreign->References."')->on('".$foreign->ON."')->onDelete('".$foreign->onDelete."');\n";
$tables[$foreign->Field] = $foreign->Field;
}
Expand Down Expand Up @@ -220,7 +221,7 @@ protected function compile()
$downConstraint = '';

// prevent of failure when no table
if (!is_null($this->schema) && count($this->schema)) {
if (!is_null($this->schema) && is_array($this->schema)) {
foreach ($this->schema as $name => $values) {
// check again for ignored tables
if (in_array($name, self::$ignore)) {
Expand Down

0 comments on commit 87ff8ee

Please sign in to comment.