Skip to content
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
6 changes: 3 additions & 3 deletions src/Generators/FactoryGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ protected function buildDefinition(Model $model)
$definition
);
} elseif (in_array($column->dataType(), ['json', 'jsonb'])) {
$default = $column->defaultValue() ?? "'{}'";
$default = $column->defaultValue() ?? "{}";
if (Blueprint::isLaravel8OrHigher()) {
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => {$default}," . PHP_EOL;
$definition .= str_repeat(self::INDENT, 3) . "'{$column->name()}' => '{$default}'," . PHP_EOL;
} else {
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => {$default}," . PHP_EOL;
$definition .= str_repeat(self::INDENT, 2) . "'{$column->name()}' => '{$default}'," . PHP_EOL;
}
} elseif ($column->dataType() === 'morphs') {
if ($column->isNullable()) {
Expand Down
2 changes: 1 addition & 1 deletion src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ protected function buildDefinition(Model $model)

foreach ($modifiers as $modifier) {
if (is_array($modifier)) {
$column_definition .= '->'.key($modifier).'('.current($modifier).')';
$column_definition .= sprintf("->%s('%s')", key($modifier), addslashes(current($modifier)));
} elseif ($modifier === 'unsigned' && Str::startsWith($dataType, 'unsigned')) {
continue;
} elseif ($modifier === 'nullable' && Str::startsWith($dataType, 'nullable')) {
Expand Down
4 changes: 2 additions & 2 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ private function buildColumn(string $name, string $definition)

if (isset(self::$modifiers[strtolower($value)])) {
$modifierAttributes = $parts[1] ?? null;
if ($modifierAttributes === null) {
if (is_null($modifierAttributes)) {
$modifiers[] = self::$modifiers[strtolower($value)];
} else {
$modifiers[] = [self::$modifiers[strtolower($value)] => $modifierAttributes];
$modifiers[] = [self::$modifiers[strtolower($value)] => preg_replace('~^[\'"]?(.*?)[\'"]?$~', '$1', $modifierAttributes)];
}
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/Feature/Generators/MigrationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ public function modelTreeDataProvider()
['drafts/with-timezones.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/with-timezones.php'],
['drafts/relationships.yaml', 'database/migrations/timestamp_create_comments_table.php', 'migrations/relationships.php'],
['drafts/indexes.yaml', 'database/migrations/timestamp_create_posts_table.php', 'migrations/indexes.php'],
['drafts/custom-indexes.yaml', 'database/migrations/timestamp_create_cooltables_table.php', 'migrations/custom-indexes.php'],
['drafts/unconventional.yaml', 'database/migrations/timestamp_create_teams_table.php', 'migrations/unconventional.php'],
['drafts/optimize.yaml', 'database/migrations/timestamp_create_optimizes_table.php', 'migrations/optimize.php'],
['drafts/model-key-constraints.yaml', 'database/migrations/timestamp_create_orders_table.php', 'migrations/model-key-constraints.php'],
Expand Down
7 changes: 4 additions & 3 deletions tests/Feature/Lexers/ModelLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,11 +664,12 @@ public function modifierAttributesProvider()
['default:0.00', 'default', 0.00],
['default:0', 'default', 0],
['default:string', 'default', 'string'],
["default:'empty'", 'default', "'empty'"],
['default:""', 'default', '""'],
["default:'empty'", 'default', 'empty'],
['default:""', 'default', ''],
['charset:utf8', 'charset', 'utf8'],
['collation:utf8_unicode', 'collation', 'utf8_unicode'],
['default:"space between"', 'default', '"space between"'],
['default:"space between"', 'default', 'space between'],
["default:'[]'", 'default', '[]'],
];
}
}
5 changes: 5 additions & 0 deletions tests/fixtures/drafts/custom-indexes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
models:
cooltable:
timestamps: false
coolcool: id foreign:coolcool.id index:custom_index_coolcool
foobar: id foreign:foobars.id index:custom_index_foobar
2 changes: 1 addition & 1 deletion tests/fixtures/migrations/columns-with-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function up()
{
Schema::create('professions', function (Blueprint $table) {
$table->id();
$table->string('title', 400)->comment("Some title for the profession");
$table->string('title', 400)->comment('Some title for the profession');
$table->string('description', 400)->nullable()->comment('Some description for the profession');
$table->timestamps();
});
Expand Down
36 changes: 36 additions & 0 deletions tests/fixtures/migrations/custom-indexes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

class CreateCooltablesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::disableForeignKeyConstraints();

Schema::create('cooltables', function (Blueprint $table) {
$table->id();
$table->foreignId('coolcool')->constrained('coolcool')->cascadeOnDelete()->index('custom_index_coolcool');
$table->foreignId('foobar')->constrained('foobars')->cascadeOnDelete()->index('custom_index_foobar');
});

Schema::enableForeignKeyConstraints();
}

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