Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate migration columns with comments #347

Merged
merged 4 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class ModelLexer implements Lexer
'primary' => 'primary',
'foreign' => 'foreign',
'ondelete' => 'onDelete',
'comment' => 'comment',
];

public function analyze(array $tokens): array
Expand Down Expand Up @@ -206,7 +207,7 @@ private function buildColumn(string $name, string $definition)
$data_type = null;
$modifiers = [];

$tokens = preg_split('#".*?"(*SKIP)(*FAIL)|\s+#', $definition);
$tokens = preg_split('#("|\').*?\1(*SKIP)(*FAIL)|\s+#', $definition);
foreach ($tokens as $token) {
$parts = explode(':', $token);
$value = $parts[0];
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Generator/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,7 @@ public function modelTreeDataProvider()
['drafts/unconventional-foreign-key.yaml', 'database/migrations/timestamp_create_states_table.php', 'migrations/unconventional-foreign-key.php'],
['drafts/resource-statements.yaml', 'database/migrations/timestamp_create_users_table.php', 'migrations/resource-statements.php'],
['drafts/enum-options.yaml', 'database/migrations/timestamp_create_messages_table.php', 'migrations/enum-options.php'],
['drafts/columns-with-comments.yaml', 'database/migrations/timestamp_create_professions_table.php', 'migrations/columns-with-comments.php'],
];
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/drafts/columns-with-comments.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
models:
Profession:
title: string:400 comment:"Some title for the profession"
description: string:400 nullable comment:'Some description for the profession'
Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought something like

Suggested change
title: string:400 comment:"Some title for the profession"
description: string:400 nullable comment:'Some description for the profession'
title: string:400 "comment:Some title for the profession"
description: string:400 nullable 'comment:Some description for the profession'

should have worked out of the box?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spaceemotion why is that?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on https://blueprint.laravelshift.com/docs/model-data-types/:

When specifying an attribute or modifier value which contains a space, you must wrap the value in double quotes ("). For example, enum:Ordered,Completed,"On Hold". Blueprint will unwrap the value during parsing.

I thought this was a general thing for all attributes.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It was. Or at least the double quotes. But in your example, you also quoted the attribute (i.e. comment)

33 changes: 33 additions & 0 deletions tests/fixtures/migrations/columns-with-comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProfessionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('professions', function (Blueprint $table) {
$table->id();
$table->string('title', 400)->comment("Some title for the profession");
$table->string('description', 400)->nullable()->comment('Some description for the profession');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('professions');
}
}