Skip to content

Commit

Permalink
angular & rest
Browse files Browse the repository at this point in the history
  • Loading branch information
mdmunir committed Sep 2, 2015
1 parent c8ffba1 commit 55600b9
Showing 1 changed file with 37 additions and 35 deletions.
72 changes: 37 additions & 35 deletions generators/migration/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public function generate()
'relations' => isset($relations[$tableSchema->name]) ? $relations[$tableSchema->name] : [],
];
}
$migrationName = 'm'.$this->migrationTime.'_'.$this->migrationName;
$file = rtrim(Yii::getAlias($this->migrationPath), '/')."/{$migrationName}.php";

$migrationName = 'm' . $this->migrationTime . '_' . $this->migrationName;
$file = rtrim(Yii::getAlias($this->migrationPath), '/') . "/{$migrationName}.php";
$files = new CodeFile($file, $this->render('migration.php', [
'tables' => $this->reorderTables($tables, $relations),
'migrationName' => $migrationName,
Expand All @@ -183,7 +183,7 @@ protected function reorderTables($tables, $relations)
{
$depencies = $orders = $result = [];
foreach ($relations as $table => $relation) {
if(isset($relation[$table])){
if (isset($relation[$table])) {
unset($relation[$table]);
}
$depencies[$table] = array_keys($relation);
Expand Down Expand Up @@ -230,24 +230,45 @@ public function getSchemaType($column)
$ref = new \ReflectionClass(Schema::className());
foreach ($ref->getConstants() as $constName => $constValue) {
if (strpos($constName, 'TYPE_') === 0) {
$this->constans[$constValue] = 'Schema::'.$constName;
$this->constans[$constValue] = '$this->' . $constValue;
}
}
$this->constans['smallint'] = '$this->smallInteger';
$this->constans['bigint'] = '$this->bigInteger';
}
if ($column->type !== Schema::TYPE_BOOLEAN && $column->size !== null) {
$size = [$column->size];
if ($column->scale !== null) {
$size[] = $column->scale;
}
$size = '('.implode(',', $size).')';
} else {
$size = '';
$size = [];
}
$result = '';
if (isset($this->constans[$column->type])) {
return [$this->constans[$column->type], $size];
$result = $this->constans[$column->type] . '(' . implode(',', $size) . ')';
if (!$column->allowNull) {
$result .= '->notNull()';
}
if ($column->defaultValue !== null) {
$default = is_string($column->defaultValue) ? "'" . addslashes($column->defaultValue) . "'" : $column->defaultValue;
$result .= "->defaultValue({$default})";
}
} else {
return [$column->dbType, ''];
$result = $column->dbType;
if (!empty($size)) {
$result.= '(' . implode(',', $size) . ')';
}
if (!$column->allowNull) {
$result .= ' NOT NULL';
}
if ($column->defaultValue !== null) {
$default = is_string($column->defaultValue) ? "'" . addslashes($column->defaultValue) . "'" : $column->defaultValue;
$result .= " DEFAULT {$default}";
}
$result = '"' . $result . '"';
}
return $result;
}

/**
Expand All @@ -261,30 +282,11 @@ protected function generateColumns($table)
$needPK = true;
foreach ($table->columns as $column) {
if ($column->autoIncrement) {
$columns[$column->name] = $column->type == Schema::TYPE_BIGINT ? 'Schema::TYPE_BIGPK' : 'Schema::TYPE_PK';
$columns[$column->name] = $column->type == Schema::TYPE_BIGINT ? '$this->bigPrimaryKey()' : '$this->primaryKey()';
$needPK = false;
continue;
}
list($type, $extra) = $this->getSchemaType($column);
$quote = "'";
if (!$column->allowNull) {
$extra .= ' NOT NULL';
}

if ($column->defaultValue !== null) {
$extra .= ' DEFAULT ';
if ($column->defaultValue instanceof Expression || is_numeric($column->defaultValue)) {
$extra .= $column->defaultValue;
} else {
$extra .= "'{$column->defaultValue}'";
$quote = '"';
}
}

if (!empty($extra)) {
$type = "$type . {$quote}$extra{$quote}";
}
$columns[$column->name] = $type;
$columns[$column->name] = $this->getSchemaType($column);
}
if ($needPK && !empty($table->primaryKey)) {
$pks = implode(']], [[', $table->primaryKey);
Expand Down Expand Up @@ -374,15 +376,15 @@ protected function getTableNames()
if (strpos($this->tableName, '*') !== false) {
if (($pos = strrpos($this->tableName, '.')) !== false) {
$schema = substr($this->tableName, 0, $pos);
$pattern = '/^'.str_replace('*', '\w+', substr($this->tableName, $pos + 1)).'$/';
$pattern = '/^' . str_replace('*', '\w+', substr($this->tableName, $pos + 1)) . '$/';
} else {
$schema = '';
$pattern = '/^'.str_replace('*', '\w+', $this->tableName).'$/';
$pattern = '/^' . str_replace('*', '\w+', $this->tableName) . '$/';
}

foreach ($db->schema->getTableNames($schema) as $table) {
if (preg_match($pattern, $table)) {
$tableNames[] = $schema === '' ? $table : ($schema.'.'.$table);
$tableNames[] = $schema === '' ? $table : ($schema . '.' . $table);
}
}
} elseif (($table = $db->getTableSchema($this->tableName, true)) !== null) {
Expand All @@ -406,9 +408,9 @@ public function generateTableName($tableName)

$db = $this->getDbConnection();
if (preg_match("/^{$db->tablePrefix}(.*?)$/", $tableName, $matches)) {
$tableName = '{{%'.$matches[1].'}}';
$tableName = '{{%' . $matches[1] . '}}';
} elseif (preg_match("/^(.*?){$db->tablePrefix}$/", $tableName, $matches)) {
$tableName = '{{'.$matches[1].'%}}';
$tableName = '{{' . $matches[1] . '%}}';
}
return $tableName;
}
Expand Down

0 comments on commit 55600b9

Please sign in to comment.