Skip to content

Commit da08287

Browse files
author
Bizley
authored
Merge pull request #61 from bizley/column-size-2x
2 parents 55e1a50 + 5ec3040 commit da08287

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1273
-221
lines changed

src/Generator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ protected function getTableColumns($indexes = [], $schema = null)
192192
'schema' => $schema,
193193
'name' => $column->name,
194194
'type' => $column->type,
195+
'defaultMapping' => $this->db->schema->queryBuilder->typeMap[$column->type],
195196
'size' => $column->size,
196197
'precision' => $column->precision,
197198
'scale' => $column->scale,

src/controllers/MigrationController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
* Generates migration file based on the existing database table and previous migrations.
2727
*
2828
* @author Paweł Bizley Brzozowski
29-
* @version 2.8.0
29+
* @version 2.9.0
3030
* @license Apache 2.0
3131
* https://github.com/bizley/yii2-migration
3232
*/
3333
class MigrationController extends Controller
3434
{
35-
protected $version = '2.8.0';
35+
protected $version = '2.9.0';
3636

3737
/**
3838
* @var string Default command action.

src/table/TableColumn.php

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ class TableColumn extends Object
2424
*/
2525
public $type;
2626

27+
/**
28+
* @var string
29+
* @since 2.9.0
30+
*/
31+
public $defaultMapping;
32+
2733
/**
2834
* @var bool|null
2935
*/
@@ -66,13 +72,15 @@ class TableColumn extends Object
6672

6773
/**
6874
* @var bool
75+
* Starting from 2.9.0 it's false by default.
6976
*/
70-
public $isPrimaryKey;
77+
public $isPrimaryKey = false;
7178

7279
/**
7380
* @var bool
81+
* Starting from 2.9.0 it's false by default.
7482
*/
75-
public $autoIncrement;
83+
public $autoIncrement = false;
7684

7785
/**
7886
* @var string
@@ -287,4 +295,52 @@ public function removePKAppend()
287295

288296
return !empty($formattedAppend) ? $formattedAppend : null;
289297
}
298+
299+
/**
300+
* @param bool $generalSchema
301+
* @return string|null
302+
* @since 2.9.0
303+
*/
304+
public function getRenderLength($generalSchema)
305+
{
306+
$length = $this->length;
307+
308+
if ($length === null) {
309+
return $length;
310+
}
311+
312+
if (!$generalSchema) {
313+
if ($length === 'max') {
314+
return '\'max\'';
315+
}
316+
317+
return (string)$length;
318+
}
319+
320+
if (str_replace(' ', '', (string)$length) !== $this->getDefaultLength()) {
321+
if ($length === 'max') {
322+
return '\'max\'';
323+
}
324+
325+
return (string)$length;
326+
}
327+
328+
return null;
329+
}
330+
331+
private function getDefaultLength()
332+
{
333+
if ($this->defaultMapping !== null) {
334+
if (preg_match('/\(([\d,]+)\)/', $this->defaultMapping, $matches)) {
335+
return $matches[1];
336+
}
337+
338+
if (preg_match('/\(max\)/', $this->defaultMapping)) {
339+
// MSSQL
340+
return 'max';
341+
}
342+
}
343+
344+
return null;
345+
}
290346
}

src/table/TableColumnBigInt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function buildSpecificDefinition($table)
4444
if ($table->generalSchema && !$table->primaryKey->isComposite() && $this->isColumnInPK($table->primaryKey)) {
4545
$this->isPkPossible = false;
4646
$this->isNotNullPossible = false;
47-
$this->definition[] = 'bigPrimaryKey()';
47+
$this->definition[] = 'bigPrimaryKey(' . $this->getRenderLength($table->generalSchema) . ')';
4848
} else {
49-
$this->definition[] = 'bigInteger(' . ($table->generalSchema ? null : $this->length) . ')';
49+
$this->definition[] = 'bigInteger(' . $this->getRenderLength($table->generalSchema) . ')';
5050
}
5151
}
5252
}

src/table/TableColumnBigPK.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function setLength($value)
4141
*/
4242
public function buildSpecificDefinition($table)
4343
{
44-
$this->definition[] = 'bigPrimaryKey(' . ($table->generalSchema ? null : $this->length) . ')';
44+
$this->definition[] = 'bigPrimaryKey(' . $this->getRenderLength($table->generalSchema) . ')';
4545
if ($table->generalSchema) {
4646
$this->isPkPossible = false;
4747
$this->isNotNullPossible = false;

src/table/TableColumnBinary.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function setLength($value)
4141
*/
4242
public function buildSpecificDefinition($table)
4343
{
44-
$this->definition[] = 'binary(' . ($table->generalSchema ? null : $this->length) . ')';
44+
$this->definition[] = 'binary(' . $this->getRenderLength($table->generalSchema) . ')';
4545
}
4646
}

src/table/TableColumnChar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ public function setLength($value)
3333
*/
3434
public function buildSpecificDefinition($table)
3535
{
36-
$this->definition[] = 'char(' . ($table->generalSchema ? null : $this->length) . ')';
36+
$this->definition[] = 'char(' . $this->getRenderLength($table->generalSchema) . ')';
3737
}
3838
}

src/table/TableColumnDateTime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public function setLength($value)
4040
*/
4141
public function buildSpecificDefinition($table)
4242
{
43-
$this->definition[] = 'dateTime(' . ($table->generalSchema ? null : $this->length) . ')';
43+
$this->definition[] = 'dateTime(' . $this->getRenderLength($table->generalSchema) . ')';
4444
}
4545
}

src/table/TableColumnDecimal.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class TableColumnDecimal extends TableColumn
2727
public function getLength()
2828
{
2929
return in_array($this->schema, $this->lengthSchemas, true)
30-
? ($this->precision . ($this->scale ? ', ' . $this->scale : null))
30+
? ($this->precision . (($this->scale || (int)$this->scale === 0) ? ', ' . $this->scale : null))
3131
: null;
3232
}
3333

@@ -38,18 +38,18 @@ public function getLength()
3838
public function setLength($value)
3939
{
4040
if (in_array($this->schema, $this->lengthSchemas, true)) {
41-
if (is_array($value)) {
42-
$length = $value;
43-
} else {
44-
$length = preg_split('/\s*,\s*/', $value);
45-
}
41+
$length = is_array($value) ? $value : preg_split('/\s*,\s*/', $value);
4642

4743
if (isset($length[0]) && !empty($length[0])) {
4844
$this->precision = $length[0];
45+
} else {
46+
$this->precision = 0;
4947
}
5048

5149
if (isset($length[1]) && !empty($length[1])) {
5250
$this->scale = $length[1];
51+
} else {
52+
$this->scale = 0;
5353
}
5454
}
5555
}
@@ -60,6 +60,6 @@ public function setLength($value)
6060
*/
6161
public function buildSpecificDefinition($table)
6262
{
63-
$this->definition[] = 'decimal(' . ($table->generalSchema ? null : $this->length) . ')';
63+
$this->definition[] = 'decimal(' . $this->getRenderLength($table->generalSchema) . ')';
6464
}
6565
}

src/table/TableColumnDouble.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ public function setLength($value)
4040
*/
4141
public function buildSpecificDefinition($table)
4242
{
43-
$this->definition[] = 'double(' . ($table->generalSchema ? null : $this->length) . ')';
43+
$this->definition[] = 'double(' . $this->getRenderLength($table->generalSchema) . ')';
4444
}
4545
}

0 commit comments

Comments
 (0)