diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 9d1afe1378..e22afe9479 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -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; @@ -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); } } @@ -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"), + ]; + } } diff --git a/modules/system/helpers/Migration.php b/modules/system/helpers/Migration.php deleted file mode 100644 index b94d958367..0000000000 --- a/modules/system/helpers/Migration.php +++ /dev/null @@ -1,65 +0,0 @@ -= 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"), - ]; - } -}