From 6bdd938f39465d91998182c0775d7bf9e8d693f6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Jul 2023 10:31:35 -0400 Subject: [PATCH 01/44] populate migration file with actual model fields as best as we can --- modules/system/console/CreateMigration.php | 118 +++++++++++++++++- .../scaffold/migration/migration.create.stub | 13 +- 2 files changed, 128 insertions(+), 3 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 5d730e6ad6..eea8e4dbbb 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -1,5 +1,6 @@ option('create') && $this->option('update')) { - $this->error('The create & update options cannot both be set at the same time'); - return false; + throw new InvalidArgumentException('The create & update options cannot both be set at the same time'); } if ($this->option('create')) { + if (empty($model)) { + throw new InvalidArgumentException('The create options requires the --model option'); + } $scaffold = 'create'; } elseif ($this->option('update')) { $scaffold = 'update'; @@ -189,6 +192,7 @@ protected function prepareVars(): array 'name' => $name, 'author' => $author, 'plugin' => $plugin, + 'model' => $model, 'version' => $version, ]; @@ -199,6 +203,116 @@ protected function prepareVars(): array return $vars; } + protected function processVars($vars): array + { + $vars = parent::processVars($vars); + + $vars['fields'] = []; + + try { + $fields_path = '$/' . $vars['plugin_url'] . '/models/' . $vars['lower_model'] . '/fields.yaml'; + $fields = Yaml::parseFile(File::symbolizePath($fields_path)); + } catch(\Exception $e) { + die($e->getMessage()); + } + + $modelName = $vars['plugin_namespace'] . '\\Models\\' . $vars['model']; + + $vars['model'] = $model = new $modelName(); + + foreach (['fields', 'tabs', 'secondaryTabs'] as $type) { + if (!isset($fields[$type])) { + continue; + } + if ($type === 'fields') { + $fieldList = $fields[$type]; + } else { + $fieldList = $fields[$type]['fields']; + } + + foreach ($fieldList as $field => $config) { + if (str_contains($field, '@')) { + list($field, $context) = explode('@', $field); + } + + $type = $config['type'] ?? 'text'; + + if (str_starts_with($field, '_') + or $field === 'id' + or str_contains($field, '[') + or in_array($type, ['fileupload','relation','relationmanager','section','hint']) + or in_array($field, $model->purgeable ?? []) + or $model->getRelationType($field) + ) { + continue; + } + + $vars['fields'][$field] = $this->mapFieldType($field, $config); + } + } + + foreach ($model->getRelationDefinitions() as $relationType => $definitions) { + if (in_array($relationType, ['belongsTo', 'hasOne'])) { + foreach (array_keys($definitions) as $relation) { + $vars['fields'][$relation . '_id'] = [ + 'type' => 'foreignId', + 'index' => true, + 'required' => true, + ]; + } + } + } + + if ($model->methodExists('getSortOrderColumn')) { + $field = $model->getSortOrderColumn(); + $vars['fields'][$field] = [ + 'type' => 'unsignedinteger', + 'required' => false, + 'index' => true, + ]; + } + + $vars['primaryKey'] = $model->getKeyName(); + $vars['jsonable'] = $model->getJsonable(); + $vars['timestamps'] = $model->timestamps; + + if ($morphable = $model->morphTo) { + $vars['morphable'] = array_keys($morphable); + } + + return $vars; + } + + protected function mapFieldType($name, $fieldConfig) + { + switch ($fieldConfig['type'] ?? 'text') { + case 'checkbox': + case 'switch': + $dbType = 'unsignedinteger'; + break; + case 'number': + case 'range': + $dbType = 'integer'; + break; + case 'datepicker': + $dbType = 'timestamp'; + break; + case 'markdown': + case 'textarea': + $dbType = 'text'; + break; + default: + $dbType = 'string'; + } + $required = $fieldConfig['required'] ?? false; + + return [ + 'type' => $dbType, + 'required' => $required, + 'index' => in_array($name, ["slug"]) or str_ends_with($name, "_id"), + ]; + } + /** * Get the next version number based on the current number. */ diff --git a/modules/system/console/scaffold/migration/migration.create.stub b/modules/system/console/scaffold/migration/migration.create.stub index f193cd696f..0fbed6f672 100644 --- a/modules/system/console/scaffold/migration/migration.create.stub +++ b/modules/system/console/scaffold/migration/migration.create.stub @@ -14,8 +14,19 @@ return new class extends Migration public function up() { Schema::create('{{ table }}', function (Blueprint $table) { - $table->id(); + $table->increments({{ primaryKey }}); +{% for field,config in fields %} + $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; +{% endfor %} +{% for field in jsonable %} + $table->text('{{ field }}')->nullable(); +{% endfor %} +{% for field in morphable %} + $table->nullableMorphs('{{ field }}', 'morphable_index'); +{% endfor %} +{% if timestamps %} $table->timestamps(); +{% endif %} }); } From f59f6f1cb9e29fb5a5c51dceb870100ba4047cf6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Jul 2023 10:37:36 -0400 Subject: [PATCH 02/44] add missing space after catch keyword --- modules/system/console/CreateMigration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index eea8e4dbbb..467f6a58b9 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -212,7 +212,7 @@ protected function processVars($vars): array try { $fields_path = '$/' . $vars['plugin_url'] . '/models/' . $vars['lower_model'] . '/fields.yaml'; $fields = Yaml::parseFile(File::symbolizePath($fields_path)); - } catch(\Exception $e) { + } catch (\Exception $e) { die($e->getMessage()); } From d50680d0b7f9cf2ae0a8a95e8c04e0b9adbdbdc4 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Jul 2023 10:40:28 -0400 Subject: [PATCH 03/44] do not hardcode the primary key --- modules/system/console/CreateMigration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 467f6a58b9..0424532a4f 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -238,7 +238,7 @@ protected function processVars($vars): array $type = $config['type'] ?? 'text'; if (str_starts_with($field, '_') - or $field === 'id' + or $field === $model->getKeyName() or str_contains($field, '[') or in_array($type, ['fileupload','relation','relationmanager','section','hint']) or in_array($field, $model->purgeable ?? []) From 191b425d761d57ceddc8ab5ea602437e36cc107e Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Jul 2023 10:49:38 -0400 Subject: [PATCH 04/44] do not process fields if not in create mode --- modules/system/console/CreateMigration.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 0424532a4f..53a13fee3c 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -207,6 +207,10 @@ protected function processVars($vars): array { $vars = parent::processVars($vars); + if (!$this->option('create')) { + return; + } + $vars['fields'] = []; try { From 8b9dd19356a9153bf6034a8f3bd1f0549f54be02 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Jul 2023 10:50:42 -0400 Subject: [PATCH 05/44] return vars --- modules/system/console/CreateMigration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 53a13fee3c..f70ab18cfe 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -208,7 +208,7 @@ protected function processVars($vars): array $vars = parent::processVars($vars); if (!$this->option('create')) { - return; + return $vars; } $vars['fields'] = []; From 934f00f4d88892f1d3b5de47c0c6109e94175ae6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 4 Jul 2023 15:12:47 -0400 Subject: [PATCH 06/44] add missing quotes around the primaryKey --- modules/system/console/scaffold/migration/migration.create.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/scaffold/migration/migration.create.stub b/modules/system/console/scaffold/migration/migration.create.stub index 0fbed6f672..15113276ce 100644 --- a/modules/system/console/scaffold/migration/migration.create.stub +++ b/modules/system/console/scaffold/migration/migration.create.stub @@ -14,7 +14,7 @@ return new class extends Migration public function up() { Schema::create('{{ table }}', function (Blueprint $table) { - $table->increments({{ primaryKey }}); + $table->increments('{{ primaryKey }}'); {% for field,config in fields %} $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; {% endfor %} From 5978677cd71fd7decd35086c886946e59bed2592 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 07:34:55 -0400 Subject: [PATCH 07/44] use mediumText type for jsonable fields --- modules/system/console/CreateMigration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index f70ab18cfe..cbfbb4ec34 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -303,7 +303,7 @@ protected function mapFieldType($name, $fieldConfig) break; case 'markdown': case 'textarea': - $dbType = 'text'; + $dbType = 'mediumText'; break; default: $dbType = 'string'; From a94012895dd14be3496864cb2ac20fd148c0cd65 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 07:54:34 -0400 Subject: [PATCH 08/44] only generate fields in create stub if --model option is supplied --- modules/system/console/CreateMigration.php | 9 ++++----- .../console/scaffold/migration/migration.create.stub | 6 +++++- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index cbfbb4ec34..976651ae26 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -162,9 +162,6 @@ protected function prepareVars(): array } if ($this->option('create')) { - if (empty($model)) { - throw new InvalidArgumentException('The create options requires the --model option'); - } $scaffold = 'create'; } elseif ($this->option('update')) { $scaffold = 'update'; @@ -192,10 +189,12 @@ protected function prepareVars(): array 'name' => $name, 'author' => $author, 'plugin' => $plugin, - 'model' => $model, 'version' => $version, ]; + if (!empty($model)) { + $vars['model'] = $model; + } if (!empty($table)) { $vars['table'] = $table; } @@ -207,7 +206,7 @@ protected function processVars($vars): array { $vars = parent::processVars($vars); - if (!$this->option('create')) { + if (empty($vars['model'])) { return $vars; } diff --git a/modules/system/console/scaffold/migration/migration.create.stub b/modules/system/console/scaffold/migration/migration.create.stub index 15113276ce..63213aac5c 100644 --- a/modules/system/console/scaffold/migration/migration.create.stub +++ b/modules/system/console/scaffold/migration/migration.create.stub @@ -14,7 +14,11 @@ return new class extends Migration public function up() { Schema::create('{{ table }}', function (Blueprint $table) { +{% if primaryKey %} $table->increments('{{ primaryKey }}'); +{% else %} + $table->id(); +{% endif %} {% for field,config in fields %} $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; {% endfor %} @@ -24,7 +28,7 @@ return new class extends Migration {% for field in morphable %} $table->nullableMorphs('{{ field }}', 'morphable_index'); {% endfor %} -{% if timestamps %} +{% if not model or timestamps %} $table->timestamps(); {% endif %} }); From ff06eabfdad2eaed62f8966e74072cb47225991b Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 08:05:21 -0400 Subject: [PATCH 09/44] autocreate model fields in --update mode as well if --model option supplied --- .../scaffold/migration/migration.update.stub | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/system/console/scaffold/migration/migration.update.stub b/modules/system/console/scaffold/migration/migration.update.stub index 6703716169..303d53b7b8 100644 --- a/modules/system/console/scaffold/migration/migration.update.stub +++ b/modules/system/console/scaffold/migration/migration.update.stub @@ -14,7 +14,15 @@ return new class extends Migration public function up() { Schema::table('{{ table }}', function (Blueprint $table) { - // +{% for field,config in fields %} + $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; +{% endfor %} +{% for field in jsonable %} + $table->text('{{ field }}')->nullable(); +{% endfor %} +{% for field in morphable %} + $table->nullableMorphs('{{ field }}', 'morphable_index'); +{% endfor %} }); } @@ -26,7 +34,15 @@ return new class extends Migration public function down() { Schema::table('{{ table }}', function (Blueprint $table) { - // +{% for field,config in fields %} + $table->dropColumn('{{ field }}'); +{% endfor %} +{% for field in jsonable %} + $table->dropColumn('{{ field }}'); +{% endfor %} +{% for field in morphable %} + $table->dropColumn('{{ field }}'); +{% endfor %} }); } }; From fcdd327009eafa61ebbde01357c2fc53b586edea Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 08:08:45 -0400 Subject: [PATCH 10/44] jsonable use mediumText in stub files now --- modules/system/console/scaffold/migration/migration.create.stub | 2 +- modules/system/console/scaffold/migration/migration.update.stub | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/system/console/scaffold/migration/migration.create.stub b/modules/system/console/scaffold/migration/migration.create.stub index 63213aac5c..d0e8633519 100644 --- a/modules/system/console/scaffold/migration/migration.create.stub +++ b/modules/system/console/scaffold/migration/migration.create.stub @@ -23,7 +23,7 @@ return new class extends Migration $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; {% endfor %} {% for field in jsonable %} - $table->text('{{ field }}')->nullable(); + $table->mediumText('{{ field }}')->nullable(); {% endfor %} {% for field in morphable %} $table->nullableMorphs('{{ field }}', 'morphable_index'); diff --git a/modules/system/console/scaffold/migration/migration.update.stub b/modules/system/console/scaffold/migration/migration.update.stub index 303d53b7b8..79aafebbfa 100644 --- a/modules/system/console/scaffold/migration/migration.update.stub +++ b/modules/system/console/scaffold/migration/migration.update.stub @@ -18,7 +18,7 @@ return new class extends Migration $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; {% endfor %} {% for field in jsonable %} - $table->text('{{ field }}')->nullable(); + $table->mediumText('{{ field }}')->nullable(); {% endfor %} {% for field in morphable %} $table->nullableMorphs('{{ field }}', 'morphable_index'); From 3337df53e9bf5415fe9af7d979cc0c36937fab0c Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 08:24:04 -0400 Subject: [PATCH 11/44] use boolean for checkbox/switch field types; use datetime for datepicker fields --- modules/system/console/CreateMigration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 976651ae26..0bca608948 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -291,14 +291,14 @@ protected function mapFieldType($name, $fieldConfig) switch ($fieldConfig['type'] ?? 'text') { case 'checkbox': case 'switch': - $dbType = 'unsignedinteger'; + $dbType = 'boolean'; break; case 'number': case 'range': $dbType = 'integer'; break; case 'datepicker': - $dbType = 'timestamp'; + $dbType = 'datetime'; break; case 'markdown': case 'textarea': From b95cac735ae416d2f7f2ab1a15ae685a0a0519ad Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 08:38:42 -0400 Subject: [PATCH 12/44] be smarted about number/range types --- modules/system/console/CreateMigration.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 0bca608948..48a2a5a2f3 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -294,8 +294,16 @@ protected function mapFieldType($name, $fieldConfig) $dbType = 'boolean'; break; case 'number': + if ($fieldConfig['step'] && is_int($fieldConfig['step'])) { + $dbType = 'integer'; + } else { + $dbType = 'double'; + } + if ($dbType === 'integer' && $fieldConfig['min'] >= 0) { + $dbType = 'unsignedInteger'; + } case 'range': - $dbType = 'integer'; + $dbType = 'unsignedInteger'; break; case 'datepicker': $dbType = 'datetime'; From 68be02b7529635aa379b4768bc27267f786fafe8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 09:05:25 -0400 Subject: [PATCH 13/44] make sure step and min configs are set --- modules/system/console/CreateMigration.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 48a2a5a2f3..4645859d24 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -294,14 +294,15 @@ protected function mapFieldType($name, $fieldConfig) $dbType = 'boolean'; break; case 'number': - if ($fieldConfig['step'] && is_int($fieldConfig['step'])) { + if (isset($fieldConfig['step']) && is_int($fieldConfig['step'])) { $dbType = 'integer'; } else { $dbType = 'double'; } - if ($dbType === 'integer' && $fieldConfig['min'] >= 0) { + if ($dbType === 'integer' && isset($fieldConfig['min']) && $fieldConfig['min'] >= 0) { $dbType = 'unsignedInteger'; } + break; case 'range': $dbType = 'unsignedInteger'; break; From 9ccbb0da008e5db0e248be2cea6dd7a69cc68ee4 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 09:43:51 -0400 Subject: [PATCH 14/44] move fields mapping into a helper class --- modules/system/console/CreateMigration.php | 42 +-------------------- modules/system/helpers/Migration.php | 43 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 modules/system/helpers/Migration.php diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 4645859d24..fc1169b034 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -5,6 +5,7 @@ use Str; use System\Console\BaseScaffoldCommand; use System\Classes\VersionManager; +use System\Helpers\Migration; use Yaml; /** @@ -250,7 +251,7 @@ protected function processVars($vars): array continue; } - $vars['fields'][$field] = $this->mapFieldType($field, $config); + $vars['fields'][$field] = Migration::mapFieldType($field, $config); } } @@ -286,45 +287,6 @@ protected function processVars($vars): array return $vars; } - protected function mapFieldType($name, $fieldConfig) - { - 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': - case 'textarea': - $dbType = 'mediumText'; - break; - default: - $dbType = 'string'; - } - $required = $fieldConfig['required'] ?? false; - - return [ - 'type' => $dbType, - 'required' => $required, - 'index' => in_array($name, ["slug"]) or str_ends_with($name, "_id"), - ]; - } - /** * Get the next version number based on the current number. */ diff --git a/modules/system/helpers/Migration.php b/modules/system/helpers/Migration.php new file mode 100644 index 0000000000..e4bf3e0f29 --- /dev/null +++ b/modules/system/helpers/Migration.php @@ -0,0 +1,43 @@ += 0) { + $dbType = 'unsignedInteger'; + } + break; + case 'range': + $dbType = 'unsignedInteger'; + break; + case 'datepicker': + $dbType = 'datetime'; + break; + case 'markdown': + case 'textarea': + $dbType = 'mediumText'; + break; + default: + $dbType = 'string'; + } + $required = $fieldConfig['required'] ?? false; + + return [ + 'type' => $dbType, + 'required' => $required, + 'index' => in_array($name, ["slug"]) or str_ends_with($name, "_id"), + ]; + } +} From 3afef8f0664a0c7214d7989b3c28512425b089c5 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 09:51:22 -0400 Subject: [PATCH 15/44] add comments and type hinting --- modules/system/helpers/Migration.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/system/helpers/Migration.php b/modules/system/helpers/Migration.php index e4bf3e0f29..3de42337cb 100644 --- a/modules/system/helpers/Migration.php +++ b/modules/system/helpers/Migration.php @@ -1,8 +1,18 @@ - $dbType, 'required' => $required, - 'index' => in_array($name, ["slug"]) or str_ends_with($name, "_id"), + 'index' => in_array($fieldName, ["slug"]) or str_ends_with($fieldName, "_id"), ]; } } From d8e2e8834c7ec28a1d8cfcfff4158a9c70e9f78a Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 10:02:16 -0400 Subject: [PATCH 16/44] add more comments and type hinting --- modules/system/console/CreateMigration.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index fc1169b034..389f072343 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -203,7 +203,10 @@ protected function prepareVars(): array return $vars; } - protected function processVars($vars): array + /** + * Create vars for model fields mappings so they can be used in update/create stubs + */ + protected function processVars(array $vars): array { $vars = parent::processVars($vars); From 65fd61de4fa0b2dfc7912ac239ea06934ec50dc8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 12:16:27 -0400 Subject: [PATCH 17/44] add comment --- modules/system/console/CreateMigration.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 389f072343..a134ce57a3 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -210,6 +210,7 @@ protected function processVars(array $vars): array { $vars = parent::processVars($vars); + // --model option needed below if (empty($vars['model'])) { return $vars; } From eb9bf9fadce65830557893df42d7d147acbb99b2 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 14:02:53 -0400 Subject: [PATCH 18/44] add unit tests for Migration helper --- .../system/tests/helpers/MigrationTest.php | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 modules/system/tests/helpers/MigrationTest.php diff --git a/modules/system/tests/helpers/MigrationTest.php b/modules/system/tests/helpers/MigrationTest.php new file mode 100644 index 0000000000..884e5174eb --- /dev/null +++ b/modules/system/tests/helpers/MigrationTest.php @@ -0,0 +1,127 @@ + 'switch', + ]); + $this->assertEquals($result['type'], 'boolean'); + } + + public function testCheckboxFieldType() + { + $result = Migration::mapFieldType('checkboxTest', [ + 'type' => 'checkbox', + ]); + $this->assertEquals($result['type'], 'boolean'); + } + + public function testNumberFieldType() + { + $result = Migration::mapFieldType('numberTest', [ + 'type' => 'number', + 'min' => 0, + 'step' => 1, + ]); + $this->assertEquals($result['type'], 'unsignedInteger'); + + $result = Migration::mapFieldType( 'integerTest', [ + 'type' => 'number', + 'step' => 2, + ]); + $this->assertEquals($result['type'], 'integer'); + + $result = Migration::mapFieldType('numberTest', [ + 'type' => 'number', + 'step' => 0.5, + ]); + $this->assertEquals($result['type'], 'double'); + } + + public function testRangeFieldType() + { + $result = Migration::mapFieldType('rangeTest', [ + 'type' => 'range', + ]); + $this->assertEquals($result['type'], 'unsignedInteger'); + } + + public function testDatepickerType() + { + $result = Migration::mapFieldType('datepickerTest', [ + 'type' => 'datepicker', + ]); + $this->assertEquals($result['type'], 'datetime'); + } + + public function testTextareaType() + { + $result = Migration::mapFieldType('textareaTest', [ + 'type' => 'textarea', + ]); + $this->assertEquals($result['type'], 'mediumText'); + } + + public function testMarkdownType() + { + $result = Migration::mapFieldType('markdownTest', [ + 'type' => 'markdown', + ]); + $this->assertEquals($result['type'], 'mediumText'); + } + + public function testTextType() + { + $result = Migration::mapFieldType('textTest', [ + 'type' => 'text', + ]); + $this->assertEquals($result['type'], 'string'); + } + + public function testPasswordType() + { + $result = Migration::mapFieldType('passwordTest', [ + 'type' => 'password', + ]); + $this->assertEquals($result['type'], 'string'); + } + + public function testDropdownType() + { + $result = Migration::mapFieldType('dropdownTest', [ + 'type' => 'dropdown', + ]); + $this->assertEquals($result['type'], 'string'); + } + + public function testRadioListType() + { + $result = Migration::mapFieldType('radiolistTest', [ + 'type' => 'radiolist', + ]); + $this->assertEquals($result['type'], 'string'); + } + + public function testBalloonSelectorType() + { + $result = Migration::mapFieldType('balloonselectorTest', [ + 'type' => 'balloonselector', + ]); + $this->assertEquals($result['type'], 'string'); + } + + public function testEmailType() + { + $result = Migration::mapFieldType('emailTest', [ + 'type' => 'email', + ]); + $this->assertEquals($result['type'], 'string'); + } +} From 3cf9b31f00409b8009306343378a033f8e771a50 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 5 Jul 2023 14:26:39 -0400 Subject: [PATCH 19/44] remove extra space --- modules/system/tests/helpers/MigrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/tests/helpers/MigrationTest.php b/modules/system/tests/helpers/MigrationTest.php index 884e5174eb..dcdcfbe44d 100644 --- a/modules/system/tests/helpers/MigrationTest.php +++ b/modules/system/tests/helpers/MigrationTest.php @@ -32,7 +32,7 @@ public function testNumberFieldType() ]); $this->assertEquals($result['type'], 'unsignedInteger'); - $result = Migration::mapFieldType( 'integerTest', [ + $result = Migration::mapFieldType('integerTest', [ 'type' => 'number', 'step' => 2, ]); From 36101b8305af5545b3e0261ac0e8375b3fa09932 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 14 Jul 2023 23:19:37 -0400 Subject: [PATCH 20/44] repeater can be ignored here, defined with the jsonable model property --- modules/system/console/CreateMigration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index a134ce57a3..888d793ee8 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -248,7 +248,7 @@ protected function processVars(array $vars): array if (str_starts_with($field, '_') or $field === $model->getKeyName() or str_contains($field, '[') - or in_array($type, ['fileupload','relation','relationmanager','section','hint']) + or in_array($type, ['fileupload','relation','relationmanager','repeater','section','hint']) or in_array($field, $model->purgeable ?? []) or $model->getRelationType($field) ) { From 5fa097535c931bb2748bc9627dd2179baf7872e6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 16 Jul 2023 10:41:44 -0400 Subject: [PATCH 21/44] check if field is required in model's rules property --- modules/system/console/CreateMigration.php | 2 +- modules/system/helpers/Migration.php | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 888d793ee8..8895085a62 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -255,7 +255,7 @@ protected function processVars(array $vars): array continue; } - $vars['fields'][$field] = Migration::mapFieldType($field, $config); + $vars['fields'][$field] = Migration::mapFieldType($field, $config, $model); } } diff --git a/modules/system/helpers/Migration.php b/modules/system/helpers/Migration.php index 3de42337cb..c7a3ee9761 100644 --- a/modules/system/helpers/Migration.php +++ b/modules/system/helpers/Migration.php @@ -2,6 +2,8 @@ namespace System\Helpers; +use Winter\Storm\Database\Model; + /** * This helper class is used in migration scaffolding console scripts * @@ -12,7 +14,7 @@ class Migration /** * Maps model fields config to DB Schema column types. */ - public static function mapFieldType(string $fieldName, array $fieldConfig) : array + public static function mapFieldType(string $fieldName, array $fieldConfig, ?Model $model = null) : array { switch ($fieldConfig['type'] ?? 'text') { case 'checkbox': @@ -42,7 +44,15 @@ public static function mapFieldType(string $fieldName, array $fieldConfig) : arr default: $dbType = 'string'; } - $required = $fieldConfig['required'] ?? false; + + if ($model) { + $rule = array_get($model->rules ?? [], $name, ''); + $rule = is_array($rule) ? implode(',', $rule) : $rule; + + $required = str_contains($rule, 'required') ? true : $fieldConfig['required'] ?? false; + } else { + $required = $fieldConfig['required'] ?? false; + } return [ 'type' => $dbType, From 112e60bef3489842b36f7dfe039ab1b38ec0c845 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 19 Jul 2023 07:58:11 -0400 Subject: [PATCH 22/44] use text column type for textarea --- modules/system/helpers/Migration.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/system/helpers/Migration.php b/modules/system/helpers/Migration.php index c7a3ee9761..3c2f03f768 100644 --- a/modules/system/helpers/Migration.php +++ b/modules/system/helpers/Migration.php @@ -38,9 +38,11 @@ public static function mapFieldType(string $fieldName, array $fieldConfig, ?Mode $dbType = 'datetime'; break; case 'markdown': - case 'textarea': $dbType = 'mediumText'; break; + case 'textarea': + $dbType = 'text'; + break; default: $dbType = 'string'; } From 24f1401a97009d7017b7a8d2e2cb9f8a975d2471 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Wed, 19 Jul 2023 08:25:01 -0400 Subject: [PATCH 23/44] fix trsts --- modules/system/tests/helpers/MigrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/tests/helpers/MigrationTest.php b/modules/system/tests/helpers/MigrationTest.php index dcdcfbe44d..e40f081e36 100644 --- a/modules/system/tests/helpers/MigrationTest.php +++ b/modules/system/tests/helpers/MigrationTest.php @@ -66,7 +66,7 @@ public function testTextareaType() $result = Migration::mapFieldType('textareaTest', [ 'type' => 'textarea', ]); - $this->assertEquals($result['type'], 'mediumText'); + $this->assertEquals($result['type'], 'text'); } public function testMarkdownType() From 449efbde0892209f244bf80bb76d152bb3279fd1 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 20 Aug 2023 15:56:14 -0400 Subject: [PATCH 24/44] fix variable name --- modules/system/helpers/Migration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/helpers/Migration.php b/modules/system/helpers/Migration.php index 3c2f03f768..b94d958367 100644 --- a/modules/system/helpers/Migration.php +++ b/modules/system/helpers/Migration.php @@ -48,7 +48,7 @@ public static function mapFieldType(string $fieldName, array $fieldConfig, ?Mode } if ($model) { - $rule = array_get($model->rules ?? [], $name, ''); + $rule = array_get($model->rules ?? [], $fieldName, ''); $rule = is_array($rule) ? implode(',', $rule) : $rule; $required = str_contains($rule, 'required') ? true : $fieldConfig['required'] ?? false; From b7139b8b5978294693946cb3aa49a4da1edb33de Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 14 Oct 2023 18:03:25 -0400 Subject: [PATCH 25/44] fix coding style --- modules/system/console/CreateMigration.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 904fd86047..9d1afe1378 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -246,11 +246,11 @@ protected function processVars(array $vars): array $type = $config['type'] ?? 'text'; if (str_starts_with($field, '_') - or $field === $model->getKeyName() - or str_contains($field, '[') - or in_array($type, ['fileupload','relation','relationmanager','repeater','section','hint']) - or in_array($field, $model->purgeable ?? []) - or $model->getRelationType($field) + || $field === $model->getKeyName() + || str_contains($field, '[') + || in_array($type, ['fileupload', 'relation', 'relationmanager', 'repeater', 'section', 'hint']) + || in_array($field, $model->purgeable ?? []) + || $model->getRelationType($field) ) { continue; } From a66eef9a6fe178f297a61eab146574e55269989e Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 14 Oct 2023 18:08:07 -0400 Subject: [PATCH 26/44] add missing space before closing twig tag --- modules/system/console/scaffold/migration/migration.create.stub | 2 +- modules/system/console/scaffold/migration/migration.update.stub | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/system/console/scaffold/migration/migration.create.stub b/modules/system/console/scaffold/migration/migration.create.stub index d0e8633519..e6932eb478 100644 --- a/modules/system/console/scaffold/migration/migration.create.stub +++ b/modules/system/console/scaffold/migration/migration.create.stub @@ -20,7 +20,7 @@ return new class extends Migration $table->id(); {% endif %} {% for field,config in fields %} - $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; + $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()' }}{{ config.index ? '->index()' }}; {% endfor %} {% for field in jsonable %} $table->mediumText('{{ field }}')->nullable(); diff --git a/modules/system/console/scaffold/migration/migration.update.stub b/modules/system/console/scaffold/migration/migration.update.stub index 79aafebbfa..7f69644ca4 100644 --- a/modules/system/console/scaffold/migration/migration.update.stub +++ b/modules/system/console/scaffold/migration/migration.update.stub @@ -15,7 +15,7 @@ return new class extends Migration { Schema::table('{{ table }}', function (Blueprint $table) { {% for field,config in fields %} - $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()'}}{{ config.index ? '->index()' }}; + $table->{{ config.type }}('{{ field }}'){{ config.required == false ? '->nullable()' }}{{ config.index ? '->index()' }}; {% endfor %} {% for field in jsonable %} $table->mediumText('{{ field }}')->nullable(); From 5830179c20aff4b8dab7c82fa9a8bb34a2d981e1 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 15 Oct 2023 08:13:43 -0400 Subject: [PATCH 27/44] move back to protected method --- modules/system/console/CreateMigration.php | 56 ++++++++++++++++++- modules/system/helpers/Migration.php | 65 ---------------------- 2 files changed, 54 insertions(+), 67 deletions(-) delete mode 100644 modules/system/helpers/Migration.php 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"), - ]; - } -} From add94506f79f25f63a66b52e78da0d865de13ae3 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 15 Oct 2023 08:26:57 -0400 Subject: [PATCH 28/44] remove Migration helper class tests --- .../system/tests/helpers/MigrationTest.php | 127 ------------------ 1 file changed, 127 deletions(-) delete mode 100644 modules/system/tests/helpers/MigrationTest.php diff --git a/modules/system/tests/helpers/MigrationTest.php b/modules/system/tests/helpers/MigrationTest.php deleted file mode 100644 index e40f081e36..0000000000 --- a/modules/system/tests/helpers/MigrationTest.php +++ /dev/null @@ -1,127 +0,0 @@ - 'switch', - ]); - $this->assertEquals($result['type'], 'boolean'); - } - - public function testCheckboxFieldType() - { - $result = Migration::mapFieldType('checkboxTest', [ - 'type' => 'checkbox', - ]); - $this->assertEquals($result['type'], 'boolean'); - } - - public function testNumberFieldType() - { - $result = Migration::mapFieldType('numberTest', [ - 'type' => 'number', - 'min' => 0, - 'step' => 1, - ]); - $this->assertEquals($result['type'], 'unsignedInteger'); - - $result = Migration::mapFieldType('integerTest', [ - 'type' => 'number', - 'step' => 2, - ]); - $this->assertEquals($result['type'], 'integer'); - - $result = Migration::mapFieldType('numberTest', [ - 'type' => 'number', - 'step' => 0.5, - ]); - $this->assertEquals($result['type'], 'double'); - } - - public function testRangeFieldType() - { - $result = Migration::mapFieldType('rangeTest', [ - 'type' => 'range', - ]); - $this->assertEquals($result['type'], 'unsignedInteger'); - } - - public function testDatepickerType() - { - $result = Migration::mapFieldType('datepickerTest', [ - 'type' => 'datepicker', - ]); - $this->assertEquals($result['type'], 'datetime'); - } - - public function testTextareaType() - { - $result = Migration::mapFieldType('textareaTest', [ - 'type' => 'textarea', - ]); - $this->assertEquals($result['type'], 'text'); - } - - public function testMarkdownType() - { - $result = Migration::mapFieldType('markdownTest', [ - 'type' => 'markdown', - ]); - $this->assertEquals($result['type'], 'mediumText'); - } - - public function testTextType() - { - $result = Migration::mapFieldType('textTest', [ - 'type' => 'text', - ]); - $this->assertEquals($result['type'], 'string'); - } - - public function testPasswordType() - { - $result = Migration::mapFieldType('passwordTest', [ - 'type' => 'password', - ]); - $this->assertEquals($result['type'], 'string'); - } - - public function testDropdownType() - { - $result = Migration::mapFieldType('dropdownTest', [ - 'type' => 'dropdown', - ]); - $this->assertEquals($result['type'], 'string'); - } - - public function testRadioListType() - { - $result = Migration::mapFieldType('radiolistTest', [ - 'type' => 'radiolist', - ]); - $this->assertEquals($result['type'], 'string'); - } - - public function testBalloonSelectorType() - { - $result = Migration::mapFieldType('balloonselectorTest', [ - 'type' => 'balloonselector', - ]); - $this->assertEquals($result['type'], 'string'); - } - - public function testEmailType() - { - $result = Migration::mapFieldType('emailTest', [ - 'type' => 'email', - ]); - $this->assertEquals($result['type'], 'string'); - } -} From b33cdc60a4bd1cf599c86bdb808a18f988624c6a Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 19 Oct 2023 13:13:23 -0400 Subject: [PATCH 29/44] leave regular exception flow --- modules/system/console/CreateMigration.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index e22afe9479..2311fe39f3 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -217,12 +217,8 @@ protected function processVars(array $vars): array $vars['fields'] = []; - try { - $fields_path = '$/' . $vars['plugin_url'] . '/models/' . $vars['lower_model'] . '/fields.yaml'; - $fields = Yaml::parseFile(File::symbolizePath($fields_path)); - } catch (\Exception $e) { - die($e->getMessage()); - } + $fields_path = '$/' . $vars['plugin_url'] . '/models/' . $vars['lower_model'] . '/fields.yaml'; + $fields = Yaml::parseFile(File::symbolizePath($fields_path)); $modelName = $vars['plugin_namespace'] . '\\Models\\' . $vars['model']; From a7f66cc745ac39d02bc9272f715dba083a0e87a2 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 09:32:16 -0400 Subject: [PATCH 30/44] add initial migration test and model --- .../system/tests/console/CreateMigration.php | 28 +++++++++++ .../winter/tester/models/TestModel.php | 40 ++++++++++++++++ .../tester/models/testmodel/fields.yaml | 46 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 modules/system/tests/console/CreateMigration.php create mode 100644 modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php create mode 100644 modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml diff --git a/modules/system/tests/console/CreateMigration.php b/modules/system/tests/console/CreateMigration.php new file mode 100644 index 0000000000..d7688a6c95 --- /dev/null +++ b/modules/system/tests/console/CreateMigration.php @@ -0,0 +1,28 @@ +artisan('create:migration Winter.Tester -c --model TestModel'); + } + + public function testCreateMigration() + { + } + + public function testUpdateMigration() + { + } + + public function tearDown(): void + { + parent::tearDown(); + } +} diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php new file mode 100644 index 0000000000..ba5688607d --- /dev/null +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php @@ -0,0 +1,40 @@ + TestUser::class + ]; + + public $hasOne = [ + 'phone' => TestPhone::class + ]; + + public $morphTo = [ + 'taggable' => [] + ]; +} + +class TestUser extends Model +{ +} + +class TestPhone extends Model +{ +} diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml new file mode 100644 index 0000000000..83d9b77b0b --- /dev/null +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml @@ -0,0 +1,46 @@ +fields: + cb: + label: Checkbox + type: checkbox + + switch: + label: Switch + type: switch + + int: + label: Integer + type: number + step: 1 + + uint: + label: Unsigned Integer + type: number + step: 1 + min: 0 + + double: + label: Double + type: number + + range: + label: Range + type: range + min: 1 + max: 10 + + datetime: + label: Datetime + type: datepicker + + md: + label: Markdown + type: markdown + + textarea: + label: Textarea + type: textarea + + text: + label: Text + required: true + From fabd10560e077bc513e13c22bc5959d5d6d888a3 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 09:49:58 -0400 Subject: [PATCH 31/44] fix test filename --- .../console/{CreateMigration.php => CreateMigrationTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename modules/system/tests/console/{CreateMigration.php => CreateMigrationTest.php} (100%) diff --git a/modules/system/tests/console/CreateMigration.php b/modules/system/tests/console/CreateMigrationTest.php similarity index 100% rename from modules/system/tests/console/CreateMigration.php rename to modules/system/tests/console/CreateMigrationTest.php From 27997fbecbbda595bdea7e45fff55e6eb04731de Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 10:27:24 -0400 Subject: [PATCH 32/44] fix test plugins_path and add $rules to TestModel --- modules/system/tests/console/CreateMigrationTest.php | 6 ++++-- .../fixtures/plugins/winter/tester/models/TestModel.php | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index d7688a6c95..92abb63d5b 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -2,14 +2,16 @@ namespace System\Tests\Console; -use System\Tests\Bootstrap\TestCase; +use System\Tests\Bootstrap\PluginTestCase; -class CreateMigrationTest extends TestCase +class CreateMigrationTest extends PluginTestCase { public function setUp(): void { parent::setUp(); + $this->app->setPluginsPath(base_path() . '/modules/system/tests/fixtures/plugins'); + $this->artisan('create:migration Winter.Tester -c --model TestModel'); } diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php index ba5688607d..8011b552a6 100644 --- a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php @@ -29,6 +29,11 @@ class TestModel extends Model public $morphTo = [ 'taggable' => [] ]; + + public $rules = [ + 'uint' => 'required', + 'range' => 'required|integer|between:1,10', + ]; } class TestUser extends Model From 4b588db1ba28a0e84227628b006ba5ae4de88727 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 10:38:30 -0400 Subject: [PATCH 33/44] use plugins_path() instead --- modules/system/console/CreateMigration.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 2311fe39f3..f2ba4c7529 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -217,8 +217,8 @@ protected function processVars(array $vars): array $vars['fields'] = []; - $fields_path = '$/' . $vars['plugin_url'] . '/models/' . $vars['lower_model'] . '/fields.yaml'; - $fields = Yaml::parseFile(File::symbolizePath($fields_path)); + $fields_path = $vars['plugin_url'] . '/models/' . $vars['lower_model'] . '/fields.yaml'; + $fields = Yaml::parseFile(plugins_path($fields_path)); $modelName = $vars['plugin_namespace'] . '\\Models\\' . $vars['model']; From e05cea9bfeb39efd5fbde18ccc82e3eaa4257293 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 10:57:28 -0400 Subject: [PATCH 34/44] call artisan within the test, not setUp() --- modules/system/tests/console/CreateMigrationTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 92abb63d5b..9bb2b0877f 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -10,17 +10,17 @@ public function setUp(): void { parent::setUp(); - $this->app->setPluginsPath(base_path() . '/modules/system/tests/fixtures/plugins'); - - $this->artisan('create:migration Winter.Tester -c --model TestModel'); + $this->app->setPluginsPath(base_path() . '/modules/system/tests/fixtures/plugins/'); } public function testCreateMigration() { + $this->artisan('create:migration Winter.Tester -c --force --model TestModel'); } - public function testUpdateMigration() + public function testCreateMigration() { + $this->artisan('create:migration Winter.Tester -u --force --model TestModel'); } public function tearDown(): void From c15776ea3dc81d1f4b8568b437b97995d9fa979a Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 10:59:16 -0400 Subject: [PATCH 35/44] fix test method name --- modules/system/tests/console/CreateMigrationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 9bb2b0877f..71e2ce48f7 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -18,7 +18,7 @@ public function testCreateMigration() $this->artisan('create:migration Winter.Tester -c --force --model TestModel'); } - public function testCreateMigration() + public function testUpdateMigration() { $this->artisan('create:migration Winter.Tester -u --force --model TestModel'); } From 36518266e438ba768c1f127227fb869eccfb6bed Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 11:45:54 -0400 Subject: [PATCH 36/44] cleanup after ourselves --- .../system/tests/console/CreateMigrationTest.php | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 71e2ce48f7..294f9b6861 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -2,6 +2,7 @@ namespace System\Tests\Console; +use File; use System\Tests\Bootstrap\PluginTestCase; class CreateMigrationTest extends PluginTestCase @@ -11,20 +12,29 @@ public function setUp(): void parent::setUp(); $this->app->setPluginsPath(base_path() . '/modules/system/tests/fixtures/plugins/'); + $this->versionFile = plugins_path('winter/tester/updates/version.yaml'); + $this->versionFolder = plugins_path('winter/tester/updates/v0.0.1'); + + File::copy($this->versionFile, $this->versionFile . '.bak'); } public function testCreateMigration() { - $this->artisan('create:migration Winter.Tester -c --force --model TestModel'); + $this->artisan('create:migration Winter.Tester -c --force --for-version v0.0.1 --model TestModel --name create_model'); + $this->assertFileExists($this->versionFolder . '/create_model.php'); } public function testUpdateMigration() { - $this->artisan('create:migration Winter.Tester -u --force --model TestModel'); + $this->artisan('create:migration Winter.Tester -u --force --for-version v0.0.1 --model TestModel --name update_model'); + $this->assertFileExists($this->versionFolder . '/update_model.php'); } public function tearDown(): void { + File::move($this->versionFile . '.bak', $this->versionFile); + File::deleteDirectory($this->versionFolder); + parent::tearDown(); } } From bdff39e53eea3da36911b4e23cefdb6203b89b20 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 13:33:17 -0400 Subject: [PATCH 37/44] complete migration tests --- modules/system/console/CreateMigration.php | 3 ++ .../tests/console/CreateMigrationTest.php | 45 ++++++++++++++++--- .../winter/tester/models/TestModel.php | 4 ++ .../tester/models/testmodel/fields.yaml | 10 +++++ 4 files changed, 55 insertions(+), 7 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index f2ba4c7529..c00f1c7282 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -323,6 +323,9 @@ protected function mapFieldType(string $fieldName, array $fieldConfig, ?Model $m break; case 'datepicker': $dbType = 'datetime'; + if (isset($fieldConfig['mode']) && $fieldConfig['mode']) { + $dbType = $fieldConfig['mode']; + } break; case 'markdown': $dbType = 'mediumText'; diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 294f9b6861..041b4c3b57 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -3,6 +3,7 @@ namespace System\Tests\Console; use File; +use Schema; use System\Tests\Bootstrap\PluginTestCase; class CreateMigrationTest extends PluginTestCase @@ -20,14 +21,44 @@ public function setUp(): void public function testCreateMigration() { - $this->artisan('create:migration Winter.Tester -c --force --for-version v0.0.1 --model TestModel --name create_model'); - $this->assertFileExists($this->versionFolder . '/create_model.php'); - } + $this->artisan('create:migration Winter.Tester -c --force --for-version v0.0.1 --model TestModel --name create_table'); + $this->assertFileExists($this->versionFolder . '/create_table.php'); - public function testUpdateMigration() - { - $this->artisan('create:migration Winter.Tester -u --force --for-version v0.0.1 --model TestModel --name update_model'); - $this->assertFileExists($this->versionFolder . '/update_model.php'); + $migration = require_once $this->versionFolder . '/create_table.php'; + $migration->up(); + + $table = 'winter_tester_test_model'; + $this->assertTrue(Schema::hasTable($table)); + + $columns = [ + 'id' => 'integer', + 'cb' => 'boolean', + 'switch' => 'boolean', + 'int' => 'integer', + 'uint' => 'integer', # 'unsignedInteger' + 'double' => 'float', # 'double' + 'range' => 'integer', # 'unsignedInteger' + 'datetime' => 'datetime', + 'date' => 'date', + 'time' => 'time', + 'md' => 'text', # 'mediumText' + 'textarea' => 'text', + 'text' => 'string', + 'phone_id' => 'integer', # 'unsignedInteger' + 'user_id' => 'integer', # 'unsignedInteger' + 'data' => 'text', # mediumText + 'taggable_type' => 'string', + 'taggable_id' => 'integer', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + ]; + + foreach ($columns as $name => $type) { + $this->assertEquals($type, Schema::getColumnType($table, $name)); + } + + $migration->down(); + $this->assertFalse(Schema::hasTable($table)); } public function tearDown(): void diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php index 8011b552a6..a1d4d8ecb2 100644 --- a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php @@ -15,6 +15,10 @@ class TestModel extends Model */ protected $guarded = []; + public $jsonable = [ + 'data', + ]; + /** * @var array Relations */ diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml index 83d9b77b0b..c5e790bcb4 100644 --- a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml @@ -32,6 +32,16 @@ fields: label: Datetime type: datepicker + date: + label: Date + type: datepicker + mode: date + + time: + label: Time + type: datepicker + mode: time + md: label: Markdown type: markdown From cc25878fb96b16647c9d34382d3d29bea2314470 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 14:52:49 -0400 Subject: [PATCH 38/44] simplify code logic --- modules/system/console/CreateMigration.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index c00f1c7282..38c5348f16 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -309,10 +309,9 @@ protected function mapFieldType(string $fieldName, array $fieldConfig, ?Model $m $dbType = 'boolean'; break; case 'number': + $dbType = 'double'; if (isset($fieldConfig['step']) && is_int($fieldConfig['step'])) { $dbType = 'integer'; - } else { - $dbType = 'double'; } if ($dbType === 'integer' && isset($fieldConfig['min']) && $fieldConfig['min'] >= 0) { $dbType = 'unsignedInteger'; @@ -322,10 +321,7 @@ protected function mapFieldType(string $fieldName, array $fieldConfig, ?Model $m $dbType = 'unsignedInteger'; break; case 'datepicker': - $dbType = 'datetime'; - if (isset($fieldConfig['mode']) && $fieldConfig['mode']) { - $dbType = $fieldConfig['mode']; - } + $dbType = $fieldConfig['mode'] ?? 'datetime'; break; case 'markdown': $dbType = 'mediumText'; From 0f436b168424ff8d1c701136ea8eddb8e769aadd Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Oct 2023 17:23:04 -0400 Subject: [PATCH 39/44] check if the index have been created --- .../tests/console/CreateMigrationTest.php | 62 +++++++++++-------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 041b4c3b57..0982daac9a 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -13,6 +13,8 @@ public function setUp(): void parent::setUp(); $this->app->setPluginsPath(base_path() . '/modules/system/tests/fixtures/plugins/'); + + $this->table = 'winter_tester_test_model'; $this->versionFile = plugins_path('winter/tester/updates/version.yaml'); $this->versionFolder = plugins_path('winter/tester/updates/v0.0.1'); @@ -27,38 +29,48 @@ public function testCreateMigration() $migration = require_once $this->versionFolder . '/create_table.php'; $migration->up(); - $table = 'winter_tester_test_model'; - $this->assertTrue(Schema::hasTable($table)); + $this->assertTrue(Schema::hasTable($this->table)); $columns = [ - 'id' => 'integer', - 'cb' => 'boolean', - 'switch' => 'boolean', - 'int' => 'integer', - 'uint' => 'integer', # 'unsignedInteger' - 'double' => 'float', # 'double' - 'range' => 'integer', # 'unsignedInteger' - 'datetime' => 'datetime', - 'date' => 'date', - 'time' => 'time', - 'md' => 'text', # 'mediumText' - 'textarea' => 'text', - 'text' => 'string', - 'phone_id' => 'integer', # 'unsignedInteger' - 'user_id' => 'integer', # 'unsignedInteger' - 'data' => 'text', # mediumText - 'taggable_type' => 'string', - 'taggable_id' => 'integer', - 'created_at' => 'datetime', - 'updated_at' => 'datetime', + 'id' => ['type'=>'integer', 'index'=>true], + 'cb' => ['type'=>'boolean'], + 'switch' => ['type'=>'boolean'], + 'int' => ['type'=>'integer'], + 'uint' => ['type'=>'integer'], + 'double' => ['type'=>'float'], + 'range' => ['type'=>'integer'], + 'datetime' => ['type'=>'datetime'], + 'date' => ['type'=>'date'], + 'time' => ['type'=>'time'], + 'md' => ['type'=>'text'], + 'textarea' => ['type'=>'text'], + 'text' => ['type'=>'string'], + 'phone_id' => ['type'=>'integer', 'index'=>true], + 'user_id' => ['type'=>'integer', 'index'=>true], + 'data' => ['type'=>'text'], + 'taggable_type' => ['type'=>'string', 'index'=>true], + 'taggable_id' => ['type'=>'integer', 'index'=>true], + 'created_at' => ['type'=>'datetime'], + 'updated_at' => ['type'=>'datetime'], ]; - foreach ($columns as $name => $type) { - $this->assertEquals($type, Schema::getColumnType($table, $name)); + $sm = Schema::getConnection()->getDoctrineSchemaManager(); + + $indexFields = collect($sm->listTableIndexes($this->table))->map(function ($item) { + return $item->getColumns(); + })->flatten()->all(); + + foreach ($columns as $name => $definition) { + $this->assertEquals(array_get($definition, 'type'), Schema::getColumnType($this->table, $name)); + + // assert an index has been created for the primary, morph and foreign keys + if (array_get($definition, 'index')) { + $this->assertTrue(in_array($name, $indexFields)); + } } $migration->down(); - $this->assertFalse(Schema::hasTable($table)); + $this->assertFalse(Schema::hasTable($this->table)); } public function tearDown(): void From fd8be03c1b813c4556402af9a5c1ebf2c7786297 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 22 Oct 2023 08:44:18 -0400 Subject: [PATCH 40/44] add Sortable trait and test sort_order column get added --- .../tests/console/CreateMigrationTest.php | 27 ++++++++++++------- .../winter/tester/models/TestModel.php | 12 +-------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 0982daac9a..0820a6c62b 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -3,6 +3,7 @@ namespace System\Tests\Console; use File; +use Illuminate\Database\Schema\Blueprint; use Schema; use System\Tests\Bootstrap\PluginTestCase; @@ -32,7 +33,7 @@ public function testCreateMigration() $this->assertTrue(Schema::hasTable($this->table)); $columns = [ - 'id' => ['type'=>'integer', 'index'=>true], + 'id' => ['type'=>'integer', 'index'=>'primary'], 'cb' => ['type'=>'boolean'], 'switch' => ['type'=>'boolean'], 'int' => ['type'=>'integer'], @@ -48,24 +49,30 @@ public function testCreateMigration() 'phone_id' => ['type'=>'integer', 'index'=>true], 'user_id' => ['type'=>'integer', 'index'=>true], 'data' => ['type'=>'text'], - 'taggable_type' => ['type'=>'string', 'index'=>true], - 'taggable_id' => ['type'=>'integer', 'index'=>true], + 'sort_order' => ['type'=>'integer', 'index'=>true], + 'taggable_id' => ['type'=>'integer', 'index'=>'morphable_index'], + 'taggable_type' => ['type'=>'string', 'index'=>'morphable_index'], 'created_at' => ['type'=>'datetime'], 'updated_at' => ['type'=>'datetime'], ]; - $sm = Schema::getConnection()->getDoctrineSchemaManager(); - - $indexFields = collect($sm->listTableIndexes($this->table))->map(function ($item) { - return $item->getColumns(); - })->flatten()->all(); + $schemaManager = Schema::getConnection()->getDoctrineSchemaManager(); + $table = $schemaManager->listTableDetails($this->table); foreach ($columns as $name => $definition) { $this->assertEquals(array_get($definition, 'type'), Schema::getColumnType($this->table, $name)); // assert an index has been created for the primary, morph and foreign keys - if (array_get($definition, 'index')) { - $this->assertTrue(in_array($name, $indexFields)); + if ($indexName = array_get($definition, 'index')) { + if ($indexName === true) { + $indexName = sprintf("%s_%s_index", $this->table, $name); + } + $this->assertTrue($table->hasIndex($indexName)); + + if ($indexName === 'morphable_index') { + $index = $table->getIndex($indexName); + $this->assertTrue(in_array($name, $index->getColumns())); + } } } diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php index a1d4d8ecb2..f43919f1e4 100644 --- a/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/TestModel.php @@ -4,24 +4,14 @@ class TestModel extends Model { + use \Winter\Storm\Database\Traits\Sortable; - /** - * @var string The database table used by the model. - */ public $table = 'winter_tester_test_model'; - /** - * @var array Guarded fields - */ - protected $guarded = []; - public $jsonable = [ 'data', ]; - /** - * @var array Relations - */ public $belongsTo = [ 'user' => TestUser::class ]; From 3869e49be89953c0ca4946881e86019fcb31fadc Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 22 Oct 2023 09:06:53 -0400 Subject: [PATCH 41/44] test required field property --- .../system/tests/console/CreateMigrationTest.php | 15 ++++++++------- .../winter/tester/models/testmodel/fields.yaml | 1 + 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 0820a6c62b..4ec5efc22a 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -33,21 +33,21 @@ public function testCreateMigration() $this->assertTrue(Schema::hasTable($this->table)); $columns = [ - 'id' => ['type'=>'integer', 'index'=>'primary'], + 'id' => ['type'=>'integer', 'index'=>'primary', 'required'=>true], 'cb' => ['type'=>'boolean'], 'switch' => ['type'=>'boolean'], 'int' => ['type'=>'integer'], - 'uint' => ['type'=>'integer'], + 'uint' => ['type'=>'integer', 'required'=>true], 'double' => ['type'=>'float'], - 'range' => ['type'=>'integer'], + 'range' => ['type'=>'integer', 'required'=>true], 'datetime' => ['type'=>'datetime'], - 'date' => ['type'=>'date'], + 'date' => ['type'=>'date', 'required'=>true], 'time' => ['type'=>'time'], 'md' => ['type'=>'text'], 'textarea' => ['type'=>'text'], - 'text' => ['type'=>'string'], - 'phone_id' => ['type'=>'integer', 'index'=>true], - 'user_id' => ['type'=>'integer', 'index'=>true], + 'text' => ['type'=>'string', 'required'=>true], + 'phone_id' => ['type'=>'integer', 'index'=>true, 'required'=>true], + 'user_id' => ['type'=>'integer', 'index'=>true, 'required'=>true], 'data' => ['type'=>'text'], 'sort_order' => ['type'=>'integer', 'index'=>true], 'taggable_id' => ['type'=>'integer', 'index'=>'morphable_index'], @@ -74,6 +74,7 @@ public function testCreateMigration() $this->assertTrue(in_array($name, $index->getColumns())); } } + $this->assertEquals(array_get($definition, 'required', false), $table->getColumn($name)->getNotnull()); } $migration->down(); diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml index c5e790bcb4..9b0c155516 100644 --- a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml @@ -36,6 +36,7 @@ fields: label: Date type: datepicker mode: date + required: true time: label: Time From af6598065c71482c4fc312849d85c21da9330cf0 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 22 Oct 2023 09:19:44 -0400 Subject: [PATCH 42/44] cleanup --- modules/system/tests/console/CreateMigrationTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/modules/system/tests/console/CreateMigrationTest.php b/modules/system/tests/console/CreateMigrationTest.php index 4ec5efc22a..77e30eea71 100644 --- a/modules/system/tests/console/CreateMigrationTest.php +++ b/modules/system/tests/console/CreateMigrationTest.php @@ -56,8 +56,7 @@ public function testCreateMigration() 'updated_at' => ['type'=>'datetime'], ]; - $schemaManager = Schema::getConnection()->getDoctrineSchemaManager(); - $table = $schemaManager->listTableDetails($this->table); + $table = Schema::getConnection()->getDoctrineSchemaManager()->listTableDetails($this->table); foreach ($columns as $name => $definition) { $this->assertEquals(array_get($definition, 'type'), Schema::getColumnType($this->table, $name)); From 6eb7b07f3e50d4ec83ef6562ef4afeb455417330 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 26 Oct 2023 07:40:32 -0400 Subject: [PATCH 43/44] fix fields.yaml indentation --- .../tester/models/testmodel/fields.yaml | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml index 9b0c155516..944d5cc0c5 100644 --- a/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml +++ b/modules/system/tests/fixtures/plugins/winter/tester/models/testmodel/fields.yaml @@ -1,57 +1,57 @@ fields: cb: - label: Checkbox - type: checkbox + label: Checkbox + type: checkbox switch: - label: Switch - type: switch + label: Switch + type: switch int: - label: Integer - type: number - step: 1 + label: Integer + type: number + step: 1 uint: - label: Unsigned Integer - type: number - step: 1 - min: 0 + label: Unsigned Integer + type: number + step: 1 + min: 0 double: - label: Double - type: number + label: Double + type: number range: - label: Range - type: range - min: 1 - max: 10 + label: Range + type: range + min: 1 + max: 10 datetime: - label: Datetime - type: datepicker + label: Datetime + type: datepicker date: - label: Date - type: datepicker - mode: date - required: true + label: Date + type: datepicker + mode: date + required: true time: - label: Time - type: datepicker - mode: time + label: Time + type: datepicker + mode: time md: - label: Markdown - type: markdown + label: Markdown + type: markdown textarea: - label: Textarea - type: textarea + label: Textarea + type: textarea text: - label: Text - required: true + label: Text + required: true From 614354e3cc6267e36364c36492fc8c135e0187e5 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Thu, 26 Oct 2023 08:21:28 -0400 Subject: [PATCH 44/44] require create or update option when table or model option is provided --- modules/system/console/CreateMigration.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/modules/system/console/CreateMigration.php b/modules/system/console/CreateMigration.php index 38c5348f16..886cb82848 100644 --- a/modules/system/console/CreateMigration.php +++ b/modules/system/console/CreateMigration.php @@ -174,6 +174,10 @@ protected function prepareVars(): array throw new InvalidArgumentException('The table or model options are required when using the create or update options'); } + if (($table || $model) && !in_array($scaffold, ['create', 'update'])) { + throw new InvalidArgumentException('One of create or update option is required when using the model or table options'); + } + $this->stubs = $this->migrationScaffolds[$scaffold]; if (!empty($this->option('for-version'))) {