-
Notifications
You must be signed in to change notification settings - Fork 5
Add - During migration manage hasOne relation type. #16
Conversation
* add transcode table for field type => datatype database * tested on SQLite and MySQL
more space is better than less, this class is very useful during development, after that will be disabled and database must be optimized with other tools. i changed because i had a problem storing serialized EXIF in array datatype
climate error correction
switch to codeformatting PSR-1,PSR-2 add hasOne in Test
…" DB fixed mysql float was mistyped uppercase
Codecov Report
@@ Coverage Diff @@
## develop #16 +/- ##
=============================================
+ Coverage 65.82% 71.25% +5.43%
- Complexity 131 135 +4
=============================================
Files 6 6
Lines 316 327 +11
=============================================
+ Hits 208 233 +25
+ Misses 108 94 -14
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop support of php < 7.2.0
@@ -15,7 +15,7 @@ | |||
} | |||
], | |||
"require": { | |||
"php": ">=7.0.0", | |||
"php": ">=7.2.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
drop php < 7.2.0
@@ -24,6 +24,7 @@ class MySQL extends \atk4\schema\Migration | |||
|
|||
/** @var array use this array in extended classes to overwrite or extend values of default mapping */ | |||
public $mapToAgile = [ | |||
0 => ['string'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default field type for Mysql = string
@@ -7,6 +7,10 @@ class SQLite extends \atk4\schema\Migration | |||
/** @var string Expression to create primary key */ | |||
public $primary_key_expr = 'integer primary key autoincrement'; | |||
|
|||
public $mapToAgile = [ | |||
0 => ['string'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default field type for SQLite = string
{ | ||
// remove parenthesis | ||
$type = trim(preg_replace('/\(.*/', '', strtolower($type))); | ||
|
||
$map = array_merge($this->defaultMapToAgile, $this->mapToAgile); | ||
$map = array_replace($this->defaultMapToAgile, $this->mapToAgile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this must be array_replace, i think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes if you use numeric keys then it should be array_replace
// FieldType is stored in the reference field | ||
if ($field->reference instanceof HasOne) { | ||
|
||
// @TODO if this can be done better? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i leave a TODO, probably can done better.
I need to get the field type of the Referenced hasOne.
To find it :
- i create the referenced model stored in $field->reference->model, using the actual persistence/connection
- i call $refModel->get($field->reference->their_field) but $their_field is protected
- i think $their_field must remain protected
- for this specific case, i think the best option is to use Reflection
- if $their_field is null use $id_field of the referenced model
- if try to get the type of this field and if is null i return integer
I use reflection because migration is not a normal "production" and performance are not so important here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically looks good. I didn't actually test. Was just checking code changes and added few comments.
src/Migration.php
Outdated
|
||
/** @var Model $refModel */ | ||
$refModel = new $refModelClass($m->persistence); | ||
$refModel->getField($their_field); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you don't need this line
src/Migration.php
Outdated
$refModel = new $refModelClass($m->persistence); | ||
$refModel->getField($their_field); | ||
|
||
$type = $refModel->getField($their_field)->type ?? 'integer'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about also checking $m->getField($refModel->our_field ?: 'id')->type before falling back to integer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i do it before, i should change the name the var to $reference_field
in place of $their_filed
which is misleading
on line 220 : $their_field = $reference_their_field ?? $field->reference->owner->id_field;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i changed comment and naming to be less missleading
{ | ||
// remove parenthesis | ||
$type = trim(preg_replace('/\(.*/', '', strtolower($type))); | ||
|
||
$map = array_merge($this->defaultMapToAgile, $this->mapToAgile); | ||
$map = array_replace($this->defaultMapToAgile, $this->mapToAgile); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes if you use numeric keys then it should be array_replace
src/Migration.php
Outdated
* | ||
* @return string | ||
*/ | ||
public function getModelPHPCode(string $table, string $model, string $id_field = 'id', string $namespace = '\Your\Project\Models') :string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this method is here?
It's of course interesting to have, but I don't see it's relation to atk4\schema.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i removed the method and the related test
when styleCi will be ok.
i will review my code directly on critical changes, just two, but i think i need to specify it clearly.