Skip to content

Commit 17381f2

Browse files
committed
Use type constants in Postgres schema.
1 parent 21b16a7 commit 17381f2

File tree

1 file changed

+42
-38
lines changed

1 file changed

+42
-38
lines changed

src/Database/Schema/PostgresSchema.php

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace Cake\Database\Schema;
1616

1717
use Cake\Database\Exception;
18+
use Cake\Database\Schema\TableSchema;
1819

1920
/**
2021
* Schema management/reflection features for Postgres.
@@ -90,56 +91,56 @@ protected function _convertColumn($column)
9091
return ['type' => $col, 'length' => null];
9192
}
9293
if (strpos($col, 'timestamp') !== false) {
93-
return ['type' => 'timestamp', 'length' => null];
94+
return ['type' => TableSchema::TYPE_TIMESTAMP, 'length' => null];
9495
}
9596
if (strpos($col, 'time') !== false) {
96-
return ['type' => 'time', 'length' => null];
97+
return ['type' => TableSchema::TYPE_TIME, 'length' => null];
9798
}
9899
if ($col === 'serial' || $col === 'integer') {
99-
return ['type' => 'integer', 'length' => 10];
100+
return ['type' => TableSchema::TYPE_INTEGER, 'length' => 10];
100101
}
101102
if ($col === 'bigserial' || $col === 'bigint') {
102-
return ['type' => 'biginteger', 'length' => 20];
103+
return ['type' => TableSchema::TYPE_BIGINTEGER, 'length' => 20];
103104
}
104105
if ($col === 'smallint') {
105-
return ['type' => 'smallinteger', 'length' => 5];
106+
return ['type' => TableSchema::TYPE_SMALLINTEGER, 'length' => 5];
106107
}
107108
if ($col === 'inet') {
108-
return ['type' => 'string', 'length' => 39];
109+
return ['type' => TableSchema::TYPE_STRING, 'length' => 39];
109110
}
110111
if ($col === 'uuid') {
111-
return ['type' => 'uuid', 'length' => null];
112+
return ['type' => TableSchema::TYPE_UUID, 'length' => null];
112113
}
113114
if ($col === 'char' || $col === 'character') {
114-
return ['type' => 'string', 'fixed' => true, 'length' => $length];
115+
return ['type' => TableSchema::TYPE_STRING, 'fixed' => true, 'length' => $length];
115116
}
116117
// money is 'string' as it includes arbitrary text content
117118
// before the number value.
118119
if (strpos($col, 'char') !== false ||
119120
strpos($col, 'money') !== false
120121
) {
121-
return ['type' => 'string', 'length' => $length];
122+
return ['type' => TableSchema::TYPE_STRING, 'length' => $length];
122123
}
123124
if (strpos($col, 'text') !== false) {
124-
return ['type' => 'text', 'length' => null];
125+
return ['type' => TableSchema::TYPE_TEXT, 'length' => null];
125126
}
126127
if ($col === 'bytea') {
127-
return ['type' => 'binary', 'length' => null];
128+
return ['type' => TableSchema::TYPE_BINARY, 'length' => null];
128129
}
129130
if ($col === 'real' || strpos($col, 'double') !== false) {
130-
return ['type' => 'float', 'length' => null];
131+
return ['type' => TableSchema::TYPE_FLOAT, 'length' => null];
131132
}
132133
if (strpos($col, 'numeric') !== false ||
133134
strpos($col, 'decimal') !== false
134135
) {
135-
return ['type' => 'decimal', 'length' => null];
136+
return ['type' => TableSchema::TYPE_DECIMAL, 'length' => null];
136137
}
137138

138139
if (strpos($col, 'json') !== false) {
139-
return ['type' => 'json', 'length' => null];
140+
return ['type' => TableSchema::TYPE_JSON, 'length' => null];
140141
}
141142

142-
return ['type' => 'string', 'length' => null];
143+
return ['type' => TableSchema::TYPE_STRING, 'length' => null];
143144
}
144145

145146
/**
@@ -149,7 +150,7 @@ public function convertColumnDescription(TableSchema $schema, $row)
149150
{
150151
$field = $this->_convertColumn($row['type']);
151152

152-
if ($field['type'] === 'boolean') {
153+
if ($field['type'] === TableSchema::TYPE_BOOLEAN) {
153154
if ($row['default'] === 'true') {
154155
$row['default'] = 1;
155156
}
@@ -351,38 +352,40 @@ public function columnSql(TableSchema $schema, $name)
351352
$data = $schema->column($name);
352353
$out = $this->_driver->quoteIdentifier($name);
353354
$typeMap = [
354-
'tinyinteger' => ' SMALLINT',
355-
'smallinteger' => ' SMALLINT',
356-
'boolean' => ' BOOLEAN',
357-
'binary' => ' BYTEA',
358-
'float' => ' FLOAT',
359-
'decimal' => ' DECIMAL',
360-
'date' => ' DATE',
361-
'time' => ' TIME',
362-
'datetime' => ' TIMESTAMP',
363-
'timestamp' => ' TIMESTAMP',
364-
'uuid' => ' UUID',
365-
'json' => ' JSONB'
355+
TableSchema::TYPE_TINYINTEGER => ' SMALLINT',
356+
TableSchema::TYPE_SMALLINTEGER => ' SMALLINT',
357+
TableSchema::TYPE_BINARY => ' BYTEA',
358+
TableSchema::TYPE_BOOLEAN => ' BOOLEAN',
359+
TableSchema::TYPE_FLOAT => ' FLOAT',
360+
TableSchema::TYPE_DECIMAL => ' DECIMAL',
361+
TableSchema::TYPE_DATE => ' DATE',
362+
TableSchema::TYPE_TIME => ' TIME',
363+
TableSchema::TYPE_DATETIME => ' TIMESTAMP',
364+
TableSchema::TYPE_TIMESTAMP => ' TIMESTAMP',
365+
TableSchema::TYPE_UUID => ' UUID',
366+
TableSchema::TYPE_JSON => ' JSONB'
366367
];
367368

368369
if (isset($typeMap[$data['type']])) {
369370
$out .= $typeMap[$data['type']];
370371
}
371372

372-
if ($data['type'] === 'integer' || $data['type'] === 'biginteger') {
373-
$type = $data['type'] === 'integer' ? ' INTEGER' : ' BIGINT';
373+
if ($data['type'] === TableSchema::TYPE_INTEGER || $data['type'] === TableSchema::TYPE_BIGINTEGER) {
374+
$type = $data['type'] === TableSchema::TYPE_INTEGER ? ' INTEGER' : ' BIGINT';
374375
if ([$name] === $schema->primaryKey() || $data['autoIncrement'] === true) {
375-
$type = $data['type'] === 'integer' ? ' SERIAL' : ' BIGSERIAL';
376+
$type = $data['type'] === TableSchema::TYPE_INTEGER ? ' SERIAL' : ' BIGSERIAL';
376377
unset($data['null'], $data['default']);
377378
}
378379
$out .= $type;
379380
}
380381

381-
if ($data['type'] === 'text' && $data['length'] !== TableSchema::LENGTH_TINY) {
382+
if ($data['type'] === TableSchema::TYPE_TEXT && $data['length'] !== TableSchema::LENGTH_TINY) {
382383
$out .= ' TEXT';
383384
}
384385

385-
if ($data['type'] === 'string' || ($data['type'] === 'text' && $data['length'] === TableSchema::LENGTH_TINY)) {
386+
if ($data['type'] === TableSchema::TYPE_STRING ||
387+
($data['type'] === TableSchema::TYPE_TEXT && $data['length'] === TableSchema::LENGTH_TINY)
388+
) {
386389
$isFixed = !empty($data['fixed']);
387390
$type = ' VARCHAR';
388391
if ($isFixed) {
@@ -394,16 +397,16 @@ public function columnSql(TableSchema $schema, $name)
394397
}
395398
}
396399

397-
$hasCollate = ['text', 'string'];
400+
$hasCollate = [TableSchema::TYPE_TEXT, TableSchema::TYPE_STRING];
398401
if (in_array($data['type'], $hasCollate, true) && isset($data['collate']) && $data['collate'] !== '') {
399402
$out .= ' COLLATE "' . $data['collate'] . '"';
400403
}
401404

402-
if ($data['type'] === 'float' && isset($data['precision'])) {
405+
if ($data['type'] === TableSchema::TYPE_FLOAT && isset($data['precision'])) {
403406
$out .= '(' . (int)$data['precision'] . ')';
404407
}
405408

406-
if ($data['type'] === 'decimal' &&
409+
if ($data['type'] === TableSchema::TYPE_DECIMAL &&
407410
(isset($data['length']) || isset($data['precision']))
408411
) {
409412
$out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
@@ -414,8 +417,9 @@ public function columnSql(TableSchema $schema, $name)
414417
}
415418

416419
if (isset($data['default']) &&
417-
in_array($data['type'], ['timestamp', 'datetime']) &&
418-
strtolower($data['default']) === 'current_timestamp') {
420+
in_array($data['type'], [TableSchema::TYPE_TIMESTAMP, TableSchema::TYPE_DATETIME]) &&
421+
strtolower($data['default']) === 'current_timestamp'
422+
) {
419423
$out .= ' DEFAULT CURRENT_TIMESTAMP';
420424
} elseif (isset($data['default'])) {
421425
$defaultValue = $data['default'];

0 commit comments

Comments
 (0)