From c9c7772ff14d2cb607c8affba84271a0011f9348 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 23 Dec 2025 18:46:53 +0900 Subject: [PATCH 1/3] [12.x] Fix Postgres sequence starting value for custom schemas/connections Use `pg_get_serial_sequence` instead of manually constructing the sequence name string. This fixes an issue where `from()` was ignored on secondary connections or custom schemas because the sequence name prediction was incorrect. --- .../Database/Schema/Grammars/PostgresGrammar.php | 11 ++++++----- tests/Database/DatabasePostgresSchemaGrammarTest.php | 6 +++--- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php index 73e78071a33e..0fa91e9d977f 100755 --- a/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php @@ -269,11 +269,12 @@ public function compileAutoIncrementStartingValues(Blueprint $blueprint, Fluent { if ($command->column->autoIncrement && $value = $command->column->get('startingValue', $command->column->get('from'))) { - [$schema, $table] = $this->connection->getSchemaBuilder()->parseSchemaAndTable($blueprint->getTable()); - - $table = ($schema ? $schema.'.' : '').$this->connection->getTablePrefix().$table; - - return 'alter sequence '.$table.'_'.$command->column->name.'_seq restart with '.$value; + return sprintf( + 'select setval(pg_get_serial_sequence(%s, %s), %s, false)', + $this->quoteString($this->wrapTable($blueprint)), + $this->quoteString($command->column->name), + $value + ); } } diff --git a/tests/Database/DatabasePostgresSchemaGrammarTest.php b/tests/Database/DatabasePostgresSchemaGrammarTest.php index ee6667ca064f..bf4221b52877 100755 --- a/tests/Database/DatabasePostgresSchemaGrammarTest.php +++ b/tests/Database/DatabasePostgresSchemaGrammarTest.php @@ -70,7 +70,7 @@ public function testCreateTableWithAutoIncrementStartingValue() $this->assertCount(2, $statements); $this->assertSame('create table "users" ("id" serial not null primary key, "email" varchar(255) not null, "name" varchar(255) collate "nb_NO.utf8" not null)', $statements[0]); - $this->assertSame('alter sequence users_id_seq restart with 1000', $statements[1]); + $this->assertSame("select setval(pg_get_serial_sequence('\"users\"', 'id'), 1000, false)", $statements[1]); } public function testAddColumnsWithMultipleAutoIncrementStartingValue() @@ -88,8 +88,8 @@ public function testAddColumnsWithMultipleAutoIncrementStartingValue() 'alter table "users" add column "id" bigserial not null primary key', 'alter table "users" add column "code" serial not null primary key', 'alter table "users" add column "name" varchar(255) not null', - 'alter sequence users_id_seq restart with 100', - 'alter sequence users_code_seq restart with 200', + "select setval(pg_get_serial_sequence('\"users\"', 'id'), 100, false)", + "select setval(pg_get_serial_sequence('\"users\"', 'code'), 200, false)", ], $statements); } From c31e546e68f83b7db2334597f9a8bdd74c53f605 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 23 Dec 2025 19:21:04 +0900 Subject: [PATCH 2/3] Fix integration test expectation for Postgres sequence --- .../Sqlite/DatabaseSchemaBlueprintTest.php | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php b/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php index cf1e71712318..3c841314bb58 100644 --- a/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php +++ b/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php @@ -77,9 +77,9 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - .'alter column "code" type integer, ' - .'alter column "code" set not null', - 'alter sequence users_code_seq restart with 10', + . 'alter column "code" type integer, ' + . 'alter column "code" set not null', + 'select setval(pg_get_serial_sequence(\'"users"\', \'code\'), 10, false)', 'comment on column "users"."code" is \'my comment\'', ], $blueprint->toSql()); @@ -89,10 +89,10 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - .'alter column "name" type char(40) collate "unicode", ' - .'alter column "name" drop not null, ' - .'alter column "name" set default \'easy\', ' - .'alter column "name" drop identity if exists', + . 'alter column "name" type char(40) collate "unicode", ' + . 'alter column "name" drop not null, ' + . 'alter column "name" set default \'easy\', ' + . 'alter column "name" drop identity if exists', 'comment on column "users"."name" is NULL', ], $blueprint->toSql()); @@ -102,11 +102,11 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - .'alter column "foo" type integer, ' - .'alter column "foo" set not null, ' - .'alter column "foo" drop default, ' - .'alter column "foo" drop identity if exists, ' - .'alter column "foo" add generated always as identity (expression)', + . 'alter column "foo" type integer, ' + . 'alter column "foo" set not null, ' + . 'alter column "foo" drop default, ' + . 'alter column "foo" drop identity if exists, ' + . 'alter column "foo" add generated always as identity (expression)', 'comment on column "users"."foo" is NULL', ], $blueprint->toSql()); @@ -116,10 +116,10 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - .'alter column "foo" type geometry(point,1234), ' - .'alter column "foo" set not null, ' - .'alter column "foo" drop default, ' - .'alter column "foo" drop identity if exists', + . 'alter column "foo" type geometry(point,1234), ' + . 'alter column "foo" set not null, ' + . 'alter column "foo" drop default, ' + . 'alter column "foo" drop identity if exists', 'comment on column "users"."foo" is NULL', ], $blueprint->toSql()); @@ -129,11 +129,11 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - .'alter column "added_at" type timestamp(2) without time zone, ' - .'alter column "added_at" set not null, ' - .'alter column "added_at" set default CURRENT_TIMESTAMP, ' - .'alter column "added_at" drop expression if exists, ' - .'alter column "added_at" drop identity if exists', + . 'alter column "added_at" type timestamp(2) without time zone, ' + . 'alter column "added_at" set not null, ' + . 'alter column "added_at" set default CURRENT_TIMESTAMP, ' + . 'alter column "added_at" drop expression if exists, ' + . 'alter column "added_at" drop identity if exists', 'comment on column "users"."added_at" is NULL', ], $blueprint->toSql()); } @@ -495,7 +495,7 @@ public function testItDoesNotSetPrecisionHigherThanSupportedWhenRenamingTimestam // Expecting something similar to: // Illuminate\Database\QueryException // SQLSTATE[42000]: Syntax error or access violation: 1426 Too big precision 10 specified for 'my_timestamp'. Maximum is 6.... - $this->fail('test_it_does_not_set_precision_higher_than_supported_when_renaming_timestamps has failed. Error: '.$e->getMessage()); + $this->fail('test_it_does_not_set_precision_higher_than_supported_when_renaming_timestamps has failed. Error: ' . $e->getMessage()); } } @@ -514,7 +514,7 @@ protected function getBlueprint( string $table, Closure $callback, ): Blueprint { - $grammarClass = 'Illuminate\Database\Schema\Grammars\\'.$grammar.'Grammar'; + $grammarClass = 'Illuminate\Database\Schema\Grammars\\' . $grammar . 'Grammar'; $connection = DB::connection(); $connection->setSchemaGrammar(new $grammarClass($connection)); From c6f0576384db7096d1b0fc08722fd6a1d4a81076 Mon Sep 17 00:00:00 2001 From: jo Date: Tue, 23 Dec 2025 19:49:18 +0900 Subject: [PATCH 3/3] Fix code style --- .../Sqlite/DatabaseSchemaBlueprintTest.php | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php b/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php index 3c841314bb58..c1fb4490c23b 100644 --- a/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php +++ b/tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php @@ -77,8 +77,8 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - . 'alter column "code" type integer, ' - . 'alter column "code" set not null', + .'alter column "code" type integer, ' + .'alter column "code" set not null', 'select setval(pg_get_serial_sequence(\'"users"\', \'code\'), 10, false)', 'comment on column "users"."code" is \'my comment\'', ], $blueprint->toSql()); @@ -89,10 +89,10 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - . 'alter column "name" type char(40) collate "unicode", ' - . 'alter column "name" drop not null, ' - . 'alter column "name" set default \'easy\', ' - . 'alter column "name" drop identity if exists', + .'alter column "name" type char(40) collate "unicode", ' + .'alter column "name" drop not null, ' + .'alter column "name" set default \'easy\', ' + .'alter column "name" drop identity if exists', 'comment on column "users"."name" is NULL', ], $blueprint->toSql()); @@ -102,11 +102,11 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - . 'alter column "foo" type integer, ' - . 'alter column "foo" set not null, ' - . 'alter column "foo" drop default, ' - . 'alter column "foo" drop identity if exists, ' - . 'alter column "foo" add generated always as identity (expression)', + .'alter column "foo" type integer, ' + .'alter column "foo" set not null, ' + .'alter column "foo" drop default, ' + .'alter column "foo" drop identity if exists, ' + .'alter column "foo" add generated always as identity (expression)', 'comment on column "users"."foo" is NULL', ], $blueprint->toSql()); @@ -116,10 +116,10 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - . 'alter column "foo" type geometry(point,1234), ' - . 'alter column "foo" set not null, ' - . 'alter column "foo" drop default, ' - . 'alter column "foo" drop identity if exists', + .'alter column "foo" type geometry(point,1234), ' + .'alter column "foo" set not null, ' + .'alter column "foo" drop default, ' + .'alter column "foo" drop identity if exists', 'comment on column "users"."foo" is NULL', ], $blueprint->toSql()); @@ -129,11 +129,11 @@ public function testNativeColumnModifyingOnPostgreSql() $this->assertEquals([ 'alter table "users" ' - . 'alter column "added_at" type timestamp(2) without time zone, ' - . 'alter column "added_at" set not null, ' - . 'alter column "added_at" set default CURRENT_TIMESTAMP, ' - . 'alter column "added_at" drop expression if exists, ' - . 'alter column "added_at" drop identity if exists', + .'alter column "added_at" type timestamp(2) without time zone, ' + .'alter column "added_at" set not null, ' + .'alter column "added_at" set default CURRENT_TIMESTAMP, ' + .'alter column "added_at" drop expression if exists, ' + .'alter column "added_at" drop identity if exists', 'comment on column "users"."added_at" is NULL', ], $blueprint->toSql()); } @@ -495,7 +495,7 @@ public function testItDoesNotSetPrecisionHigherThanSupportedWhenRenamingTimestam // Expecting something similar to: // Illuminate\Database\QueryException // SQLSTATE[42000]: Syntax error or access violation: 1426 Too big precision 10 specified for 'my_timestamp'. Maximum is 6.... - $this->fail('test_it_does_not_set_precision_higher_than_supported_when_renaming_timestamps has failed. Error: ' . $e->getMessage()); + $this->fail('test_it_does_not_set_precision_higher_than_supported_when_renaming_timestamps has failed. Error: '.$e->getMessage()); } } @@ -514,7 +514,7 @@ protected function getBlueprint( string $table, Closure $callback, ): Blueprint { - $grammarClass = 'Illuminate\Database\Schema\Grammars\\' . $grammar . 'Grammar'; + $grammarClass = 'Illuminate\Database\Schema\Grammars\\'.$grammar.'Grammar'; $connection = DB::connection(); $connection->setSchemaGrammar(new $grammarClass($connection));