Skip to content

Commit

Permalink
Execute creating the Primary Key in single Query
Browse files Browse the repository at this point in the history
Remove the primary item from the fluent indexes and add it to the modifiers
  • Loading branch information
khalilst committed Jun 17, 2021
1 parent bda382f commit 55b6c77
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ protected function addImpliedCommands(Grammar $grammar)
protected function addFluentIndexes()
{
foreach ($this->columns as $column) {
foreach (['primary', 'unique', 'index', 'spatialIndex'] as $index) {
foreach (['unique', 'index', 'spatialIndex'] as $index) {
// If the index has been specified on the given column, but is simply equal
// to "true" (boolean), no name has been specified for this index so the
// index method can be called without a name and it will generate one.
Expand Down
16 changes: 15 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MySqlGrammar extends Grammar
* @var string[]
*/
protected $modifiers = [
'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable',
'Primary', 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable',
'Srid', 'Default', 'Increment', 'Comment', 'After', 'First',
];

Expand Down Expand Up @@ -1014,6 +1014,20 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a primary column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
public function modifyPrimary(Blueprint $blueprint, Fluent $column)
{
if (! $column->autoIncrement && ! is_null($column->primary)) {
return ' primary key';
}
}

/**
* Get the SQL for a default column modifier.
*
Expand Down
16 changes: 15 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PostgresGrammar extends Grammar
*
* @var string[]
*/
protected $modifiers = ['Collate', 'Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs'];
protected $modifiers = ['Primary', 'Collate', 'Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -979,6 +979,20 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column)
return $column->nullable ? ' null' : ' not null';
}

/**
* Get the SQL for a primary column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
public function modifyPrimary(Blueprint $blueprint, Fluent $column)
{
if (! $column->autoIncrement && ! is_null($column->primary)) {
return ' primary key';
}
}

/**
* Get the SQL for a default column modifier.
*
Expand Down
21 changes: 17 additions & 4 deletions src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SQLiteGrammar extends Grammar
*
* @var string[]
*/
protected $modifiers = ['VirtualAs', 'StoredAs', 'Nullable', 'Default', 'Increment'];
protected $modifiers = ['Primary', 'VirtualAs', 'StoredAs', 'Nullable', 'Default', 'Increment'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -55,12 +55,11 @@ public function compileColumnListing($table)
*/
public function compileCreate(Blueprint $blueprint, Fluent $command)
{
return sprintf('%s table %s (%s%s%s)',
return sprintf('%s table %s (%s%s)',
$blueprint->temporary ? 'create temporary' : 'create',
$this->wrapTable($blueprint),
implode(', ', $this->getColumns($blueprint)),
(string) $this->addForeignKeys($blueprint),
(string) $this->addPrimaryKeys($blueprint)
(string) $this->addForeignKeys($blueprint)
);
}

Expand Down Expand Up @@ -894,6 +893,20 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a primary column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
public function modifyPrimary(Blueprint $blueprint, Fluent $column)
{
if (! $column->autoIncrement && ! is_null($column->primary)) {
return ' primary key';
}
}

/**
* Get the SQL for a default column modifier.
*
Expand Down
16 changes: 15 additions & 1 deletion src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SqlServerGrammar extends Grammar
*
* @var string[]
*/
protected $modifiers = ['Increment', 'Collate', 'Nullable', 'Default', 'Persisted'];
protected $modifiers = ['Primary', 'Increment', 'Collate', 'Nullable', 'Default', 'Persisted'];

/**
* The columns available as serials.
Expand Down Expand Up @@ -860,6 +860,20 @@ protected function modifyNullable(Blueprint $blueprint, Fluent $column)
}
}

/**
* Get the SQL for a primary column modifier.
*
* @param \Illuminate\Database\Schema\Blueprint $blueprint
* @param \Illuminate\Support\Fluent $column
* @return string|null
*/
public function modifyPrimary(Blueprint $blueprint, Fluent $column)
{
if (! $column->autoIncrement && ! is_null($column->primary)) {
return ' primary key';
}
}

/**
* Get the SQL for a default column modifier.
*
Expand Down

0 comments on commit 55b6c77

Please sign in to comment.