Skip to content

Commit

Permalink
move back to protected method
Browse files Browse the repository at this point in the history
  • Loading branch information
mjauvin committed Oct 15, 2023
1 parent a66eef9 commit 5830179
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 67 deletions.
56 changes: 54 additions & 2 deletions modules/system/console/CreateMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use InvalidArgumentException;
use System\Classes\VersionManager;
use System\Console\BaseScaffoldCommand;
use System\Helpers\Migration;
use Winter\Storm\Database\Model;
use Winter\Storm\Support\Str;
use Yaml;

Expand Down Expand Up @@ -255,7 +255,7 @@ protected function processVars(array $vars): array
continue;
}

$vars['fields'][$field] = Migration::mapFieldType($field, $config, $model);
$vars['fields'][$field] = $this->mapFieldType($field, $config, $model);
}
}

Expand Down Expand Up @@ -301,4 +301,56 @@ protected function getNextVersion($currentVersion): string
$parts[count($parts) - 1] = (int) $parts[count($parts) - 1] + 1;
return 'v' . implode('.', $parts);
}

/**
* Maps model fields config to DB Schema column types.
*/
protected function mapFieldType(string $fieldName, array $fieldConfig, ?Model $model = null) : array
{
switch ($fieldConfig['type'] ?? 'text') {
case 'checkbox':
case 'switch':
$dbType = 'boolean';
break;
case 'number':
if (isset($fieldConfig['step']) && is_int($fieldConfig['step'])) {
$dbType = 'integer';
} else {
$dbType = 'double';
}
if ($dbType === 'integer' && isset($fieldConfig['min']) && $fieldConfig['min'] >= 0) {
$dbType = 'unsignedInteger';
}
break;
case 'range':
$dbType = 'unsignedInteger';
break;
case 'datepicker':
$dbType = 'datetime';
break;
case 'markdown':
$dbType = 'mediumText';
break;
case 'textarea':
$dbType = 'text';
break;
default:
$dbType = 'string';
}

if ($model) {
$rule = array_get($model->rules ?? [], $fieldName, '');
$rule = is_array($rule) ? implode(',', $rule) : $rule;

$required = str_contains($rule, 'required') ? true : $fieldConfig['required'] ?? false;
} else {
$required = $fieldConfig['required'] ?? false;
}

return [
'type' => $dbType,
'required' => $required,
'index' => in_array($fieldName, ["slug"]) or str_ends_with($fieldName, "_id"),
];
}
}
65 changes: 0 additions & 65 deletions modules/system/helpers/Migration.php

This file was deleted.

0 comments on commit 5830179

Please sign in to comment.