Skip to content
This repository has been archived by the owner on Oct 15, 2020. It is now read-only.

Commit

Permalink
Merge pull request #13 from atk4/feature/field-types
Browse files Browse the repository at this point in the history
Improve field types support and extending
  • Loading branch information
romaninsh authored May 14, 2019
2 parents 8a3a4dc + 4d1e86a commit a7fa1e5
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 69 deletions.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
"atk4/dsql": "dev-develop"
},
"suggest": {
"atk4/dev-develop": "*",
"atk4/data": "*",
"atk4/ui": "*",
"jdorn/sql-formatter": "*"
},
"require-dev": {
"atk4/data": "dev-develop",
"atk4/ui": "dev-develop",
"phpunit/phpunit": "<6",
"phpunit/dbunit": ">=1.2",
"phpunit/phpcov": "*",
Expand Down
107 changes: 43 additions & 64 deletions src/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,38 @@ class Migration extends Expression
/** @var string Expression to create primary key */
public $primary_key_expr = 'integer primary key autoincrement';

/** @var array Conversion mapping from Agile Data types to persistence types */
protected $defaultMapToPersistence = [
['varchar', 255], // default
'boolean' => ['tinyint', 1],
'integer' => ['int'],
'money' => ['decimal', 12, 2],
'float' => ['decimal', 16, 6],
'date' => ['date'],
'datetime' => ['date'],
'time' => ['varchar', 8],
'text' => ['text'],
'array' => ['text'],
'object' => ['text'],
];

/** @var array use this array in extended classes to overwrite or extend values of default mapping */
public $mapToPersistence = [];

/** @var array Conversion mapping from persistence types to Agile Data types */
protected $defaultMapToAgile = [
[null], // default
'tinyint' => ['boolean'],
'int' => ['integer'],
'decimal' => ['float'],
'numeric' => ['float'],
'date' => ['datetime'],
'text' => ['text'],
];

/** @var array use this array in extended classes to overwrite or extend values of default mapping */
public $mapToAgile = [];

/**
* Create new migration.
*
Expand All @@ -46,12 +78,12 @@ public function __construct($source, $params = [])
$this->connection = $source;

return;
} elseif ($source instanceof \atk4\data\Persistence_SQL || $source instanceof \atk4\data\Persistence\SQL) {
} elseif ($source instanceof \atk4\data\Persistence\SQL) {
$this->connection = $source->connection;

return;
} elseif ($source instanceof \atk4\data\Model) {
if ($source->persistence && ($source->persistence instanceof \atk4\data\Persistence_SQL || $source->persistence instanceof \atk4\data\Persistence\SQL)) {
if ($source->persistence && ($source->persistence instanceof \atk4\data\Persistence\SQL)) {
$this->connection = $source->persistence->connection;

$this->setModel($source);
Expand Down Expand Up @@ -369,33 +401,17 @@ public function describeTable($table)
*
* @param string $type SQL field type
*
* @return string
* @return string|null
*/
public function getModelFieldType($type)
{
$type = preg_replace('/\(.*/', '', strtolower($type)); // remove parenthesis

if (substr($type, 0, 7) == 'varchar' || substr($type, 0, 4) == 'char' || substr($type, 0, 4) == 'enum') {
$type = null;
}
// remove parenthesis
$type = trim(preg_replace('/\(.*/', '', strtolower($type)));

if ($type == 'tinyint') {
$type = 'boolean';
}

if ($type == 'int') {
$type = 'integer';
}

if ($type == 'decimal') {
$type = 'float';
}

if ($type == 'longtext' || $type == 'longblob') {
$type = 'text';
}
$map = array_merge($this->defaultMapToAgile, $this->mapToAgile);
$a = array_key_exists($type, $map) ? $map[$type] : $map[0];

return $type;
return $a[0];
}

/**
Expand All @@ -409,48 +425,11 @@ public function getModelFieldType($type)
public function getSQLFieldType($type, $options = [])
{
$type = strtolower($type);
$len = null;

switch ($type) {
case 'boolean':
$type = 'tinyint';
$len = 1;
break;
case 'integer':
$type = 'int';
break;
case 'money':
$type = 'decimal';
$len = '12,2';
break;
case 'float':
$type = 'decimal';
$len = '16,6';
break;
case 'date':
case 'datetime':
$type = 'date';
break;
case 'time':
$type = 'varchar';
$len = '8';
break;
case 'text':
$type = 'longtext';
break;
case 'array':
case 'object':
$type = 'longtext';
break;
default:
$type = 'varchar';
$len = '255';
break;
}

$len = $options['len'] ?? $len;
$map = array_merge($this->defaultMapToPersistence, $this->mapToPersistence);
$a = array_key_exists($type, $map) ? $map[$type] : $map[0];

return $len !== null ? $type.'('.$len.')' : $type;
return $a[0].(count($a) > 1 ? ' ('.implode(',', array_slice($a, 1)).')' : '');
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/Migration/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ class MySQL extends \atk4\schema\Migration
/** @var string Expression to create primary key */
public $primary_key_expr = 'integer primary key auto_increment';

/** @var array use this array in extended classes to overwrite or extend values of default mapping */
public $mapToPersistence = [
'text' => ['longtext'],
'array' => ['longtext'],
'object' => ['longtext'],
];

/** @var array use this array in extended classes to overwrite or extend values of default mapping */
public $mapToAgile = [
'longtext' => ['text'],
'longblob' => ['text'],
];

/**
* Return database table descriptions.
* DB engine specific.
Expand Down
18 changes: 17 additions & 1 deletion src/Migration/PgSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

namespace atk4\schema\Migration;

// NOT IMPLEMENTED !!!
// ONLY PARTIALLY IMPLEMENTED
class PgSQL extends \atk4\schema\Migration
{
/** @var string Expression to create primary key */
public $primary_key_expr = 'bigint generated by default as identity(start with 1) primary key';

/** @var array use this array in extended classes to overwrite or extend values of default mapping */
public $mapToPersistence = [
'boolean' => ['boolean'],
'date' =>  ['date'],
'datetime' =>  ['timestamp'],
'time' => ['time'],
];

/** @var array use this array in extended classes to overwrite or extend values of default mapping */
public $mapToAgile = [
'boolean' => ['boolean'],
'date' => ['date'],
'datetime' => ['datetime'],
'time' => ['time'],
];
}
9 changes: 7 additions & 2 deletions src/MigratorConsole.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@
namespace atk4\schema;

