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

Don't duplicate id/created_at/updated_at fields if they are passed in #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions spec/Migrations/SyntaxBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,41 @@ function it_creates_the_php_syntax_for_the_schema()
$this->create($schema, ['table' => 'posts', 'action' => 'create'])['down']->shouldBe("Schema::dropIfExists('posts');");
}

function it_doesnt_add_duplicate_id_field()
{
$schema = [
[
'name' => 'id',
'type' => 'increments',
'arguments' => [],
'options' => [],
], [
"name" => "email",
"type" => "string",
"arguments" => ["100"],
"options" => [
"unique" => true,
"nullable" => true,
"default" => '"[email protected]"'
]
]
];

$this->create($schema, ['table' => 'posts', 'action' => 'create'])['up']->shouldBe(getStub());
}

function it_doesnt_add_duplicate_timestamp_fields()
{
$schema = [[
'name' => 'created_at',
'type' => 'date',
'arguments' => [],
'options' => [],
]];

$this->create($schema, ['table' => 'posts', 'action' => 'create'])['up']->shouldBe(getTimestampStub());
}

}

function getStub()
Expand All @@ -40,3 +75,13 @@ function getStub()
});
EOT;
}

function getTimestampStub()
{
return <<<EOT
Schema::create('{{table}}', function(Blueprint \$table) {
\$table->increments('id');
\$table->date('created_at');
});
EOT;
}
121 changes: 120 additions & 1 deletion src/Migrations/SyntaxBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ private function constructSchema($schema, $direction = 'Add')
{
if (!$schema) return '';

if (!$this->hasDefinedIdColumn($schema)) {
$schema = $this->addIdColumn($schema);
}

if (!$this->hasDefinedTimestamps($schema)) {
$schema = $this->addTimestamps($schema);
}

$fields = array_map(function ($field) use ($direction) {
$method = "{$direction}Column";

Expand All @@ -169,7 +177,7 @@ private function constructSchema($schema, $direction = 'Add')
*/
private function addColumn($field)
{
$syntax = sprintf("\$table->%s('%s')", $field['type'], $field['name']);
$syntax = sprintf("\$table->%s(%s)", $field['type'], empty($field['name']) ? '' : "'$field[name]'");

// If there are arguments for the schema type, like decimal('amount', 5, 2)
// then we have to remember to work those in.
Expand All @@ -196,4 +204,115 @@ private function dropColumn($field)
{
return sprintf("\$table->dropColumn('%s');", $field['name']);
}

/**
* Check to see if the user has already provided an id field
*
* @param array $schema
*
* @return bool
*/
private function hasDefinedIdColumn(array $schema)
{
foreach ($schema as $definition) {
if ($definition['name'] === 'id') {
return true;
}
}

return false;
}

/**
* Check to see if the user has already defined timestamp field(s)
*
* @param array $schema
*
* @return bool
*/
private function hasDefinedTimestamps(array $schema)
{
$created_at = false;
$updated_at = false;

foreach ($schema as $definition) {
if ($definition['name'] === 'created_at') {
$created_at = true;
} else if ($definition['name'] === 'updated_at') {
$updated_at = true;
}
}

return ($created_at || $updated_at);
}

/**
* Adds an ID field to the beginning of the schema definition
*
* @param array $schema
*
* @return array
*/
private function addIdColumn(array $schema)
{
return $this->appendFieldToSchema($schema, 'id', 'increments');
}

/**
* Adds the timestamps option to the end of the schema definition
*
* @param array $schema
*
* @return array
*/
private function addTimestamps(array $schema)
{
return $this->prependFieldToSchema($schema, '', 'timestamps');
}

/**
* Adds a field to the start of the schema definition
*
* @param array $schema
* @param string $name
* @param string $type
* @param array $arguments
* @param array $options
*
* @return array
*/
private function appendFieldToSchema(array $schema, $name, $type, $arguments = [], $options = [])
{
array_unshift($schema, [
'name' => $name,
'type' => $type,
'arguments' => $arguments,
'options' => $options,
]);

return $schema;
}

/**
* Adds a field to the end of the schema definition
*
* @param array $schema
* @param string $name
* @param string $type
* @param array $arguments
* @param array $options
*
* @return array
*/
private function prependFieldToSchema(array $schema, $name, $type, $arguments = [], $options = [])
{
array_push($schema, [
'name' => $name,
'type' => $type,
'arguments' => $arguments,
'options' => $options,
]);

return $schema;
}
}
2 changes: 0 additions & 2 deletions src/stubs/schema-create.stub
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
Schema::create('{{table}}', function (Blueprint $table) {
$table->increments('id');
{{schema_up}}
$table->timestamps();
});