From 3e498ee293c2e67ccdd18dc520ce0a1806ea42fb Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 25 Jun 2021 14:21:34 +0200 Subject: [PATCH 1/3] Revert "Test to cover the unsigned modifier in presence of the primary modifier (#37802)" This reverts commit 860de189a58c049385e542888ec23449903eb9ef. --- tests/Database/DatabaseMySqlSchemaGrammarTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 69f73326569f..1182c2cff147 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -51,7 +51,7 @@ public function testBasicCreateWithPrimaryKey() { $blueprint = new Blueprint('users'); $blueprint->create(); - $blueprint->integer('foo')->primary()->unsigned(); + $blueprint->string('foo')->primary(); $conn = $this->getConnection(); $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8'); @@ -61,7 +61,7 @@ public function testBasicCreateWithPrimaryKey() $statements = $blueprint->toSql($conn, $this->getGrammar()); $this->assertCount(1, $statements); - $this->assertSame("create table `users` (`foo` int unsigned primary key not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]); + $this->assertSame("create table `users` (`foo` varchar(255) primary key not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]); } public function testAutoIncrementStartingValue() From 34435136b6ea075a7f0933b8b029471370c47f85 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 25 Jun 2021 14:22:34 +0200 Subject: [PATCH 2/3] Revert "Order of Modifiers Amended (#37782)" This reverts commit 48ce53f760255eed8a8418b7da7b6dc99d1a0257. --- src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index ad773a433ec1..ceb6c0f955d3 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -15,7 +15,7 @@ class MySqlGrammar extends Grammar * @var string[] */ protected $modifiers = [ - 'Unsigned', 'Primary', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', + 'Primary', 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', 'Srid', 'Default', 'Increment', 'Comment', 'After', 'First', ]; From 63dcfc4ba4c601e68693861827b1d4f8ce3b249a Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Fri, 25 Jun 2021 14:25:26 +0200 Subject: [PATCH 3/3] Revert "[8.x] Solve the Primary Key issue in databases with sql_require_primary_key enabled (#37715)" This reverts commit 77f45b7cba5d6695f1c903f0658cb78c1903a876. --- src/Illuminate/Database/Schema/Blueprint.php | 2 +- .../Database/Schema/Grammars/MySqlGrammar.php | 40 ++++++----------- .../Schema/Grammars/PostgresGrammar.php | 16 +------ .../Schema/Grammars/SQLiteGrammar.php | 45 +++++++------------ .../Schema/Grammars/SqlServerGrammar.php | 16 +------ .../DatabaseMySqlSchemaGrammarTest.php | 17 ------- .../DatabasePostgresSchemaGrammarTest.php | 11 ----- .../DatabaseSQLiteSchemaGrammarTest.php | 4 +- .../DatabaseSqlServerSchemaGrammarTest.php | 11 ----- 9 files changed, 34 insertions(+), 128 deletions(-) diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index c1a0a2acc92f..e2b968ab6bd4 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -208,7 +208,7 @@ protected function addImpliedCommands(Grammar $grammar) protected function addFluentIndexes() { foreach ($this->columns as $column) { - foreach (['unique', 'index', 'spatialIndex'] as $index) { + foreach (['primary', '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. diff --git a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php index ceb6c0f955d3..b6e4e3568d8e 100755 --- a/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/MySqlGrammar.php @@ -15,7 +15,7 @@ class MySqlGrammar extends Grammar * @var string[] */ protected $modifiers = [ - 'Primary', 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', + 'Unsigned', 'Charset', 'Collate', 'VirtualAs', 'StoredAs', 'Nullable', 'Srid', 'Default', 'Increment', 'Comment', 'After', 'First', ]; @@ -927,30 +927,30 @@ protected function typeComputed(Fluent $column) } /** - * Get the SQL for a primary column modifier. + * Get the SQL for a generated virtual column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ - public function modifyPrimary(Blueprint $blueprint, Fluent $column) + protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) { - if (! $column->autoIncrement && ! is_null($column->primary)) { - return ' primary key'; + if (! is_null($column->virtualAs)) { + return " as ({$column->virtualAs})"; } } /** - * Get the SQL for an auto-increment column modifier. + * Get the SQL for a generated stored column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ - protected function modifyIncrement(Blueprint $blueprint, Fluent $column) + protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) { - if (in_array($column->type, $this->serials) && $column->autoIncrement) { - return ' auto_increment primary key'; + if (! is_null($column->storedAs)) { + return " as ({$column->storedAs}) stored"; } } @@ -1029,30 +1029,16 @@ protected function modifyDefault(Blueprint $blueprint, Fluent $column) } /** - * Get the SQL for a generated virtual column modifier. - * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column - * @return string|null - */ - protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) - { - if (! is_null($column->virtualAs)) { - return " as ({$column->virtualAs})"; - } - } - - /** - * Get the SQL for a generated stored column modifier. + * Get the SQL for an auto-increment column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ - protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) + protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { - if (! is_null($column->storedAs)) { - return " as ({$column->storedAs}) stored"; + if (in_array($column->type, $this->serials) && $column->autoIncrement) { + return ' auto_increment primary key'; } } diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index fb9b53136430..133da288f19d 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -19,7 +19,7 @@ class PostgresGrammar extends Grammar * * @var string[] */ - protected $modifiers = ['Primary', 'Collate', 'Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs']; + protected $modifiers = ['Collate', 'Increment', 'Nullable', 'Default', 'VirtualAs', 'StoredAs']; /** * The columns available as serials. @@ -979,20 +979,6 @@ 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. * diff --git a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php index bae761d944d0..b7e406f578ef 100755 --- a/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SQLiteGrammar.php @@ -16,7 +16,7 @@ class SQLiteGrammar extends Grammar * * @var string[] */ - protected $modifiers = ['Primary', 'VirtualAs', 'StoredAs', 'Nullable', 'Default', 'Increment']; + protected $modifiers = ['VirtualAs', 'StoredAs', 'Nullable', 'Default', 'Increment']; /** * The columns available as serials. @@ -55,11 +55,12 @@ public function compileColumnListing($table) */ public function compileCreate(Blueprint $blueprint, Fluent $command) { - return sprintf('%s table %s (%s%s)', + return sprintf('%s table %s (%s%s%s)', $blueprint->temporary ? 'create temporary' : 'create', $this->wrapTable($blueprint), implode(', ', $this->getColumns($blueprint)), - (string) $this->addForeignKeys($blueprint) + (string) $this->addForeignKeys($blueprint), + (string) $this->addPrimaryKeys($blueprint) ); } @@ -848,30 +849,30 @@ protected function typeComputed(Fluent $column) } /** - * Get the SQL for a primary column modifier. + * Get the SQL for a generated virtual column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ - public function modifyPrimary(Blueprint $blueprint, Fluent $column) + protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) { - if (! $column->autoIncrement && ! is_null($column->primary)) { - return ' primary key'; + if (! is_null($column->virtualAs)) { + return " as ({$column->virtualAs})"; } } /** - * Get the SQL for an auto-increment column modifier. + * Get the SQL for a generated stored column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ - protected function modifyIncrement(Blueprint $blueprint, Fluent $column) + protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) { - if (in_array($column->type, $this->serials) && $column->autoIncrement) { - return ' primary key autoincrement'; + if (! is_null($column->storedAs)) { + return " as ({$column->storedAs}) stored"; } } @@ -908,30 +909,16 @@ protected function modifyDefault(Blueprint $blueprint, Fluent $column) } /** - * Get the SQL for a generated virtual column modifier. - * - * @param \Illuminate\Database\Schema\Blueprint $blueprint - * @param \Illuminate\Support\Fluent $column - * @return string|null - */ - protected function modifyVirtualAs(Blueprint $blueprint, Fluent $column) - { - if (! is_null($column->virtualAs)) { - return " as ({$column->virtualAs})"; - } - } - - /** - * Get the SQL for a generated stored column modifier. + * Get the SQL for an auto-increment column modifier. * * @param \Illuminate\Database\Schema\Blueprint $blueprint * @param \Illuminate\Support\Fluent $column * @return string|null */ - protected function modifyStoredAs(Blueprint $blueprint, Fluent $column) + protected function modifyIncrement(Blueprint $blueprint, Fluent $column) { - if (! is_null($column->storedAs)) { - return " as ({$column->storedAs}) stored"; + if (in_array($column->type, $this->serials) && $column->autoIncrement) { + return ' primary key autoincrement'; } } } diff --git a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php index 788a1fae1ded..caec020401ca 100755 --- a/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/SqlServerGrammar.php @@ -19,7 +19,7 @@ class SqlServerGrammar extends Grammar * * @var string[] */ - protected $modifiers = ['Primary', 'Increment', 'Collate', 'Nullable', 'Default', 'Persisted']; + protected $modifiers = ['Increment', 'Collate', 'Nullable', 'Default', 'Persisted']; /** * The columns available as serials. @@ -832,20 +832,6 @@ protected function typeComputed(Fluent $column) return "as ({$column->expression})"; } - /** - * 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 collation column modifier. * diff --git a/tests/Database/DatabaseMySqlSchemaGrammarTest.php b/tests/Database/DatabaseMySqlSchemaGrammarTest.php index 1182c2cff147..05e92a085435 100755 --- a/tests/Database/DatabaseMySqlSchemaGrammarTest.php +++ b/tests/Database/DatabaseMySqlSchemaGrammarTest.php @@ -47,23 +47,6 @@ public function testBasicCreateTable() $this->assertSame('alter table `users` add `id` int unsigned not null auto_increment primary key, add `email` varchar(255) not null', $statements[0]); } - public function testBasicCreateWithPrimaryKey() - { - $blueprint = new Blueprint('users'); - $blueprint->create(); - $blueprint->string('foo')->primary(); - - $conn = $this->getConnection(); - $conn->shouldReceive('getConfig')->once()->with('charset')->andReturn('utf8'); - $conn->shouldReceive('getConfig')->once()->with('collation')->andReturn('utf8_unicode_ci'); - $conn->shouldReceive('getConfig')->once()->with('engine')->andReturn(null); - - $statements = $blueprint->toSql($conn, $this->getGrammar()); - - $this->assertCount(1, $statements); - $this->assertSame("create table `users` (`foo` varchar(255) primary key not null) default character set utf8 collate 'utf8_unicode_ci'", $statements[0]); - } - public function testAutoIncrementStartingValue() { $blueprint = new Blueprint('users'); diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index 4d97e180d9f2..3a7f80ae3865 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -37,17 +37,6 @@ public function testBasicCreateTable() $this->assertSame('alter table "users" add column "id" serial primary key not null, add column "email" varchar(255) not null', $statements[0]); } - public function testBasicCreateWithPrimaryKey() - { - $blueprint = new Blueprint('users'); - $blueprint->create(); - $blueprint->string('foo')->primary(); - $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); - - $this->assertCount(1, $statements); - $this->assertSame('create table "users" ("foo" varchar(255) primary key not null)', $statements[0]); - } - public function testCreateTableWithAutoIncrementStartingValue() { $blueprint = new Blueprint('users'); diff --git a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php index f7566b56f7d3..226c58bf2b34 100755 --- a/tests/Database/DatabaseSQLiteSchemaGrammarTest.php +++ b/tests/Database/DatabaseSQLiteSchemaGrammarTest.php @@ -196,7 +196,7 @@ public function testAddingPrimaryKey() $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); $this->assertCount(1, $statements); - $this->assertSame('create table "users" ("foo" varchar primary key not null)', $statements[0]); + $this->assertSame('create table "users" ("foo" varchar not null, primary key ("foo"))', $statements[0]); } public function testAddingForeignKey() @@ -209,7 +209,7 @@ public function testAddingForeignKey() $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); $this->assertCount(1, $statements); - $this->assertSame('create table "users" ("foo" varchar primary key not null, "order_id" varchar not null, foreign key("order_id") references "orders"("id"))', $statements[0]); + $this->assertSame('create table "users" ("foo" varchar not null, "order_id" varchar not null, foreign key("order_id") references "orders"("id"), primary key ("foo"))', $statements[0]); } public function testAddingUniqueKey() diff --git a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php index 117b1146c33e..e675df85dfbb 100755 --- a/tests/Database/DatabaseSqlServerSchemaGrammarTest.php +++ b/tests/Database/DatabaseSqlServerSchemaGrammarTest.php @@ -45,17 +45,6 @@ public function testBasicCreateTable() $this->assertSame('create table "prefix_users" ("id" int identity primary key not null, "email" nvarchar(255) not null)', $statements[0]); } - public function testBasicCreateWithPrimaryKey() - { - $blueprint = new Blueprint('users'); - $blueprint->create(); - $blueprint->string('foo')->primary(); - $statements = $blueprint->toSql($this->getConnection(), $this->getGrammar()); - - $this->assertCount(1, $statements); - $this->assertSame('create table "users" ("foo" nvarchar(255) primary key not null)', $statements[0]); - } - public function testCreateTemporaryTable() { $blueprint = new Blueprint('users');