/**
* Makes sure your database is adjusted for one or serveral models,
* Makes sure your database is adjusted for one or several models,
* that you specify.
*/
class MigratorConsole extends \atk4\ui\Console
{
/** @var string Name of migrator class to use */
public $migrator_class = Migration\Mysql::class;

/**
* Provided with array of models, perform migration for each of them.
*
* @param array $models
*/
public function migrateModels($models)
{
Expand All @@ -24,7 +29,7 @@ public function migrateModels($models)
$p->add($model);
}

$m = new \atk4\schema\Migration\MySQL($model);
$m = new $this->migrator_class($model);
$result = $m->migrate();

$c->debug(' '.get_class($model).'.. '.$result);
Expand Down
2 changes: 1 addition & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testImportTable()

$m2->mode('create');

$q1 = preg_replace('/\([0-9,]*\)/i', '', $m->getDebugQuery()); // remove parentesis otherwise we can't differe money from float etc.
$q1 = preg_replace('/\([0-9,]*\)/i', '', $m->getDebugQuery()); // remove parenthesis otherwise we can't differ money from float etc.
$q2 = preg_replace('/\([0-9,]*\)/i', '', $m2->getDebugQuery());
$this->assertEquals($q1, $q2);
}
Expand Down

0 comments on commit a7fa1e5

Please sign in to comment.