Skip to content
Draft
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
11 changes: 6 additions & 5 deletions src/Illuminate/Database/Schema/Grammars/PostgresGrammar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Database/DatabasePostgresSchemaGrammarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}

Expand Down
42 changes: 21 additions & 21 deletions tests/Integration/Database/Sqlite/DatabaseSchemaBlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());

Expand All @@ -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());
}
Expand Down
Loading