From 3c8581bdf2fee0754d97cdc510b2cea3bfbf8624 Mon Sep 17 00:00:00 2001 From: Jason McCreary Date: Wed, 18 Dec 2024 10:42:39 -0500 Subject: [PATCH] Output new Laravel 11 geo column types --- src/Generators/MigrationGenerator.php | 4 +++ src/Lexers/ModelLexer.php | 1 + .../Generators/MigrationGeneratorTest.php | 1 + tests/fixtures/drafts/geometry-columns.yaml | 7 +++++ .../fixtures/migrations/geometry-columns.php | 31 +++++++++++++++++++ 5 files changed, 44 insertions(+) create mode 100644 tests/fixtures/drafts/geometry-columns.yaml create mode 100644 tests/fixtures/migrations/geometry-columns.php diff --git a/src/Generators/MigrationGenerator.php b/src/Generators/MigrationGenerator.php index b8c63a5c..9f46c2b9 100644 --- a/src/Generators/MigrationGenerator.php +++ b/src/Generators/MigrationGenerator.php @@ -152,6 +152,10 @@ protected function buildDefinition(Model $model): string if (!empty($columnAttributes) && !$this->isIdColumnType($column->dataType())) { $column_definition .= ', '; + if (in_array($column->dataType(), ['geography', 'geometry'])) { + $columnAttributes[0] = Str::wrap($columnAttributes[0], "'"); + } + if (in_array($column->dataType(), ['set', 'enum'])) { $column_definition .= json_encode($columnAttributes); } else { diff --git a/src/Lexers/ModelLexer.php b/src/Lexers/ModelLexer.php index ffab3b0b..8ec44a64 100644 --- a/src/Lexers/ModelLexer.php +++ b/src/Lexers/ModelLexer.php @@ -36,6 +36,7 @@ class ModelLexer implements Lexer 'enum' => 'enum', 'float' => 'float', 'fulltext' => 'fullText', + 'geography' => 'geography', 'geometry' => 'geometry', 'geometrycollection' => 'geometryCollection', 'increments' => 'increments', diff --git a/tests/Feature/Generators/MigrationGeneratorTest.php b/tests/Feature/Generators/MigrationGeneratorTest.php index b949289b..3736da42 100644 --- a/tests/Feature/Generators/MigrationGeneratorTest.php +++ b/tests/Feature/Generators/MigrationGeneratorTest.php @@ -646,6 +646,7 @@ public static function modelTreeDataProvider() ['drafts/foreign-key-on-delete.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/foreign-key-on-delete.php'], ['drafts/nullable-columns-with-foreign.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/nullable-columns-with-foreign.php'], ['drafts/omits-length-for-integers.yaml', 'database/migrations/timestamp_create_omits_table.php', 'migrations/omits-length-for-integers.php'], + ['drafts/geometry-columns.yaml', 'database/migrations/timestamp_create_locations_table.php', 'migrations/geometry-columns.php'], ]; } } diff --git a/tests/fixtures/drafts/geometry-columns.yaml b/tests/fixtures/drafts/geometry-columns.yaml new file mode 100644 index 00000000..3e7e9edf --- /dev/null +++ b/tests/fixtures/drafts/geometry-columns.yaml @@ -0,0 +1,7 @@ +models: + Location: + name: string:100 + description: text nullable + coordinates: geography:point,4326 + positions: geometry:point,0 + diff --git a/tests/fixtures/migrations/geometry-columns.php b/tests/fixtures/migrations/geometry-columns.php new file mode 100644 index 00000000..8b54483e --- /dev/null +++ b/tests/fixtures/migrations/geometry-columns.php @@ -0,0 +1,31 @@ +id(); + $table->string('name', 100); + $table->text('description')->nullable(); + $table->geography('coordinates', 'point', 4326); + $table->geometry('positions', 'point', 0); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('locations'); + } +};